What degree is this palindrome?

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 , so the code with the fewest bytes wins.

Oliver Ni

Posted 2016-11-03T05:14:16.863

Reputation: 9 650

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

zzzzz → zzz → zz → z so 4 is correct but cenea obviously wrong – Angs – 2016-11-03T08:54:45.467

I 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.533

no restriction on how input is provided? – ardnew – 2016-11-03T20:17:22.900

@ardnew Nope.-- – Oliver Ni – 2016-11-03T20:21:37.330

Answers

3

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"

Dada

Posted 2016-11-03T05:14:16.863

Reputation: 8 279

2

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

ETHproductions

Posted 2016-11-03T05:14:16.863

Reputation: 47 880

2

Haskell, 58 bytes

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

Angs

Posted 2016-11-03T05:14:16.863

Reputation: 4 825

1

05AB1E, 14 bytes

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

Uses the CP-1252 encoding. Try it online!

Adnan

Posted 2016-11-03T05:14:16.863

Reputation: 41 965

1

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

corvus_192

Posted 2016-11-03T05:14:16.863

Reputation: 1 889

0

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.

Maltysen

Posted 2016-11-03T05:14:16.863

Reputation: 25 023

@ETHproductions i'm not sure if that works, but thanks, I'll look into that – Maltysen – 2016-11-03T19:10:20.867

0

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!

FlipTack

Posted 2016-11-03T05:14:16.863

Reputation: 13 242

0

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;}

Numberknot

Posted 2016-11-03T05:14:16.863

Reputation: 885

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 – 2016-11-04T08:11:22.250