Files
freeCodeCamp/curriculum/challenges/german/22-rosetta-code/rosetta-code-challenges/abc-problem.md
2024-01-24 19:52:36 +01:00

2.3 KiB

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
594810f028c0303b75339acc ABC-Problem 1 302220 abc-problem

--description--

You are given a collection of ABC blocks (e.g., childhood alphabet blocks). There are 20 blocks with two letters on each block. A complete alphabet is guaranteed amongst all sides of the blocks. The sample collection of blocks:

(B O)
(X K)
(D Q)
(C P)
(N A)
(G T)
(R E)
(T G)
(Q D)
(F S)
(J W)
(H U)
(V I)
(A N)
(O B)
(E R)
(F S)
(L Y)
(P C)
(Z M)

--instructions--

Implementiere eine Funktion, die einen String (Wort) nimmt und bestimmt, ob das Wort mit der angegeben Sammlung von Blöcken geschrieben werden kann.

Einige Regeln sind zu beachten:

  • Once a letter on a block is used, that block cannot be used again.
  • Bei der Funktion sollte die Groß- und Kleinschreibung nicht beachtet werden.

--hints--

canMakeWord sollte eine Funktion sein.

assert(typeof canMakeWord === 'function');

canMakeWord sollte einen Boolean zurückgeben.

assert(typeof canMakeWord('hi') === 'boolean');

canMakeWord("bark") sollte true zurückgeben.

assert(canMakeWord(words[0]));

canMakeWord("BooK") sollte false zurückgeben.

assert(!canMakeWord(words[1]));

canMakeWord("TReAT") sollte true zurückgeben.

assert(canMakeWord(words[2]));

canMakeWord("COMMON") sollte false zurückgeben.

assert(!canMakeWord(words[3]));

canMakeWord("squAD") sollte true zurückgeben.

assert(canMakeWord(words[4]));

canMakeWord("conFUSE") sollte true zurückgeben.

assert(canMakeWord(words[5]));

--seed--

--after-user-code--

const words = ['bark', 'BooK', 'TReAT', 'COMMON', 'squAD', 'conFUSE'];

--seed-contents--

function canMakeWord(word) {

}

--solutions--

function canMakeWord(word) {
  const characters = 'BO XK DQ CP NA GT RE TG QD FS JW HU VI AN OB ER FS LY PC ZM';
  const blocks = characters.split(' ').map(pair => pair.split(''));

  const letters = [...word.toUpperCase()];
  let length = letters.length;
  const copy = new Set(blocks);

  letters.forEach(letter => {
    for (let block of copy) {
      const index = block.indexOf(letter);

      if (index !== -1) {
        length--;
        copy.delete(block);
        break;
      }
    }
  });
  return !length;
}