The Soundex algorithm is a phonetic algorithm developed nearly 100 years ago for indexing names based on how they are pronounced in English. The aim is to encode homophones to the same representation, allowing for matching despite minor spelling variations.
Task Overview
In this task, you will implement a variation of the Soundex algorithm known as "American Soundex." Your goal is to encode strings according to the following steps:
Save the first letter of the name.
Remove all occurrences of h and w except for the first letter.
Replace consonants (including the first letter) with digits based on the following mapping:
b, f, p, v → 1
c, g, j, k, q, s, x, z → 2
d, t → 3
l → 4
m, n → 5
r → 6
Replace adjacent same digits with a single digit.
Remove all occurrences of a, e, i, o, u, y except for the first letter.
If the first symbol is a digit, replace it with the letter saved in step 1.
Append three zeros if the result contains less than three digits.
Retain only the first letter and the three digits that follow it.
Input Format
A space-separated string of one or more names.
Example: Sarah Connor
Output Format
A space-separated string of equivalent Soundex codes.
The first character of each Soundex code must be uppercase.
Example: S600 C560
Example
Input:Sarah Connor
Output:S600 C560
Implementation
Please place your solution in the /app/ directory.
Important: place your implementation at /app/solution.py so the verifier can run it.