Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <script>
- function magicGrid(input) {
- var encryptedString = input[0];
- var magicNumber = parseInt(input[1]);
- var matrix = input.slice(2);
- var rows = matrix.length;
- var flattenedMatrixString = matrix.join(" ");
- var flattenedMatrixArray = flattenedMatrixString.split(" ").map(Number);
- var row_col = findPositionBySum(magicNumber, flattenedMatrixArray, rows)
- var key = row_col[0]["row"] + row_col[0]["col"] + row_col[1]["row"] + row_col[1]["col"];
- var output = "";
- for (var i = 0; i < encryptedString.length; i++) {
- var ascii = encryptedString.charCodeAt(i);
- var asciiResult = i % 2 == 0 ? ascii + key : ascii - key;
- output += String.fromCharCode(asciiResult);
- }
- function findPositionBySum(sum, array, rows) {
- var retvalTupple = [
- {
- "row": -1,
- "col": -1
- },
- {
- "row": -1,
- "col": -1
- }
- ];
- for (var i = 0; i < array.length; i++) {
- for (var j = i + 1; j < array.length; j++) {
- if (array[i] + array[j] == sum) {
- retvalTupple[0]["row"] = parseInt(i / rows);
- retvalTupple[0]["col"] = parseInt(i % rows);
- retvalTupple[1]["row"] = parseInt(j / rows);
- retvalTupple[1]["col"] = parseInt(j % rows);
- break;
- }
- }
- }
- return retvalTupple;
- }
- return output;
- }
- var input = ["QqdvSpg",
- "400",
- "100 200 120",
- "120 300 310",
- "150 290 370"
- ];
- console.log(magicGrid(input));
- /***********************************************/
- function captainObvious(input) {
- var firstText = input[0];
- var secondText = input[1];
- var wordsFirstText = firstText.split(/\W/)
- .filter(function(word) {
- return !!word;
- })
- .map(function(word) {
- return word.toLowerCase();
- });
- var wordsFirstTextOccurences = {};
- wordsFirstText.forEach(function(word) {
- if (!(word in wordsFirstTextOccurences)) {
- wordsFirstTextOccurences[word] = 0;
- }
- wordsFirstTextOccurences[word]++;
- });
- var neededWords = Object.keys(
- wordsFirstTextOccurences
- ).filter(function(word) {
- return wordsFirstTextOccurences[word] >= 3;
- });
- var allRepeatedWordsCount = neededWords.length;
- if (allRepeatedWordsCount < 1) {
- return "No words";
- }
- var output = "";
- var senteces = secondText.split(/[.?!]/)
- .filter(function(sentece) {
- return !!sentece;
- });
- senteces.forEach(function(sentence) {
- var localWords = sentence.split(/\W/)
- .filter(function(word) {
- return !!word;
- });
- var occurred = 0;
- neededWords.forEach(function(word) {
- if (localWords.indexOf(word) > -1) {
- occurred++;
- }
- });
- if (occurred == 2 || occurred == allRepeatedWordsCount) {
- output += sentence.trim();
- var senteceIndex = secondText.indexOf(sentence);
- var charPosition = senteceIndex + sentence.length;
- var sentenceEndingChar = secondText[charPosition];
- secondText = secondText.substring(charPosition+1, secondText.length);
- output += sentenceEndingChar + "\n";
- }
- });
- if (output.trim() == "") {
- return "No sentences";
- }
- return output.trim();
- }
- input =
- ["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!",
- "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."]
- console.log(captainObvious(input));
- </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement