Consolidated two scripts reverse_letters.py and reverse_long_words.py into one (#10140)

* Conolidated two scripts reverse_letters.py and reverse_long_words.py into one because of similar functionality

* Added a new line to accomodate characters without going over 88 char limit

* fixed grammar to pass pre-commit

* Changed faulty test case entirely to pass pre commit

* fixed a test case which was wrong

---------

Co-authored-by: Keyboard-1 <142900182+Keyboard-1@users.noreply.github.com>
This commit is contained in:
Anshu Sharma 2023-10-09 01:47:22 +05:30 committed by GitHub
parent 2d02500332
commit 66e4ea6a62
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 33 deletions

View File

@ -1197,7 +1197,6 @@
* [Rabin Karp](strings/rabin_karp.py)
* [Remove Duplicate](strings/remove_duplicate.py)
* [Reverse Letters](strings/reverse_letters.py)
* [Reverse Long Words](strings/reverse_long_words.py)
* [Reverse Words](strings/reverse_words.py)
* [Snake Case To Camel Pascal Case](strings/snake_case_to_camel_pascal_case.py)
* [Split](strings/split.py)

View File

@ -1,19 +1,24 @@
def reverse_letters(input_str: str) -> str:
def reverse_letters(sentence: str, length: int = 0) -> str:
"""
Reverses letters in a given string without adjusting the position of the words
>>> reverse_letters('The cat in the hat')
'ehT tac ni eht tah'
>>> reverse_letters('The quick brown fox jumped over the lazy dog.')
'ehT kciuq nworb xof depmuj revo eht yzal .god'
>>> reverse_letters('Is this true?')
'sI siht ?eurt'
>>> reverse_letters("I love Python")
'I evol nohtyP'
Reverse all words that are longer than the given length of characters in a sentence.
If unspecified, length is taken as 0
>>> reverse_letters("Hey wollef sroirraw", 3)
'Hey fellow warriors'
>>> reverse_letters("nohtyP is nohtyP", 2)
'Python is Python'
>>> reverse_letters("1 12 123 1234 54321 654321", 0)
'1 21 321 4321 12345 123456'
>>> reverse_letters("racecar")
'racecar'
"""
return " ".join([word[::-1] for word in input_str.split()])
return " ".join(
"".join(word[::-1]) if len(word) > length else word for word in sentence.split()
)
if __name__ == "__main__":
import doctest
doctest.testmod()
print(reverse_letters("Hey wollef sroirraw"))

View File

@ -1,21 +0,0 @@
def reverse_long_words(sentence: str) -> str:
"""
Reverse all words that are longer than 4 characters in a sentence.
>>> reverse_long_words("Hey wollef sroirraw")
'Hey fellow warriors'
>>> reverse_long_words("nohtyP is nohtyP")
'Python is Python'
>>> reverse_long_words("1 12 123 1234 54321 654321")
'1 12 123 1234 12345 123456'
"""
return " ".join(
"".join(word[::-1]) if len(word) > 4 else word for word in sentence.split()
)
if __name__ == "__main__":
import doctest
doctest.testmod()
print(reverse_long_words("Hey wollef sroirraw"))