-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestPalindrome.py
More file actions
23 lines (23 loc) · 841 Bytes
/
Copy pathLongestPalindrome.py
File metadata and controls
23 lines (23 loc) · 841 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#Codewars Challange - Longest Palindrome
#This script finds the longest palindrome in a given string and returns its length
def longest_palindrome (s):
if len(s) == 0:
return (0)
elif len(s) == 1:
return (1)
elif len(s) == 2:
return (2)
else:
all_palindromes = []
longest_length = int()
for x in range(len(s)+1):
for i in range(len(s)):
string = s[i:i+x]
#Check if it is a palindrome
if string == string[::-1]:
all_palindromes.append(string)
#Return the length of the longest palindrome in all_palindromes
for i in range(len(all_palindromes)):
if len(all_palindromes[i]) > len(all_palindromes[i-1]):
longest_length = len(all_palindromes[i])
return (longest_length)