+ 1

what is the solution of this exercise

Write a function translate() that will translate a text into "rövarspråket" (Swedish for "robber's language"). That is, double every consonant and place an occurrence of "o" in between. For example, translate("this is fun") should return the string"tothohisos isos fofunon".

20th Oct 2016, 9:22 PM
Djaber Meghar
Djaber Meghar - avatar
2 odpowiedzi
+ 4
def translate(s): r = "" for c in s: r += c if c in "bcdfghjklmnpqrstvwxz": r += 'o' + c return r print(translate("this is fun"))
20th Oct 2016, 9:38 PM
Zen
Zen - avatar
+ 2
We can also use regular expressions to write a one-liner. from re import sub def translate(s): return sub("[bcdfghjklmnpqrstvwxz]", lambda mo: mo.group(0)+'o'+mo.group(0), s) print(translate("this is fun"))
20th Oct 2016, 9:56 PM
novice