-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path788.py
More file actions
71 lines (44 loc) · 1.18 KB
/
788.py
File metadata and controls
71 lines (44 loc) · 1.18 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# Problem #788
# Write a program that checks whether an integer is a palindrome.
# For example, 121 is a palindrome, as well as 888. 678 is not a palindrome.
# Do not convert the integer into a string.
def getDigits(num):
count = 0
while num > 0:
count += 1
num = num // 10
return count
def get_digit(number, n):
return number // 10**n % 10
def createArray(num, digits):
digitArray = []
while digits > 0:
div = get_digit(num, digits-1)
digitArray.append(div)
digits = digits - 1
return digitArray
def checkPalindrome(num):
digitArray = createArray(num, getDigits(num))
lenCounter = len(digitArray)-1
if len(digitArray) == 1:
return True;
for i in range(len(digitArray)):
if(digitArray[i] != digitArray[lenCounter]):
return False
lenCounter = lenCounter - 1
return True
x = 595
assert(checkPalindrome(x) == True)
x = 5555
assert(checkPalindrome(x) == True)
x = 5955
assert(checkPalindrome(x) == False)
x = 5
assert(checkPalindrome(x) == True)
x = 7777
assert(checkPalindrome(x) == True)
x = 7474
assert(checkPalindrome(x) == False)
x = 78888888887
assert(checkPalindrome(x) == True)
print("All tests passed.")