6
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ To be clear, the unpalindromize a string, you take half of it, including the center if the original had odd length? \$\endgroup\$ – xnor Nov 3 '16 at 5:20
  • \$\begingroup\$ @xnor Yes.----- \$\endgroup\$ – Oliver Ni Nov 3 '16 at 5:28
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ – xnor Nov 3 '16 at 5:32
  • 16
    \$\begingroup\$ 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. \$\endgroup\$ – xnor Nov 3 '16 at 5:36
  • \$\begingroup\$ zzzzz → zzz → zz → z so 4 is correct but cenea obviously wrong \$\endgroup\$ – Angs Nov 3 '16 at 8:54
3
\$\begingroup\$

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"
\$\endgroup\$
2
\$\begingroup\$

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
\$\endgroup\$
2
\$\begingroup\$

Haskell, 58 bytes

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

05AB1E, 14 bytes

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

Uses the CP-1252 encoding. Try it online!

\$\endgroup\$
1
\$\begingroup\$

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)=...

\$\endgroup\$
0
\$\begingroup\$

Pyth - 21 bytes

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

.xxK.uhc2NQ)hf!_ITKlK

Test Suite.

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

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;}
\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – Kevin Cruijssen Nov 4 '16 at 8:11
0
\$\begingroup\$

Python 2, 94 87 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!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.