-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheck_Palindrome.py
More file actions
50 lines (38 loc) · 1.21 KB
/
Check_Palindrome.py
File metadata and controls
50 lines (38 loc) · 1.21 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
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 2 14:52:41 2020
@author: Daniel
"""
'''
Determine whether an integer is a palindrome.
An integer is a palindrome when it reads the same backward as forward.
Example 1 Input: 121 Output: true
Example 2 Input: 121 Output: false Explanation: From left to right, it reads 121.
From right to left, it becomes 121. Therefore it is not a palindrome.
Example 3 Input: 10 Output: false Explanation : Reads 01 from right to left.
Therefore it is not a palindrome.
'''
#defining function palindrome
def palindrome(num):
"""
:type num: int
:return type: bool
"""
forward = str(num) #casting integer num to a string type
#Base Cases
if len(forward) == 1: #single digits are palindromes
return True
if forward[0] == "-": #negative integers are not palindromes
return False
backward = "".join(reversed(forward)) #reversing the string
if forward == backward: #checking if palindrome
return True
else:
return False
'''Test cases'''
print(palindrome(121))
#Output: True
print(palindrome(10))
#Output: False
print(palindrome(-525))
#Output: False