-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpalindromic_str.py
146 lines (134 loc) · 3.77 KB
/
palindromic_str.py
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import copy as cp
def compare(a, b):
if len(a) > len(b):
return a
else:
return b
def isSubsequencePalindrome(s):
n=len(s)
isPalindrome=True
j =n
for i in range(n+1):
j=j-1
if s[i]==s[j]:
isPalindrome = True
if ((n-1)//2)== j:
break
else:
isPalindrome=False
break
return isPalindrome
def longestPalSubsB(s):
word_list =[]
for i in s:
word_list.append(i)
if len(word_list)<3:
print('TOO SHORT! Cannot be a Palindrome! Try word with atleast 3 characters!')
return
if isSubsequencePalindrome(s) == True:
return s
else:
flag2 =True
flag3 =True
flag4 =True
flag5 =True
next0 = 0
next1 = 0
front = cp.deepcopy(word_list)
end = cp.deepcopy(word_list)
frontNend = cp.deepcopy(word_list)
endNfront = cp.deepcopy(word_list)
length = len(word_list)
# Takes one at a time on the left and then all from the right
for i in range(len(word_list)):
frontNend = cp.deepcopy(front)
for j in range(len(front)):
del frontNend[-1]
if isSubsequencePalindrome(frontNend)==True:
next0 = 1
flag2 = True
flag3 = True
break
if len(frontNend)==3:
flag2 = False
flag3 =False
break
if next0 == 1:
break
else:
del front[0]
if isSubsequencePalindrome(front)==True:
flag2 = True
flag3 = False
break
if len(front)==3:
flag2 = False
flag3 = False
break
#Take one at a time on the right(end) and then all from the left(front)
for i in range(len(word_list)):
endNfront = cp.deepcopy(end)
for j in range(len(end)):
del endNfront[0]
if isSubsequencePalindrome(endNfront)==True:
next1 = 1
flag4 = True
flag5 = True
break
if len(endNfront)==3:
flag4 = False
flag5 = False
break
if next1 == 1:
break
else:
del end[-1]
if isSubsequencePalindrome(end)==True:
flag4 = True
flag5 = False
break
if len(end)==3:
flag4 = False
flag5 = False
break
#Getting out of loop and catching all the outcomes in the container:
container = []
if flag2 == flag3 == flag4 == flag5 == False:
print('No Palindrome in the word! Try another word!')
return
else:
if flag2 == True and flag3 == False:
container.append(str(front))
if flag2 == True and flag3 == True:
container.append(str(frontNend))
if flag4 == True and flag5 == False:
container.append(str(end))
if flag4 == True and flag5 == True:
container.append(str(endNfront))
j=0
for i in range(len(container)):
j =+ 1
if j == 1:
return container[0]
if j == 2 and (len(container[0]) == len(container[1])):
return container[0] + ' ' + container[1]
else:
return compare(container[0], container[1])
word = raw_input('Enter a word: ')
catch = longestPalSubsB(word)
if catch == None:
print('NO Palindrome!')
else:
print('The longest palindrome/s in the given word: ')
print(catch)
'''
print(longestPalSubsB("aceexcivicgrfdds"))
print(longestPalSubsB("civicgrfdds"))
print(longestPalSubsB("aceexcivic"))
print(longestPalSubsB("civic"))
print(longestPalSubsB("123abba1"))
print(longestPalSubsB("abba1"))
print(longestPalSubsB("123abba"))
print(longestPalSubsB("12345"))
print(longestPalSubsB(""))
'''