Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
41 lines (35 loc) · 994 Bytes

File metadata and controls

41 lines (35 loc) · 994 Bytes
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#!/usr/bin/env python
'''
Leetcode: Palindrome number
Determine whether an integer is a palindrome. Do this without extra space.
* Should be interesting if it is a float, but it is hard to determine the accuracy
http://leetcode.com/2012/01/palindrome-number.html
'''
from __future__ import division
import math
# number with k digit
# 10**(k-1) <= num < 10**k
def pali_num(num):
print 'Check:', num,
if num < 0: return False
if num == 0: return True
k = int(math.log10(num)) + 1
base = 10**(k-1)
while num > 0 and base > 1:
# left-most digit?
left = num // base
num -= left*base
base /= 100
# right-most digit?
right = num % 10
num = num // 10
if left != right: return False
return True
if __name__ == '__main__':
print pali_num(12345)
print pali_num(12321)
print pali_num(123321)
print pali_num(1236666321)
print pali_num(-1)
print pali_num(-0)
print pali_num(0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.