Skip to content
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

Fix/consolidated reverse letters and reverse long words #10107

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
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
1 change: 0 additions & 1 deletion DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -1196,7 +1196,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)
Expand Down
25 changes: 14 additions & 11 deletions strings/reverse_letters.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
def reverse_letters(input_str: str) -> str:
def reverse_letters(sentence: str, length: int) -> str:
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
def reverse_letters(sentence: str, length: int) -> str:
def reverse_letters(sentence: str, length: int = 0) -> str:

Let's make the length an optional parameter so that we can still preserve the same functionality as the original reverse_letters

Please also add a test case for length = 0

"""
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.

>>> 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", 5)
'1 12 123 1234 54321 123456'
"""
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", 3))
21 changes: 0 additions & 21 deletions strings/reverse_long_words.py

This file was deleted.