Shared Digit

Create a function that returns true if each pair of adjacent numbers in an array shares at least one digit and false otherwise.

Examples

sharedDigits([33, 53, 6351, 12, 2242, 44]) ➞ true
// 33 and 53 share 3, 53 and 6351 share 3 and 5, etc.

sharedDigits([1, 11, 12, 13, 14, 15, 16]) ➞ true

sharedDigits([33, 44, 55, 66, 77]) ➞ false

sharedDigits([1, 12, 123, 1234, 1235, 6789]) ➞ false

function sharedDigits(arr) {
	for(let i = 1; i<arr.length; i++ ){
		let str1 = Array.from(new Set(String(arr[i-1]).split(""))).join("");
		let str2 = Array.from(new Set(String(arr[i]).split(""))).join("");
		let set = new Set(str1.split("").concat(str2.split("")));
		if((str1+str2).length === set.size)
			return false;
	}	
	
	return true;
}

Tap Code Translation

Tap code is a way to communicate messages via a series of taps (or knocks) for each letter in the message. Letters are arranged in a 5×5 polybius square, with the letter “K” being moved to the space with “C”.

   1  2  3  4  5
1  A  B C\K D  E
2  F  G  H  I  J
3  L  M  N  O  P
4  Q  R  S  T  U
5  V  W  X  Y  Z

Each letter is translated by tapping out the row and column number that the letter appears in, leaving a short pause in-between. If we use “.” for each tap, and a single space to denote the pause:

text = "break"

"B" = (1, 2) = ". .."
"R" = (4, 2) = ".... .."
"E" = (1, 5) = ". ....."
"A" = (1, 1) = ". ."
"K" = (1, 3) = ". ..."

Another space is added between the groups of taps for each letter to give the final code:

"break" = ". .. .... .. . ..... . . . ..."

Write a function that returns the tap code if given a word, or returns the translated word (in lower case) if given the tap code.

Examples

tapCode("break") ➞ ". .. .... .. . ..... . . . ..."

tapCode(".... ... ... ..... . ..... ... ... .... ....") ➞ "spent"

Notes

For more information on tap code, please see the resources section. The code was widely used in WW2 as a way for prisoners to communicate.

Problem URL

Solution:

Its brute-force one, elegant solution can be derived but I am just too stupid and lazy atm🤪

function tapCode(text) {
  const morseCode = {
    a: ". .",
    b: ". ..",
    c: ". ...",
    d: ". ....",
    e: ". .....",
    f: ".. .",
    g: ".. ..",
    h: ".. ...",
    i: ".. ....",
    j: ".. .....",
    k: ". ...",
    l: "... .",
    m: "... ..",
    n: "... ...",
    o: "... ....",
    p: "... .....",
    q: ".... .",
    r: ".... ..",
    s: ".... ...",
    t: ".... ....",
    u: ".... .....",
    v: "..... .",
    w: "..... ..",
    x: "..... ...",
    y: "..... ....",
    z: "..... ....."
  };

  const revMorse = {
    ". .": "a",
    ". ..": "b",
    ". ...": "c",
    ". ....": "d",
    ". .....": "e",
    ".. .": "f",
    ".. ..": "g",
    ".. ...": "h",
    ".. ....": "i",
    ".. .....": "j",
    "... .": "l",
    "... ..": "m",
    "... ...": "n",
    "... ....": "o",
    "... .....": "p",
    ".... .": "q",
    ".... ..": "r",
    ".... ...": "s",
    ".... ....": "t",
    ".... .....": "u",
    "..... .": "v",
    "..... ..": "w",
    "..... ...": "x",
    "..... ....": "y",
    "..... .....": "z"
  };
  sol = "";
  if (text.indexOf(".") < 0) {
    for (ch of text.split("")) {
      sol += morseCode[ch];
      sol += " ";
    }
  } else {
    let str = text.split(" ");
    for (let i = 0; i < str.length; i += 2) {
      console.log([str[i] + " " + str[i + 1]]);
      sol += revMorse[str[i] + " " + str[i + 1]];
    }
  }
  return sol.trim();
}

 

Stranger Danger

For this challenge, the input will be a (long) string.

A word encountered for the first time is a stranger. A word encountered thrice becomes an acquaintance. A word encountered 5 times becomes a friend.

Create a function that takes the string and returns an array of two arrays. The first is an array of acquaintances in the order they became an acquaintance (see example). The second is an array of friends in the order they became a friend. Words in the friend array should no longer be in the acquaintance array.

Examples

noStrangers("See Spot run. See Spot jump. Spot likes jumping. See Spot fly.")
➞ [["spot", "see"], []]
// "see" was encountered first but "spot" became an acquaintance earlier.

Notes

  • All words should be in lowercase.
  • Punctuation should be stripped except for apostrophes (e.g. doesn’t, aren’t, etc).

URL: Problem Link

Solution:

function noStrangers(str) {
  let aq = new Set();
  let fr = new Set();
  let strArr = str
    .toLowerCase()
    .replace(/[^a-z' ]/g, "")
    .split(" ");
  let uniqueWords = new Set(strArr);
  let wordObj = {};
  for (word of [...uniqueWords]) {
    wordObj[word] = 0;
  }
  //console.log(wordObj);

  for (word of strArr) {
    wordObj[word] += 1;
    if (wordObj[word] >= 3 && wordObj[word] < 5) aq.add(word);
    else if (wordObj[word] >= 5) fr.add(word);
    //console.log("length: ",len, "Word: ", word);
  }

  for (word of fr) {
    aq.delete(word);
  }

  return [[...aq], [...fr]];
}