-
Notifications
You must be signed in to change notification settings - Fork 1
/
36.amazon.py
41 lines (38 loc) · 1.06 KB
/
36.amazon.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
def palindrome(s):
temp = []
res1 = []
for i in range(len(s)):
j = 1
while j < min(len(s[:i+1]),len(s[i:]) )and s[i+j] == s[i-j]:
if temp == []:
temp.append(s[i])
temp.insert(0, s[i-j])
temp.append(s[i+j])
j+=1
res1, temp = max(temp,res1,key=len) , []
temp = []
res2 = []
for i in range(len(s)):
j = 1
while j < min(len(s[:i+1]),len(s[i:]) ) and s[i] == s[i+1] and s[i-j] == s[i+1+j]:
if temp == []:
temp.append(s[i])
temp.append(s[i])
temp.insert(0, s[i-j])
temp.append(s[i+1+j])
j+=1
res2, temp = max(temp,res2) , []
return "".join(max(res1,res2,key=len))
s = 'aabcdcb'
print(palindrome(s))
# Çözüm-1
def longestpalindrom(text):
result = ""
for i in range(len(text)):
for j in range(len(text),i,-1):
if (text[i:j] == text[i:j][::-1]) and len(result) < len(text[i:j]):
result = text[i:j]
return result
# Çözüm-2
def longestpalindrom2(text):
return max([text[i:j] for i in range(len(text)) for j in range(len(text),i,-1) if (text[i:j] == text[i:j][::-1])], key = len)