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

add inf2-SS21-exam

the makefile is now capable of converting many notebooks
parent ca922763
Pipeline #10003 passed with stage
in 6 minutes and 49 seconds
......@@ -27,7 +27,7 @@ build:
- make
- pacman -S python-pip python-virtualenv --noconfirm
- sudo -u build virtualenv nbgrader
- sudo -u build sh -c '. nbgrader/bin/activate; pip install git+https://github.com/jupyter/nbgrader; make -C exam-practice'
- sudo -u build sh -c '. nbgrader/bin/activate; pip install git+https://github.com/jupyter/nbgrader; for d in $(ls -d exam-practice/*/); do make --dry-run -f ../nbgrader-source-to-release.mk -C $d; done'
artifacts:
paths:
- "*.pdf"
......
%% Cell type:markdown id:7427058e tags:
# inf2 exam
- 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
## 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
%% Cell type:code id:a44067ce tags:
``` python
from ipywidgets import interact, fixed # interactive testing
```
%% Cell type:markdown id:af2d83a5 tags:
# Q (8p)
You have the following strings. You are searching for substrings which can be found on every of these strings.
%% Cell type:code id:e57e1d34 tags:
``` python
strs = (
'ABCDDDCBBABDBABABAADACBCCCBADBDDDCAAAABAACBCAAACCBDDCCC',
'ABCDCBADCAADADCDCCADDBCAABABACCADCADCDDBBBCABACAABACBDD',
'ABCDAABABABDABACDACCADBCCDDBBACCAAADADBBCCDAAABCDBDDDBA',
'ABCDBAABDBBDBBCAABDCCAAABDAABDDACDBBDDDDABABAAAADDDCCBA',
'ABCDBDCCACBCCCDDBBCACCABABACCDACDBCCCCCCADDDCDDCDCCDBDB',
)
```
%% Cell type:markdown id:e9d30e1b tags:
For example, the three-letter long substrings `'ABC'` and `'BCD'` can be found in all of them.
Write a function which returns a collection of `n`-long substrings which are contained in all of them.
Hint:
- The location of the common substring in each of the strings is not important.
%% Cell type:code id:bf1f8e98 tags:
``` python
@interact(strings=fixed(strs), n=3)
def common_strs(strings, n):
### BEGIN SOLUTION
n_long_substrs_sets = [
set(str[i:i+n] for i in range(len(str)))
for str in strs
]
return set.intersection(*n_long_substrs_sets)
### END SOLUTION
```
%% Output
%% Cell type:code id:571f7cac tags:
``` python
assert common_strs(strs, 3) == {'BAB', 'BCD', 'ABC', 'ABA'}
assert common_strs(strs, 4) == {'ABAB', 'ABCD', 'BABA'}
assert common_strs(strs, 5) == {'ABABA'}
assert common_strs(strs, 6) == set()
### BEGIN HIDDEN TESTS
def _str_gen():
import random
return ''.join(random.choices('ABCD', k=50))
_strs = [_str_gen() for _ in range(10)]
def _substrings(string, n):
return [string[i:i+n] for i in range(len(string)-n-1)]
for n in range(10):
set.intersection(
*[set(_substrings(string, n)) for string in _strs
]) == common_strs(strs, n)
### END HIDDEN TESTS
```