Skip to content

Homework 01

Overall Info
Due date: 2027-03-22
Submit a *.py file with comments for the code
In groups of 5-6 students

1. Datatypes, Control Flows, Functions

Note

For the following short exercises, no use of numpy.

1.1 Using conditional statements and loops, count the number of numbers between 0 and \(10\,000\) which are divisible by 3 or 7 and print it.

1.2 Write a program that prints the following pattern

ooooooooooooooooo                                                       
ooooooooooooooooo                                                       
ooooooooooooooooo                                                       
oooo                                                                    
oooo                                                                    
oooo                                                                    
ooooooooooooooooo                                                       
ooooooooooooooooo                                                       
ooooooooooooooooo                                                       
             oooo                                                       
             oooo                                                       
             oooo                                                       
ooooooooooooooooo                                                       
ooooooooooooooooo                                                       
ooooooooooooooooo

1.3 Given a list ["apple", "orange", "cabbage", "lemon", "potato"] extract the subarray containing cabbage and potato, and return their indices in the original list. Furthermore, write a program that inverts the order of the list.

1.4 Write a function that takes as input one of the names of the 12 months and returns the number of days of this month (February has 28)

1.5 Write a function that takes a list of numbers and returns an ordered list, i.e. [1, 4, 2] -> [1, 2, 4]. You can test this function by creating arbitrary lists from numpy arrays: x = list(np.random.rand(N)).

2. Numpy

2.1 Using numpy and its functionalities redo the exercises 1.1 and 1.5

2.2 Create two arrays of numbers between -1 and 1. The first one has consecutive numbers equally spaced by 0.01 while the second one has exactly 100 elements.

2.3 Given natural numbers \(n\) and \(d\), create a \(n\times d\) matrix of the form

[
    [0, 1, ..., d-1],
    [d, d+1, ..., 2d-1]
    ...
    [(n-1)d,...,(nd) -1 ]
]

Note

There are several ways to do it, but remember that loops are not efficient in python. Have a look at the shape manipulations in numpy.

2.4 Create a 10x10 matrix where each line contains all the numbers 0 to 9 but shuffled uniformly randomly (check the random routines of numpy documentation)

2.5 Normalize a random 5x5 matrix such that the smallest element is 0 and the largest is 1

2.6 Round a random 5x5 array with two digits after the comma

2.7 Given a random array of size 100, find the position and value of the element closest to 0.6

2.8 Write a function which, given an array of size \(N\) of numbers between 0 and 1, returns the histogram of the array by bins of 0.1 (that is, the number of elements between 0 and 0.1, between 0.1 and 0.2, ..., between 0.9 and 1)

3. Numpy and Plotly: Collatz Conjecture

Collatz Conjecture

Consider the following sequence \(u = (u_i)\) of natural numbers given by

\[ \begin{equation} u_{i+1} = \begin{cases} u_i /2 & \text{if }u_i \text{ is even}\\ 3u_i + 1 & \text{if }u_i \text{ is odd} \end{cases} \end{equation} \]

starting with a number \(u_0 = n \in \mathbb{N}\).

The Collatz Conjecture states that whatever the starting integer \(n \in \mathbb{N}\), the sequence ultimately reaches \(1\). The conjecture is still an open problem; some partial answers in this direction were given by Terence Tao in 2019.

For \(n \in \mathbb{N}\), denote by

\[ \tau(n) : = \inf\{i \colon u_i = 1\} \]

which represents the first time where the sequence starting from \(n\) reaches \(1\). If this sequence never reaches \(1\) starting with \(n\), then \(\tau(n) = \infty\). Collatz conjecture states that \(\tau(n)<\infty\) for every \(n\).

There is strong confidence that this conjecture is true, and empirically, it holds for every natural number smaller than \(2^{68}\)

3.1 Using the control flow while, given an integer \(n\) write a function that returns a numpy array \((u_0, u_1, \ldots, u_{\tau(n)})\). Mathematically, also write down the domain and codomain of this function.

3.2 Using plotly, provide a function where given a numpy array \(x = (n_0, \ldots, n_{d-1})\) it plots \(d\) paths \((u_0, \ldots, u_{\tau(n_k)})\) where \(u_0 = n_k\) for \(k=0, \ldots, d-1\).

3.3 Write a third function where, given a numpy array \(x = (1, \ldots, d)\) it returns the array \((\tau(1), \ldots, \tau(d))\).

3.4 Using the bar plotting functionality (fig.add_bar(...)) of plotly, write a function that plots the histogram of the previous function, that is \((l_1, l_2, \ldots)\) where

\[ \begin{equation} l_k = \text{Cardinality}\{n\colon \tau(n) = k, \quad 0\leq n \leq N\} \end{equation} \]

for (depending on the performance of your computer) \(N=1000, 10000, 100000\).

4. Scipy

4.1 Consider the function \(x \mapsto f(x) = x^2 + 10 \sin(x)\) for \(x \in \mathbb{R}\)

  • plot the function
  • find its minimum

4.2 For the following distributions:

  • normal with std = 1, 2, 5
  • Student's t with degrees of freedom 2, 3, 4, 6

Plot the pdf and cdf of each distribution for the different parameters and compare