Write a function that takes a string of letters and spaces (no need to handle non-letters) and performs the ANNOUNCER VOICE translation algorithm as follows:
- First, uppercase everything.
- For each word,
- Elongate each consonant cluster by tripling each letter; except, if the word begins with a consonant cluster, do not elongate that cluster. For example,
other
should becomeOTTTHHHEEERRR
butmother
should becomeMOTTTHHHEEERRR
. - Elongate the final vowel by tripling it.
- Elongate each consonant cluster by tripling each letter; except, if the word begins with a consonant cluster, do not elongate that cluster. For example,
- In both cases of elongation, if you're tripling a letter, first coalesce it with duplicate letters on either side. For example,
hill
should becomeHIIILLL
andbookkeeper
should becomeBOOKKKEEPPPEEERRR
. - For the purposes of this challenge,
y
counts as a consonant. - Clarification/simplification: You may assume that each pair of words is separated by a single space, and that the input contains no consecutive spaces, and that the input will not be the empty string.
- Shortest code wins!
Test vectors:
> sunday sunday
SUNNNDDDAAAYYY SUNNNDDDAAAYYY
> mia hamm
MIAAA HAAAMMM
> chester alan arthur
CHESSSTTTEEERRR ALLLAAANNN ARRRTTTHHHUUURRR
> attention please
ATTTENNNTTTIOOONNN PLEASSSEEE
> supercalifragilisticexpialidocious
SUPPPERRRCCCALLLIFFFRRRAGGGILLLISSSTTTICCCEXXXPPPIALLLIDDDOCCCIOUUUSSS
> moo
MOOO
> Aachen
AACCCHHHEEENNN
> Oooh
OOOHHH
> grifffest
GRIFFFEEESSSTTT
> k
K
> aaaabbbbc
AAAABBBBCCC
Here's a reference implementation which I would move to an answer except that as of this morning the question's been closed. :P
import itertools,re
def j(s):return re.match('^[AEIOU]+$',s)
def c(s):return ''.join(sum(([h,h,h]for h in[k for k,g in itertools.groupby(s)]),[]))
def v(s):
while len(s)>=2 and s[-2]==s[-1]:s=s[:-1]
return s+s[-1]+s[-1]
def a(n):
r=''
for w in n.split():
if r:r+=' '
ss=re.split('([AEIOU]+)', w.upper())
for i,s in enumerate(ss):
r += [v(s),s][any(j(t) for t in ss[i+1:])]if j(s)else[s,c(s)][i>0]
return r
while 1:print a(raw_input('> '))