Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* You are given a sequence of text lines, holding symbols, small and capital Latin letters.
- Your task is to remove all "plus shapes" in the text. They may consist of small and capital letters
- at the same time, or of any symbol. Every "plus shape" is 3 by 3 symbols crossing each other on 3 lines.
- Single "plus shape" can be part of multiple "plus shapes". If new "plus 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 "plus shapes". */
- function plusShapesRemover(args) {
- 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 result = clone(args);
- var i;
- for (i = 0; i < args.length; i += 1) {
- args[i] = args[i].toLowerCase();
- result[i] = result[i].split('');
- }
- for (i = 0; i < args.length; i += 1) {
- for (var ch = 0; ch < args[i].length; ch += 1) {
- findAndRemovePlusShapes(args, i, ch, result);
- }
- console.log(result[i].join(''));
- }
- function findAndRemovePlusShapes(matrix, row, col, output) {
- var plus = matrix[row][col];
- if (row + 2 < matrix.length && col + 1 < matrix[row + 1].length &&
- matrix[row + 1][col - 1] && matrix[row + 1][col - 1] == plus &&
- matrix[row + 1][col] == plus && matrix[row + 1][col + 1] == plus &&
- matrix[row + 2][col] == plus) {
- output[row][col] = '';
- output[row + 1][col - 1] = '';
- output[row + 1][col] = '';
- output[row + 1][col + 1] = '';
- output[row + 2][col] = '';
- }
- }
- }
- plusShapesRemover([
- 'ab**l5',
- 'bBb*555',
- 'absh*5',
- 'ttHHH',
- 'ttth'
- ]);
- plusShapesRemover([
- '888**t*',
- '8888ttt',
- '888ttt<<',
- '*8*0t>>hi'
- ]);
- plusShapesRemover([
- '@s@a@p@una',
- 'p@@@@@@@@dna',
- '@6@t@*@*ego',
- 'vdig*****ne6',
- 'li??^*^*'
- ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement