# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernel_info:
#     name: python3
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Awesome Class - Assignment X
#
# ## Formalities
# **Submit in a group of 2-3 people until xx.xx.xxxx 23.59CET. The deadline is strict!**
#
# ## Evaluation and Grading
# **General advice for programming exercises at _Awesome Institution_**
#
# Evaluation of your submission is done semi-automatically.
# Think of it as this notebook being executed once.
# Afterwards, some test functions are appended to this file and executed respectively.
#
# Therefore:
# - Submit valid _Python3_ code only!
# - Use **external** libraries if and only if specified by task.
#   Using the standard library is fine unless you're told otherwise.
# - Ensure your definitions (functions, classes, methods, variables) follow the specification.
#   The signature of a function/method/class usually can be inferred from task description, code skeletons and test cases.
# - Ensure your code does not rely on the current notebook or system state!
#   - Use `Kernel --> Restart & Run All` to see if you are using any definitions, variables etc. that are not in scope anymore.
#   - Double check if your code relies on presence of files or directories other than those mentioned in given tasks.
#   - Tests run under Linux, hence don't use Windows style paths (`some\path`, `C:\another\path`).
#     `pathlib` provides neat abstractions for that, use it!
#     Also, use paths only that are relative to and within your working directory (OK: `some/path`, `./some/path`; NOT OK: `/home/alice/python`, `../../python`).
# - Keep your code [idempotent](https://en.wikipedia.org/wiki/Idempotence)!
#   Running your code or parts of it multiple times must not yield different results.
#   Minimize usage of global variables.
# - Ensure your code/notebook terminates in reasonable time.
#   We enforce timeouts during notebook/test execution.
#   Exceeding those results in an error and potentially no points for that sub-task.
# - Textual answers must always be backed by code and may not refer to results that are not part of your submission.
#
# **There's a story behind each of these points! Don't expect us to fix your code!**

# + [markdown] pycharm={"name": "#%% md\n"}
# Credentials of all team members (you may add or remove items from the list)

# + pycharm={"name": "#%%\n"}
team_members = [
    {
        'first_name': 'Alice',
        'last_name': 'Foo',
        'student_id': 12345
    },
    {
        'first_name': 'Bob',
        'last_name': 'Bar',
        'student_id': 54321
    }
]

# + pycharm={"name": "#%%\n"}
import sys
import time

import matplotlib.pyplot as plt
import pandas as pd

# + pycharm={"name": "#%%\n"}
SOME_CONSTANT = 42

# + [markdown] pycharm={"name": "#%% md\n"}
# Let's do some plotting...

# + pycharm={"name": "#%%\n"}
df = pd.DataFrame([[1, 1], [2, 4], [3, 9], [4, 16], [5, 25]])
df.plot()

# + [markdown] pycharm={"name": "#%% md\n"}
# ... or render a table

# + pycharm={"name": "#%%\n"}
from IPython.display import display

display(df)

# + [markdown] pycharm={"name": "#%% md\n"}
# Now, read some data from the file system

# + pycharm={"name": "#%%\n"}
with open('foo.txt', mode='rt') as f:
    data = f.read()

print(data[:len(data) // 2])
print(data[len(data) // 2:], file=sys.stderr)

# + [markdown] pycharm={"name": "#%% md\n"}
# ... or modify an existing file

# + pycharm={"name": "#%%\n"}
with open('bar.txt', mode='a') as f:
    f.write('>> modification by notebook <<')

# + [markdown] pycharm={"name": "#%% md\n"}
# ... or create a new file

# + pycharm={"name": "#%%\n"}
with open('fnord.txt', mode='wt') as f:
    f.write('let\'s leave some artifacts in the system')


# + [markdown] pycharm={"name": "#%% md\n"}
# Explicit calls to `plt.show` will be re-directed to `auto_save_figure`, storing the plot in a file

# + pycharm={"name": "#%%\n"}
for e in [2, 3]:
    plt.plot(*zip(*((v, v ** e) for v in range(-10, 10))))
    plt.show()

# + [markdown] pycharm={"name": "#%% md\n"}
# However, we can also save a figure explicitly!

# + pycharm={"name": "#%%\n"}
plt.plot(*zip(*((v, v ** 5 + (v - 5) ** 3 - (3 * v) ** 2) for v in range(-20, 20))))
plt.savefig('plot.png')


# + [markdown] pycharm={"name": "#%% md\n"}
# Here are some more functions we can write tests for

# + pycharm={"name": "#%%\n"}
def square(x: float) -> float:
    return x ** 2


def cube(x: float) -> float:
    return x ** 3


abs_cube = cube


def fail():
    raise ValueError('no chance, this function crashes so badly')


def sleep(x):
    time.sleep(x)


# + [markdown] pycharm={"name": "#%% md\n"}
# Tests may define timeouts for cell and test execution.
# Hence, the following code may rise a `TimeoutError`!

# + pycharm={"name": "#%%\n"}
sleep(.1)

# + [markdown] pycharm={"name": "#%% md\n"}
# Importing _autograde_ may fail due to import policy, see `NotebookTest.set_import_filter`

# + pycharm={"name": "#%%\n"}
import autograde

print(autograde.__version__)


# -

# Importing _autograde_ is an issue as it could be used to monkey-patch the library, e.g.
#
# ```Python
# import autograde
#
# def mock(self, *_, **__):
#     return self.score, '🖕'
#
# autograde.notebook_test.UnitTest.__call__ = mock
# ```

# + [markdown] pycharm={"name": "#%% md\n"}
# Import filters also work at test time and for various ways of importing

# + pycharm={"name": "#%%\n"}
def illegal_import():
    exec('import autograde as ag')


# + [markdown] pycharm={"name": "#%% md\n"}
# Unlike normal Python scripts, notebook execution continues even if a cell crashes!

# + pycharm={"name": "#%%\n"}
assert False, 'this cell will crash under any circumstances ...'

# + pycharm={"name": "#%%\n"}
print('... but this cell is not affected :^)')

# + [markdown] pycharm={"name": "#%% md\n"}
# Tests may access contents of markdown comments.
# This is useful when students are asked to elaborate on their solutions.
# -

# **Q1:** _How many roads must a man walk down before you call him a man?_

# **A1:** The answer, my friend, is blowin' in the wind. The answer is blowin' in the wind.

# **Q2:** _What's the answer to life, universe and everything?_

# + [markdown] pycharm={"name": "#%% md\n"}
# **A2:** 42 (forty-two)

StarcoderdataJupyterScriptsDedupFiltered