import random

# Note:
# This script does not produce a truly uniform random cube state.
# Instead, it simulates 20 legal moves from a solved cube while avoiding moves on the same axis consecutively.
# Generating a truly random state over all legal positions requires more advanced methods and/or external libraries.

# --- Helper: rotate a face 90° clockwise ---
def rotate_face(face):
    # A 90° clockwise rotation of a 3x3 matrix.
    return [list(row) for row in zip(*face[::-1])]

# --- Move implementations ---
def move_U(cube):
    # Rotate U face.
    cube['U'] = rotate_face(cube['U'])
    # Cycle the top rows of F, R, B, L.
    temp = cube['F'][0][:]
    cube['F'][0] = cube['R'][0][:]
    cube['R'][0] = cube['B'][0][:]
    cube['B'][0] = cube['L'][0][:]
    cube['L'][0] = temp

def move_D(cube):
    # Rotate D face.
    cube['D'] = rotate_face(cube['D'])
    # Cycle the bottom rows; note the order.
    temp = cube['F'][2][:]
    cube['F'][2] = cube['L'][2][:]
    cube['L'][2] = cube['B'][2][:]
    cube['B'][2] = cube['R'][2][:]
    cube['R'][2] = temp

def move_R(cube):
    cube['R'] = rotate_face(cube['R'])
    # Save U right column.
    temp = [cube['U'][i][2] for i in range(3)]
    for i in range(3):
        cube['U'][i][2] = cube['F'][i][2]
        cube['F'][i][2] = cube['D'][i][2]
        # The left column of B is reversed.
        cube['D'][i][2] = cube['B'][2-i][0]
        cube['B'][2-i][0] = temp[i]

def move_L(cube):
    cube['L'] = rotate_face(cube['L'])
    # Save U left column.
    temp = [cube['U'][i][0] for i in range(3)]
    for i in range(3):
        # B’s right column (reversed) moves to U left column.
        cube['U'][i][0] = cube['B'][2-i][2]
        cube['B'][2-i][2] = cube['D'][i][0]
        cube['D'][i][0] = cube['F'][i][0]
        cube['F'][i][0] = temp[i]

def move_F(cube):
    cube['F'] = rotate_face(cube['F'])
    # Affected stickers: U bottom row, L right column, D top row, and R left column.
    temp = cube['U'][2][:]
    # U bottom row gets L's right column (in reverse order).
    cube['U'][2][0] = cube['L'][2][2]
    cube['U'][2][1] = cube['L'][1][2]
    cube['U'][2][2] = cube['L'][0][2]
    # L right column gets D top row.
    cube['L'][0][2] = cube['D'][0][0]
    cube['L'][1][2] = cube['D'][0][1]
    cube['L'][2][2] = cube['D'][0][2]
    # D top row gets R left column (reversed).
    cube['D'][0][0] = cube['R'][2][0]
    cube['D'][0][1] = cube['R'][1][0]
    cube['D'][0][2] = cube['R'][0][0]
    # R left column gets saved U bottom row.
    cube['R'][0][0] = temp[0]
    cube['R'][1][0] = temp[1]
    cube['R'][2][0] = temp[2]

def move_B(cube):
    cube['B'] = rotate_face(cube['B'])
    # Affected stickers: U top row, R right column, D bottom row, and L left column.
    temp = cube['U'][0][:]
    # U top row gets R's right column.
    cube['U'][0][0] = cube['R'][0][2]
    cube['U'][0][1] = cube['R'][1][2]
    cube['U'][0][2] = cube['R'][2][2]
    # R right column gets D bottom row (reversed).
    cube['R'][0][2] = cube['D'][2][2]
    cube['R'][1][2] = cube['D'][2][1]
    cube['R'][2][2] = cube['D'][2][0]
    # D bottom row gets L's left column.
    cube['D'][2][0] = cube['L'][2][0]
    cube['D'][2][1] = cube['L'][1][0]
    cube['D'][2][2] = cube['L'][0][0]
    # L left column gets saved U top row (reversed).
    cube['L'][0][0] = temp[2]
    cube['L'][1][0] = temp[1]
    cube['L'][2][0] = temp[0]

# --- Map base moves to their functions ---
moves_func = {
    'U': move_U,
    'D': move_D,
    'R': move_R,
    'L': move_L,
    'F': move_F,
    'B': move_B,
}

# --- Function to perform a single move ---
def perform_move(cube, move):
    base = move[0]
    # Determine the number of 90° clockwise turns.
    if len(move) == 1:
        turns = 1
    elif move[1] == "2":
        turns = 2
    elif move[1] == "'":
        turns = 3  # 3 clockwise turns = 1 counterclockwise turn
    else:
        turns = 1
    for _ in range(turns):
        moves_func[base](cube)

# --- Cube creation and printing ---
def create_cube():
    # Standard color scheme:
    # U: White, D: Yellow, F: Green, B: Blue, L: Orange, R: Red.
    return {
        'U': [['W'] * 3 for _ in range(3)],
        'D': [['Y'] * 3 for _ in range(3)],
        'F': [['G'] * 3 for _ in range(3)],
        'B': [['B'] * 3 for _ in range(3)],
        'L': [['O'] * 3 for _ in range(3)],
        'R': [['R'] * 3 for _ in range(3)],
    }

def print_cube(cube):
    # Print each face.
    for face in ['U', 'L', 'F', 'R', 'B', 'D']:
        print(f"{face} face:")
        for row in cube[face]:
            print("  " + " ".join(row))
        print()

# --- Scramble generator ---
def generate_scramble(n=20):
    base_moves = ['U', 'D', 'R', 'L', 'F', 'B']
    modifiers = ["", "'", "2"]
    # Define axis groups to avoid moves on the same axis consecutively.
    axes = {
        'U': 'UD', 'D': 'UD',
        'R': 'RL', 'L': 'RL',
        'F': 'FB', 'B': 'FB'
    }
    
    scramble = []
    prev_move = None
    
    for _ in range(n):
        while True:
            move_face = random.choice(base_moves)
            # If there's no previous move or the move isn't on the same axis, accept it.
            if prev_move is None or axes[move_face] != axes[prev_move]:
                break
        modifier = random.choice(modifiers)
        scramble_move = move_face + modifier
        scramble.append(scramble_move)
        prev_move = move_face  # Track base face for axis check.
    return scramble

# --- Main function ---
def main():
    # Start with a solved cube.
    cube = create_cube()
    
    # Generate a scramble sequence.
    scramble = generate_scramble(20)
    print("Scramble sequence:")
    print(" ".join(scramble))
    print("\nApplying scramble moves...\n")
    
    # Apply each scramble move to the cube.
    for move in scramble:
        perform_move(cube, move)
    
    # Print the cube state after scrambling.
    print("Cube state after scramble:")
    print_cube(cube)

if __name__ == '__main__':
    main()
 
by

Python Online Compiler

Write, Run & Share Python code online using OneCompiler's Python online compiler for free. It's one of the robust, feature-rich online compilers for python language, supporting both the versions which are Python 3 and Python 2.7. Getting started with the OneCompiler's Python editor is easy and fast. The editor shows sample boilerplate code when you choose language as Python or Python2 and start coding.

Taking inputs (stdin)

OneCompiler's python online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample python program which takes name as input and print your name with hello.

import sys
name = sys.stdin.readline()
print("Hello "+ name)

About Python

Python is a very popular general-purpose programming language which was created by Guido van Rossum, and released in 1991. It is very popular for web development and you can build almost anything like mobile apps, web apps, tools, data analytics, machine learning etc. It is designed to be simple and easy like english language. It's is highly productive and efficient making it a very popular language.

Tutorial & Syntax help

Loops

1. If-Else:

When ever you want to perform a set of operations based on a condition IF-ELSE is used.

if conditional-expression
    #code
elif conditional-expression
    #code
else:
    #code

Note:

Indentation is very important in Python, make sure the indentation is followed correctly

2. For:

For loop is used to iterate over arrays(list, tuple, set, dictionary) or strings.

Example:

mylist=("Iphone","Pixel","Samsung")
for i in mylist:
    print(i)

3. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while condition  
    #code 

Collections

There are four types of collections in Python.

1. List:

List is a collection which is ordered and can be changed. Lists are specified in square brackets.

Example:

mylist=["iPhone","Pixel","Samsung"]
print(mylist)

2. Tuple:

Tuple is a collection which is ordered and can not be changed. Tuples are specified in round brackets.

Example:

myTuple=("iPhone","Pixel","Samsung")
print(myTuple)

Below throws an error if you assign another value to tuple again.

myTuple=("iPhone","Pixel","Samsung")
print(myTuple)
myTuple[1]="onePlus"
print(myTuple)

3. Set:

Set is a collection which is unordered and unindexed. Sets are specified in curly brackets.

Example:

myset = {"iPhone","Pixel","Samsung"}
print(myset)

4. Dictionary:

Dictionary is a collection of key value pairs which is unordered, can be changed, and indexed. They are written in curly brackets with key - value pairs.

Example:

mydict = {
    "brand" :"iPhone",
    "model": "iPhone 11"
}
print(mydict)

Supported Libraries

Following are the libraries supported by OneCompiler's Python compiler

NameDescription
NumPyNumPy python library helps users to work on arrays with ease
SciPySciPy is a scientific computation library which depends on NumPy for convenient and fast N-dimensional array manipulation
SKLearn/Scikit-learnScikit-learn or Scikit-learn is the most useful library for machine learning in Python
PandasPandas is the most efficient Python library for data manipulation and analysis
DOcplexDOcplex is IBM Decision Optimization CPLEX Modeling for Python, is a library composed of Mathematical Programming Modeling and Constraint Programming Modeling