Dictionaries 5 - First challenges
Download exercises zip
We now propose some exercises without solution, do you accept the challenge?
Challenge - The Institute for Advanced Technotronics
✪ The Institute for Advanced Technotronics builds computing machines which require thousands of cables, They are categorized according to their section diameter, in intervals of two numbers. Each interval is associated with a color. The Institute records everything in a journal, but now it’s refurbishing the inventory and wants to build a better database. Write some code that given a list diameters
of exactly 6 numbers and another list colors
of exactly 3 strings, like
diameters = [5, 9, 13, 18, 27, 90]
colors = ['yellow', 'orange', 'red' ]
outputs a dictionary in this format:
{(5, 9) : 'yellow',
(13, 18): 'orange',
(27, 90): 'red'}
DO NOT write constant list elements in your code (i.e. no 27
)
[1]:
diameters,colors = [5, 9, 13, 18, 27, 90], ['yellow', 'orange', 'red' ]
# write here
Challenge - Incredible Machines contest
✪ Each week the Institute promotes a contest. The participants in turns must inspect a new incredible machine and understand how it works. During each competition, the winner
gains prize
points which are then recorded in a registry
, adding them to points gained in previous competitions.
The machines are shiny new, and the Institute is aware the first participant may experience some issue with machines being run for the first time. To compensate for this, the first participant is always granted an extra
amount of points.
participants interact with the machine in alphabetical order
Write some code to MODIFY the registry
with the winner
prize
, and extra
amount of points for the first contestant. Print also the process.
DO NOT use loops
DO NOT write constant participants names (so no
'Carl'
…)Display participant order nicely
Example - given:
extra, winner, prize = 30, 'Marianne', 200
registry = { 'Lisa' : 10,
'Robert' : 30,
'Marianne': 20,
'Carl' : 20,
'Sara' : 60,
'Suzanne' : 30}
your code should print
Participants order: Carl, Lisa, Marianne, Robert, Sara, Suzanne
Carl begins, receives extra grant of 30 points
Marianne won, receives 200 points
Updated registry is
{'Carl': 50,
'Lisa': 10,
'Marianne': 220,
'Robert': 30,
'Sara': 60,
'Suzanne': 30}
HINT: to print nicely, use pprint
:
from pprint import pprint
pprint(registry)
NOTE: pprint
(and also jupyter) display keys in alphabetical order, but that’s just aesthetic: the original order in memory remains unchanged.
[2]:
extra, winner, prize = 30, 'Marianne', 200
registry = { 'Lisa' : 10,
'Robert' : 30,
'Marianne': 20,
'Carl' : 20,
'Sara' : 60,
'Suzanne' : 30}
#extra, winner, prize, registry = 10, 'Sebastian', 300, {'Sebastian':40,'Alfio':20}
# write here
Challenge - Going nuts
✪ The Institute has a large storage
room where component parts are kept. Sometimes mechanics come in a hurry yelling they need a given amount
of some item
.
Write some code which prints True
if there are enough items in the storage
, and False
otherwise.
NOTE: a mechanic may ask for an item
which is not recorded at all in the storage
DO NOT write string constants in your code (so no
'knobs'
…)DO NOT use loops
DO NOT use
if
statementsHINT if you don’t know how to do it, have a look at boolean evaluation order
[3]:
item, amount = 'knobs', 15 # True
#item, amount = 'knobs', 16 # False
#item, amount = 'nuts', 9 # True
#item, amount = 'rivets', 8 # False
#item, amount = 'clamps', 1 # False
#item, amount = 'pins', 1 # False
storage = {'nuts':11,
'knobs':15,
'pins':0,
'bolts':7}
# write here
Challenge - Galactic storm
✪✪ A micro black hole passed through the solar system, bringing chaos into all moon orbits. The probes sent by the Institute recorded exactly 3 jumps
of moons, where for each jump the last moon of one planet became the last moon of another.
Write some code which give given a dictionary galaxy
mapping planets to their moons, MODIFIES galaxy
according to another dictionary jumps
assume there cannot be chains of jumps (i.e. no Jupyter -> Mars -> Neptune)
DO NOT write constant planet names in your code (so no
'Jupyter'
)DO NOT replace the assignments in
galaxy
(so nogalaxy[planet] = ...
)DO NOT use search methods (so no
.remove
,.index
…)
Example - given:
(note for astrophiles: we didn’t care putting all moons, nor used any particular order)
galaxy = {
'Jupiter' : ['Io','Europa','Ganymede','Callisto'],
'Saturn' : ['Mimas', 'Enceladus', 'Dione','Rhea','Titan','Hyperion'],
'Mars' : ['Phobos','Deimos'],
'Earth' : ['Moon'],
'Neptune' : ['Triton', 'Proteus', 'Despina', 'Thalassa'],
'Uranus' : ['Titania', 'Oberon']
}
jumps ={
'Jupiter' : 'Mars',
'Saturn' : 'Neptune',
'Earth' : 'Uranus'
}
After your code, it must result:
>>> galaxy
{'Jupiter': ['Io', 'Europa', 'Ganymede'],
'Saturn': ['Mimas', 'Enceladus', 'Dione', 'Rhea', 'Titan'],
'Mars': ['Phobos', 'Deimos', 'Callisto'],
'Earth': [],
'Neptune': ['Triton', 'Proteus', 'Despina', 'Thalassa', 'Hyperion'],
'Uranus': ['Titania', 'Oberon', 'Moon']}
[4]:
galaxy = {
'Jupiter' : ['Io','Europa','Ganymede','Callisto'],
'Saturn' : ['Mimas', 'Enceladus', 'Dione','Rhea','Titan','Hyperion'],
'Mars' : ['Phobos','Deimos'],
'Earth' : ['Moon'],
'Neptune' : ['Triton', 'Proteus', 'Despina', 'Thalassa'],
'Uranus' : ['Titania', 'Oberon']
}
jumps ={
'Jupiter' : 'Mars',
'Saturn' : 'Neptune',
'Earth' : 'Uranus'
}
# write here
[ ]: