The body of the question.
Given a string, write a revers_words() function that returns a string that reverses each word in units of spaces. However, it is assumed that the input string is given without a line break.
def reverse_words(s):
Lyrics = "Rabbit, where are you going?"
rev_lyrics = "Are you a kittosan yakitori?"
splitStr = s.split(" ")
splitStr = list(map(lambda x: x[::-1], splitStr))
return print(" ".join(splitStr))
assert reverse_words(lyrics) == rev_lyrics
I tried it like this, but it didn't work, so what should I do?
python
Hello.
I have the text of the question, but there is no expected result. I received s, but it is declared, so I understand that lyrics
should be changed to rev_lyrics
.
If you have each word divided by a space, you can put that word back together.
def reverse_words(s):
SplitStr = s.split(") # Divide by space
splitStr = list (map(lambda str: str[::-1], splitStr)) # Flip
"return" ".join(splitStr) # Reattach and return
If you use it well, you can write it short as follows.
def reverse_words(s):
return " ".join(list(map(lambda str: str[::-1], s.split(" "))))
© 2024 OneMinuteCode. All rights reserved.