Used Python 3 for a solution to a Reddit /r/DailyProgrammer challenge.
When we Swedes are young, we are taught a SUPER-SECRET language that only kids know, so we can hide secrets from our confused parents. This language is known as "Rövarspråket" (which means "Robber's language", more or less), and is surprisingly easy to become fluent in, at least when you're a kid...
Rövarspråket is not very complicated: you take an ordinary word and replace the consonants with the consonant doubled and with an "o" in between. So the consonant "b" is replaced by "bob", "r" is replaced with "ror", "s" is replaced with "sos", and so on. Vowels are left intact. It's made for Swedish, but it works just as well in English. Your task today is to write a program that can encode/decode a string of text into Rövarspråket.
Encoder:
import string
def main():
word = input('Enter Phrase : ')
final_word = rövarspråket(word)
print(final_word)
def is_vowel(letter):
VOWELS = ['A', 'E', 'I', 'O', 'U', 'Y', 'Å', 'Ä', 'Ö']
if str.upper(letter) in VOWELS:
return True
else:
return False
def rövarspråket(word):
new_word = []
for character in word:
if character.isalpha() and not is_vowel(character):
new_word.append(character + 'o' + str.lower(character))
else:
new_word.append(character)
return ''.join(new_word)
if __name__ == '__main__':
main()
Decoder program is identical to the Encoder except for a different central function of course:
def decode_rövarspråket(word):
original_word = []
i = 0
while i <= (len(word) - 1):
original_word.append(word[i])
if word[i].isalpha() and not is_vowel(word[i]):
i += 3
else:
i += 1
return ''.join(original_word)
I'm newer to Python and one of my main concerns is writing code that is idiomatic and properly "Pythonic". Another is the loop in the decoder program. The algorithm I decided to go with revolves around iterating one character for vowels and three for consonants (to skip the o and repeated letter). Using a for-loop seemed like it would involve jumping through more hoops than just biting the bullet and using a while instead.