Skip to content

Commit 4669a7a

Browse files
committed
reverse string
1 parent f3cc077 commit 4669a7a

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

recursion/reverse-string.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
string: str = "This string will be reversed"
3+
4+
5+
def reverse_string(string: str) -> str:
6+
7+
def helper(s: str, end: int) -> str:
8+
if end < 0:
9+
return ""
10+
else:
11+
return s[end] + helper(s, end - 1)
12+
13+
return helper(string, len(string) - 1)
14+
15+
16+
print(reverse_string(string))
17+
18+
19+
def reverse_string_ex_one(string: str) -> str:
20+
if len(string) == 0:
21+
return ""
22+
else:
23+
return reverse_string_ex_one(string[1:]) + string[0]
24+
25+
26+
print(reverse_string_ex_one(string))
27+
28+
29+
def reverse_string_ex_two(string: str) -> str:
30+
if len(string) == 0:
31+
return ""
32+
else:
33+
return string[len(string) - 1] + reverse_string_ex_two(string[0: len(string) - 1])
34+
35+
36+
print(reverse_string_ex_two(string))

0 commit comments

Comments
 (0)