Skip to content

Commit 66e4ea6

Browse files
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 <[email protected]>
1 parent 2d02500 commit 66e4ea6

File tree

3 files changed

+16
-33
lines changed

3 files changed

+16
-33
lines changed

Diff for: DIRECTORY.md

-1
Original file line numberDiff line numberDiff line change
@@ -1197,7 +1197,6 @@
11971197
* [Rabin Karp](strings/rabin_karp.py)
11981198
* [Remove Duplicate](strings/remove_duplicate.py)
11991199
* [Reverse Letters](strings/reverse_letters.py)
1200-
* [Reverse Long Words](strings/reverse_long_words.py)
12011200
* [Reverse Words](strings/reverse_words.py)
12021201
* [Snake Case To Camel Pascal Case](strings/snake_case_to_camel_pascal_case.py)
12031202
* [Split](strings/split.py)

Diff for: strings/reverse_letters.py

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

1519

1620
if __name__ == "__main__":
1721
import doctest
1822

1923
doctest.testmod()
24+
print(reverse_letters("Hey wollef sroirraw"))

Diff for: strings/reverse_long_words.py

-21
This file was deleted.

0 commit comments

Comments
 (0)