-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathreverse-string.py
More file actions
36 lines (21 loc) · 743 Bytes
/
reverse-string.py
File metadata and controls
36 lines (21 loc) · 743 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
string: str = "This string will be reversed"
def reverse_string(string: str) -> str:
def helper(s: str, end: int) -> str:
if end < 0:
return ""
else:
return s[end] + helper(s, end - 1)
return helper(string, len(string) - 1)
print(reverse_string(string))
def reverse_string_ex_one(string: str) -> str:
if len(string) == 0:
return ""
else:
return reverse_string_ex_one(string[1:]) + string[0]
print(reverse_string_ex_one(string))
def reverse_string_ex_two(string: str) -> str:
if len(string) == 0:
return ""
else:
return string[len(string) - 1] + reverse_string_ex_two(string[0: len(string) - 1])
print(reverse_string_ex_two(string))