Skip to content

Commit

Permalink
Initial commit. Palindrome finder
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Panaguiton authored and Peter Panaguiton committed Aug 15, 2012
0 parents commit e902487
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions palin_fin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
a_string = "FourscoreandsevenyearsagoourfaathersbroughtforthonthiscontainentanewnationconceivedinzLibertyanddedicatedtothepropositionthatallmenarecreatedequalNowweareengagedinagreahtcivilwartestingwhetherthatnaptionoranynartionsoconceivedandsodedicatedcanlongendureWeareqmetonagreatbattlefiemldoftzhatwarWehavecometodedicpateaportionofthatfieldasafinalrestingplaceforthosewhoheregavetheirlivesthatthatnationmightliveItisaltogetherfangandproperthatweshoulddothisButinalargersensewecannotdedicatewecannotconsecratewecannothallowthisgroundThebravelmenlivinganddeadwhostruggledherehaveconsecrateditfaraboveourpoorponwertoaddordetractTgheworldadswfilllittlenotlenorlongrememberwhatwesayherebutitcanneverforgetwhattheydidhereItisforusthelivingrathertobededicatedheretotheulnfinishedworkwhichtheywhofoughtherehavethusfarsonoblyadvancedItisratherforustobeherededicatedtothegreattdafskremainingbeforeusthatfromthesehonoreddeadwetakeincreaseddevotiontothatcauseforwhichtheygavethelastpfullmeasureofdevotionthatweherehighlyresolvethatthesedeadshallnothavediedinvainthatthisnationunsderGodshallhaveanewbirthoffreedomandthatgovernmentofthepeoplebythepeopleforthepeopleshallnotperishfromtheearth"
print a_string
print len(a_string)

def palindromes(a_string):

i = 0
pals = []
new_pal = ""

while i < len(a_string):
try:
now = a_string[i]
next = a_string[i+1]
before = a_string[i-1]
except IndexError:
return pals
if now == next:
back = i
front = i + 1
new_pal = now + next
elif before == next:
back = i-1
front = i + 1
new_pal = before + now + next

if new_pal:
pals.append(new_pal)
add_pals(a_string,pals,new_pal,front,back)
new_pal = ""

i = i + 1

return pals

def add_pals(a_string,pals,new_pal,front,back):
while front < len(a_string):
front = front + 1
back = back - 1
if back < 0:
break
try:
if a_string[front] == a_string[back]:
new_pal = a_string[front] + new_pal + a_string[back]
pals.append(new_pal)
else:
return
except IndexError:
return

pals = palindromes(a_string)
print "--o-o--"
print len(pals)
for pal in pals:
print pal

0 comments on commit e902487

Please sign in to comment.