Two Distinct Elements [Edabit]

Edabit

In each input array, every number repeats at least once, except for two. Write a function that returns the two unique numbers.

Examples

returnUnique([1, 9, 8, 8, 7, 6, 1, 6]) ➞ [9, 7]

returnUnique([5, 5, 2, 4, 4, 4, 9, 9, 9, 1]) ➞ [2, 1]

returnUnique([9, 5, 6, 8, 7, 7, 1, 1, 1, 1, 1, 9, 8]) ➞ [5, 6]

Notes

Keep the same ordering in the output.

Solution:

function returnUnique(arr) {
  let sol = [];
  for (let i = 0; i < arr.length; i++) {
    if (arr.lastIndexOf(arr[i]) === arr.indexOf(arr[i])) {
      sol.push(arr[i]);
    }
  }
  return sol;
}

Divide Array into Chunks [Edabit]

Edabit

Write a function that divides an array into chunks of size n, where n is the length of each chunk.

Examples

chunkify([2, 3, 4, 5], 2) ➞ [[2, 3], [4, 5]]

chunkify([2, 3, 4, 5, 6], 2) ➞ [[2, 3], [4, 5], [6]]

chunkify([2, 3, 4, 5, 6, 7], 3) ➞ [[2, 3, 4], [5, 6, 7]]

chunkify([2, 3, 4, 5, 6, 7], 1) ➞ [[2], [3], [4], [5], [6], [7]]

chunkify([2, 3, 4, 5, 6, 7], 7) ➞ [[2, 3, 4, 5, 6, 7]]

Notes

  • It’s O.K. if the last chunk is not completely filled (see example #2).
  • Integers will always be single-digit.

Solution:

function chunkify(arr, size) {
  let sol = [];
  while (arr.length) {
    let temp = [];
    let len = size;
    while (len--) {
      if (!arr.length) break;
      temp.push(arr.shift());
    }
    sol.push(temp);
  }
  return sol;
}
function chunkify(arr, size) {
  let sol = [];
  while (arr.length) {
    sol.push(arr.splice(0,size));
  }
  return sol;
}

Remove the Last Vowel [Edabit]

Edabit

Write a function that removes the last vowel in each word in a sentence.

Examples

removeLastVowel("Those who dare to fail miserably can achieve greatly.")
➞ "Thos wh dar t fal miserbly cn achiev gretly."

removeLastVowel("Love is a serious mental disease.")
➞ "Lov s  serios mentl diseas"

removeLastVowel("Get busy living or get busy dying.")
➞ "Gt bsy livng r gt bsy dyng"

Notes

Vowels are: a, e, i, o, u (both upper and lowercase).

Solution:

function removeLastVowel(str) {
  let strArr = str.split(" ").map((word) => {
    let sol = "",
      flag = false;
    for (let i = word.length - 1; i >= 0; i--) {
      if ("aeiouAEIOU".includes(word[i]) && !flag) {
        sol += "";
        flag = true;
      } else {
        sol += word[i];
      }
    }
    return sol.split("").reverse().join("");
  });
  return strArr.join(" ");
}

Switching Between Pencils [Edabit]

Edabit

When coloring a striped pattern, you may start by coloring each square sequentially, meaning you spend time needing to switch coloring pencils.

Create a function where given an array of colors cols, return how long it takes to color the whole pattern. Note the following times:

  • It takes 1 second to switch between pencils.
  • It takes 2 seconds to color a square.

See the example below for clarification.

color_pattern_times(["Red", "Blue", "Red", "Blue", "Red"]) ➞ 14

// There are 5 colors so it takes 2 seconds to color each one (2 x 5 = 10).
// You need to switch the pencils 4 times and it takes 1 second to switch (1 x 4 = 4).
// 10 + 4 = 14

Examples

colorPatternTimes(["Blue"]) ➞ 2

colorPatternTimes(["Red", "Yellow", "Green", "Blue"]) ➞ 11

colorPatternTimes(["Blue", "Blue", "Blue", "Red", "Red", "Red"]) ➞ 13

Notes

  • Only change coloring pencils if the next color is different to the color before it.
  • Return a number in seconds.

Solution:

function colorPatternTimes(cols) {
  let arr = [cols[0]];
  for (let col of cols) {
    if (arr[arr.length - 1] !== col) arr.push(col);
  }
  return arr.length - 1 + cols.length * 2;
}

isCryptSolution [CodeSignal]

CodeSignal

cryptarithm is a mathematical puzzle for which the goal is to find the correspondence between letters and digits, such that the given arithmetic equation consisting of letters holds true when the letters are converted to digits.

You have an array of strings crypt, the cryptarithm, and an an array containing the mapping of letters and digits, solution. The array crypt will contain three non-empty strings that follow the structure: [word1, word2, word3], which should be interpreted as the word1 + word2 = word3 cryptarithm.

If crypt, when it is decoded by replacing all of the letters in the cryptarithm with digits using the mapping in solution, becomes a valid arithmetic equation containing no numbers with leading zeroes, the answer is true. If it does not become a valid arithmetic solution, the answer is false.

Note that number 0 doesn’t contain leading zeroes (while for example 00 or 0123 do).

Example

For crypt = ["SEND", "MORE", "MONEY"] and

solution = [['O', '0'],
            ['M', '1'],
            ['Y', '2'],
            ['E', '5'],
            ['N', '6'],
            ['D', '7'],
            ['R', '8'],
            ['S', '9']]

the output should be
solution(crypt, solution) = true.

When you decrypt "SEND""MORE", and "MONEY" using the mapping given in crypt, you get 9567 + 1085 = 10652 which is correct and a valid arithmetic equation.

For crypt = ["TEN", "TWO", "ONE"] and

solution = [['O', '1'],
            ['T', '0'],
            ['W', '9'],
            ['E', '5'],
            ['N', '4']]

the output should be
solution(crypt, solution) = false.

Even though 054 + 091 = 145054 and 091 both contain leading zeroes, meaning that this is not a valid solution.

Solution:

function solution(crypt, solution) {
  let solObj = {};
  for (let sol of solution) {
    solObj[sol[0]] = sol[1];
  }
  let cryptWords = [];
  for (let word of crypt) {
    let temp = "";
    for (let ch of word) {
      temp += solObj[ch];
    }
    cryptWords.push(temp);
  }
  console.log(cryptWords);
  let [num1, num2, num3] = cryptWords.map((n) => Number(n));
  let [len1, len2, len3] = cryptWords.map((n) => n.length);
  if (len1 === 1 && len2 === 1 && len3 === 1) {
    return num1 + num2 === 0;
  }
  if (
    cryptWords[0][0] !== "0" &&
    cryptWords[1][0] !== "0" &&
    cryptWords[2][0] !== "0"
  ) {
    return num1 + num2 === num3;
  }
  return false;
}

Count the Lone Ones [Edabit]

Edabit

Create a function which counts how many lone 1s appear in a given number. Lone means the number doesn’t appear twice or more in a row.

Examples

countLoneOnes(101) ➞ 2

countLoneOnes(1191) ➞ 1

countLoneOnes(1111) ➞ 0

countLoneOnes(462) ➞ 0

Notes

Tests will include positive whole numbers only.

Solution:

function countLoneOnes(n) {
  let arr = n
    .toString()
    .split("")
    .map((n) => Number(n));
  arr.push(0);
  arr.unshift(0);
  let sol = 0;
  for (let i = 1; i < arr.length - 1; i++) {
    if (arr[i] == 1) {
      if (arr[i] !== arr[i - 1] && arr[i] !== arr[i + 1]) {
        sol++;
      }
    }
  }
  return sol;
}