+ 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".
2 Answers
+ 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"))
+ 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"))