# What degree is this palindrome?

Your task is to determine the palindromic degree of a given non-empty string.

To do this, loop until one of the following conditions are met:

• The string is not a palindrome.
• You encounter a string that has already been encountered.

And do the following:

• If it is a palindrome, add one to the degree.
• Depalindromize.

For example, if I had the string babababab, the calculation would go:

babababab     1
babab         2
bab           3
ba


For the string zzzzz, it would go:

zzzzz         1
zzz           2
zz            3
z             4
z


### Test cases

abc 0
cenea 0
pappap 2
aa 2
aba 1
aaa 3
cccc 3
zzzzz 4


Explanation:

abc : 0
cenea : 0
pappap -> pap -> pa : 2
aa -> a -> a : 2
aba -> ab : 1
aaa -> aa -> a -> a: 3
cccc -> cc -> c -> c: 3
zzzzz -> zzz -> zz -> z -> z : 4


Remember, this is , so the code with the fewest bytes wins.

• To be clear, the unpalindromize a string, you take half of it, including the center if the original had odd length? – xnor Nov 3 '16 at 5:20
• @xnor Yes.----- – Oliver Ni Nov 3 '16 at 5:28
• This gives a loop once you get down to length 1, since those un-palindromize to themselves. You should make explicit if you define them to have degree 1. – xnor Nov 3 '16 at 5:32
• Also, I don't understand the test cases. How do you get cenea -> 1? zzzzz -> 4? You really should iron these things out in the sandbox. – xnor Nov 3 '16 at 5:36
• zzzzz → zzz → zz → z so 4 is correct but cenea obviously wrong – Angs Nov 3 '16 at 8:54

## Perl, 53 bytes

52 bytes of code + 1 byte for -p.

$\++while s/^((.+).?)(??{reverse$2})$/$1/||s/^.$//}{  To run it : perl -pE '$\++while s/^((.+).?)(??{reverse$2})$/$1/||s/^.$//}{' <<< "zzzzz"


# JavaScript (ES6), 85 bytes

f=s=>s?(t=s.slice(0,l=-~s.length/2))==[...s.slice(-l)].reverse().join?1+f(t):0:1


p[a]=1
p a|reverse a==a=1+p(take(div(1+length a)2)a)
p _=0


# 05AB1E, 14 bytes

[ÐÂÊ#¼g#2ä¬])\


Uses the CP-1252 encoding. Try it online!

# Scala, 56 bytes

s=>if(s.reverse==s&&s.size>1)1+h(s.take(s.size/2))else 2


requires an assignmentt to a variable with type declaration:

val f:(String=>Int)=...

# Pyth - 21 bytes

The case of palindromes never running out really cost me, will look for a better way.

.xxK.uhc2NQ)hf!_ITKlK

• @ETHproductions i'm not sure if that works, but thanks, I'll look into that – Maltysen Nov 3 '16 at 19:10

# Java 7,147 bytes

int c(String s,int t){int l=s.length();return l<2?t+1:s.equals(new StringBuilder(s).reverse().toString())?c(s.substring(0,l%2>0?l/2+1:l/2),++t):t;}

• new StringBuilder(s).reverse().toString() can be new StringBuffer(s).reverse()+"" and l%2>0?l/2+1:l/2 can be l/2+l%2. EDIT: l/2+l%2 can be -~l/2 instead. So it becomes 128 bytes. – Kevin Cruijssen Nov 4 '16 at 8:11

# Python 2, 9487 77 bytes

The recursive approach... this currently assumes that empty & single letter strings get a score of 1.

def P(x,c=0):a=len(x);return c+(a<2)if(x[::-1]!=x)+(a<2)else P(x[:-~a/2],-~c)


Try it online!