Last active
June 19, 2016 21:18
-
-
Save DaniloOliveira28/9957a3300d85106dbe09af09434befa3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def is_palindrome(palavra): | |
return palavra[::-1] == palavra | |
def palindrome(texto): | |
if len(texto) <= 1: | |
return 0 | |
res_pal = is_palindrome(texto) | |
if res_pal is True: | |
return 0 | |
M = dict() | |
M[0] = 0 | |
for j in range(1, len(texto)): | |
values = [] | |
if is_palindrome(texto[0: j + 1]) is True: | |
M[j] = 0 | |
else: | |
for i in range(0, j): | |
if is_palindrome(texto[i + 1: j + 1]) is True: | |
values.append(M[i] + 1) | |
M[j] = min(values) | |
return M[len(texto) - 1] | |
if __name__ == "__main__": | |
print palindrome("abbadanilo") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment