import math
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
Exam (2nd attempt)
NES—Programmiertechniken (Introduction to scientific programming)
12.03.2024
Please work through the following tasks. You have 75 minutes to complete the exam.
Make sure you executed all relevant code cells and save the notebook before the end of the exam.
Question 1: Multiple-choice (5 points)
To tick correct answers, replace - [ ] with - [x]
Careful: wrongly ticked answers will give negative points!
(a) Which of the following statements are true?
- A python script with the file ending
.py
can be run from the terminal with thepython
command. conda
can be used to install software required to use python on laptops.- A terminal cannot run a python interpreter.
- Imagine you work on different work/research projects using python that rely on different packages. In this case it is a good idea to keep the software installation of python and its packages in separate conda environments, one for each project.
- Using the terminal is an outdated form of communicating with the computer. Nowadays, every task can be done with graphical user interfaces and sophisticated programs.
(b) Which of the following statements are true?
- Logical indexing is a vectorized notation that can replace explicit for loops in python.
- Each python script or program has exactly one namespace.
- In python, not all container data types are mutable.
- If a function modifies and then returns its input parameters, it is said to cause side effects, which should generally be avoided.
- The
break
statement is used to terminate a python program.
Question 2: datetime calculations (6 points)
- Create a DataFrame
df
with a DatetimeIndex ranging from2024-01-01
to2024-12-31
in daily sampling.
# <your code goes here>
- Create a column named
day_of_year
that stores the day of the year from 1 to however many days there are in 2024.
# <your code goes here>
- Create another column
squared
that computes the square ofday_of_year
(i.e.,day_of_year
to the power of two.)
# <your code goes here>
- At which date is
squared
equal to 3600?
# <your code goes here>
- Resample the Series
squared
to their monthly minimum and maximum values and plot them in a quick working plot.
The code cell for this task should contain a maximum of five lines.
# <your code goes here>
Question 3: Spreadsheet data (9 points)
Your working directory contains a spreadsheet data set.
Solve the following tasks:
- Load the data contained in the spreadsheet
dataset.csv
.
# <your code goes here>
- How many different
class
es are contained in the data set and what are their names?
# <your code goes here>
- It looks like there are some NaN’s in the DataFrame. How many are there in each column?
# <your code goes here>
- Set the
value
for eachclass
that is NaN to120
.
# <your code goes here>
- Extract the average
value
of each class and compute the standard deviation of the resulting vector.
There are several ways to achieve this. If you are unsure, implement the solution with a loop.
# <your code goes here>
- Use the
.pivot()
method to convert the DataFrame from a long format to a wide format.
x
andy
become the columns and the index, while the cells should be filled with thevalue
column.
# <your code goes here>
- Using the wide DataFrame, re-create the following Figure as closely as possible (incl. labels).
# <your code goes here>
Question 4: Code snippets! (7 points)
(a) Loop with user input:
The following code block misses some code. Fill in the gaps where indicated by the comments.
= []
numbers = 0
counter = 10
maxiter while counter < maxiter:
## Update the counter
# <your code goes here>
= input('Enter a number or finish with [q]: ')
inp
## Stop the loop if user provides the letter q:
# <your code goes here>
try:
## Convert the user provided input to a floating point number
## and add it to the list `numbers`.
# <your code goes here>
except:
print('Invalid input')
print(f'\nProvided numbers: {numbers}')
(b) Mutation with side effect:
The following code cell first defines two variables x and y. Then, it prints the sum of y, mutates an element in x, and prints the sum of y again. The two print statements differ.
Make an adjustment to line 2, so that y is not affected by changes in x any more.
= np.arange(10)
x = np.flip(x) # changes to the code are only allowed in this line!
y
print(y.sum())
0] = -9
x[print(y.sum())
(c) Functions:
Define a function called approx_sin
which implements an approximation of the sine function given by the equation
\[ sin(x) = \sum_{i=0}^n (-1)^i \frac{x^{2i+1}}{(2i+1)!}\]
where \(!\) denotes the number’s factorial and can be computed with the function math.factorial()
.
The function should take \(x\) and \(n\) as inputs, where \(n\) should default to 5 if not provided by the user.
# <your code goes here>
When you implemented your function, run the following code block to view the approximation error for different \(n\):
(This is not a task anymore, you don’t need to make any changes to the following code block!)
= math.pi / 2
x = math.sin(x)
true_value for n in [1, 3, 5, 7]:
= abs(approx_sin(x, n) - true_value)
error print(f'Error for x=π/2 and {n=}: {error:e}')