Skip to content

Commit 902fe1c

Browse files
realDuYuanChaogithub-actions
and
github-actions
authored
Fixed reverse words algorithm (TheAlgorithms#2469)
* updated reversed words * fixup! Format Python code with psf/black push Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent 4a3b8d6 commit 902fe1c

File tree

3 files changed

+9
-10
lines changed

3 files changed

+9
-10
lines changed

bit_manipulation/binary_and_operator.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# https://www.tutorialspoint.com/python3/bitwise_operators_example.htm
22

3+
34
def binary_and(a: int, b: int):
45
"""
56
Take in 2 integers, convert them to binary,

bit_manipulation/binary_xor_operator.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# https://www.tutorialspoint.com/python3/bitwise_operators_example.htm
22

3+
34
def binary_xor(a: int, b: int):
45
"""
56
Take in 2 integers, convert them to binary,

strings/reverse_words.py

+7-10
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
1-
# Created by sarathkaul on 18/11/19
2-
# Edited by farnswj1 on 4/4/20
3-
4-
51
def reverse_words(input_str: str) -> str:
62
"""
73
Reverses words in a given string
8-
>>> sentence = "I love Python"
9-
>>> reverse_words(sentence) == " ".join(sentence.split()[::-1])
10-
True
11-
>>> reverse_words(sentence)
4+
>>> reverse_words("I love Python")
125
'Python love I'
6+
>>> reverse_words("I Love Python")
7+
'Python Love I'
138
"""
14-
return " ".join(reversed(input_str.split(" ")))
9+
return " ".join(input_str.split()[::-1])
1510

1611

1712
if __name__ == "__main__":
18-
print(reverse_words("INPUT STRING"))
13+
import doctest
14+
15+
doctest.testmod()

0 commit comments

Comments
 (0)