-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchallenge_1.py
More file actions
executable file
·49 lines (38 loc) · 1.13 KB
/
challenge_1.py
File metadata and controls
executable file
·49 lines (38 loc) · 1.13 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env python3
def main():
pairs = [
(2331, 1332),
(123456789, 987654321),
(-12, -21),
(-9834937, -7394389),
]
for forward, backward in pairs:
if reverse_int(forward) == backward:
print(f"{forward} reversed is {backward}: CORRECT!")
else:
print(
f"Whups, {forward} resulted in {reverse_int(forward)} "
f"instead of {backward}. WRONG!"
)
def reverse_int(num:int):
"""
Accepts an integer, and returns the same integer with the numbers in reverse order.
For example, reverse_int(2355) should return 5532.
It may get tricky, though, because reverse_int(-17) should return -71, not 71-.
"""
# check if num is negative
if num < 0:
neg = True
else:
neg = False
# convert types in order to reverse
num = abs(num)
num_list = list(str(num))
num_list.reverse()
num_str = ''.join([str(elem) for elem in num_list])
if neg == True:
num_str = '-' + num_str
num = int(num_str)
return num
if __name__=='__main__':
main()