Skip to content

Commit

Permalink
Create reverse word
Browse files Browse the repository at this point in the history
  • Loading branch information
masterpol authored Jul 31, 2023
1 parent 97faf42 commit 33feeab
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions reverse-word.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"""
Problem: Reverse Words in a String
Given an input string s, reverse the order of the words in the string.
A word is defined as a sequence of non-space characters. The input string does not contain leading or trailing spaces,
and the words are separated by a single space.
Write a function called reverse_words that takes a string s as input and returns a new string with the words reversed.
"""

def reverse_words(s: str) -> str:
l = s.split(" ")
l.reverse()
return " ".join(filter(lambda e : len(e) > 0, l))

print(reverse_words("hello world")) # Output: "world hello"
print(reverse_words("programming is fun")) # Output: "fun is programming"
print(reverse_words(" hello world ")) # Output: "world hello"

0 comments on commit 33feeab

Please sign in to comment.