Confident · ages 13+ · lists · permutations · while loop · maths
Simulate a 2×2 Rubik's Cube move in Python.
Represent a 2×2 cube as 24 stickers, apply the moves R and U as rearrangements, and count how many times R U repeats before the cube is solved again.
The program
Press Run to see what it prints. Change anything you like, then run it again.
# Stickers are numbered 0 to 23 (6 faces of 4). A move says where each sticker goes.
R = [0, 22, 2, 20, 5, 7, 4, 6, 8, 1, 10, 3, 12, 9, 14, 11, 16, 17, 18, 19, 15, 21, 13, 23]
U = [1, 3, 0, 2, 8, 9, 6, 7, 16, 17, 10, 11, 12, 13, 14, 15, 20, 21, 18, 19, 4, 5, 22, 23]
def apply(state, move):
new = list(state)
for i in range(24):
new[move[i]] = state[i]
return new
solved = list(range(24))
state = solved
repeats = 0
while True:
state = apply(apply(state, R), U)
repeats = repeats + 1
if state == solved:
break
print("R U repeats", repeats, "times on a 2x2 before the cube is solved again")R U repeats 15 times on a 2x2 before the cube is solved again
How it works
- The cube has 24 stickers. A move is a list that says which place each sticker goes to. Doing a move means moving every sticker to its new place.
applymakes a new list and puts each sticker where the move sends it. Doing R and then U is two calls toapply.- The loop repeats
R Uuntil every sticker is back where it began. The count is the order of the move. See the maths of the cube for the 3×3 version.
Try this
- Count the repeats of
Ralone. What do you expect? - Try three moves in a row, such as R, U and R again.
- Print the state after one R U to see the rearrangement.
Stuck on an error? Read how to debug Python.
Similar ideas
More programs
Countdown
Count down from 10 and finish with a message.
Beginner · ages 8+Fibonacci numbers
Build the Fibonacci sequence, where each number is the sum of the two before it.
Easy · ages 10+The Collatz sequence
Start with any number. If it is even, halve it; if odd, triple it and add 1. See how long it takes to reach 1.
Easy · ages 11+FAQ
Simulate a 2×2 Rubik's Cube move: questions
What does the simulate a 2×2 rubik's cube move program do?
Represent a 2×2 cube as 24 stickers, apply the moves R and U as rearrangements, and count how many times R U repeats before the cube is solved again. It prints: R U repeats 15 times on a 2x2 before the cube is solved again
Which Python ideas does it use?
lists, permutations, while loop, maths. Confident level, suitable from about age 13.
How do I run it?
Press Run on this page. To run it on your own computer, install Python 3 and save the code in a file ending in .py. It prints the same thing.
What should I try next?
Count the repeats of R alone. What do you expect? Try three moves in a row, such as R, U and R again. Print the state after one R U to see the rearrangement.
Start a conversation
Want a teacher to guide the learning?
Tell us the learner's age and what they can already do, and we will suggest a starting point. A parent or guardian can make the enquiry.
- Tell us the learner’s age or grade and goals
- We’ll suggest a suitable class or learning path
- We’ll confirm format, timing and fees with a parent or guardian
Play it, don't just read it
Write some code right now
Free games that need no sign-up. Pick one and play for five minutes.
Pick a game
- Code BuilderThe lines of a Python program are mixed up. Tap them into the right…Play
- Predict the OutputRead a short Python program and choose what it prints. Then watch it run…Play
- Bug HunterA Python program prints the wrong answer. Find the one line with the…Play
- Code JungleWrite real Python to guide Miko the monkey to every banana. 24 levels:…Play