My python one-liner scramble generator

A while ago I created this one. It's very readable.

Code:
import random

faces = "ULFRBD"
modifiers = ["", "'", "2"]

def random_move():
    """Random face turn."""
   
    face = faces[random.randint(0, 5)]
    modifier = modifiers[random.randint(0, 2)]

    return face+modifier

def same(face1, face2):
    """Checks if two moves are of the same type."""
    return face1[0] == face2[0]

def intersection(face1, face2):
    """Checks if two moves do not intersect.""" # avoiding U D U.

    if face1[0] in "UD": return face2[0] not in "UD"
    if face1[0] in "LR": return face2[0] not in "LR"
    if face1[0] in "FB": return face2[0] not in "FB"

def generate():

    n = 20 # size of the scramble
    scramble = []
    todos = []

    while len(scramble) < n:

        move = random_move()
        flag = True
       
        if len(scramble)>0 and same(move, scramble[-1]): flag = False
        elif len(scramble)>1 and (not intersection(scramble[-2], scramble[-1]) and not intersection(scramble[-1], move)): flag = False
       
        if flag: scramble.append(move)
        todos.append(move)
   
    return " ".join(scramble)

scrambles = 10
for i in range(scrambles):
    print generate()

Sample output.
Code:
R' L' D R2 B R' U D L2 F' U' F L' F' U B U F' D' U2
B F' L2 R2 F2 R' D' B' L' D2 U2 L' U' F B' U' L B2 D' U'
F' R L2 B D' F' D2 B2 D' F L2 F' D2 U' F2 D2 F' R' D' F
U2 L R2 F' U' R F2 U' L B2 D2 R F2 R' U D' B2 F R U'
D' B' U R F2 L2 F' U F2 U2 F B2 R2 D' R F2 U2 F' D R
D R' U2 R2 B2 F D2 R' U' R' L' U2 D2 B L2 D L' R2 D U'
F' U' R2 B2 D2 F L' B' L F2 B2 L' U2 L' B D B2 F D' B
R2 B U2 L' F' L' F D2 B L B2 D' U' R' U R2 L2 U R2 F2
R' L U' R2 B2 U L2 B2 L U' L B' L2 R B D' F2 R' L' U'
F L2 D2 L B' L' B' U2 B' L' D L D' B D2 U2 R2 B D2 R
B D F' R B2 D U R' B' F' L2 B' U R2 L U D' F R' U'
B' L B R L F R2 D' R' U' F U2 L2 R2 B D2 F' U' L' U'
L' F' L' D' B' F' R' U' L2 F D R2 U2 L' R' F2 L' U D' F2
D2 L2 U' L F' U' R U2 D L' D F' U L' F2 D L2 U L D'
L' U' D R2 B' L2 U2 B2 R' F U2 F U' R2 U2 L' F' R B2 D2
B D F2 U' B' R B R F2 R2 U F' D U R F2 R D' R2 U'
B2 D F2 R2 F R U2 D2 F L B2 L2 F B' L2 D' F U' B L2
B' F D U2 F R' L U' R' L D F B L U F2 B D' F2 L
L2 D L2 R' D U2 L F L' R' U2 R' F R' D2 R F L2 U F'
L2 D' R B' D2 B' L F D2 F2 L2 R F U L U2 R F D U
 
python3 megaminx scrambler:

Python:
import random as r;pow = lambda a, b: lambda: r.choice([a,b]);RD = pow('++','--');U = pow("","'");print('\n'.join([' '.join(['R%s D%s' % (RD(), RD()) for i in range(5)]) + " U" + U() for ii in range(7)]))
 
274 bytes
its a world record, I guess. (until someone do it on perl...)
Code:
c,a,l,f,g=__import__('random').choice,['FB','UD','RL'],[],lambda y:lambda x:x in y,lambda y:lambda x:x not in y;h=[l.append(c(filter(g(l[-1:]),c(filter(g(filter(f(a),[''.join(l[-2:][::x]) for x in [-1,1]])),a))))) for u in range(25)];print ' '.join([h+c(" '2",) for h in l])

It works. copy and paste onto python interpreter. Or save it on a .py file.
You could also tweak it to generate a lot of scrambles:

just put a
Code:
for n in range(12):
in the beggining,
and in DOS do:

Code:
scramble.py > output.txt

result:



:)

EDIT:
Code:
from random import*;c=choice;w,l=['RL','UD','BF'],[]
for u in range(25):
a=c(w)
if sum([ord(x) for x in l[-2:]]) in [158,153,136]:
  while l[-1] in a:a=c(w)
t=c(a)
while l[-1:]==[t]:t=c(a)
l=l+[t]

print ' '.join([x+c(" '2") for x in l])
============================================

from random import*;c=choice;w,l=['RL','UD','BF'],[]
def k():
m=[y.replace(l[-1],'') for y in w] if l[-1:] else w
h=[int(''.join(l[-2:]) in [x,x[::-1]]) for x in w]
if 1 in h:m.pop(h.index(1))
return m

for u in range(25):
l.append(c(c(k())))

print ' '.join([x+c(" '2") for x in l])
first is 242 chars but is brute force.
second is 288 chars but is more pythonic.
I originally posted here as towwdso.

Today I implemented a python class that generates scrambles or reverse algs, take a look.

 
Last edited by a moderator:
Back
Top