def soundex(name, size=4):
"""
:type name:str
:rtype:str
:param name:
:param size:
:return:
"""
soundex_digits = '01230120022455012623010202'
sndx = ''
fc = ''
for c in name.upper():
if c.isalpha():
if not fc:
fc = c
d = soundex_digits[ord(c) - ord('A')]
if not sndx or d != sndx[-1]:
sndx += d
sndx = fc + sndx[1:]
sndx = sndx.replace('0', '')
return (sndx + size * '0')[:size]
print soundex('white')
print soundex('wood')
soundex算法