6
1
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.
To be clear, the unpalindromize a string, you take half of it, including the center if the original had odd length? – xnor – 2016-11-03T05:20:30.343
@xnor Yes.----- – Oliver Ni – 2016-11-03T05:28:41.427
2This 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 – 2016-11-03T05:32:04.447
16Also, 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 – 2016-11-03T05:36:29.400zzzzz → zzz → zz → z
so 4 is correct butcenea
obviously wrong – Angs – 2016-11-03T08:54:45.467I have clarified the question. – Oliver Ni – 2016-11-03T18:05:23.890
Can the input be the empty string? Looks clear otherwise. I'd suggest the test cases be just the input and outputs for easy copy-pasting, with the derivations shown separately. – xnor – 2016-11-03T18:23:57.017
Can you include a longer 1-palindrome (e.g.
abcdedcba
)? – ETHproductions – 2016-11-03T18:58:40.533no restriction on how input is provided? – ardnew – 2016-11-03T20:17:22.900
@ardnew Nope.-- – Oliver Ni – 2016-11-03T20:21:37.330