My python one-liner scramble generator

Hello,

I am just learning Python and Pygame, and I am trying to create my own puzzle timer. I am struggling with creating a basic scrambler function that does not repeat moves, i.e., U2 U'. So far, I have this:

Code:
#Import libraries
import random

#Function that creates scrambling sequences
def scramble():
  #Create list with turns 
  turns = ["R ","R' ","R2 ","L ","L' ","L2 ","F ","F' ","F2 ","B ","B' ","B2 ","U ","U' ","U2 ","D ","D' ","D2 "]
  #Initialize empty string for scramble
  random_scramble=""
  #Generate 20 random turns and store in string
  for i in range(0,20):
    random_scramble+=random.choice(turns)
  #Return string
  return random_scramble

Any help would be greatly appreciated!

-DTCuber

P.S. Sorry for the massive bump. :D
 
Hello,

I am just learning Python and Pygame, and I am trying to create my own puzzle timer. I am struggling with creating a basic scrambler function that does not repeat moves, i.e., U2 U'. So far, I have this:

Code:
#Import libraries
import random

#Function that creates scrambling sequences
def scramble():
  #Create list with turns 
  turns = ["R ","R' ","R2 ","L ","L' ","L2 ","F ","F' ","F2 ","B ","B' ","B2 ","U ","U' ","U2 ","D ","D' ","D2 "]
  #Initialize empty string for scramble
  random_scramble=""
  #Generate 20 random turns and store in string
  for i in range(0,20):
    random_scramble+=random.choice(turns)
  #Return string
  return random_scramble

Any help would be greatly appreciated!

-DTCuber

P.S. Sorry for the massive bump. :D

Code:
#Import libraries
import random

#Function that creates scrambling sequences
def scramble():
  #Create list with turns 
  turns = [ ["R ","R' ","R2 "],["L ","L' ","L2 "],["F ","F' ","F2 "],["B ","B' ","B2 "],["U ","U' ","U2 "],["D ","D' ","D2 "]]
  #Initialize empty string for scramble
  random_scramble=""
  #Generate 20 random turns and store in string
  for i in range(0,20):
    random_scramble+=turns[random.randrange(6)][random.randrange(3)]
  #Return string
  return random_scramble

https://repl.it/uRS
 
Code:
#Import libraries
import random

#Function that creates scrambling sequences
def scramble():
  #Create list with turns 
  turns = [ ["R ","R' ","R2 "],["L ","L' ","L2 "],["F ","F' ","F2 "],["B ","B' ","B2 "],["U ","U' ","U2 "],["D ","D' ","D2 "]]
  #Initialize empty string for scramble
  random_scramble=""
  #Generate 20 random turns and store in string
  for i in range(0,20):
    random_scramble+=turns[random.randrange(6)][random.randrange(3)]
  #Return string
  return random_scramble

https://repl.it/uRS

What is that supposed to be?
 
What is that supposed to be?
You're right, it does the exact same thing

Code:
#Import libraries
import random

def nextnumber(currentnumber):
    #return a random number in range(0,6) different than currentnumber
    return (currentnumber+1+random.randrange(5))%6

#Function that creates scrambling sequences
def scramble():
  #Create list with turns 
  turns = [ ["R ","R' ","R2 "],["L ","L' ","L2 "],["F ","F' ","F2 "],["B ","B' ","B2 "],["U ","U' ","U2 "],["D ","D' ","D2 "]]
  #Initialize empty string for scramble
  random_scramble=""
  number=0
  #Generate 20 random turns and store in string
  for i in range(0,20):
    number=nextnumber(number)
    random_scramble+=turns[number][random.randrange(3)]
  #Return string
  return random_scramble
  
print (scramble())

https://repl.it/uRS/1
 
Absolutely pushing the boundaries of my capability: Python

Note: Outputs scrambles 1 move at a time, so you'd have to run it in IDLE, etc to see the full sequence.

90 chars (spaces)
82 chars (no spaces)
108 bytes.

Code:
import random as r
for i in range(0,25):
    print("".join(r.choice("UDLRBF")+r.choice("'2 ")))
 
Last edited:
Code:
import random as r;s=[];l='';m='';t=['R','R2',"R'",'F','F2',"F'",'U','U2',"U'"];while len(s)<11:m=r.choice(t);if l:if l[0]==m[0]:continue;s.append(m);l=m;print(' '.join(s))

172 characters for a simple 2x2 scrambler. Not much of a code golfer personally.
 
Absolutely pushing the boundaries of my capability: Python

Note: Outputs scrambles 1 move at a time, so you'd have to run it in IDLE, etc to see the full sequence.

90 chars (spaces)
82 chars (no spaces)
108 bytes.

Code:
import random as rfor i in range(0,25):
	print("".join(r.choice("UDLRBF")+r.choice("'2 ")))



There needs to be a new line between "import random as r" and "for i in range(0,25): or else it won't compile. It doesn't add another character but does add another byte.
 
Code:
import random as r;s=[];l='';m='';t=['R','R2',"R'",'F','F2',"F'",'U','U2',"U'"];while len(s)<11:m=r.choice(t);if l:if l[0]==m[0]:continue;s.append(m);l=m;print(' '.join(s))

172 characters for a simple 2x2 scrambler. Not much of a code golfer personally.

What language is that supposed to be? Somewhat looks like Python but clearly isn't ("SyntaxError: invalid syntax").
 
What language is that supposed to be? Somewhat looks like Python but clearly isn't ("SyntaxError: invalid syntax").

It is Python. There should be a new line after the import that I forgot. Besides that I think it should work. But I don't have a computer around so can't check.

Edit: There is something else wrong. I'll figure it out.
 
Last edited:
There needs to be a new line between "import random as r" and "for i in range(0,25): or else it won't compile. It doesn't add another character but does add another byte.

Thanks for pointing this out, this was clearly a pasting error, since the stats are correct for the correct version of the code and the code was written correctly in my .py file. Original post has been updated.
 
You're right, it does the exact same thing

Code:
#Import libraries
import random

def nextnumber(currentnumber):
    #return a random number in range(0,6) different than currentnumber
    return (currentnumber+1+random.randrange(5))%6

#Function that creates scrambling sequences
def scramble():
  #Create list with turns 
  turns = [ ["R ","R' ","R2 "],["L ","L' ","L2 "],["F ","F' ","F2 "],["B ","B' ","B2 "],["U ","U' ","U2 "],["D ","D' ","D2 "]]
  #Initialize empty string for scramble
  random_scramble=""
  number=0
  #Generate 20 random turns and store in string
  for i in range(0,20):
    number=nextnumber(number)
    random_scramble+=turns[number][random.randrange(3)]
  #Return string
  return random_scramble
  
print (scramble())

https://repl.it/uRS/1

Thanks for your help! I really appreciate it!

How did you learn Python? I'm looking for other Python resources. Thanks!

-DTCuber
 
hey gyus, I tried the scrambler on the original post(the one with actual newlines, not the insane one), and after commenting out the first part, It worked. Im using it for a times enterer I made in python(I have a computer with no internet in my room, so a lot of my cubing stuff I coded myself in python.)
the problem: it spits out very, very long scrambles, sometimes 100 moves or more. anyone have a solution to this problem?
 
hey gyus, I tried the scrambler on the original post(the one with actual newlines, not the insane one), and after commenting out the first part, It worked. Im using it for a times enterer I made in python(I have a computer with no internet in my room, so a lot of my cubing stuff I coded myself in python.)
the problem: it spits out very, very long scrambles, sometimes 100 moves or more. anyone have a solution to this problem?
The program literally just picks 25 random moves where moves on the same axis can't be repeated. If you're not getting 25 moves, you've copied and pasted wrong.

If you haven't already, you should go through the program carefully, and see how he goes from that to the one-liner.

Long term, I recommend writing a random state scrambler. Just follow the instructions on Kociemba's site - he explains the technical details pretty well and even gives some code snippets (in Delphi, but they're easy to follow).
 
The program literally just picks 25 random moves where moves on the same axis can't be repeated. If you're not getting 25 moves, you've copied and pasted wrong.

If you haven't already, you should go through the program carefully, and see how he goes from that to the one-liner.

Long term, I recommend writing a random state scrambler. Just follow the instructions on Kociemba's site - he explains the technical details pretty well and even gives some code snippets (in Delphi, but they're easy to follow).
I did have the whole thing (minus the imports) in a def(), and I changed the print to a return because I was using it in a different program
 
I did have the whole thing (minus the imports) in a def(), and I changed the print to a return because I was using it in a different program
You only need the part after the ==========, but I assume that's what you mean by commenting out the first part.

There are a couple things that could have gone wrong and they most likely have to do with the first line, which should actually be written more like this (if he wasn't trying to make the code as few characters as possible)

Code:
from random import choice
c = choice # One letter alias for random.choice function
w = ['RL','UD','BF'] # Available moves on each axis
l = [] # Initialise the scramble

If your initialisation is outside your def, you'll keep adding to the same scramble and it will get longer and longer.
 
Back
Top