Exercise: Tumbler Wheels Lock combinations
You have just bought a new safe to keep all the riches you will gain from becoming a Clojure developer (hopefully). The safe has a 3 tumbler wheel combination lock to protect your new found wealth. Each tumbler wheel has the numbers 0 to 9.
Represent a single combination lock
How would you represent the possible numbers in just one of the tumbler wheels
[]
- The combination is managed by three tumbler wheels
- Each tumbler wheel has the same range of numbers on then, 0 to 9
Each tumbler wheel could have all the numbers it contains within a Collection in Clojure. The simplest approach would be to put the numbers 0 to 9 into a Vector (an array-like collection).
[0 1 2 3 4 5 6 7 8 9]
Generating the number range
As the numbers on the tumbler wheel are just a range between 0 and 9, then rather than type out all the numbers, is there a function to generate all the numbers for us.
()
When we give the range function one argument, it will create all the whole numbers from 0 to the number before that of the argument. In the following example, we give range
the argument of 10 and we receive the numbers from 0 to 9.
(range 10)
You can also give range
two arguments, such as '(range 5 15)'.
Be careful not to call the
range
function by itself, or it will try and generate an infinite range of numbers (until your computer memory is all used up).
Create all the Combinations
Generate all the possible combinations of the lock using three tumbler wheels We have given you a bit of a clue
(for [tumbler-one (range 10)]
)
Total number of combinations
You did the hard work already, now return just the total number of combinations
()
Make the combinations harder to guess
To make our lock harder to break into, we should only allow the combinations where each tumbler wheel has a different number. So you should exclude combinations like 1-1-1, 1-2-2, 1-2-1, etc.
How many combinations does that give us?
()
Complete the following code to create a 3-tumbler wheel combination lock, where none of the numbers are the same
Hint: Beware not to enter (range) without an argument as Clojure may try and evaluate infinity
(count
(for [tumbler-1 (range 10)
tumbler-2 (range 10)
tumbler-3 (range 10)
:when (or (= tumbler-1 tumbler-2)
(= tumbler-2 tumbler-3)
(= tumbler-3 tumbler-1))]
[tumbler-1 tumbler-2 tumbler-3]))
Here is a suggested example of the completed 3-lock challenges.