2.4. Exercises#
Note
You don’t need to worry about using Interactive Jupyter Notebooks for these exercises. You can simply ignore the rocket icon () and go ahead to answer the questions using the knowledge you’ve gained from the chapter. Have fun!
2.4.1. Exercise#
One of the most crucial applications of if
statements is filtering the data from errors and checking whether an error is within a certain limit.
For example, checking whether the difference between an estimated value and the actual value are within a certain range.
Mathematically speaking, this can be expressed as \(|\hat{y} - y| < \epsilon\)
where \(\hat{y}\) is your estimated value, \(y\) is the actual value and \(\epsilon\) is a certain error threshold.
The function check_error_size()
below must do the same — it should return True
if the error is within the acceptable range eps
and False
if it is not.
def check_error_size(estimated_value, true_value, eps):
if abs(estimated_value - true_value) <= eps:
return True
2.4.2. Exercise 2.2.2#
You have used the knowledge obtained to write your very own function to classify soil samples based on the average grain size.
def classify_soil(avg_grain_size):
if avg_grain_size < 0.002:
return 'Clay'
elif avg_grain_size >= 0.002 and avg_grain_size < 0.063:
return 'Silt'
elif avg_grain_size >= 0.063 and avg_grain_size < 2:
return 'Sand'
elif avg_grain_size >= 2:
return 'Gravel'
print(classify_soil(1.5))
2.4.3. (Searching) Exercise#
Conditional expression is a way how one can compress an if
statement to a more compact (and logical) statement. Your task is to rewrite the below if
statement by using the ’conditional expression’ technique.
y = 0
x = 5
if x % 2 == 0:
y = x ** 2
else:
y = x % 3
2.4.4. Exercise 2.4.1#
The function celsius_to_fahrenheit
is not working properly, can you detect the error?
def celsius_to_fahrenheit(temp_celsius):
temp_fahrenheit = 0
for i in range(len(temp_celsius)):
temp_fahrenheit.append(temp_celsius[i] * 9 / 5 + 32)
return temp_fahrenheit
temp_celsius = [-1, -1.2, 1.3, 6.4, 11.2, 14.8, 17.8, 17.7, 13.7, 8.5, 4.1, 0.9]
print(celsius_to_fahrenheit(temp_celsius))
2.4.5. Exercise#
Here you need to write a function that is able to sort any list consisting only of real numbers, in the descending order. For example, the list \([19, 5, 144, 6]\) becomes \([144, 19, 6, 5]\). However there are three possible options to complete correctly the code where the dots ...
are placed.
def sort_list(unsorted_list):
return sorted(unsorted_list)...
2.4.6. Exercise#
In this exercise you will perform downsampling of a provided ‘regular’ 2D list. Downsampling is a procedure where only a subset of the data is sampled (to reduce its size, for example). Below a visual aid of what downsampling of a 2D list is. Instead of using all the data available, your task is to downsample the 2D list to keep only the data of every \(a\)-th row and every \(b\)-th column, including the first element \((a_{0,0})\).
$\(A = \left[\begin{array}{ccccc}
a_{0,0} & \dots & a_{0,b} & \dots & a_{0,2b} & \dots & \dots & a_{0,nb} & \dots\\
\vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots\\
a_{a,0} & \dots & a_{a,b} & \dots & a_{a,2b} & \dots & \dots & a_{a,nb} & \dots\\
\vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots\\
a_{2a,0} & \dots & a_{2a,b} & \dots & a_{2a,2b} & \dots & \dots & a_{2a,nb} & \dots\\
\vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots\\
\vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots\\
a_{ma,0} & \dots & a_{ma,b} & \dots & a_{ma,2b} & \dots & \dots & a_{ma,nb} & \dots \\
\vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots
\end{array}\right]\)$
Notice the ...
you need to fill with the correct code. Additionally there are some errors in the functions can you detect the errors?
Option A
def downsample(data, a, b):
downsample_data = [0]
for i in range(len(data)):
row = []
# create a for loop over len(data[i]), which is the number of columns
for ...
if i % a == 0 and j % b == 0:
row.append(data[i][j])
if len(row) > 0:
downsample_data.append(row)
return downsample_data
Option B
def downsample(data, a, b):
downsample_data = []
for i in range(len(data)):
row = []
# create a for loop over len(data[i]), which is the number of columns
for ...
if i % a == 0 and j % b == 0:
row.append(data[i][j])
if len(row) > 0:
downsample_data.append(row)
return downsample_data
Option C
def downsample(data, a, b):
downsample_data = 0
for i in range(len(data)):
row = []
# create a for loop over len(data[i]), which is the number of columns
for ...
if i % a == 0 and j % b == 0:
row.append(data[i][j])
if len(row) > 0:
downsample_data.append(row)
return downsample_data