Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
deleted reverse_long_words.py and modified code of reverse_letters.py…
… to integrate functionality of reverse_long_words.py
  • Loading branch information
AnshuSharma111 committed Oct 8, 2023
commit bc99983a7a95f227bbff9a7834a9cc7abba9129c
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
27 changes: 15 additions & 12 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"))
21 changes: 0 additions & 21 deletions strings/reverse_long_words.py

This file was deleted.