# 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.----- 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"


# Jelly, 11 9 bytes

ŒHḢ$ƬUƑƤS  Try it online! -2 bytes thanks to Unrelated String! ## How it works ŒHḢ$ƬUƑƤS - Main link. Takes a string S on the left
\$Ƭ     - Until reaching a previously encountered string:
ŒH        -   Split into halves
Ḣ       -   Take the first one
Ƥ  - Over each prefix:
U    -   Reverse each string in the prefix
Ƒ   -   Does that equal the prefix?
S - Sum

• As sweet as that \a is, -3 Jul 26 at 5:38
• @UnrelatedString Very nice use of Ƒ and U's vectorisation! Jul 26 at 10:24

# JavaScript (ES6), 85 bytes

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


# Haskell, 58 bytes

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 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. 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!

# Pyth, 17 bytes

a!_IQl.u?_IJhc2NJ


Test suite

The previous Pyth answer uses an older version of the language and a significantly different approach.

##### Explanation:
a!_IQl.u?_IJhc2NJ   | Full program
a!_IQl.u?_IJhc2NJNQ | with implicit variables filled
--------------------+-------------------------------------------------------------------------------------------------------------------------
.u          Q | Repeat the following until a repeated value is found, collecting the intermediate results, with N starting as Q (input):
Jhc2N    |  J = the first half of N (longer half is necessary)
?_I     JN  |  if J is invariant over reversal (i.e. is a palindrome), N = J, else N = N
l              | take the length of the resulting list
a                   | subtract
!_IQ               |  0 if Q is a palindrome, 1 otherwise


# Python 3, 53 bytes

f=lambda x:2>>len(x)or-~f(x[len(x)//2:])*(x==x[::-1])


Try it online!

## How it works:

• 2>>len(x) will give us 1 if len(x)==1 and 0 if len(x)>1 (we don't have to treat the case where x is the empty string)

• if the previous gives 0 then we recurse with x[len(x)//2:] which is the second part of our palindrome (including the center), -~ we add 1 and multiply by (x==x[::-1]) to return 0 if it is not a palidrome