Advertisement
dimipan80

Cognate Words (on JavaScript)

Dec 16th, 2014
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* You are a given string in a single line. Assume “words” are all sequences of Latin letters.
  2.  For example in the input string "java..?|basics/*-+=javabasics" we have 3 words: "java", "basics" and "javabasics".
  3.  Write a program to find in the input string all unique sets of 3 “words” {a, b, c}, such that a|b = c.
  4.  Assume that "a|b" means to concatenate the “word” b after a. We call these “words” {a, b, c} cognate words.
  5.  All “words” must be case sensitive! Don't repeat the cognate words in the output.
  6.  Print at the console all cognate words {a, b, c} found in the input sequence in format "a|b=c" (without any spaces),
  7.  each at a separate line. The order of the output lines is not important. Print "No" in case no cognate words exist
  8.  among the input sequence of characters. */
  9.  
  10. "use strict";
  11.  
  12. function findUniqueSetsOf3Words(args) {
  13.     args = args[0].split(/[^a-z]+/i).filter(Boolean);
  14.  
  15.     var cognateWords = [];
  16.     if (args && args.length > 2) {
  17.         var i, j, k;
  18.         for (i = 0; i < args.length; i += 1) {
  19.             for (j = 0; j < args.length; j += 1) {
  20.                 for (k = 0; k < args.length; k += 1) {
  21.                     if (j != i && k != j && k != i) {
  22.                         if (args[i].concat(args[j]) === args[k] && cognateWords.indexOf(args[k]) < 0) {
  23.                             console.log("%s|%s=%s", args[i], args[j], args[k]);
  24.                             cognateWords.push(args[k]);
  25.                         }
  26.                     }
  27.                 }
  28.             }
  29.         }
  30.     }
  31.  
  32.     if (!cognateWords.length) {
  33.         console.log('No');
  34.     }
  35. }
  36.  
  37. findUniqueSetsOf3Words(["java..?|basics/*-+=javabasics"]);
  38. findUniqueSetsOf3Words(["Hi, Hi, Hihi"]);
  39. findUniqueSetsOf3Words(["Uni(lo,.ve=I love SoftUni (Soft)"]);
  40. findUniqueSetsOf3Words(["a a aa a"]);
  41. findUniqueSetsOf3Words(["x a ab b aba a hello+java a b aaaaa"]);
  42. findUniqueSetsOf3Words(["aa bb bbaa"]);
  43. findUniqueSetsOf3Words(["ho hoho"]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement