18
1
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,
othershould becomeOTTTHHHEEERRRbutmothershould 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,
hillshould becomeHIIILLLandbookkeepershould becomeBOOKKKEEPPPEEERRR. - For the purposes of this challenge,
ycounts 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('> '))
2A request for the future: please avoid words and phrases like consonant cluster, coalesce, and elongation. A non-native English speaker like myself might need a dictionary to understand your post. – Dennis – 2017-07-21T15:51:27.427
It should be the vowels that are "elongated" :( – Devil's Advocate – 2017-07-21T19:56:28.437
What is consonant cluster? – MilkyWay90 – 2018-11-11T20:41:12.940