-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckIfNumberIsPalindrome.py
More file actions
36 lines (29 loc) · 945 Bytes
/
CheckIfNumberIsPalindrome.py
File metadata and controls
36 lines (29 loc) · 945 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
# First Way: Use the stack
def check_palindrome(num):
num_str = str(num)
num_list = list()
for i in range(len(num_str)):
num_list.append(num_str[i])
for i in range((len(num_str) // 2) + 1):
if num_list.pop() != num_str[i]: return False
return True
# Second way: Use two pointers
def check_palindrome_num(num):
num_str = str(num)
j = len(num_str) - 1
for i in range((len(num_str) // 2) + 1):
if num_str[i] != num_str[j]: return False
j -= 1
return True
# Third way: Reverse the num and check if it equals the original:
def check_palindrome_3(num):
temp_num = num
reverse_num = 0
while temp_num > 0:
remainder = temp_num % 10
reverse_num = (reverse_num * 10) + remainder
temp_num = temp_num // 10
return num == reverse_num
print(check_palindrome(4334))
print(check_palindrome_num(4334))
print(check_palindrome_3(4334))