This task is part of project07 which is due at 23:00 EDT on 2024-10-29.
You have the option to work with a partner on this task if you wish. Working with a partner requires more work to coordinate schedules, but if you work together and make sure that you are both understanding the code you write, you will make progress faster and learn more.
You can download the starter code for this task using this link.
You can submit this task using this link.
Put all of your work for this task into the file
trainInventory.py
(you will create this file from scratch)
In this task, you will practice nested loops by implementing a program to manage inventory on trains. Each train is represented as a list of train cars, and each train car is a list of tuples indicating the name and total weight (in kilograms) of each item stored in that car. For example, the following train is carrying grain in one car and some furniture in another.
train = [
[('grain', 100000)],
[('chairs', 2000), ('sofas', 2300), ('chairs', 1000), ('tables', 4000)]
]
We've written it split across multiple lines for easy reading, but note that if you printed it, it would appear on one line.
Note that as usual, we require that you include docstrings in every function and we expect that you will complete the project faster if you write these first, before you start to write any code.
Your first goal is define a function
printInventory
that displays the
inventory in a train split across multiple lines of output so that it's
easier to read. It takes a single argument, which is a train (i.e., a
list of lists of tuples which contain strings an numbers). It must use
print
to display the
contents of the train such that
'kg'
, and finally
their name. Each item starts with two spaces of indentation (whereas
the car names aren't indented).These printInventory
examples demonstrate
how it should work.
Notes:
printInventory
function must use a nested
loopprintInventory
and the other trainInventory functions
by running the file test_trainInventory.py
. Next, you must define a function
totalWeightOf
which, given a train
and a specific item name, counts the total weight of items with that
name anywhere on the train. Remember that
each item is stored as a tuple with a name and a weight; you need to add
up those weights, but only for items whose name matches.
These totalWeightOf
examples demonstrate how
it should work.
Notes:
totalWeightOf
function must use a nested
loopDefine a function listItems
which,
given a train and a specific item name, returns a list containing each
item with that name from throughout the
train. Remember that each item is stored as a
tuple with a name and a weight, your goal here is to create a list
containing all such tuples whose names match the specified name. The
items in the list should appear in the same order that they appear in the
train.
These listItems
examples demonstrate how it
should work.
Notes:
listItems
function must use a nested
looplistItems
function may NOT modify the train it is
givenDefine a function listWeights
which,
given a train, returns a list containing the total weight of each car in
the train. Remember that each item is stored as a
tuple with a name and a weight, your goal here is to sum up the weights
within each car, and then append those sums to a list.
These listWeights
examples demonstrate how it
should work.
Notes:
listWeights
function must use a nested
looplistWeights
function may NOT modify the train it is
givenWrite a function heaviestCar
which
when given a train, returns the index within that train of the heaviest
single car, or the index of the earliest
heaviest car if several are tied for
heaviest. Although you may choose to
use a nested loop to solve this problem, you do not have to.
These heaviestCar
examples demonstrate how it
should work.
Define a function named repackCars
which takes a train and a weight limit, and then tries to re-pack the
items from the train into new cars so that each of them is below the
weight limit, returning a new train
list-of-lists. This function must work as
follows:
There are more efficient ways to repack things in terms of using fewer train cars, but this algorithm only requires considering each item once, and only considers one or two places to put that item.
These repackCars
examples demonstrate how it
should work.
Note that:
repackCars
must use a nested
loop.repackCars
must NOT modify the original
train. It will create a new
train with references to the same items without removing them from
the original train.If you'd like a completely optional and un-graded challenge, we've
included some tests in the test_trainInventory.py
file which will test
the following function:
A function called removeItems
can be written which takes a train and an
item name, and removes all items with that name from the train, putting
them into a separate list which it returns, preserving their order from
the train. It needs to both return the result list and modify the
original train.
This is quite challenging to do correctly and is not part of the rubric for this task, so only attempt it if you want an extra challenge. As a hint, it will be useful to iterate using a backwards index loop as part of this problem.
printInventory
examples
These examples demonstrate how printInventory
should word. Note the indentation and added units ('kg').
In []:PrintsprintInventory( [ [('grain', 100000)], [('chairs', 2000), ('sofas', 2300), ('chairs', 1000), ('tables', 4000)], ] )
Car #0 100000 kg grain Car #1 2000 kg chairs 2300 kg sofas 1000 kg chairs 4000 kg tablesIn []:PrintsprintInventory( [ [('electronics', 2000), ('cloth', 40000), ('toys', 8000)], [('racoons', 8), ('opossum', 5), ('rabbits', 37), ('deer', 82)], [('steel', 22000), ('iron', 45000), ('cobalt', 12000)], [('helium', 4000), ('electronics', 50)], ] )
Car #0 2000 kg electronics 40000 kg cloth 8000 kg toys Car #1 8 kg racoons 5 kg opossum 37 kg rabbits 82 kg deer Car #2 22000 kg steel 45000 kg iron 12000 kg cobalt Car #3 4000 kg helium 50 kg electronics
totalWeightOf
examples
These examples demonstrate how totalWeightOf
should word.
In []:Out[]:totalWeightOf( [ [('grain', 100000)], [('chairs', 2000), ('sofas', 2300), ('chairs', 1000), ('tables', 4000)], ], 'grain' )
In []:100000
Out[]:totalWeightOf( [ [('grain', 100000)], [('chairs', 2000), ('sofas', 2300), ('chairs', 1000), ('tables', 4000)], ], 'chairs' )
In []:3000
Out[]:totalWeightOf( [ [('electronics', 2000), ('cloth', 40000), ('toys', 8000)], [('racoons', 8), ('opossum', 5), ('rabbits', 37), ('deer', 82)], [('steel', 22000), ('iron', 45000), ('cobalt', 12000)], [('helium', 4000), ('electronics', 50)], ], 'electronics' )
2050
listItems
examples
These examples demonstrate how listItems
should word. Note that items are listed in the same order they appear in within the train.
In []:Out[]:listItems( [ [('grain', 100000)], [('chairs', 2000), ('sofas', 2300), ('chairs', 1000), ('tables', 4000)], ], 'chairs' )
In []:[('chairs', 2000), ('chairs', 1000)]
Out[]:listItems( [ [('wheat', 122000)], [('wheat', 148000)], [('semolina', 90000)], [('semolina', 93000)], [('semolina', 89000)], [('spelt', 91000), ('barley', 24000)], ], 'barley' )
In []:[('barley', 24000)]
Out[]:listItems( [ [('electronics', 2000), ('cloth', 40000), ('toys', 8000)], [('racoons', 8), ('opossum', 5), ('rabbits', 37), ('deer', 82)], [('steel', 22000), ('iron', 45000), ('cobalt', 12000)], [('helium', 4000), ('electronics', 50)], ], 'electronics' )
[('electronics', 2000), ('electronics', 50)]
listWeights
examples
These examples demonstrate how listWeights
should word.
In []:Out[]:listWeights( [ [('grain', 100000)], [('chairs', 2000), ('sofas', 2300), ('chairs', 1000), ('tables', 4000)], ] )
In []:[100000, 9300]
Out[]:listWeights( [ [('wheat', 122000)], [('wheat', 148000)], [('semolina', 90000)], [('semolina', 93000)], [('semolina', 89000)], [('spelt', 91000), ('barley', 24000)], ] )
In []:[122000, 148000, 90000, 93000, 89000, 115000]
Out[]:listWeights( [ [('electronics', 2000), ('cloth', 40000), ('toys', 8000)], [('racoons', 8), ('opossum', 5), ('rabbits', 37), ('deer', 82)], [('steel', 22000), ('iron', 45000), ('cobalt', 12000)], [('helium', 4000), ('electronics', 50)], ] )
[50000, 132, 79000, 4050]
heaviestCar
examples
These examples demonstrate how heaviestCar
should word. Note that the return value is an index, starting from 0.
In []:Out[]:heaviestCar( [ [('grain', 100000)], [('chairs', 2000), ('sofas', 2300), ('chairs', 1000), ('tables', 4000)], ] )
In []:0
Out[]:heaviestCar( [ [('wheat', 122000)], [('wheat', 148000)], [('semolina', 90000)], [('semolina', 93000)], [('semolina', 89000)], [('spelt', 91000), ('barley', 24000)], ] )
In []:1
Out[]:heaviestCar( [ [('electronics', 2000), ('cloth', 40000), ('toys', 8000)], [('racoons', 8), ('opossum', 5), ('rabbits', 37), ('deer', 82)], [('steel', 22000), ('iron', 45000), ('cobalt', 12000)], [('helium', 4000), ('electronics', 50)], ] )
2
repackCars
examples
These examples demonstrate how repackCars
should word.Note how we pack items in order and don't go back to earlier cars even if they have room for a new item.
In []:Out[]:repackCars( [ [('grain', 100000)], [('chairs', 2000), ('sofas', 2300), ('chairs', 1000), ('tables', 4000)], ], 200000 )
In []:[ [ ('grain', 100000), ('chairs', 2000), ('sofas', 2300), ('chairs', 1000), ('tables', 4000), ], ]
Out[]:repackCars( [ [('wheat', 122000)], [('wheat', 148000)], [('semolina', 90000)], [('semolina', 93000)], [('semolina', 89000)], [('spelt', 91000), ('barley', 24000)], ], 300000 )
In []:[ [('wheat', 122000), ('wheat', 148000)], [('semolina', 90000), ('semolina', 93000), ('semolina', 89000)], [('spelt', 91000), ('barley', 24000)], ]
Out[]:repackCars( [ [('electronics', 2000), ('cloth', 40000), ('toys', 8000)], [('racoons', 8), ('opossum', 5), ('rabbits', 37), ('deer', 82)], [('steel', 22000), ('iron', 45000), ('cobalt', 12000)], [('helium', 4000), ('electronics', 50)], ], 48000 )
[ [('electronics', 2000), ('cloth', 40000)], [ ('toys', 8000), ('racoons', 8), ('opossum', 5), ('rabbits', 37), ('deer', 82), ('steel', 22000), ], [('iron', 45000)], [('cobalt', 12000), ('helium', 4000), ('electronics', 50)], ]
printInventory
must print the correct output
printInventory
function is run must match the solution output.totalWeightOf
must return the correct result
totalWeightOf
function is run must match the solution result.listItems
must return the correct result
listItems
function is run must match the solution result.listItems
must not modify the train.
listItems
to make sure it does not alter values in the train it is given.listWeights
must return the correct result
listWeights
function is run must match the solution result.listWeights
must not modify the train.
listWeights
to make sure it does not alter values in the train it is given.heaviestCar
must return the correct result
heaviestCar
function is run must match the solution result.repackCars
must return the correct result
repackCars
function is run must match the solution result.repackCars
must not modify the train.
repackCars
to make sure it does not alter values in the train it is given.totalWeightOf
must return the correct result
totalWeightOf
function is run must match the solution result.heaviestCar
must return the correct result
heaviestCar
function is run must match the solution result.repackCars
must return the correct result
repackCars
function is run must match the solution result.printInventory
def
to define printInventory
printInventory
, use any kind of loop in at least one place.printInventory
, use any kind of loop in at least one place.print
printInventory
, call print
in at least one place.totalWeightOf
def
to define totalWeightOf
totalWeightOf
, use any kind of loop in at least one place.totalWeightOf
, use any kind of loop in at least one place.listItems
def
to define listItems
listItems
, use any kind of loop in at least one place.listItems
, use any kind of loop in at least one place.listWeights
def
to define listWeights
listWeights
, use any kind of loop in at least one place.listWeights
, use any kind of loop in at least one place.heaviestCar
def
to define heaviestCar
repackCars
def
to define repackCars
repackCars
, use any kind of loop in at least one place.repackCars
, use any kind of loop in at least one place.