Skip to content

Commit b75b63a

Browse files
realDuYuanChaogithub-actions
and
github-actions
authored
Development (#17)
* reverse string * Formatted with psf/black * remove whitespace * Formatted with psf/black * format code * Formatted with psf/black Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent 4c0c4c1 commit b75b63a

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

strings/remove_whitespace.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
def remove_whitespace(original: str) -> str:
2+
"""
3+
>>> remove_whitespace("I Love Python")
4+
'ILovePython'
5+
>>> remove_whitespace("I Love Python")
6+
'ILovePython'
7+
>>> remove_whitespace(' I Love Python')
8+
'ILovePython'
9+
>>> remove_whitespace("")
10+
''
11+
"""
12+
return "".join(original.split())
13+
14+
15+
def remove_whitespace2(original: str) -> str:
16+
"""
17+
>>> remove_whitespace2("I Love Python")
18+
'ILovePython'
19+
>>> remove_whitespace2("I Love Python")
20+
'ILovePython'
21+
>>> remove_whitespace2(' I Love Python')
22+
'ILovePython'
23+
>>> remove_whitespace2("")
24+
''
25+
"""
26+
return original.replace(" ", "")
27+
28+
29+
if __name__ == "__main__":
30+
from doctest import testmod
31+
32+
testmod()

strings/reverse.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
def reverse(original: str) -> str:
2+
"""
3+
>>> reverse("abc")
4+
'cba'
5+
>>> reverse('1234')
6+
'4321'
7+
>>> reverse("cba321")
8+
'123abc'
9+
>>> reverse("")
10+
''
11+
"""
12+
return original[::-1]
13+
14+
15+
def reverse2(original: str) -> str:
16+
"""
17+
>>> reverse2("abc")
18+
'cba'
19+
>>> reverse2('1234')
20+
'4321'
21+
>>> reverse2("cba321")
22+
'123abc'
23+
>>> reverse2("")
24+
''
25+
"""
26+
original = list(original)
27+
i, j = 0, len(original) - 1
28+
while i < j:
29+
original[i], original[j] = (
30+
original[j],
31+
original[i],
32+
)
33+
i += 1
34+
j -= 1
35+
return "".join(original)
36+
37+
38+
if __name__ == "__main__":
39+
from doctest import testmod
40+
41+
testmod()

0 commit comments

Comments
 (0)