October 2025
Intermediate to advanced
485 pages
13h 2m
English
These are the solutions to the Exercises.
Here’s one possible approach, which randomly selects 3 indexes from the array and returns the values at those indexes.
| | import random |
| | |
| | |
| | def pick_3(array): |
| | chosen_indexes = [] |
| | |
| | for _ in range(3): |
| | random_index = random.randint(0, len(array) - 1) |
| | while random_index in chosen_indexes: |
| | random_index = random.randint(0, len(array) - 1) |
| | |
| | chosen_indexes.append(random_index) |
| | |
| | # sort the indexes to ensure we return the values |
| | # in their original order: |
| | chosen_indexes.sort() |
| | |
| | chosen_values = [] |
| | for index in chosen_indexes: |
| | chosen_values.append(array[index]) |
| | |
| | return chosen_values |
Here, we grab all the hash table’s keys(), convert the result into ...
Read now
Unlock full access