Skip to content

Commit 2565797

Browse files
Sarath Kaulcclauss
Sarath Kaul
authored andcommitted
Reverse Words (TheAlgorithms#1581)
* Word Occurence Script Added * Word Occurence Script Updated * Added doctest using collections.Counter https://docs.python.org/3/library/collections.html#collections.Counter * Reverse Word Script Added * Reverse Word Script Added * Reverse Word Script Added * Reverse Word Script Added * Word Occurence Script Added * Reverse Word Script Added * Reverse Word Script Added * Reverse Words DocTest Updated * Word Occurence Updated * Doctest Updated * Doctest Updated * Doctest Updated
1 parent 0832e1e commit 2565797

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

strings/reverse_words.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Created by sarathkaul on 18/11/19
2+
3+
4+
def reverse_words(input_str: str) -> str:
5+
"""
6+
Reverses words in a given string
7+
>>> sentence = "I love Python"
8+
>>> reverse_words(sentence) == " ".join(sentence.split()[::-1])
9+
True
10+
>>> reverse_words(sentence)
11+
'Python love I'
12+
"""
13+
input_str = input_str.split(" ")
14+
new_str = list()
15+
16+
for a_word in input_str:
17+
new_str.insert(0, a_word)
18+
19+
return " ".join(new_str)
20+
21+
22+
if __name__ == "__main__":
23+
print(reverse_words("INPUT STRING"))

0 commit comments

Comments
 (0)