Trucks¶
A transport company wants to distribute the cargo of certain products
in their trucks. It has a nested list where each
sublist corresponds to a product and consists of two elements: the
product code (str) and its quantity in kg (int).
Write the modifier function assign_truck() that from a
list like the one mentioned, the code of a truck (str) and
the amount that it can carry, in Kg (int), modifies the list of
products so that the sublists with the products assigned to the truck
have a third item corresponding to the code of the truck.
The allocation of the products to the truck is done according to the following criteria:
the order of product allocation is the same order in which products appear in the list,
a product must be assigned completely: the corresponding amount can not be split, and
we must allocate as many products as they fit in the truck.
Save this function into file trucks.py. Example:
>>> lprod = [['AB4', 100], ['GH6', 71], ['PK7', 65], ['LJ8', 78], ['TF', 14]]
>>> a = assign_truck(lprod, 'C34', 250)
>>> lprod
[['AB4', 100, 'C34'], ['GH6', 71, 'C34'], ['PK7', 65, 'C34'], ['LJ8', 78], ['TF', 14, 'C34']]
In this case, product 'LJ8' cannot be assigned to the truck
because the associated amount, 78, would cause the truck’s
capacity to be exceeded, 250. Conversely, product 'TF' can be
assigned because the associated quantity 14, added to the quantity
of previously assigned products, does not exceed the capacity of the
truck.
More tests are provided in file:
test-assign_truck.txt.
Solution
A solution is provided in file trucks.py