Reputation: 174
I have got a string alphabet = "abcdefghijklmn"
and would like to replace each vowel in the string with values from a substitution matrix matrix = { 'a' : 'FIRST VOWEL', 'e' : 'SECOND VOWEL', 'i' : 'THIRD VOWEL' }
. I guess this is a case for recursion, so I tried the following function:
import re
def iterrepl(string):
m = re.search(r'[aeiou]',string)
if m:
string = string[:m.start()]+matrix.get(m.group())+string[m.end():]
iterrepl(string)
else:
return(string)
matrix = { 'a' : 'FIRST VOWEL', 'e' : 'SECOND VOWEL', 'i' : 'THIRD VOWEL' }
alphabet = "abcdefghijklmn"
print(iterrepl(alphabet))
The final result, however, is None
, whereas I was hoping for FIRST VOWELbcdSECOND VOWELfghTHIRD VOWELjklmn
. When I add a print
statement before calling iterrepl
within the function, I can see that the result of the last but one recursion is the desired one:
FIRST VOWELbcdefghijklmn
FIRST VOWELbcdSECOND VOWELfghijklmn
FIRST VOWELbcdSECOND VOWELfghTHIRD VOWELjklmn
None
I cannot figure out what is wrong with the end of the recursion and would appreciate any advice!
Upvotes: 0
Views: 48
Reputation: 41872
Assuming recursion is, as you imply, your choice, and not a requirement, then you're making this harder than necessary as Python has builtin functions for this:
matrix = {'a': 'FIRST VOWEL', 'e': 'SECOND VOWEL', 'i': 'THIRD VOWEL'}
alphabet = "abcdefghijklmn"
translation_table = str.maketrans(matrix)
print(alphabet.translate(translation_table))
OUTPUT
> python3 test1.py
FIRST VOWELbcdSECOND VOWELfghTHIRD VOWELjklmn
>
Or, we could simply do:
matrix = {'a': 'FIRST VOWEL', 'e': 'SECOND VOWEL', 'i': 'THIRD VOWEL'}
alphabet = "abcdefghijklmn"
for old, new in matrix.items():
alphabet = alphabet.replace(old, new)
print(alphabet)
OUTPUT
> python3 test2.py
FIRST VOWELbcdSECOND VOWELfghTHIRD VOWELjklmn
>
Or if it must be recursive, then perhaps something like:
matrix = {'a': 'FIRST VOWEL', 'e': 'SECOND VOWEL', 'i': 'THIRD VOWEL'}
alphabet = "abcdefghijklmn"
def replace_vowels(string):
def replace_vowels_recursive(string, dictionary):
if dictionary:
(vowel, replacement), *rest = dictionary.items()
return replace_vowels_recursive(string.replace(vowel, replacement), dict(rest))
return string
return replace_vowels_recursive(string, matrix)
print(replace_vowels(alphabet))
OUTPUT
> python3 test3.py
FIRST VOWELbcdSECOND VOWELfghTHIRD VOWELjklmn
>
Upvotes: 1
Reputation: 11
Your question is not very clear, however if your desired return from the function is "FIRST VOWELbcdSECOND VOWELfghTHIRD VOWELjklmn" then try this:
def iterrepl(string):
string_arr = []
for x in range(0, len(string)):
string_arr.append(string[x:x+1]) #enter every char into a list.
for y in range(0, len(matrix)):
if string_arr[x] == matrix.keys()[y]: #search list for match.
string_arr[x] = matrix[matrix.keys()[y]] #found. replace list item and break loop.
break
return str(''.join(string_arr)) #convert back to string.
matrix = { 'a' : 'FIRST VOWEL', 'e' : 'SECOND VOWEL', 'i' : 'THIRD VOWEL' }
alphabet = "abcdefghijklmn"
print(iterrepl(alphabet))
Upvotes: 0