0

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!

3
  • 2
    You need at least return iterrepl(string) otherwise what's it returning? Commented Feb 26, 2020 at 0:10
  • 1
    A generally more straightforward approach without recursion would be to use "re.sub" with a function as replacement. Commented Feb 26, 2020 at 0:14
  • 2
    Or let str.translate do the work for you. trans=str.maketrans({ 'a' : 'FIRST VOWEL', 'e' : 'SECOND VOWEL', 'i' : 'THIRD VOWEL' });print("abcdefghijklmn".translate(trans)) Commented Feb 26, 2020 at 0:18

2 Answers 2

1

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
>
Sign up to request clarification or add additional context in comments.

Comments

0

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))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.