Wave mechanism in python

I tried to apply wave to my assigned word. But I faced with the situation that it is needed to handle duplicate characters. Can someone helps me for this case?

def wave(people):
    return [people.replace(people[i], people[i].upper()) for i in range(len(people))]

wave("letter")

Output:

['Letter', 'lEttEr', 'leTTer', 'leTTer', 'lEttEr', 'letteR']

Real output should be:

['Letter', 'lEtter', 'leTter', 'letTer', 'lettEr', 'letteR']

1 answer

  • answered 2021-06-19 19:13 Apo

    return [people[:i] + people[i].upper() + people[i+1:] for i in range(len(people))]
    

    the issue with your version is that you have multiple instances of the same letter and the replace method applies change on all the same letters at once.