Exercises
The following exercises provide you with the opportunity to practice with randomization algorithms. The solutions to these exercises are found in the section Chapter 3.
-
Write a function that randomly chooses 3 different values from an array, and returns a brand-new array that contains those 3 values. Ensure that the values appear in the same order as they appeared in the original array.
For example, if the original array is [7, 1, 5, 2, 9, 0, 3, 6, 4], and the computer selects the 9, 6, and 5 (in that order), we should return [5, 9, 6] since that’s the order in which those three integers appear in the original array.
-
Write a function that randomly chooses a single key from a hash table (in other words, a dictionary, if you’re using ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access