Advertisement
dimipan80

Exams - Plus-Remove

Dec 22nd, 2014
191
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 symbols, small and capital Latin letters.
  2.  Your task is to remove all "plus shapes" in the text. They may consist of small and capital letters
  3.  at the same time, or of any symbol. Every "plus shape" is 3 by 3 symbols crossing each other on 3 lines.
  4.  Single "plus shape" can be part of multiple "plus shapes". If new "plus shapes" are formed after the first removal
  5.  you don't have to remove them. The input data comes as array of strings, holding the text lines.
  6.  Print at the console the input data after removing all "plus shapes". */
  7.  
  8. function plusShapesRemover(args) {
  9.     function clone(obj) {
  10.         var deepCopyObj = new obj.constructor();
  11.         for (var property in obj) {
  12.             if (obj.hasOwnProperty(property)) {
  13.                 switch (typeof obj[property]) {
  14.                     case 'object':
  15.                         deepCopyObj[property] = clone(obj[property]);
  16.                         break;
  17.                     default:
  18.                         deepCopyObj[property] = obj[property];
  19.                         break;
  20.                 }
  21.             }
  22.         }
  23.  
  24.         return deepCopyObj;
  25.     }
  26.  
  27.     var result = clone(args);
  28.     var i;
  29.     for (i = 0; i < args.length; i += 1) {
  30.         args[i] = args[i].toLowerCase();
  31.         result[i] = result[i].split('');
  32.     }
  33.  
  34.     for (i = 0; i < args.length; i += 1) {
  35.         for (var ch = 0; ch < args[i].length; ch += 1) {
  36.             findAndRemovePlusShapes(args, i, ch, result);
  37.         }
  38.         console.log(result[i].join(''));
  39.     }
  40.  
  41.     function findAndRemovePlusShapes(matrix, row, col, output) {
  42.         var plus = matrix[row][col];
  43.         if (row + 2 < matrix.length && col + 1 < matrix[row + 1].length &&
  44.             matrix[row + 1][col - 1] && matrix[row + 1][col - 1] == plus &&
  45.             matrix[row + 1][col] == plus && matrix[row + 1][col + 1] == plus &&
  46.             matrix[row + 2][col] == plus) {
  47.             output[row][col] = '';
  48.             output[row + 1][col - 1] = '';
  49.             output[row + 1][col] = '';
  50.             output[row + 1][col + 1] = '';
  51.             output[row + 2][col] = '';
  52.         }
  53.     }
  54. }
  55.  
  56. plusShapesRemover([
  57.     'ab**l5',
  58.     'bBb*555',
  59.     'absh*5',
  60.     'ttHHH',
  61.     'ttth'
  62. ]);
  63.  
  64. plusShapesRemover([
  65.     '888**t*',
  66.     '8888ttt',
  67.     '888ttt<<',
  68.     '*8*0t>>hi'
  69. ]);
  70.  
  71. plusShapesRemover([
  72.     '@s@a@p@una',
  73.     'p@@@@@@@@dna',
  74.     '@6@t@*@*ego',
  75.     'vdig*****ne6',
  76.     'li??^*^*'
  77. ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement