-1

I'm trying to create code for the following problem:

In this simple Kata your task is to create a function that turns a string into a Mexican Wave. You will be passed a string and you must return that string in an array where an uppercase letter is a person standing up.

Following is my tenative solution to the problem:

def wave(people):
    newstring = [*people]
    final=[]
    for i in range(len(newstring)):
        newstring[i].title()
        final.append("".join(newstring))
    return final

The example 'should' return:

wave("hello") => ["Hello", "hEllo", "heLlo", "helLo", "hellO"]

whereas mine returns;

['hello', 'hello', 'hello', 'hello', 'hello']

I'm new to coding, but the way it works in my head is that I split up the string, iterate through my loop capitalizing the character corresponding to the loop number, then I append the newly joined string to my empty list. This loop continues supposedly to go through the same string, capitalizing the next character, and adding to the "final" list. This (in my head at least) should produce the example return above. Unfortunately, I get my output as above. What gives?

Thanks

  • 1
    The method `title` returns a new string but doesn't modify the string on which it is called (no method does this, by the way). – Michael Butscher Apr 21 '23 at 21:06
  • Does this answer your question? [Why doesn't calling a string method (such as .replace or .strip) modify (mutate) the string?](https://stackoverflow.com/questions/9189172/why-doesnt-calling-a-string-method-such-as-replace-or-strip-modify-mutate) – Pranav Hosangadi Apr 21 '23 at 21:17
  • 1
    Also remember that you need to change the character _back_ to lowercase when you're done with it. And you can just do `.upper()` instead of `.title()`, since that makes it more clear what you want to do (although both have the same effect on a 1-character string) – Pranav Hosangadi Apr 21 '23 at 21:18

1 Answers1

0

newstring[i].title() does not modify the newstring variable. You have to do something like this:

def wave(people):
    newstring = [*people]
    final=[]
    for i in range(len(newstring)):
        newstring = [*people]
        newstring[i] = newstring[i].title()
        final.append("".join(newstring))
    return final
wave("hello")

Output:

['Hello', 'hEllo', 'heLlo', 'helLo', 'hellO']
Rayken
  • 121
  • 4