Skip to content

Instantly share code, notes, and snippets.

@derv82
Created August 28, 2013 04:52
Show Gist options
  • Save derv82/6362250 to your computer and use it in GitHub Desktop.
Save derv82/6362250 to your computer and use it in GitHub Desktop.
Count the number of palindromes that appear in a string
#!/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