Exercise 0: Python warmup (not assessed)

This notebook has some warmup questions, for getting used to Python. It also shows you how to use the automated ticker.

This notebook also serves as a guide on how to structure your own notebooks. It's all too easy to create spaghetti code in Jupyter, and if you follow the structure here you'll make life easier for yourself. (For the IA Scientific Computing course you will be assessed ONLY on your answers and on whether you are able to explain your code, NOT on how well structured your code is. Nevertheless, if your code is tangled then you may have trouble explaining it!

  1. Initialization
  2. Run-once setup
  3. Answer the questions
  4. Submit your answers

1. Initialization

Start by importing any required modules.

Note. If you're using hub.cl.cam.ac.uk then it has all the necessary libraries installed. On other systems you might get an error message like
----> 1 import pandas
ModuleNotFoundError: No module named 'pandas'
This means you need to install pandas before you are able to import it. Install it from within Jupyter by running
!pip install pandas
If you're running on your own machine, you only need to do this once. If you're running on Google Colab, for example, you need to do it every time you get a new server.

2. Run-once setup

Load in any datasets you are using. (For these warmup questions, there aren't any datasets needed.)

If there are any general-purpose functions that you've defined, which you plan to use to answer several questions, then place them here. (For these warmup questions, you don't need any such general-purpose functions.)

3. Answer the questions

If a question asks you for a value, just compute it. If a question asks you to define a function, define that function. (For now, just prepare your answers. Submitting them to the automated ticker will come later, in step 4.)

When you're asked for a function, you should run one or more tests. Work out by hand the answer you expect, then check your code gives that answer. For these warmup questions, I've suggested testsl; and you can also check your code by looking at the model solutions at the bottom of this notebook. For the assessed ticks, it's up to you to invent the tests, and there's no checking of your code — the ticker only looks at the answers you compute.

Organize your code so that it will work correctly if you run it all top-to-bottom. You can check it using "Kernel | Restart & Run All". If your code relies on running the cells in a screwy order, you will confuse yourself and your readers.

Question (q0). Write a function repeat(x,n) that, given an integer $n$ and a value $x$, produces a list consisting of $n$ copies of $x$.

Question (q1). Produce a triple consisting of

Question (q2). Let $x$ be a list of strings. Write a one-line function to sort $x$ by length, breaking ties alphabetically.

Question (q3). Let $x$ be a list of numbers. Write a one-line function to find the number of unique elements in $x$.

Question (q4). A simple queue can be simulated by the following equations. Let $q_t$ be the queue size just before timestep $t$, let the service rate be $C$, and let $a_t$ be the amount of work arriving at timestep $t$. Then $$ q_{t+1} = \max(q_t + a_t - C, 0). $$ This is called Lindley's Recursion. Write a function sim(q0,C,a) to compute the queue sizes. It should accept an initial queue size q0 and a list a consisting of $[a_0,a_1,\dots,a_{t-1}]$, and it should return a list $[q_1,\dots,q_t]$.

Question (q5). We can represent a tree as a nested list, for example

x = [1,[[2,4,3],9],[5,[6,7],8]]

Define a function maptree(x, f) which returns a tree of the same shape but with the function f applied to to every leaf.

4. Submit your answers

Run the following two lines to set up the automatic ticker. (The questions are grouped by section, and here we're working on section ex0. For the ticks, you'll be told which section to use.)

It will ask you to log in via Raven, and show you your progress so far.

Note. If you get the error message
----> 1 import ucamcl
ModuleNotFoundError: No module named 'ucamcl'
it means you need to install ucamcl. See the comment at the top of this notebook.

For each question, you'll be told how to submit your answers. In brief: you call GRADER.fetch_question to get a question-object with paramaters, you compute your answer on those parameters, then you call GRADER.submit_answer to submit your answer.

The reason you're asked to call fetch_question is so that different students work with different parameters. You also get different parameters each time you make a fresh attempt. If you print the question-object, it'll show what parameters it has.

Model solutions to the warmup exercises