Passed
Push — master ( 2a32d2...dc515b )
by Ken M.
01:12
created

the_longest_palindromic   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 35
rs 10
c 0
b 0
f 0
wmc 13

3 Functions

Rating   Name   Duplication   Size   Complexity  
A is_palindromic() 0 7 3
A yield_substring() 0 6 3
B longest_palindromic() 0 11 7
1
def is_palindromic(text):
2
    index = 0
3
    while index < len(text) / 2:
4
        if text[index] != text[len(text) - index - 1]:
5
            return False
6
        index += 1
7
    return True
8
9
10
def yield_substring(text, ele):
11
    # return substring of given text with ele as the first & last char
12
    indexes = [i for i in range(len(text)) if text[i] == ele]
13
    for i in range(len(indexes) - 1):
14
        for j in range(i + 1, len(indexes)):
15
            yield text[indexes[i] : indexes[j] + 1]
16
17
18
def longest_palindromic(text):
19
    text_elements = set(text)
20
    palindromic = text[0]
21
    for i in text_elements:
22
        for j in yield_substring(text, i):
23
            if is_palindromic(j):
24
                if len(j) == len(palindromic) and text.find(j) < text.find(palindromic):
25
                    palindromic = j
26
                elif len(j) > len(palindromic):
27
                    palindromic = j
28
    return palindromic
29
30
31
if __name__ == '__main__':  # pragma: no cover
32
    assert longest_palindromic("artrartrt") == "rtrartr", "The Longest"
33
    assert longest_palindromic("abacada") == "aba", "The First"
34
    assert longest_palindromic("aaaa") == "aaaa", "The A"
35