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:
What is an object?
What is the scope of attributes/methods?
How can I inherit properties from an object?
How can I implement an interface in Python?
How does a filter work?
How does an iterator work?
etc.
These topics are covered in the following resources (given in no particular order):
McKinney (2022)Online, Section 4 (numpy) and 5 (pandas)
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:
No two universities can have the same name?
A university and a student can not have the same name?
No student studies at more than one university?
Each student studies at at least one university?
Two students with the same name can not study at the same university?
Two students with the same name can not study at different universities?
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 **2iterator =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) =}")
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:
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: