python and palindromes
Posted
by
tekknolagi
on Stack Overflow
See other posts from Stack Overflow
or by tekknolagi
Published on 2011-01-12T07:39:11Z
Indexed on
2011/01/12
7:53 UTC
Read the original article
Hit count: 214
i recently wrote a method to cycle through /usr/share/dict/words
and return a list of palindromes using my ispalindrome(x)
method
here's some of the code...what's wrong with it? it just stalls for 10 minutes and then returns a list of all the words in the file
def reverse(a): return a[::-1] def ispalindrome(a): b = reverse(a) if b.lower() == a.lower(): return True else: return False wl = open('/usr/share/dict/words', 'r') wordlist = wl.readlines() wl.close() for x in wordlist: if not ispalindrome(x): wordlist.remove(x) print wordlist
© Stack Overflow or respective owner