The Rubik's Cube was invented in 1974, and is still a popular puzzle today.
Rubik's Cube video: © Mari1408/Dreamstime.com
There are a number of solutions to the Rubik’s Cube, each involving a sequence of steps. In the Rubik's Cube video, the Lego robot solves the cube using one of these sequences, with a colour sensor to monitor the colours while working. However, the Rubik's Cube can even be solved blindfolded! By repeating a sequence of steps enough times, the solution can be reached even without looking at the colours.
A sequence of steps to solve a problem is referred to as an algorithm.
Invite students to play this game in pairs:
Students can write this game out as a simple algorithm (flowchart or pseudocode is not necessary).
Guess 10 (halfway between 1 and 20).
If told higher, ignore the numbers 1 to 10, and guess halfway between 11 and 20.
If told lower, ignore the numbers 11 to 20, and guess halfway between 1 and 9.
Keep repeating this process, throwing out the irrelevant numbers and guessing halfway between what’s left.
The solution is an algorithm called a binary search. For an example of a binary search, view The famous phonebook video clip.
In this lesson, students will:
Begin by watching the video demonstrating the higher/lower game, which uses loops and variables:
BEGIN answer ← 0 Repeat While answer != 12 answer ← Input ‘How many months are in a year?’ End Repeat Display ‘You got it!’ END
Here is a more complex version of the program. Read it carefully, then answer the questions.
BEGIN answer ← 0 tries ← 0 Repeat While answer != 12 And tries < 3 answer ← Input ‘How many months are in a year?’ tries ← tries + 1 End Repeat If answer = 12 Then Display ‘You got it!’ Else Display ‘You ran out of tries.’ End If END
For more on setting up and choosing a language, see Setting Up.
First, change the range for the secret number from 1–20 to a higher number; for example 1–50.
Next, modify the game so that the player only gets a maximum of guesses; for example 5 - otherwise they lose. The higher/lower game already records the number of guesses the player makes.
These challenges use the skills covered so far. By writing or modifying their own programs, students have an opportunity to demonstrate Application and Creation.
1. Design and code a program to provide a cumulative total as prices are entered, until the user chooses to stop. See the sample output below:
Welcome! Enter your first price: 17 The total so far is $17. Enter another price, or enter STOP to finish: 5 The total so far is $22. Enter another price, or enter STOP to finish: 12 The total so far is $34. Enter another price, or enter STOP to finish: STOP Thank you for using this program.
a. Prepare pseudocode first.
BEGIN total ← 0 Display ‘Welcome! Enter your first price: ’ price ← input from user Repeat While price is not equal to ‘STOP’ total ← total + price Display ‘The total so far is $’, total, ‘.’ Display ‘Enter another price, or enter STOP to finish: ’ price ← input from user End Repeat Display ‘Thank you for using this program.’ END
2. Code a vowel replacer program by following the video below.
3. (OPTIONAL) Ask your students to write a higher/lower game where the roles are reversed. The human player thinks of a secret number, and the computer must guess the answer.
Here are some prompt questions to help students develop their pseudocode: