rauf
rauf

Reputation: 43

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']

Upvotes: 2

Views: 107

Answers (1)

Apo
Apo

Reputation: 338

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.

Upvotes: 2

Related Questions