We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent d4c0e94 commit 36cd4d8Copy full SHA for 36cd4d8
palindromeNumber.py
@@ -0,0 +1,28 @@
1
+# @return an boolean
2
+def isPalindrome(x):
3
+ if x < 0:
4
+ return False
5
+ if x < 10:
6
+ return True
7
+
8
+ # count digits
9
+ count = 0
10
+ xx = x
11
+ while xx > 0:
12
+ xx = xx / 10
13
+ count += 1
14
15
+ i = 0
16
+ while i < count - 1:
17
+ if ((x % (10 ** (i + 1))) / (10 ** i)) != ((x % (10 ** count)) / (10 ** (count - 1))):
18
19
+ i += 1
20
+ count -= 1
21
22
23
24
+assert isPalindrome(121) == True
25
+assert isPalindrome(12321) == True
26
+assert isPalindrome(123) == False
27
+assert isPalindrome(1) == True
28
+assert isPalindrome(-1) == False
0 commit comments