10
\$\begingroup\$

The string

abaaba

Is a palindrome, meaning it doesn't change when it's reversed. However we can split it in half to make two palindromes

aba aba

We could also split it up this way:

a baab a

And all three of those are palindromes.

In fact there are only two ways to split the string into smaller strings such that none of them are palindromes:

abaa ba
ab aaba

Any other way we split this there is going to be at least one palidrome.

So we will say that this string has a palindromy number of 2. More generally a string's palindromy number is the number of ways to partition it into contiguous substrings such that none are a palindrome.

Your task will be to take a string as input and determine its palindromy number. Your answer must work correctly for inputs containing lowercase letters (a-z) along with any characters used in the source code of your program.

Scoring

Your answer will be scored by it's own palindromy number. With a lower score being the goal. In the case of ties the tie breaker is

This scoring does mean if you solve this in brainfuck you can get a lenguage answer with a primary score of 0. So if you are a lenguage enthusiast this is just code-golf.

Here's a program to score your code.

Test cases

a -> 0
aba -> 0
ababa -> 0
abababa -> 0
ffffffff -> 0
xyz -> 1
abaa -> 1
abaaa -> 1
abaaaaa -> 1
ooooooops -> 1
kvxb -> 2
kayak -> 2
abaaba -> 2
abaaaba -> 2
avakava -> 2
aaaaakykaaaaa -> 2
abaaaaba -> 3
ababab -> 4
abababab -> 8
\$\endgroup\$
3
  • \$\begingroup\$ Why doesn't the scoring code partition homology into length 1 partitions? E.g, shouldn't xyz be x yz and xy z , therefore 2? I feel like I'm missing something. \$\endgroup\$
    – M Virts
    Sep 2, 2021 at 3:25
  • \$\begingroup\$ Is the answer that len 1 partitions are considered palindromes? Wouldn't that make xyz score 0? Or is no partition considered a partitioning? \$\endgroup\$
    – M Virts
    Sep 2, 2021 at 3:29
  • 1
    \$\begingroup\$ @MVirts The scoring doesn't partition into strings of length 1 because they are always palindromes. Any partition with a length 1 string has a palindrome partition (the same is true of length 0 partitions). The 1 partition of xyz is indeed the trivial partition. xyz is not a palindrome so the partition of one element contains no palindromes. \$\endgroup\$
    – Wheat Wizard
    Sep 2, 2021 at 4:36

5 Answers 5

5
\$\begingroup\$

Jelly, score 3, 755962277730462996760664 bytes

...‘‘‘‘ṃØJV

Try it online!

-5 score and +tons of bytes thanks to @ovs's idea.

...‘‘‘‘ part contains exactly 755962277730462996760660 copies of character, whose length encodes the following program in base 256:

Jelly, 10 bytes

ɠŒṖŒḂ€€§¬S

Try it online!

The main code is a minor variation of Dude's answer (use of alternative built-ins, and input method changed to "a single line from stdin"), so here goes an explanation for the encoding part instead:

...‘‘‘‘ṃØJV    Main link (niladic).
...‘‘‘‘        Increment from zero 755..whatever..660 times
       ṃØJ     Base decompress into Jelly's code page
          V    Eval as a nilad (input is handled in the code itself)

The 3 non-palindromic partitions of ...aaaabcde are:

...aaaabcde
...aaaabc|de
...aaaab|cde
\$\endgroup\$
5
  • \$\begingroup\$ I think you can replace ...““““”L with 383640471319762569300 times \$\endgroup\$
    – ovs
    Sep 3, 2021 at 9:48
  • 2
    \$\begingroup\$ @ovs No, it errors since the program is a monad taking a string, and placing a leading 0 defeats the purpose of the encoding. EDIT: It might actually save some score if I change the input method. \$\endgroup\$
    – Bubbler
    Sep 3, 2021 at 9:55
  • \$\begingroup\$ Ah I see, I didn't include the input when I tried this. But could you make the encoded program read the input from stdin instead? \$\endgroup\$
    – ovs
    Sep 3, 2021 at 10:01
  • 1
    \$\begingroup\$ Yeah, I was thinking of exactly that. \$\endgroup\$
    – Bubbler
    Sep 3, 2021 at 10:03
  • 3
    \$\begingroup\$ You've turned Jelly into Lenguage. Well done. \$\endgroup\$
    – xigoi
    Sep 3, 2021 at 11:16
3
\$\begingroup\$

Jelly, score 14 12, 9 bytes

ŒṖŒḂ€€§¬§

Try it online!

I thought this would be more of a baseline contender, but any modification I make to it to try to make it more palindromy only increases it instead. So, this is just the straight forward implementation.

-2 points thanks to xigoi

How it works

ŒṖŒḂ€€§¬§ - Main link. Takes a string S on the left
ŒṖ        - Get all partitions of S
     €    - Over each partition:
    €     -   Over each substring in the partition:
  ŒḂ      -     Is the substring a palindrome?
      §   - Get the sum of each (counting how many substrings are palindromic)
       ¬  - Logical not. Map zeros to one, everything else to zero
        § - Sum, counting the zeros
\$\endgroup\$
1
  • 3
    \$\begingroup\$ -2 by replacing ċ0 with ¬§: Try it online! \$\endgroup\$
    – xigoi
    Sep 2, 2021 at 19:17
2
\$\begingroup\$

05AB1E, score 18 (9 bytes)

.œεεÂÊ]PO

Try it online or verify all test cases, including itself.

Explanation:

.œ      # Get all partitions of the (implicit) input-string
  ε     # Map over each partition:
   ε    #  Map over each part in this partition:
        #   Check that it's NOT a palindrome by:
    Â   #    Bifurcating it (short for Duplicate & Reverse copy)
     Ê  #    And check that this reversed copy is not equal to the original string
  ]     # Close both nested maps
   P    # Check for each partition whether no part was a palindrome
    O   # Sum to get the amount of truthy values
        # (after which the result is output implicitly)

I don't think this score nor byte-count can be improved, but I'd like to be proven wrong. Increasing the program size overall increases the score, otherwise Ê]PO to Q]O_O would have been a decent change with the palindrome O_O (this scores 22 at 10 bytes btw).

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

Vyxal, 25 bytes, score 46368

LṄvṖ⁽JḭUƛvɾ:?f$•;'ƛḂ≠;A;L

Try it Online!

-3 thanks to Aaron Miller.

A big mess. Part of the code shamelessly stolen from Lyxal.

L                         # Length
 ṄvṖ                      # All permutations of partitions
    ⁽Jḭ                   # Flatten by 1 level
       U                  # Uniquify
        ƛ       ;         # Foreach
         vɾ:?f$•          # Mold input to those pieces (see above link)
                 '     ;L # Number of values where
                  ƛḂ≠;A   # All aren't palindromes. 
\$\endgroup\$
1
1
\$\begingroup\$

Stax, score 21 (9 bytes)

Æ₧ⁿ+Ü█>la

Run and debug it

the packed representation scores lesser, as shown in the tests.

Despite the existence of f!f and {{ in the unpacked version, the number of characters simply doubles the score.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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