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).
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
function solve (input){ var words = []; var wordsFinal = []; for (var iii = 0; iii < input.length ; iii++) { words[iii] = input[iii]; wordsFinal[iii] = input[iii]; } String.prototype.replaceAt=function(index, character) { return this.substr(0, index) + character + this.substr(index+character.length); } for (var i = 0; i |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
function revealTriangles(input) { // Initialize the output as char[][], holding the input chars var output = []; for (var row = 0; row < input.length; row++) { output[row] = input[row].split(''); } // Replace all triangles with '*' (with overlapping) for (var row = 1; row < input.length; row++) { var maxCol = Math.min( input[row - 1].length - 2, input[row].length - 3); for (var col = 0; col <= maxCol; col++) { var a = input[row][col]; var b = input[row][col + 1]; var c = input[row][col + 2]; var d = input[row - 1][col + 1]; if (a == b && b == c & c == d) { // Triangle found --> fill it with '*' output[row][col] = '*'; output[row][col + 1] = '*'; output[row][col + 2] = '*'; output[row - 1][col + 1] = '*'; } } } // Print the result for (var row = 0; row < input.length; row++) { console.log(output[row].join('')); } } // ------------------------------------------------------------ // Read the input from the console as array and process it // Remove all below code before submitting to the judge system! // ------------------------------------------------------------ var arr = []; require('readline').createInterface({ input: process.stdin, output: process.stdout }).on('line', function (line) { arr.push(line); }).on('close', function () { revealTriangles(arr); }); |
Enjoy the code! And have fun in general! 😀