Skip to content

Commit 76851cf

Browse files
committed
Improve doctest coverage for remove_duplicates function
Contributes to #9943 - Added comprehensive doctests covering edge cases - Added tests for empty strings, single words, all duplicates - Added tests for mixed case, numbers, and special characters - Improved documentation with better examples
1 parent c79034c commit 76851cf

File tree

1 file changed

+49
-6
lines changed

1 file changed

+49
-6
lines changed

strings/remove_duplicate.py

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,58 @@
11
def remove_duplicates(sentence: str) -> str:
22
"""
3-
Remove duplicates from sentence
4-
>>> remove_duplicates("Python is great and Java is also great")
5-
'Java Python also and great is'
6-
>>> remove_duplicates("Python is great and Java is also great")
7-
'Java Python also and great is'
3+
Remove duplicates from sentence and return words in sorted order
4+
5+
Args:
6+
sentence: Input string containing words separated by spaces
7+
8+
Returns:
9+
String with unique words sorted alphabetically
10+
11+
Examples:
12+
Basic functionality:
13+
>>> remove_duplicates("Python is great and Java is also great")
14+
'Java Python also and great is'
15+
16+
Multiple spaces handling:
17+
>>> remove_duplicates("Python is great and Java is also great")
18+
'Java Python also and great is'
19+
20+
Edge cases:
21+
>>> remove_duplicates("")
22+
''
23+
24+
>>> remove_duplicates(" ")
25+
''
26+
27+
>>> remove_duplicates("hello")
28+
'hello'
29+
30+
>>> remove_duplicates("hello hello hello")
31+
'hello'
32+
33+
Mixed case (case sensitive):
34+
>>> remove_duplicates("Python python PYTHON")
35+
'PYTHON Python python'
36+
37+
Numbers and special characters:
38+
>>> remove_duplicates("1 2 3 1 2 3")
39+
'1 2 3'
40+
41+
>>> remove_duplicates("hello world hello world!")
42+
'hello world world!'
43+
44+
Single character words:
45+
>>> remove_duplicates("a b c a b c")
46+
'a b c'
47+
48+
Mixed content:
49+
>>> remove_duplicates("The quick brown fox jumps over the lazy dog")
50+
'The brown dog fox jumps lazy over quick the'
851
"""
952
return " ".join(sorted(set(sentence.split())))
1053

1154

1255
if __name__ == "__main__":
1356
import doctest
1457

15-
doctest.testmod()
58+
doctest.testmod()

0 commit comments

Comments
 (0)