Codewars 6 kyu The Vowel Code
https://www.codewars.com/kata/53697be005f803751e0015aa/train/python
Task
Step 1: Create a function called encode() to replace all the lowercase vowels in a given string with numbers.
Step 2: Now create a function called decode() to turn the numbers back into vowels.
Verbalization
As encode(), replace a -> 1, e -> 2, i -> 3, o -> 4, u -> 5
As decode(), replace 1 -> a, 2 -> e, 3 -> i, 4 -> o, 5 -> u
Code
def encode(st):
st = st.replace('a', '1')
st = st.replace('e', '2')
st = st.replace('i', '3')
st = st.replace('o', '4')
st = st.replace('u', '5')
return st
def decode(st):
st = st.replace('1', 'a')
st = st.replace('2', 'e')
st = st.replace('3', 'i')
st = st.replace('4', 'o')
st = st.replace('5', 'u')
return st
Other solution.
#use dictionary
def encode(st):
encode_dict = {'a': '1', 'e': '2', 'i': '3', 'o': '4', 'u': '5'}
result = ""
for char in st:
result += encode_dict.get(char, char)
return result
def decode(st):
decode_dict = {'1': 'a', '2': 'e', '3': 'i', '4': 'o', '5': 'u'}
result = ""
for char in st:
result += decode_dict.get(char, char)
return result