| Total Complexity | 13 |
| Total Lines | 35 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 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 |