A python function to shift the positions of a vowel by 2 places?

Question:

r task is to shift the vowels by two positions contained in the string, where vowels are
the symbols a e i o u (and their capitalised versions A E I O U), If the input is "a cat", the output is "i cit"

vowels = ["a", "e", "i", "o", "u"]
vowel=['a','e','i','o','u']

mapper = dict(zip(vowels, vowels[1:] + [vowels[0]]))
message=input('enter mesaage')

new_message = ""
for letter in message:
    if letter not in vowels:
        new_message += letter
    else:
        i = vowels.index(letter)
        i+=2
        new_message += vowels[i]
print(new_message)

this throws an index out of bound error

Asked By: Kabir Gujral

||

Answers:

vowels = ["a", "e", "i", "o", "u", "a", "e", "i", "o", "u"]

message=input('enter mesaage')

new_message = ""
for letter in message:
    if letter not in vowels:
        new_message += letter
    else:
        i = vowels.index(letter)
        i+=2
        new_message += vowels[i]
print(new_message)

This gives me maad for mood if that is what you wanted

Answered By: sos

You need a function count_repetition which tells you how many consecutive vowels there are starting at any position. This is also the number of vowels in vowels array that you should skip. The new vowel can be found using vowels[(steps+i+1)%size] where size is the length of vowels array and i is the index of the original vowel.

vowels = ["a", "e", "i", "o", "u"]


def count_repetition(message, i):
    # counts the number of consecutive chars starting at index i
    count = 1
    for j in range(i, len(message)-1):
        if(message[j] == message[j+1]):
            count += 1
        else:
            return count
    return count


message = input('enter message: ')
new_message = ""
size = len(vowels)
j = 0

while(j < len(message)):
    letter = message[j]
    if letter.lower() not in vowels:
        new_message += letter
        j += 1
    else:
        i = vowels.index(letter)
        # count number of consecutive letters
        steps = count_repetition(message, j)
        for _ in range(0, steps):
            new_message += vowels[(steps+i+1) % size]
        j += steps

print(new_message)
# meeduuu -> muudooo
# a cat -> i cit
# meed -> muud
# maad -> mood

Version 2 preserving capitalisation

def count_repetition(message, i):
    # counts the number of consecutive chars starting at index i, ignoring
    # uppercase/lowercase
    count = 1
    for j in range(i, len(message)-1):
        if(message[j].lower() == message[j+1].lower()):
            count += 1
        else:
            return count
    return count


def changeLetterCase(letter, toUpper):
    # changes letter to lowercase or uppercase
    # changeLetterCase('a', True) => 'A'
    # changeLetterCase('A', False) => a'
    if toUpper:
        return letter.upper()
    return letter.lower()


def solve(message):
    vowels = ["a", "e", "i", "o", "u"]
    SIZE = len(vowels)

    new_message = ""
    j = 0

    while(j < len(message)):

        letter = message[j].lower()
        isUpperCase = message[j].isupper()

        if letter not in vowels:
            new_message += changeLetterCase(letter, isUpperCase)
            j += 1
        else:
            # get position of current vowel in `vowels`
            i = vowels.index(letter)

            # count number of consecutive letters
            steps = count_repetition(message, j)

            # get replacement vowel
            new_vowel = vowels[(steps+i+1) % SIZE]

            # update consecutive vowels taking into consideration
            # uppercase/lowercase
            for step in range(0, steps):
                isUpperCase = message[j].isupper()

                new_message += changeLetterCase(
                    new_vowel, isUpperCase)
                j += 1
    return new_message


print(solve('mUUd'))  # mIId
print(solve('maaApp'))  # muuUpp
print(solve('a cat'))  # i cit
print(solve('meed'))  # muud
print(solve('maaaad'))  # maaaad
Answered By: Bunny