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 long words and reverse letters #10105

Closed
wants to merge 2 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
26 changes: 14 additions & 12 deletions strings/reverse_letters.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
def reverse_letters(input_str: str) -> str:
def reverse_letters(sentence: str, length: int) -> 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.

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

Check failure on line 21 in strings/reverse_letters.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W292)

strings/reverse_letters.py:21:50: W292 No newline at end of file

Check failure on line 21 in strings/reverse_letters.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W292)

strings/reverse_letters.py:21:50: W292 No newline at end of file
21 changes: 0 additions & 21 deletions strings/reverse_long_words.py

This file was deleted.

Loading