Skip to content

Commit 20c2db0

Browse files
authored
Update reverse_words.py (TheAlgorithms#1825)
The following update results in less lines of code and faster performance while preserving functionality.
1 parent 9b376a5 commit 20c2db0

File tree

1 file changed

+2
-4
lines changed

1 file changed

+2
-4
lines changed

strings/reverse_words.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Created by sarathkaul on 18/11/19
2+
# Edited by farnswj1 on 4/4/20
23

34

45
def reverse_words(input_str: str) -> str:
@@ -13,10 +14,7 @@ def reverse_words(input_str: str) -> str:
1314
input_str = input_str.split(" ")
1415
new_str = list()
1516

16-
for a_word in input_str:
17-
new_str.insert(0, a_word)
18-
19-
return " ".join(new_str)
17+
return ' '.join(reversed(input_str))
2018

2119

2220
if __name__ == "__main__":

0 commit comments

Comments
 (0)