Skip to content

Reverse Words #1581

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Nov 18, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions strings/reverse_words.py
Original file line number Diff line number Diff line change
@@ -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)
Comment on lines +14 to +19
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just got the idea that return " ".join(input_str[::-1]) would have probably worked the same way.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line 8

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed it being used as a doctest, and NOT the actual function. 😅 Hence, the comment.



if __name__ == "__main__":
print(reverse_words("INPUT STRING"))