CodeWars:Reversed Words
Challenge Name:
Reversed Words
Challenge Description:
Complete the solution so that it reverses all of the words within the string passed in.
Challenge link:
https://www.codewars.com/kata/51c8991dee245d7ddf00000e/python
Example:
"The greatest victory is that which requires no battle" --> "battle no requires which that is victory greatest The"
Code:
def reverse_words(s):
res=[]
for word in s.split(" "):
res.append(word)
return " ".join(res[::-1])
Explanation:
1.declare an empty list 'res'
2.split 's' using split(" ")
3.append each word to list 'res'
4.reverse the list 'res'
5.join res using join(" ") by adding spaces
If any suggestions and any mistakes please comment !!
0 Comments
Post a Comment