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 code-golf, so the code with the fewest bytes wins.
cenea -> 1
?zzzzz -> 4
? You really should iron these things out in the sandbox. \$\endgroup\$zzzzz → zzz → zz → z
so 4 is correct butcenea
obviously wrong \$\endgroup\$