Problem 139630 · hard · Level 01 Prerequisites & Setup

The Census Clerk's Sound Code

strings · loops · conditionals · lookup strings · state

Census clerks filed surnames by how they sound, so that Meyer and Maier land in the same drawer. Given name, return its four-character sound code.

First ignore every character that is not a letter, and ignore case. If no letters remain, return "". Letters belong to these groups:

  • group 1: b f p v
  • group 2: c g j k q s x z
  • group 3: d t
  • group 4: l
  • group 5: m n
  • group 6: r
  • vowels: a e i o u y
  • silent: h w
  1. The code starts with the first letter, in upper case.
  2. Go through the remaining letters in order. A group letter adds its digit, unless the last group seen (see rule 3) is the same group; then it adds nothing.
  3. The "last group seen" starts as the group of the first letter (if it has one). Every group letter sets it. A vowel clears it, so the same digit may appear again after a vowel. A silent letter changes nothing: two letters of one group with only h or w between them add a single digit.
  4. Stop once the code has four characters; if it is shorter, add 0s at the end.

Examples

Input:  name = "Fishkin"
Output: "F250"
Explanation: i clears, s -> 2, h is silent, k is group 2 again (skipped), i clears, n -> 5, pad with 0.

Input:  name = "Pfeiffer"
Output: "P160"
Explanation: the first f is in P's own group, so it adds nothing; after the vowels, f -> 1 and r -> 6.

Input:  name = "O'Hara"
Output: "O600"

Constraints

  • 0 <= len(name) <= 100; name holds ASCII letters, spaces, hyphens and apostrophes

Goals

  • Translate letters to digit groups with a lookup
  • Track the previous code carefully when some letters separate and others do not
  • Pad or cut the result to a fixed length
Starting Python…