mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-17 14:58:10 +00:00
Reverse Words (#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
This commit is contained in:
parent
0832e1ec58
commit
2565797504
23
strings/reverse_words.py
Normal file
23
strings/reverse_words.py
Normal file
|
@ -0,0 +1,23 @@
|
|||
# Created by sarathkaul on 18/11/19
|
||||
|
||||
|
||||
def reverse_words(input_str: str) -> str:
|
||||
"""
|
||||
Reverses words in a given string
|
||||
>>> sentence = "I love Python"
|
||||
>>> reverse_words(sentence) == " ".join(sentence.split()[::-1])
|
||||
True
|
||||
>>> reverse_words(sentence)
|
||||
'Python love I'
|
||||
"""
|
||||
input_str = input_str.split(" ")
|
||||
new_str = list()
|
||||
|
||||
for a_word in input_str:
|
||||
new_str.insert(0, a_word)
|
||||
|
||||
return " ".join(new_str)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(reverse_words("INPUT STRING"))
|
Loading…
Reference in New Issue
Block a user