Created
August 28, 2013 04:52
-
-
Save derv82/6362250 to your computer and use it in GitHub Desktop.
Count the number of palindromes that appear in a string
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
#!/usr/bin/python | |
''' | |
Count number of palindromes that appear in a string | |
''' | |
from sys import argv | |
def count(drome): | |
total = 0 | |
i = 1 | |
while i <= len(drome): | |
for j in xrange(0, len(drome) - i + 1): | |
print drome[j:j+i], | |
if ispalin(drome[j:j+i]): | |
total += 1 | |
print "<-- Palindrome" | |
else: | |
print '' | |
i += 1 | |
return total | |
def ispalin(txt): | |
if len(txt) == 1: return False | |
i = 0 | |
j = len(txt) - 1 | |
while i <= j: | |
if txt[i] != txt[j]: | |
return False | |
i += 1 | |
j -= 1 | |
return True | |
if __name__ == '__main__': | |
drome = ' '.join(argv[1:]) | |
print count(drome) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment