Rubik’s Cube Challenge

Have you ever wondered how a computer keeps track of all the coloured squares on a Rubik’s Cube? Although the puzzle looks complicated, each face of the cube can actually be stored using a simple 3 × 3 two-dimensional (2D) array.

Instead of storing colours directly, programmers often represent each colour using a number from 1 to 6. For example:

    1: red
    2: white
    3: yellow
    4: orange
    5: green
    6: blue

A single face of the cube could therefore be stored like this:

In this Python challenge, your task is to complete a program that rotates one face of the Rubik’s Cube by 90 degrees. Using the flowchart provided, you will implement the algorithm needed to perform either a clockwise or an anti-clockwise rotation of the 2D array.

As you work through the challenge, you will discover how rows and columns change position during a rotation and how nested loops can be used to move data from one array into another. This is a great opportunity to practise working with 2D arrays, nested loops, indexing, and algorithmic thinking—skills that are widely used in computer graphics, games, image processing and artificial intelligence.

Algorithm Flowchart

Here is the flowchart to implement a rotation on a 2D-Array:

Python Code

Complete the following code using the algorithm described in the above flowchart

Extension Challenge

Once you have successfully rotated the 3 × 3 Rubik’s Cube face, take your solution one step further. Replace the array with a 4 × 4 version and see whether your algorithm still works correctly. Does your code depend on the array always being 3 × 3, or has it been written in a way that can rotate a square array of any size?

array = [[1, 2, 1, 4],
         [5, 4, 3, 5],
         [2, 1, 6, 3],
         [1, 4, 6, 2]]
unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area

Did you like this challenge?

Click on a star to rate it!

Average rating 5 / 5. Vote count: 1

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!