Commit ed9fc8e3 authored by Gökçe Aydos's avatar Gökçe Aydos
Browse files

inf2-exam-WS2122

parent ca34a3c7
Pipeline #12298 passed with stage
in 12 minutes and 30 seconds
%% Cell type:markdown id:7427058e tags:
# inf2 exam WS2122 (2021-11-26)
- browse the problems before you begin with the exam
- the exam contains many practical problems which may take vast amount of time if you do not know the time-efficient approach. Do not waste much time on a single question.
- in coding questions, you may also explain your approach in words if your code does not work straightaway
- pay attention to the difference between *returning* and *printing* in functions
- use the `interact` widgets and test cells to test your answers.
- `Q` stands for *question* and `QQ` for *subquestion*
- in multiple-choice and `True`/`False` questions a wrong answer has penalty. The penalty equals to the half of the points of a correct answer.
- it should be sufficient to modify the `#YOUR CODE HERE; raise NotImplementedError` lines in coding problems
- code style according to [`PEP8`](https://pep8.org/) will also be graded.
## Good luck! 🤞
---
# Questions
To display the notebooks, run the following cell
%% Cell type:code id:ac9716f4 tags:
``` python
import _header; _header.display_html()
```
%% Output
<IPython.core.display.HTML object>
"""
Calculates total points on every notebook and displays them on an HTML table
"""
import glob
import json
README_NOTEBOOK_FILENAME = '_README.ipynb'
def notebook_to_points_dict():
ipynbs = glob.glob('*.ipynb')
ipynbs.remove(README_NOTEBOOK_FILENAME)
nb_pts = dict() # filename: points
for ipynb in ipynbs:
with open(ipynb) as f:
nbjson = json.load(f)
#display(nbjson)
total_points = 0
for cell in nbjson['cells']:
try:
# 'points' key exists in autograder cells only
points = cell['metadata']['nbgrader']['points']
except KeyError:
continue
total_points += points
nb_pts[ipynb] = total_points
return nb_pts
def ipynb_points_html(notebook_to_points):
# remove '.ipynb' from filenames
notebook_to_points = {k[:-6]: v for k, v in notebook_to_points.items()}
# add sum
footer = dict()
footer['∑'] = sum(v for v in notebook_to_points.values())
from jinja2 import Template
template_content = """
<table>
<thead>
<tr>
<th> Notebook </th>
<th> Points </th>
</tr>
</thead>
<tbody>
{% for key, value in data.items() %}
<tr>
<th> <a href="{{ key }}.ipynb" target="_blank"> {{ key }} </a> </th>
<td> {{ value }} </td>
</tr>
{% endfor %}
</tbody>
<tfoot>
{% for key, value in footer.items() %}
<tr>
<th> {{ key }} </th>
<td> {{ value }} </td>
</tr>
{% endfor %}
</tfoot>
</table>"""
template = Template(template_content)
# template rendering embedded in the HTML representation
return template.render(data=notebook_to_points, footer=footer)
def display_html():
from IPython.display import HTML
return HTML(ipynb_points_html(notebook_to_points_dict()))
\ No newline at end of file