Skip to content

Instantly share code, notes, and snippets.

@sgobin
Created December 22, 2015 19:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sgobin/1f9abd1d4febcf7329de to your computer and use it in GitHub Desktop.
Save sgobin/1f9abd1d4febcf7329de to your computer and use it in GitHub Desktop.
Achar palíndromos em uma lista usando python. Substitua o arquivo em open() com o que você deseja.
#Abrir o arquivo
f = open('pt-BR-utf8', 'r')
#ler uma linha
for line in f:
palavra = line.replace("\n", "")
palavra = palavra.lower()
#inverter a palavra
palavra_reversa = palavra[::-1]
#comparar se igual, printar
if len(palavra) == 1:
continue
elif palavra == palavra_reversa:
print(palavra)
@sgobin
Copy link
Author

sgobin commented Dec 22, 2015

Todo's

  • adicionar um args para especificar o arquivo
  • tentar detectar "text-encoding" do arquivo passado

Notas

  • Por enquanto só suporta arquivos em formato UTF-8
  • O dicionário que usei de exemplo foi do projeto Mozilla

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment