Objects

Let’s consider a dictionary with information about a number of objects with the following format:

  • The keys are 2-component tuple:

    • the name of the object (str)

    • the identification number (int)

  • The values are 3-component list:

    • a color (str)

    • a number of items (int)

    • a price (float)

For example:


>>> objD = {
... ('box', 11): ['red', 2, 2.25],
... ('tree', 23): ['yellow', 3, 1.75], 
... ('table', 31): ['blue', 2, 1.25],
... ('lamp', 44): ['red', 1, 2.25],
... ('chair', 51): ['blue', 3, 0.5],
... ('bed', 69): ['blue', 2, 3.75],
... ('spoon', 77): ['red', 1, 3.55],
... }

You are required to deliver the following functions in the module objects (file objects.py).


The first function is:

list_objects(objD)

such that

given objD, a dict as described above

returns a new list of 5-component tuple whith all the information of that object in the key and value order, namely: the name of the object (str), the id number (int), the color (str), the number of items (int) and the price (float).

For example:


>>> l = list_objects(objD)

>>> solL = [
... ('box', 11, 'red', 2, 2.25), 
... ('tree', 23, 'yellow', 3, 1.75), 
... ('table', 31, 'blue', 2, 1.25), 
... ('lamp', 44, 'red', 1, 2.25), 
... ('chair', 51, 'blue', 3, 0.5),
... ('bed', 69, 'blue', 2, 3.75), 
... ('spoon', 77, 'red', 1, 3.55),
... ]

Warning

This function must be implement using comprehension lists.

Doctests are available in the list_objects.test file.


The second function is:

sort_objects(L)

such that

given L, a list as the output of the previous function (described above)

returns a new list with the same tuples but ordered according to the following criteria (listed in decreasing priority):

  1. The color, alphabetically.

  2. The numbrer of items, increasingly.

  3. The price, decreasingly.

For example:


>>> solL = [
... ('bed', 69, 'blue', 2, 3.75),
... ('table', 31, 'blue', 2, 1.25),
... ('chair', 51, 'blue', 3, 0.5),
... ('spoon', 77, 'red', 1, 3.55),
... ('lamp', 44, 'red', 1, 2.25),
... ('box', 11, 'red', 2, 2.25),
... ('tree', 23, 'yellow', 3, 1.75)
... ]

Warning

This input list must not be modified by the function.

Doctests are available in the sort_objects.test file.