Python, 274 285 241 235 225 200 190 183 179 174 166 161
- Fixed last clause (H or W as consonant separators). Ashcraft now has the right result.
- Made the dict smaller
- Formating is smaller (doesn't require python 2.6)
- Simpler dict search for k
- Changed vowel value from '*'
to ''
and .append
to +=[i]
- List comprehension FTW
- Removed call to upper
:D
I Can't golf any further. Actually I did. Now I think I can't golf any further! Did it again...
Using translate table:
def f(n):z=n.translate(65*'_'+'#123#12_#22455#12623#1_2#2'+165*'_').replace('_','');return n[0]+(''.join(('',j)[j>'#']for i,j in zip(z[0]+z,z)if i!=j)+'000')[:3]
Old list comprehension code:
x=dict(zip('CGJKQSXZDTLMNRBFPV','2'*8+'3345561111'))
def f(n):z=[x.get(i,'')for i in n if i not in'HW'];return n[0]+(''.join(j for i,j in zip([x.get(n[0])]+z,z)if i!=j)+'000')[:3]
Old code:
x=dict(zip('CGJKQSXZDTLMNRBFPV','2'*8+'3345561111'))
def f(n):
e=a=[];k=n[0]in x
for i in[x.get(i,'')for i in n.upper()if i not in'HW']:
if i!=a:e+=[i]
a=i
return n[0]+(''.join(e)+'000')[k:3+k]
Test:
[f(i) for i in ['WASHINGTON', 'LEE', 'GUTIERREZ', 'PFSTER', 'JACKSON',
'TYMCZAK', 'VANDEUSEN', 'ASHCRAFT']]
Gives:
['W252', 'L000', 'G362', 'P236', 'J250', 'T522', 'V532', 'A261']
As expected.