Fixed reverse words algorithm (#2469)

* updated reversed words

* fixup! Format Python code with psf/black push

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
Du Yuanchao 2020-09-24 19:12:52 +08:00 committed by GitHub
parent 4a3b8d682e
commit 902fe1c907
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 10 deletions

View File

@ -1,5 +1,6 @@
# https://www.tutorialspoint.com/python3/bitwise_operators_example.htm # https://www.tutorialspoint.com/python3/bitwise_operators_example.htm
def binary_and(a: int, b: int): def binary_and(a: int, b: int):
""" """
Take in 2 integers, convert them to binary, Take in 2 integers, convert them to binary,

View File

@ -1,5 +1,6 @@
# https://www.tutorialspoint.com/python3/bitwise_operators_example.htm # https://www.tutorialspoint.com/python3/bitwise_operators_example.htm
def binary_xor(a: int, b: int): def binary_xor(a: int, b: int):
""" """
Take in 2 integers, convert them to binary, Take in 2 integers, convert them to binary,

View File

@ -1,18 +1,15 @@
# Created by sarathkaul on 18/11/19
# Edited by farnswj1 on 4/4/20
def reverse_words(input_str: str) -> str: def reverse_words(input_str: str) -> str:
""" """
Reverses words in a given string Reverses words in a given string
>>> sentence = "I love Python" >>> reverse_words("I love Python")
>>> reverse_words(sentence) == " ".join(sentence.split()[::-1])
True
>>> reverse_words(sentence)
'Python love I' 'Python love I'
>>> reverse_words("I Love Python")
'Python Love I'
""" """
return " ".join(reversed(input_str.split(" "))) return " ".join(input_str.split()[::-1])
if __name__ == "__main__": if __name__ == "__main__":
print(reverse_words("INPUT STRING")) import doctest
doctest.testmod()