Java Script -Reveal Triangles – Exam

As I have mentioned in the previous post, I have participated in a Java Script competition from the SoftUni, where I made it to the top 31% of the competition 🙂 (not something to be extremely proud of, but considering the lack of time I had it is an acceptable result … somehow).

triangularMatrix

So, back to the second problem, which granted me another 100 points for the final ranking. This is how the problem was defined:


Problem 2 – Reveal Triangles

You are given a sequence of text lines, holding small Latin letters. Your task is to reveal all triangles in the text by changing their letters with ‘*‘. Triangles consist of equal letters in the form of triangle:

a

aaa

x

xxx

xxxxx

p

ppp

ppppp

ppppppp

etc.

Triangles can span different sizes: 2 lines, 3 lines, 4 lines, etc. Triangles can overlap (some letters can take part in several triangles).

Input

The input data comes as array of strings, holding the text lines.

Output

Print at the console the input data after replacing all triangles by ‘*‘.

Constraints

  • The input will be passed to the first JavaScript function found in your code as array of strings.
  • Each input line will hold 1…100 Latin letters.
  • The number of input lines will be in the range [1..100].
    • Allowed working time: 0.2 seconds. Allowed memory: 16 MB.

Examples

Input

Output

 

Input

Output

 

Input

Output

 

Input

Output

abcdexgh 

bbbdxxxh

abcxxxxx

a*cde*gh 

***d***h

abc*****

aa 

aaa

aaaa

aaaaa

a* 

***

****

*****

ax 

xxx

b

bbb

bbbb

a* 

***

b

b**

****

dffdsgyefg 

ffffeyeee

jbfffays

dagfffdsss

dfdfa

dadaaadddf

sdaaaaa

daaaaaaasf

d**dsgy*fg 

****ey***

jb***ays

dag***dsss

dfdf*

dad***dddf

sd*****

d*******sf

Hint: to simplify your work, you can reveal only triangles of size “2 lines”, because all bigger triangles consist of several overlapping triangles of size “2 lines”.


So, this one was obviously a tough one – I had to use arrays or lists in order to get this thing working. Somehow, I am not a programming guy who is a real fan of lists and arrays, but after taking a look at the other 2 problems I realized that this one is the most doable one 🙂

What I did? I actually managed to resolve the problem using quite a few lines, which was a surprise for me. I created two identical arrays and I simply checked whether we have elements on the triangle positions in the first array, which are the same. Once, they were, I copied them in the second array. Easy to explain, tough to understand and to make it. It took me about 2 hours to finish it. (4 hours for both first and second problem, considering a small 20 minute pause in the middle).

This is how my magic was done:

Not something tough, actually the code is human readable. Actually, this time I think that my idea, for using two arrays is better than the original solution, where they are using only one and are extremely careful when filling it up, not to overlap with the stars. Anyway, this is the original one:

Enjoy the code! And have fun in general! 😀

About

A VBA Developer.

Tagged with: , ,