Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* 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.
- Triangles can span different sizes: 2 lines, 3 lines, 4 lines, etc. Triangles can overlap (some letters
- can take part in several triangles). Print at the console the input data after replacing all triangles by '*'. */
- "use strict";
- function revealTriangles(arr) {
- for (var i = 0; i < arr.length; i += 1) {
- arr[i] = arr[i].split('').filter(Boolean);
- }
- function clone(obj) {
- var deepCopyObj = new obj.constructor();
- for (var property in obj) {
- if (obj.hasOwnProperty(property)) {
- switch (typeof obj[property]) {
- case 'object':
- deepCopyObj[property] = clone(obj[property]);
- break;
- default:
- deepCopyObj[property] = obj[property];
- break;
- }
- }
- }
- return deepCopyObj;
- }
- var output = clone(arr);
- for (var row = 0; row < arr.length; row += 1) {
- for (var col = 0; col < arr[row].length; col += 1) {
- if (row + 1 < arr.length && col - 1 >= 0 && col + 1 < arr[row + 1].length &&
- arr[row][col] == arr[row + 1][col - 1] && arr[row][col] == arr[row + 1][col] &&
- arr[row][col] == arr[row + 1][col + 1]) {
- output[row][col] = '*';
- output[row + 1][col - 1] = '*';
- output[row + 1][col] = '*';
- output[row + 1][col + 1] = '*';
- }
- }
- output[row] = output[row].join('');
- }
- return output.join('\n');
- }
- console.log(revealTriangles([
- 'abcdexgh',
- 'bbbdxxxh',
- 'abcxxxxx'
- ]));
- console.log(revealTriangles([
- 'aa',
- 'aaa',
- 'aaaa',
- 'aaaaa'
- ]));
- console.log(revealTriangles([
- 'ax',
- 'xxx',
- 'b',
- 'bbb',
- 'bbbb'
- ]));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement