Advertisement
dimipan80

Exams - X-Remove

Dec 20th, 2014
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* You are given a sequence of text lines, holding only visible symbols, small and capital Latin letters.
  2. Your task is to remove all X shapes in the text. They may consist of small and capital letters at the same time,
  3. or any visible symbol. An X Shape is 3 by 3 symbols crossing each other on 3 lines. Single X shape can be part
  4. of multiple X shapes. If new X Shapes are formed after the first removal you don't have to remove them.
  5. The input data comes as array of strings, holding the text lines. Print at the console the input data
  6. after removing all X shapes. */
  7.  
  8. "use strict";
  9.  
  10. function xShapeRemover(args) {
  11.     var resultArr = clone(args);
  12.     function clone(arr) {
  13.         var deepCopyObj = new arr.constructor();
  14.         for (var property in arr) {
  15.             if (arr.hasOwnProperty(property)) {
  16.                 switch (typeof arr[property]) {
  17.                     case 'object':
  18.                         deepCopyObj[property] = clone(arr[property]);
  19.                         break;
  20.                     default:
  21.                         deepCopyObj[property] = arr[property];
  22.                         break;
  23.                 }
  24.             }
  25.         }
  26.  
  27.         return deepCopyObj;
  28.     }
  29.  
  30.     for (var i = 0; i < resultArr.length; i += 1) {
  31.         resultArr[i] = resultArr[i].split('').filter(Boolean);
  32.     }
  33.  
  34.     for (var row = 0; row < args.length; row += 1) {
  35.         for (var ch = 0; ch < args[row].length; ch += 1) {
  36.             removeX_Shape(args, row, ch, resultArr);
  37.         }
  38.         console.log(resultArr[row].join(''));
  39.     }
  40.  
  41.     function removeX_Shape(matrix, row, col, output) {
  42.         var charX = matrix[row][col].toLowerCase();
  43.         if (row + 2 < matrix.length && col + 2 < matrix[row].length &&
  44.             matrix[row][col + 2].toLowerCase() == charX && matrix[row + 1][col + 1] &&
  45.             matrix[row + 1][col + 1].toLowerCase() == charX && matrix[row + 2][col] &&
  46.             matrix[row + 2][col].toLowerCase() == charX && matrix[row + 2][col + 2] &&
  47.             matrix[row + 2][col + 2].toLowerCase() == charX) {
  48.             output[row][col] = '';
  49.             output[row][col + 2] = '';
  50.             output[row + 1][col + 1] = '';
  51.             output[row + 2][col] = '';
  52.             output[row + 2][col + 2] = '';
  53.         }
  54.     }
  55. }
  56.  
  57. xShapeRemover([
  58.     'abnbjs',
  59.     'xoBab',
  60.     'Abmbh',
  61.     'aabab',
  62.     'ababvvvv'
  63. ]);
  64.  
  65. xShapeRemover([
  66.     '8888888',
  67.     '08*8*80',
  68.     '808*888',
  69.     '0**8*8?'
  70. ]);
  71.  
  72. xShapeRemover([
  73.     '^u^', 'j^l^a',
  74.     '^w^WoWl',
  75.     'adw1w6',
  76.     '(WdWoWgPoop)'
  77. ]);
  78.  
  79. xShapeRemover([
  80.     'puoUdai',
  81.     'miU',
  82.     'ausupirina',
  83.     '8n8i8',
  84.     'm8o8a',
  85.     '8l8o860',
  86.     'M8i8',
  87.     '8e8!8?!'
  88. ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement