GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — coding-problems/Other/palindrome_integer.py at master · developertyrone/coding-problems

Expand file tree

/

palindrome_integer.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
86 lines (66 loc) · 2.01 KB
/

palindrome_integer.py

Copy path

File metadata and controls

86 lines (66 loc) · 2.01 KB
Raw
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
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
'''
Palindrome Integer
Determine whether an integer is a palindrome.
An integer is a palindrome when it reads the same backward as forward.
Input: 121
Output: True
Input: -121
Output: False
Output explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
Input: 10
Output: false
Output oxplanation: Reads 01 from right to left. Therefore it is not a palindrome.
=========================================
Juste reverse the number and compare it with the original.
Time Complexity: O(N) , N = number of digits
Space Complexity: O(1)
If you care about integer overflow (in Python you shouldn't care about this), then reverse only a half of the number
and compare it with the other half. Also this solution is faster than the previous one because iterates only a half of the number.
Time Complexity: O(N)
Space Complexity: O(1)
'''
##############
# Solution 1 #
##############
def palindrome_integer_1(x):
if x < 0:
return False
rev = 0
temp = x
while temp > 0:
rev = (rev * 10) + (temp % 10)
temp //= 10
return rev == x
##############
# Solution 2 #
##############
def palindrome_integer_2(x):
# check if negative or ends with zero
if (x < 0) or (x > 0 and x % 10 == 0):
return False
rev = 0
# if the reversed number is bigger from the original
# that means the reversed number has same number of digits or more (1 or 2 more)
while x > rev:
rev = (rev * 10) + (x % 10)
x //= 10
# first comparison is for even number of digits and the second for odd number of digits
return (rev == x) or (rev // 10 == x)
###########
# Testing #
###########
# Test 1
# Correct result => True
x = 121
print(palindrome_integer_1(x))
print(palindrome_integer_2(x))
# Test 2
# Correct result => False
x = -121
print(palindrome_integer_1(x))
print(palindrome_integer_2(x))
# Test 2
# Correct result => False
x = 10
print(palindrome_integer_1(x))
print(palindrome_integer_2(x))
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.