4
\$\begingroup\$

Note that this is similar to my other challenge, but it is widened to be similar to most challenges (input -> truthy/falsey), instead of asking you all to brute force something. If this makes it a dupe, let me know, but I posted it on Sandbox and asked on chat, and didn't get any negative responses.

Input

Input will be a "word" consisting of a-z, A-Z, -, and '. The case is important. Instead of my previous challenge, where you run through all permutations, in this challenge, you will be given a permutation. Your program's response on invalid input does not matter. Input can be taken in reasonable text input formats, and can contain a trailing newline if required.

Output

Output will be a standard truthy/falsey values on if the permutation can be split in half. A permutation can be split in half if the width values (see below) of one half of the word equal the point values of the other half, or if the same is true if the middle word is removed. Trailing newlines allowed if required.

Please note that "half" does not mean that you have moved halfway into the string. "Half" means that the points on both sides are equal.

Examples:

W is 5 points. i is 1 point. Splitting the permutation Wiiiii in half will result in W | iiiii, with 5 points on each side of the |.

T is 3 points. Splitting the permutation TTTT in half will result in TT | TT, with 6 points on each side of the |.

w is 4 points. a is 3 points. Splitting the permutation waw in half will result in w (a) w, with 5.5 points on each side. The points from a are distributed to both sides, as a is split in half.

See the test cases for more clarification.

Letter Scores

Width  Characters
1      i l I '
2      f j r t -
3      a b c d e g h k n o p q s u v x y z
4      m w A B C D E F G H J K L N O P Q R S T U V X Y Z
5      M W

Test Cases

( ) indicate that the permutation can be split if the character inside the parantheses is cut in half. | indicates that the permutation can be split in between those two letters.

Input : Output : Visualization

a : truthy : (a)
ab : truthy : a|b
Ab : falsey
AB : truthy : A|B
r- : truthy : r|-
iiiiw : truthy : iiii|w
iiiiiiW : truthy : iiiii(i)W
STEPHEN : truthy : STE(P)HEN
STEphen : truthy : STE|phen
STEpheN : falsey
splitwoRdS : truthy : splitw|oRdS

And here's a visualization for splitwoRdS:

s p l i t w | o R d S 
3 3 1 1 2 4   3 4 3 4 
\$\endgroup\$
3
\$\begingroup\$

Jelly, 52 50 bytes

Œuðẇ⁾MW+=
ẇЀ“fjrt“il-“I'”T_@Ç+3µ€Hx2FŒṖṖLÐṂS€E$€Ṁ

Try it online! (exceeds memory limitations on TIO for the "splitwoRdS" test case)

Although slower and more memory hungry than the below 50 byte alternative that borrows the reduction method from fireflame241's answer:

Œuðẇ⁾MW+=
ẇЀ“fjrt“il-“I'”T_@Ç+3µ€Hx2F+\©ð_ø®ṪHµẠ¬

How?

Œuðẇ⁾MW+= - Link 1, character index offset helper: character list c (of length one)
Œu        - to uppercase
  ð       - dyadic chain separation (call that u)
    ⁾MW   - char pair literal ['M', 'W']
   ẇ      - sublist exists in? (is u in there - True if c is 'm', 'w', 'M', or 'W')
        = - equal? (vectorised u=c? [1] for uppercase or non-alpha c, [0] for lowercase c)
       +  - add the two results (vectorises)

ẇЀ“fjrt“il-“I'”T_@Ç+3µ€Hx2FŒṖṖLÐṂS€E$€Ṁ - Main link: character list w
                      µ€                 - for each c in w:
   “fjrt“il-“I'”                         -   list of character lists: ["fjrt","il-","I'"]
ẇЀ                                      -   sublist (w) exists in, mapped across right
                T                        -   truthy indexes (gets [the index at which the character was found] or an empty list)
                   Ç                     -   call the last link (1) as a monad
                 _@                      -   subtract with reversed @rguments
                    +3                   -   add 3
                        H                - halve (vectorises)
                         x2              - times two (repeats each list)
                           F             - flatten into a single list
                            ŒṖ           - partitions (all ways to split the list into contiguous slices)
                              Ṗ          - pop (remove the right-most; the only one of length 1)
                                ÐṂ       - filter keep those with minimal:
                               L         -   length
                                     $€  - last two links as a monad for €ach
                                  S€     -   sum €ach
                                    E    -   all (two) equal? (1 for a split, 0 otherwise)
                                       Ṁ - maximum (1 if any split was possible, 0 otherwise)
\$\endgroup\$
3
\$\begingroup\$

Jelly, 79 bytes

“¡-Ị’b26
“µııẈḟ(µ⁹\fy¦°Ẓ=ȮH@Q0Ẋƈẏ9Ė=ẹṬɗƇ’ðœ?øØa;ØA;⁾'-ð,ø5Rx¢
¢yHx2+\©ð_ø®ṪHµẠ¬

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Together we could do it in 50. \$\endgroup\$ – Jonathan Allan May 5 '17 at 13:42
2
\$\begingroup\$

Retina, 88 bytes

T`MWi\lI'`551
T`-fjrt`2
T`m\wL`4
T`l`3
\d
$*1 
+`^ ?1(1* 1*( 1*)+)1 ?$
$1
^ ?(1+) \1? ?$

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.