Skip to content

Instantly share code, notes, and snippets.

@IanMcT
Created January 26, 2017 03:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IanMcT/301e7e7632b12b650e77bcf0cb6eea77 to your computer and use it in GitHub Desktop.
Save IanMcT/301e7e7632b12b650e77bcf0cb6eea77 to your computer and use it in GitHub Desktop.
Hidden palindrome solution.
"""J3 Hidden Palindrome
http://cemc.math.uwaterloo.ca/contests/computing/2016/stage%201/juniorEn.pdf
This strategy loops from the longest possible palindrome to the shortest"""
word=input()
#0-3
#1-2
foundpalindrome = True #sentinal value
keeplooping = True#Stop when palindrom found
#start with the longest possible, work in
for i in range(len(word),1,-1):
#loop through every combination with this many letters
if not keeplooping:
break
for comb in range(len(word)-i+1):
if not keeplooping:
break
#print('i',i,'comb',comb)#used for testing
#check if this combination is a palindrome
foundpalindrome = True#reset
for pal_check in range(i//2):
#print('palcheck',comb+pal_check,'other',comb+i-1-pal_check)#for testing
#check if letters don't match
if word[comb+pal_check] != word[comb+i-1-pal_check]:
foundpalindrome = False
if foundpalindrome:
print('palindrome: ',i)
keeplooping = False
break
if keeplooping:
print(1)
@Aayush9029
Copy link

this seems to work fine, here's what I did, did I make any mistakes? thanks in advance

def pali(word):
w = list(word)
total = 0
start = 0
end = -1
print(end)
for letters in w:
if w[start] == w[end]:
total += 1
start += 1
end -= 1
else:
start += 1
print(total)

pali('abracadabra')

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