Exercise Notebook 3 - Part 1 - Data structures and loops

Exercise Notebook 3 - Part 1 - Data structures and loops#

You can download this notebook and the additional files by clicking the download button() in the top right and selecting “Exercise notebook”.

Exercise 3.1.1

Now, let’s get down to practice.

Your first task is to finish the pack_variables() function — a function which will combine all inputs in one list and return it.

More precisely, this function will receive \(5\) arguments as input and you have to return a list with these \(5\) elements inside.

def pack_variables(arg1, arg2, arg3, arg4, arg5):
    ...


print(pack_variables(1, 2, 4, 22, 7))
Exercise 3.1.2

Here, you will have to perform a quality assessment on a received list of GNSS measurements. But a simple one.

You have to check whether the received data has more than \(1000\) measurements, in that case we know the receiver was running for a long time without being interrupted. If it is shorter than \(1000\), we assume there were some interruptions. Hence, your function should return a message whether the data is fine or not.

You may want to add print(measurements) to the cell to check what goes on inside the function.

def check_data(measurements):
   ...


#You can check your code below, where we simulate 1250 measurements
import random 
gnss_data = [random.random() * 2.2e8 for i in range(1250)]

print(check_data(gnss_data))

explanations for the test data will follow later, but in brief:

  • random.random() returns a random number between 0 and 1

  • [... for i in range(...)] is a list comprehension, creating a list from an expression.

  • here a list of 1250 random numbers is created

Exercise 3.1.3

In this exercise you will practice how to access data from a dictionary. Sometimes it is easier to work with the most convenient data type, that is best suited for a specific task. In this example, you have to write a function which accepts data stored in a dictionary and saves some data from it. More precisely, you have to select the \(x\) and \(y\) coordinates saved in the input_dict dictionary, and return them as a tuple, with \(x\) being the first entry.

# you do not have to change anything in this cell, just run it

input_dict = {
    'ID': '334856',
    'operator_name': 'Jarno',
    'observation_amount': '2485',
    'loc_x': [2, 5, 10, 12, -5, 8, 27, 1],
    'loc_y': [6, 1, -5, 15, 4, 8, 0, 10]
}
def unpack_dictionary(input_dict):

    loc_x = ...
    loc_y = ...
    locations = ...
    # insert your for loop here to fill locations
    ...
    return locations


print(unpack_dictionary(input_dict))
Exercise 3.2.1

In this exercise you will write your own Celsius to Fahrenheit converter! Your task is to write a function, which will accept the list of temperatures temp_c, in Celsius, and will output a list with the same temperatures, but in Fahrenheit.

Hint: create an empty list for the result, then append values to this list. See the “personalized greeting” example in Section 3.2.

# you do not need to change anything in this cell

temperatures_c = [-1, -1.2, 1.3, 6.4, 11.2, 14.8, 17.8, 17.7, 13.7, 8.5, 4.1, 0.9]
def celsius_to_fahrenheit(temp_c):
    ...

print(celsius_to_fahrenheit(temperatures_c))
Exercise 3.2.2

Your task here is to write a function which will analyze a broadcasting message of the following format: "satellite_ids;date", where the first part of the message contains unique lowercase letters, each corresponding to a different satellite ID, and the last part contains the date of the message.

Here are some examples: "agf;06062022" (3 satellites: a, g, and f), "abcdefgops;03121999" (10 satellites), "xyz;11112011" (3 satellites).

Your task is to write a function, which for a provided broadcast message, will count the number of satellites mentioned in the message.
def count_satellites(message):
    ...

# Check that with the example below you count 6 satellites
print(count_satellites("hpzdet;12122007"))
Exercise 3.2.3

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]\).

Hint: use a built-in sorted() function to sort the list in ascending order, and then think of a clever way to change the order this list to descending order.

def sort_list(unsorted_list):
    ...

print(sort_list([9, 3, -1, 5, 1, -9, 1]))
Exercise 3.2.4

Use a for loop and what you learned about range() to print out every third element of the list L, i.e., the output should be ['b', 'd', 'f', 'h'].
L = ['a', '1', 'b', 'c', '2', 'd', 'e', '3', 'f', 'g', '4', 'h'] # you don't need to change L

# your loop here
...
Exercise 3.2.5

Write a function that converts a number from degrees to radians.
def DegToRad(deg):
    ...


Angle = 180 # Degrees
print(f"An angle of {Angle} Degrees is equal to {DegToRad(Angle):.3f} radians")
Exercise 3.2.6

Write a function that takes four inputs:

\((x_1, y_1, x_2, y_2)\) and computes the Euclidian distance between point 1 \((x_1,y_1)\) and point 2 \((x_2,y_2)\).

def distance ...



x1, y1 = 1, 1
x2, y2 = 2, 3

print(f"Distance between points ({x1}, {y1}) and ({x2}, {y2}) is {distance(x1, y1, x2, y2):.3f}")