-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
19 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |