Appendix C — Self Study Session - Programming paradigms

In this section we are mainly concerned with the programming paradigms of Python, nevertheless you are expected to use git and pdm to solve the problems at hand.

Once you are familiar with how Python handles these topics you will be able to answer questions like:

These topics are covered in the following resources (given in no particular order):

Exercises

Exercise C.1 (UML fact checker)  

classDiagram
Student "*" --* University : Composition
    class University{
        - name: string
        - address: Address 
    }
    class Student{
        - name: string 
        - fee: float 
    }

Which of the following can be answered with Yes and which with No:

  1. No two universities can have the same name?
  2. A university and a student can not have the same name?
  3. No student studies at more than one university?
  4. Each student studies at at least one university?
  5. Two students with the same name can not study at the same university?
  6. Two students with the same name can not study at different universities?
  7. Two universities can have the same address?

Is there a way to specify a attribute in UML as unique?

Exercise C.2 (From text to UML) Convert the following description into an appropriate UML diagram, with multiplicities and associations.

  • Every person has a name, a phone number and an e-mail address.
  • Every address is only occupied by one person, some addresses might have no inhabitant.
  • Each address is associated with a street, a city, an area code, and a country.
  • Addresses can be verified as a home address for a person and printed for post service.
  • There are two types of persons:
    • Students that can enlist for a class
    • Professors that can receive a salary
  • A student has a study ID and an average grade.

Exercise C.3 (From UML to Python) Convert the UML diagram from Exercise C.2 into Python code and test your classes.

Exercise C.4 (Interface) Consider the following UML diagram and implement it in Python with interfaces. Set lives for a Cat to the appropriate 7, feed some cat food generated with random integers uniformly distributed between \(-10\) and \(10\) and look how much food it can eat until it dies. Plot this for different initial values of currentFood.

classDiagram
    Cat --|> Animal
    Cat ..|> FourLegged
    Cat ..|> OxygenBreather
    class FourLegged{
        <<interface>>
        + run(destination)
    }
    class OxygenBreather{
        <<interface>>
        + breath()
    }
    class Animal{
        - lives: int
        - currentFood: int
        + eat(food: int)
    }
    class Cat{
        + getLivesLeft()
        + getCurrentAmountofFood()
        - decreaseLives()
    }

Exercise C.5 (Iterators) Explain the output of the following code fragment, see Section 6.1:

square = lambda a: a ** 2
iterator = map(square, range(1, 5))

for i in iterator:
    print(i)

print(f"{list(iterator) = }")
iterator = map(square, range(1, 5))
print(f"{list(iterator) = }")
1
4
9
16
list(iterator) = []
list(iterator) = [1, 4, 9, 16]

Exercise C.6 (Accumulator) In Section 6.1 we discussed the functools module. On function included there is accumulate that takes an iterator and a function to apply to it. It can, for example be used to compute partial sum or product:

from itertools import accumulate
from operator import add, mul

print(f"{list(accumulate([1, 2, 3, 4, 5], add)) = }")
print(f"{list(accumulate([1, 2, 3, 4, 5], mul)) = }")
list(accumulate([1, 2, 3, 4, 5], add)) = [1, 3, 6, 10, 15]
list(accumulate([1, 2, 3, 4, 5], mul)) = [1, 2, 6, 24, 120]

Use it for the following tasks:

  1. Compute a running maximum in a list of numbers.

  2. Compute an amortization schedule for a loan of \(1000\) with an interest of \(5\%\) and a payment of \(100\) for \(10\) years. Hint: You can provide an initial value as third argument to accumulate and the result should look something like the following:

    [1000, 950, 898, 843, 785, 724, 660, 593, 523, 449, 371]

    Feel free to add account fees to make it more realistic.

Exercise C.7 (Filter) Use functional programming and in particular the filter function to:

  1. Find all palindromes in a list of strings.

  2. Count the number of vowels in a string.

Exercise C.8 (Creating a module) Create your own module. The module should have the following functions:

  • For a given year, check if it is a leap year.
  • For a given date, return the day of the week, use the algorithm outlined here. Hint: Dictionaries can help you for some of the mappings.
  • Return the week number of the year of a certain date, you can use the algorithm outlined here.
  • Provide doc strings for each of your methods and for the module itself.

Exercise C.9 (Using your own module) Use the module created in Exercise C.8 and build a program on it.

  • Include the local module via pdm.
  • Use the module to generate from a date the following output
    • leap year -> true/false
    • day of the week
    • week of the year
  • How can you deal with european and american styled day-month order?
  • Update your module by adding the above described function where the returned value should be a dictionary with keys: leapyear, weekday,week.
  • Can you use the new function in the module right away or do you need to reinstall it somehow?