forked from TrellixVulnTeam/Python-Automation_KTIW
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrograms.py
More file actions
65 lines (57 loc) · 1.88 KB
/
Copy pathPrograms.py
File metadata and controls
65 lines (57 loc) · 1.88 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
#check if a number is a palindrome or not
import string
class Test:
def isPalindromeNumber(self):
num=int(input("Enter a number:"))
temp=num
rev=0
while(num>0):
dig=num%10
rev=rev*10+dig
num=num//10
if(temp==rev):
print("The number is a palindrome!")
else:
print("Not a palindrome!")
#check if a string is palindrome or not
def isPalindromeString(self):
s=input("Enter a string:")
valid = set(string.ascii_letters)
result_s=''.join([ch for ch in s if ch in valid])
a = result_s.casefold()
if(a == a[::-1]):
print("The string is a palindrome!")
else:
print("Not a palindrome")
def longestpalindromes(self,text):
text = text.lower()
results = []
for i in range(len(text)):
for j in range(0, i):
chunk = text[j:i + 1]
if chunk == chunk[::-1]:
results.append(chunk)
return text.rindex(max(results, key=len)), results
def longestPalindrome(self,s) -> str:
# Create a string to store our resultant palindrome
palindrome = ''
# loop through the input string
for i in range(len(s)):
# loop backwards through the input string
for j in range(len(s), i, -1):
# Break if out of range
if len(palindrome) >= j - i:
break
# Update variable if matches
elif s[i:j] == s[i:j][::-1]:
palindrome = s[i:j]
break
return palindrome
if __name__ == "__main__":
a = Test()
#a.isPalindromeNumber()
#a.isPalindromeString()
# b=a.longestpalindromes('forgeeksskeegfor')
# print(b)
b = a.longestPalindrome('forgeeksskeegfor')
print(b)