Advertisement
Guest User

gridandobvious

a guest
Jan 28th, 2016
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script>
  2.     function magicGrid(input) {
  3.         var encryptedString = input[0];
  4.         var magicNumber = parseInt(input[1]);
  5.         var matrix = input.slice(2);
  6.         var rows = matrix.length;
  7.         var flattenedMatrixString = matrix.join(" ");
  8.  
  9.         var flattenedMatrixArray = flattenedMatrixString.split(" ").map(Number);
  10.  
  11.         var row_col = findPositionBySum(magicNumber, flattenedMatrixArray, rows)
  12.         var key = row_col[0]["row"] + row_col[0]["col"] + row_col[1]["row"] + row_col[1]["col"];
  13.         var output = "";
  14.         for (var i = 0; i < encryptedString.length; i++) {
  15.             var ascii = encryptedString.charCodeAt(i);
  16.             var asciiResult = i % 2 == 0 ? ascii + key : ascii - key;
  17.             output += String.fromCharCode(asciiResult);
  18.         }
  19.  
  20.         function findPositionBySum(sum, array, rows) {
  21.             var retvalTupple = [
  22.                 {
  23.                     "row": -1,
  24.                     "col": -1
  25.                 },
  26.                 {
  27.                     "row": -1,
  28.                     "col": -1
  29.                 }
  30.             ];
  31.             for (var i = 0; i < array.length; i++) {
  32.                 for (var j = i + 1; j < array.length; j++) {
  33.                     if (array[i] + array[j] == sum) {
  34.                         retvalTupple[0]["row"] = parseInt(i / rows);
  35.                         retvalTupple[0]["col"] = parseInt(i % rows);
  36.  
  37.                         retvalTupple[1]["row"] = parseInt(j / rows);
  38.                         retvalTupple[1]["col"] = parseInt(j % rows);
  39.  
  40.                         break;
  41.                     }
  42.                 }
  43.             }
  44.  
  45.             return retvalTupple;
  46.         }
  47.  
  48.         return output;
  49.     }
  50.  
  51.     var input = ["QqdvSpg",
  52.         "400",
  53.         "100 200 120",
  54.         "120 300 310",
  55.         "150 290 370"
  56.     ];
  57.     console.log(magicGrid(input));
  58.  
  59.  
  60.  
  61.     /***********************************************/
  62.  
  63.     function captainObvious(input) {
  64.         var firstText = input[0];
  65.         var secondText = input[1];
  66.         var wordsFirstText = firstText.split(/\W/)
  67.             .filter(function(word) {
  68.                 return !!word;
  69.             })
  70.             .map(function(word) {
  71.                 return word.toLowerCase();
  72.             });
  73.  
  74.         var wordsFirstTextOccurences = {};
  75.         wordsFirstText.forEach(function(word) {
  76.             if (!(word in wordsFirstTextOccurences)) {
  77.                 wordsFirstTextOccurences[word] = 0;
  78.             }
  79.             wordsFirstTextOccurences[word]++;
  80.         });
  81.         var neededWords = Object.keys(
  82.            wordsFirstTextOccurences
  83.         ).filter(function(word) {
  84.            return wordsFirstTextOccurences[word] >= 3;
  85.         });
  86.  
  87.         var allRepeatedWordsCount = neededWords.length;
  88.  
  89.         if (allRepeatedWordsCount < 1) {
  90.             return "No words";
  91.         }
  92.  
  93.         var output = "";
  94.         var senteces = secondText.split(/[.?!]/)
  95.             .filter(function(sentece) {
  96.                 return !!sentece;
  97.             });
  98.  
  99.         senteces.forEach(function(sentence) {
  100.             var localWords = sentence.split(/\W/)
  101.                 .filter(function(word) {
  102.                     return !!word;
  103.                 });
  104.             var occurred = 0;
  105.             neededWords.forEach(function(word) {
  106.                 if (localWords.indexOf(word) > -1) {
  107.                     occurred++;
  108.                 }
  109.             });
  110.             if (occurred == 2 || occurred == allRepeatedWordsCount) {
  111.                 output += sentence.trim();
  112.                 var senteceIndex = secondText.indexOf(sentence);
  113.                 var charPosition = senteceIndex + sentence.length;
  114.                 var sentenceEndingChar = secondText[charPosition];
  115.                 secondText = secondText.substring(charPosition+1, secondText.length);
  116.                 output += sentenceEndingChar + "\n";
  117.             }
  118.         });
  119.  
  120.         if (output.trim() == "") {
  121.             return "No sentences";
  122.         }
  123.  
  124.         return output.trim();
  125.     }
  126.  
  127.     input =
  128.         ["Captain Obvious was walking down the street. As the captain was walking a person came and told him: You are Captain Obvious! He replied: Thank you CAPTAIN OBVIOUS you are the man!",
  129.         "The captain was walking and he was obvious. He did not know what was going to happen to you in the future. Was he curious? We do not know."]
  130.  
  131.     console.log(captainObvious(input));
  132.  
  133. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement