8
\$\begingroup\$

Cops post

A riffle shuffle is a way of shuffling cards where the deck is split into 2 roughly equal sections and the sections are riffled into each other in small groups. This is how to riffle shuffle a string:

  • Split the string into equal sections.
  • Reverse the string, and starting from the start of each string.
  • Put runs of a random length between 1 and the number of characters left into the final string
  • Then remove these characters from the strings.

An example

"Hello World!"      Output string = ""
"Hello ", "World!"  ""
"Hell", "World!"    " o"
"Hell", "World"     " o!"
"Hel", "World"      " o!l"
"Hel", "Wo"         " o!ldlr"
"H", "Wo"           " o!ldlrle"
"H", ""             " o!ldlrleoW"
"", ""              " o!ldlrleoWH"

The final product from Hello World! could be o!ldlrleoWH and that is what you would output.

Robbers

Your task is to take a Cops submission and rearrange so that it produces a valid riffle shuffler program. You may not add, remove or change any characters in the original program to result in your solution.

If the program works, even if it is not the cops intended solution, it is a valid crack.

The winner is the person who cracks the most Cops submissions!

\$\endgroup\$
1

5 Answers 5

2
\$\begingroup\$

Python 3, HyperNeutrino

from random import*
def r(i):
    s=len(i)//2;L,R=i[:s][::-1],i[s:][::-1];o=[]
    while L and R:s=randint(1,len(L));o+=L[:s];L,R=R,L[s:]
    return o+L+R

Try it online!

Proof of Same Character Set

\$\endgroup\$
1
  • \$\begingroup\$ Sorry, my original post had a slight issue. The new version shouldn't be too hard to determine from your current progress. Good job though, this was my original solution! \$\endgroup\$
    – hyperneutrino
    Commented Jan 1, 2018 at 17:31
2
\$\begingroup\$

Jelly, HyperNeutrino

œs2UŒṖ€X€ż/

Try it online!

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

Pyth, notjagan

jk.iFmO./_dc2Q

Try it here!

Explanation

The Pyth interpreter also offers a nice pseudo-code if you check the "Debug" box.

  • c2Q chops the input into 2 pieces of equal length, with initial pieces one longer if necessary.

  • m maps a function over the pieces. Variable: d.

    • _d reverses d
    • ./ partitions the reverse of d; returns all divisions of the reversed d into disjoint contiguous substrings.
    • O chooses a randOm partition.
  • .iF Folds (reduces) the list by .interleaving.
  • And finally jk joins the list of strings.
\$\endgroup\$
1
\$\begingroup\$

R, Giuseppe

function(s){n<-nchar(s)
s<-methods::el(strsplit(s,''))
l<-head(s,n/2)
r<-tail(s,n/2)
o<-{}
while(length(l)|length(r)){if(length(r)){y<-sample(length(r),1)
o<-c(o,rev(head(r,y)))
r<-tail(r,-y)}
if(length(l)){x<-sample(length(l),1)
o<-c(o,rev(head(l,x)))
l<-tail(l,-x)}}
cat(o,sep='')}

Try it online!

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

Pushy, FlipTack

L2/:v;O@N[1LU:'.;FL0=?x?i

Try It Online

\$\endgroup\$

Your Answer

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

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