Code Lab

Mode entretien. Minuteur de 15 min, sans indices ni autocomplétion

Count every word

DifficileMap and Set15 min

Write a function countWords(sentence) that counts how many times every word occurs.

  • Return a Map: the key is the word in lower case, the value is the count.
  • A word is a run of letters and digits (Latin and Cyrillic). Spaces and punctuation separate words.
  • Case does not matter: Test and test are the same word.
  • If there are no words, return an empty Map.
  • Key order is not checked.

Exemples

countWords('the cat and the hat')

Renvoie Map(4) { 'the' => 2, 'cat' => 1, 'and' => 1, 'hat' => 1 }

countWords('Test, test!')

Renvoie Map(1) { 'test' => 2 }

Source

Chargement de l'éditeur...
  1. EntréecountWords('the cat and the hat')
    AttenduMap(4) { 'the' => 2, 'cat' => 1, 'and' => 1, 'hat' => 1 }
  2. EntréecountWords('Test test TEST')
    AttenduMap(1) { 'test' => 3 }
  3. EntréecountWords('')
    AttenduMap(0) {}

Les tests cachés se lancent à la soumission