You are on page 1of 3

PROBLEM SET 12

Submit your answers as a single pdf-file with the following name: set12 Lastname1 Lastname2.pdf.
Always comment on your results. Make sure that your code is clear, readable and that comments
clarify your lines of code. For instructions on how to compile your answers into a readable pdf: see
Canvas. In the description of the problems you find additional instructions on submitting python code:
• Include your Python code in your results document as well.
Make sure you copy and paste your code into the document (see Canvas for instructions on how
top this efficiently and readable.).
• Submit the .py-file.
In this case you need to submit your python script along with the pdf file.

1 Classes I
Include your Python code in your results document as well.
Submit the .py-file.

a) Copy the class definition of the Rectangle class from the instructions to your own script. Check
if it works by defining an instance of the class and e.g. calculate the area by using one of the class
methods.

b) Add a method to the class that returns the perimeter (sum of the lengths of all sides) of the
rectangle. Add a few lines of code to show that it works.

2 Classes II
Include your Python code in your results document as well.
Submit the .py-file.

a) Define a class Person. The class should have two attributes: name and birth year. The class
should have an init () method.

b) Define a method age(). The method should return the current age of the person5 .


To get the current time and year make use of the module datetime as shown in the
following code snippet:

Editor
# import the module
import datetime

# get the current time


time = datetime . datetime . now ()

# get the current year


year = time . year

5 Just define the age as the current year minus the birth year, so ignoring months and days.

123
c) Add a str () method to the class such that if you use the print() function to print an instance
of the class. The output could look like e.g.:
Name : Joop Zoetemelk
Birth year : 1946
Age : 74

3 Classes: Inheritance
Include your Python code in your results document as well.
Submit the .py-file.

You are going to define a class to hold data of some physicists. Since a scientist is also a person, you
can make use of inheritance by defining a new class based on the class Person as defined in the previous
exercise.

a) Create a class Physicist using inheritance of the class Person. The class should have one addi-
tional attribute: nobel. This attribute denotes if the scientist has ever won a Nobel prize or not
(what type would you use for this?).
b) Overwrite the str () method of the class such that if you use the print() function to print an
instance of the class the output now includes info on the Nobel prize.

c) Add a method award nobel that can award a Nobel prize. E.g. calling scientist.award nobel()
should change the nobel attribute accordingly.
d) Create instances of the class, one for each scientist listed in the table:

name birth year nobel prize

Albert Einstein 1879 yes


Niels Bohr 1885 yes
Stephen Hawking 1942 no

e) Award Stephen Hawking a Nobel prize (he deserves it!).


f) Create a list scientists that contains all instances created in (d).

g) Create a function nobel winners(alist) that accepts the list created in (f) and returns the names
of all Nobelprize winners as a list of strings.

4 Euler’s constant*
Include your Python code in your results document as well.
Submit the .py-file.

Euler’s constant (γ = 0.5772...) can be approximated by the following series:

N
1
γ = − ln N +
n=1
n

a) compute with a single line (excluding definition of N and imports) of code the approximation of γ
for N = 1000

124
5 Counting vowels*
Include your Python code in your results document as well.
Submit the .py-file.

Make a function num vowels(text) that returns the number of vowels (a, e, i, u, o) in the string text.
The function should be case insensitive. For example:

IPython Console
In [1]: num_vowels ( ' Programmeren En Dataverwerking ')
Out[1]: 10

6 The bisection method*


Include your Python code in your results document as well.
Submit the .py-file.

You are given the following function:

f (x) = x3 − x − 2
Write a script that computes the intersection of the function f (x) with the x-axis with an accuracy of
10−10 . The script should use the bisection method.

In the bisection method you start with two values for x (e.g. x = a and x = b) such that you know that
the intersection is somewhere on the interval (a, b). The function is shown in the figure, from which it is
clear that the intersection is somewhere between x = 0 and x = 2. Obviously the sign of f (a) and f (b)
are opposite. Next, you choose a third value for x (e.g. x = c) exactly in the middle of the interval (a, b),
and you evaluate f (c). Depending on the sign of f (c) you replace either the value of a or the value of
b with the value of c such that the new interval (a, b) still contains the intersection. By repeating this
procedure the boundaries of the interval will get closer and closer to the intersection.

125

You might also like