Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* You are given a sequence of text lines, holding only visible symbols, small and capital Latin letters.
- Your task is to remove all X shapes in the text. They may consist of small and capital letters at the same time,
- or any visible symbol. An X Shape is 3 by 3 symbols crossing each other on 3 lines. Single X shape can be part
- of multiple X shapes. If new X Shapes are formed after the first removal you don't have to remove them.
- The input data comes as array of strings, holding the text lines. Print at the console the input data
- after removing all X shapes. */
- "use strict";
- function xShapeRemover(args) {
- var resultArr = clone(args);
- function clone(arr) {
- var deepCopyObj = new arr.constructor();
- for (var property in arr) {
- if (arr.hasOwnProperty(property)) {
- switch (typeof arr[property]) {
- case 'object':
- deepCopyObj[property] = clone(arr[property]);
- break;
- default:
- deepCopyObj[property] = arr[property];
- break;
- }
- }
- }
- return deepCopyObj;
- }
- for (var i = 0; i < resultArr.length; i += 1) {
- resultArr[i] = resultArr[i].split('').filter(Boolean);
- }
- for (var row = 0; row < args.length; row += 1) {
- for (var ch = 0; ch < args[row].length; ch += 1) {
- removeX_Shape(args, row, ch, resultArr);
- }
- console.log(resultArr[row].join(''));
- }
- function removeX_Shape(matrix, row, col, output) {
- var charX = matrix[row][col].toLowerCase();
- if (row + 2 < matrix.length && col + 2 < matrix[row].length &&
- matrix[row][col + 2].toLowerCase() == charX && matrix[row + 1][col + 1] &&
- matrix[row + 1][col + 1].toLowerCase() == charX && matrix[row + 2][col] &&
- matrix[row + 2][col].toLowerCase() == charX && matrix[row + 2][col + 2] &&
- matrix[row + 2][col + 2].toLowerCase() == charX) {
- output[row][col] = '';
- output[row][col + 2] = '';
- output[row + 1][col + 1] = '';
- output[row + 2][col] = '';
- output[row + 2][col + 2] = '';
- }
- }
- }
- xShapeRemover([
- 'abnbjs',
- 'xoBab',
- 'Abmbh',
- 'aabab',
- 'ababvvvv'
- ]);
- xShapeRemover([
- '8888888',
- '08*8*80',
- '808*888',
- '0**8*8?'
- ]);
- xShapeRemover([
- '^u^', 'j^l^a',
- '^w^WoWl',
- 'adw1w6',
- '(WdWoWgPoop)'
- ]);
- xShapeRemover([
- 'puoUdai',
- 'miU',
- 'ausupirina',
- '8n8i8',
- 'm8o8a',
- '8l8o860',
- 'M8i8',
- '8e8!8?!'
- ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement