Programming 1, 2021-22
Week 9: Exercises

1. Flip a Dictionary

Write a function that takes a dictionary such as { 'green' : 'zelený', 'red' : 'červený' } and returns a corresponding dictionary in which the keys and values have been flipped, e.g. { 'zelený' : 'green', 'červený' : 'red'} .

2. Time Class

Write a class Time that represents a time of day with 1-second resolution, e.g. 11:32:07.

3. Date Class

Write a class Date representing a month and day. The class should support these operations:

4. Polynomial Class

Write a class Polynomial representing a polynomial of a single variable. The class should support these operations:

5. Matrix Sum

Write a function that computes the sum of two matrices A and B, represented as lists of lists. Assert that the matrices have the same dimensions.

6. Matrix Product

Write a function that computes the product of two matrices A and B, represented as lists of lists. Assert that the matrices have dimensions that are compatible for multiplication.

7. Queue

a) Write a class LinkedQueue that represents a FIFO queue with methods enqueue() and dequeue(). Use a linked list, so that these methods run in O(1).

b) Write a subclass StatQueue that is like LinkedQueue, but also keeps some extra statistics. The class should have these methods:

8. Flatten

Write a function flatten that takes a list of lists and returns a list containing all the values from all sublists:

>>> flatten([[5, 2], [6], [8, 3]])
[5, 2, 6, 8, 3]

9. Deep Flatten

Write a function deep_flatten that takes a list that may contain sublists nested to any level. The function should return a list containing all the values from all the sublists:

>>> deep_flatten([ [[5, 2], 6], [8, 3], [[[[10]]]] ])
[5, 2, 6, 8, 3, 10]