Assignment 4.`1

from assertions import *
from test_cells import *

Create a function that takes three participants’ names (name1, name2, name3), sorts them by alphabet and returns the sorted list.

Expected input

"Petra", "Adam", "Eva"

Expected return values

["Adam", "Eva", "Petra"]
# Code for assignment 4.1
def assignment_4_1(name1, name2, name3):
    # YOUR CODE HERE
    raise NotImplementedError()
# Test for assignment 4.1

# Press Ctrl-Enter (PC) or Command-Enter (Mac) after changing the function 
# above and pressing Ctrl-Enter (PC) or Command-Enter (Mac)

test_cell_4_1(assignment_4_1)
# Assertion for assignment 4.1

check_assertion(assignment_4_1)

Assignment 4.2

Write a function that takes a list of dictionaries (list_d) containing participant IDs and locations, and generates file names by combining the participant ID and location.

Expected input

[{"id":"001", "location":"Rotterdam"},{"id":"002", "location":"Utrecht"},{"id":"003", "location":"Amsterdam"},{"id":"004", "location":"Nijmegen"},{"id":"005", "location":"Breda"}]

Expected return values

["001_Rotterdam.txt","002_Utrecht.txt","003_Amsterdam.txt","004_Nijmegen.txt","005_Breda.txt"]
# Code for assignment 4.2
def assignment_4_2(list_d):
    # YOUR CODE HERE
    raise NotImplementedError()
# Test for assignment 4.2

# Press Ctrl-Enter (PC) or Command-Enter (Mac) after changing the function 
# above and pressing Ctrl-Enter (PC) or Command-Enter (Mac)

test_cell_4_2(assignment_4_2)
# Assertion for assignment 4.2

check_assertion(assignment_4_2)

Assignment 4.3

Write a function that takes a list of file names and changes their ending depending on their name. - if the file name contains “table” it should be a .csv file - if the file name contains “text” it should be a .txt file - if the file name contains neither “table” nor “text” it should remain unchanged

Input

["table1.txt", "table2.csv", "text1.txt", "text2.csv", "plot.jpg", "plot.csv"]

Expected return values

["table1.csv", "table2.csv", "text1.txt", "text2.txt", "plot.jpg", "plot.csv"]
# Code for assignment 4.3
  
def assignment_4_3(file_names_list):
    # YOUR CODE HERE
    raise NotImplementedError()
# Test for assignment 4.3

# Press Ctrl-Enter (PC) or Command-Enter (Mac) after changing the function 
# above and pressing Ctrl-Enter (PC) or Command-Enter (Mac)

test_cell_4_3(assignment_4_3)
# Assertion for assignment 4.3

check_assertion(assignment_4_3)

Assignment 5.1

Find the error in the following code, by using try and except and looking at the raised exception.

def assignment_5_1(persons):
    """function takes list of people and prints every element (one per line)"""
    persons = ["Lisa", "Julia", "Veronika", "Anton", "Karl", "Vanessa"]
    
    for person_index in range(len(persons)+1):
        person = persons[person_index]
        print(person)

Expected input

["Lisa", "Julia", "Veronika", "Anton", "Karl", "Vanessa"]

Expected print

Lisa
Julia
Veronika
Anton
Karl
Vanessa
# Code for assignment 5.1

def assignment_5_1(persons):
    """function takes list of people and prints every element (one per line)"""
    # YOUR CODE HERE
    raise NotImplementedError()
# Test for assignment 5.1

# Press Ctrl-Enter (PC) or Command-Enter (Mac) after changing the function 
# above and pressing Ctrl-Enter (PC) or Command-Enter (Mac)

test_cell_5_1(assignment_5_1)
# Assertion for assignment 5.1

check_assertion(assignment_5_1)

Assignment 5.2

Find the error(s) and correct the following function:

dict_people = [{"first_name": "Petra", "age":15}, {"first_name": "Klaudia", "age":20}, {"first_name": "Klaudia", "age":33}, {"first_name": "Adam", "age":30}, {"first_name": "Joshua", "age":15}, {"first_name": "Michel", "age":21}, {"last_name": "Bauer", "age":55}]

def assignment_5_2(dict_people):
    """function takes a list of dictionaries containing names and ages of people 
       and returns a list of all their names."""
    list_names = set()
    for person in dict_people:
        list_names.append(person["first_name"])
    return list_names

Hint: remember try and except – you can use it to detect and skip problematic cases. Do NOT change the value of dict_people.

Expected input

[{"first_name": "Petra", "age":15}, {"first_name": "Klaudia", "age":20}, {"first_name": "Klaudia", "age":33}, {"first_name": "Adam", "age":30}, {"first_name": "Joshua", "age":15}, {"first_name": "Michel", "age":21}, {"last_name": "Bauer", "age":55}]

Expected return values

["Petra", "Klaudia", "Klaudia", "Adam", "Joshua", "Michel"]
# Code for assignment 5.2

def assignment_5_2(dict_people):
    """function takes a list of dictionaries containing names and ages of people
       and returns a list of all their names."""
    # YOUR CODE HERE
    raise NotImplementedError()
# Test for assignment 5.2

# Press Ctrl-Enter (PC) or Command-Enter (Mac) after changing the function 
# above and pressing Ctrl-Enter (PC) or Command-Enter (Mac)

test_cell_5_2(assignment_5_2)
# Assertion for assignment 5.2

check_assertion(assignment_5_2)

Assignment 5.3

Correct the following code. Check the variable names, keys and the syntax. Use try and except for potential error messages. Do NOT change the value of dict_people.

dict_people = {"001": {"first_name": "Petra", "age":15}, "002":{"First_name": "Klaudia", "age":20}, "003": {"first_name": "Klaudia", "age":33}, "004":{"first_name": "Adam", "age":30}, "005":{"First_name": "Joshua", "age":15}, "006":{"first_name": "Michel", "age":21}, "007":{"last_name": "Bauer", "age":55}}

def assignment_5_3(dict_people):
    list_names = set()
    for person in dict_people.values():
        list_names.append(person["first_name"])
        return list_names

Expected input

{"001": {"first_name": "Petra", "age":15}, "002":{"First_name": "Klaudia", "age":20}, "003": {"first_name": "Klaudia", "age":33}, "004":{"first_name": "Adam", "age":30}, "005":{"First_name": "Joshua", "age":15}, "006":{"first_name": "Michel", "age":21}, "007":{"last_name": "Bauer", "age":55}}

Expected return values

['Petra', 'Klaudia', 'Klaudia', 'Adam', 'Joshua', 'Michel']
# Code for assignment 5.3
def assignment_5_3(dict_people):
    # YOUR CODE HERE
    raise NotImplementedError()
# Test for assignment 5.3

# Press Ctrl-Enter (PC) or Command-Enter (Mac) after changing the function 
# above and pressing Ctrl-Enter (PC) or Command-Enter (Mac)

test_cell_5_3(assignment_5_3)
# Assertion for assignment 5.3

check_assertion(assignment_5_3)

Assignment 5.4

Correct the following code, but this time see if you can manage without using try and except. The function should return a unique list of first names from the dictionary.

dict_people = {'001': {'first_name': 'Petra', 'age': 15}, '002': {'First_name': 'Klaudia', 'age': 20}, '003': {'first_name': 'Klaudia', 'age': 33}, '004': {'first_name': 'Adam', 'age': 30}, '005': {'First_name': 'Joshua', 'age': 15}, '006': {'first_name': 'Michel', 'age': 21}, '007': {'last_name': 'Bauer', 'age': 55}}

def assignment_5_4(dict_people):
    list_names = set()
    for person in dict_people.values():
        list_names.append(person["first_name"])
        return list_names

Expected input

{'001': {'first_name': 'Petra', 'age': 15}, '002': {'First_name': 'Klaudia', 'age': 20}, '003': {'first_name': 'Klaudia', 'age': 33}, '004': {'first_name': 'Adam', 'age': 30}, '005': {'First_name': 'Joshua', 'age': 15}, '006': {'first_name': 'Michel', 'age': 21}, '007': {'last_name': 'Bauer', 'age': 55}}

Expected return values

{'Petra', 'Adam', 'Klaudia', 'Michel', 'Joshua'}
# Code for assignment 5.4
def assignment_5_4(dict_people):
    list_names = set()
    # YOUR CODE HERE
    raise NotImplementedError()
    
    for person in dict_people.values():
        list_names.add(person["first_name"])
        return list_names
# Test for assignment 5.4

# Press Ctrl-Enter (PC) or Command-Enter (Mac) after changing the function 
# above and pressing Ctrl-Enter (PC) or Command-Enter (Mac)

test_cell_5_4(assignment_5_4)
# Assertion for assignment 5.4

check_assertion(assignment_5_4)

Assignment 6.1

Read the csv file static/first_names.txt and store the contents in a list. Sort the participants by first names and print them.

CSV file content

name
Peter
Alex
Sandra
Eva
Adam
Fred
Klaudia
Helena
Anna
Xavier
Yonne
Timo
Joeri
Noralie
Kevin
Ruben
Mick
Tom
Berta

Expected print on console

Adam
Alex
Anna
Berta
Eva
Fred
Helena
Joeri
Kevin
Klaudia
Mick
Noralie
Peter
Ruben
Sandra
Timo
Tom
Xavier
Yonne
# Code for assignment 6.1

def assignment_6_1():
    file_location = "static/first_names.txt"
    # YOUR CODE HERE
    raise NotImplementedError()
# Test for assignment 6.1

# Press Ctrl-Enter (PC) or Command-Enter (Mac) after changing the function 
# above and pressing Ctrl-Enter (PC) or Command-Enter (Mac)

test_cell_6_1(assignment_6_1)
# Assertion for assignment 6.1

check_assertion(assignment_6_1)

Assignment 6.2

Read the csv file static/names_age_location.csv and store the contents in a list. Sort the participants by age and print their age and name (seperated by a tab).

CSV file content

name,age,location
Peter,21,Rotterdam
Alex,45,Rotterdam
Sandra,65,Rotterdam
Eva,76,Utrecht
Adam,31,Utrecht
Fred,90,Utrecht
Klaudia,45,Utrecht
Helena,32,Nijmegen
Anna,33,Nijmegen
Xavier,33,Nijmegen
Yonne,54,Nijmegen
Timo,67,Nijmegen
Joeri,89,Amsterdam
Noralie,10,Amsterdam
Kevin,11,Amsterdam
Ruben,90,Amsterdam
Mick,1,Amsterdam
Tom,23,Groningen
Berta,54,Groningen

Expected print on console

1   Mick
10  Noralie
11  Kevin
21  Peter
23  Tom
31  Adam
32  Helena
33  Anna
33  Xavier
45  Alex
45  Klaudia
54  Berta
54  Yonne
65  Sandra
67  Timo
76  Eva
89  Joeri
90  Fred
90  Ruben
# Code for assignment 6.2

def assignment_6_2():
    import csv
    file_location = "static/names_age_location.csv"
    # YOUR CODE HERE
    raise NotImplementedError()
# Test for assignment 6.2

# Press Ctrl-Enter (PC) or Command-Enter (Mac) after changing the function 
# above and pressing Ctrl-Enter (PC) or Command-Enter (Mac)

test_cell_6_2(assignment_6_2)
# Assertion for assignment 6.2

check_assertion(assignment_6_2)

Assignment 6.3

Read the csv file static/names_age_location.csv and store the contents in a dictionary of dictionaries. Give each participant an id (incremental integers starting at 0). Save the dictionary in a JSON file in output/names_age_location.json and return the dictionary at the end.

CSV file content

name,age,location
Peter,21,Rotterdam
Alex,45,Rotterdam
Sandra,65,Rotterdam
Eva,76,Utrecht
Adam,31,Utrecht
Fred,90,Utrecht
Klaudia,45,Utrecht
Helena,32,Nijmegen
Anna,33,Nijmegen
Xavier,33,Nijmegen
Yonne,54,Nijmegen
Timo,67,Nijmegen
Joeri,89,Amsterdam
Noralie,10,Amsterdam
Kevin,11,Amsterdam
Ruben,90,Amsterdam
Mick,1,Amsterdam
Tom,23,Groningen
Berta,54,Groningen

Expected return values

{
 0: {'name': 'Peter', 'age': '21', 'location': 'Rotterdam'},
 1: {'name': 'Alex', 'age': '45', 'location': 'Rotterdam'},
 2: {'name': 'Sandra', 'age': '65', 'location': 'Rotterdam'},
 3: {'name': 'Eva', 'age': '76', 'location': 'Utrecht'},
 4: {'name': 'Adam', 'age': '31', 'location': 'Utrecht'},
 5: {'name': 'Fred', 'age': '90', 'location': 'Utrecht'},
 6: {'name': 'Klaudia', 'age': '45', 'location': 'Utrecht'},
 7: {'name': 'Helena', 'age': '32', 'location': 'Nijmegen'},
 8: {'name': 'Anna', 'age': '33', 'location': 'Nijmegen'},
 9: {'name': 'Xavier', 'age': '33', 'location': 'Nijmegen'},
 10: {'name': 'Yonne', 'age': '54', 'location': 'Nijmegen'},
 11: {'name': 'Timo', 'age': '67', 'location': 'Nijmegen'},
 12: {'name': 'Joeri', 'age': '89', 'location': 'Amsterdam'},
 13: {'name': 'Noralie', 'age': '10', 'location': 'Amsterdam'},
 14: {'name': 'Kevin', 'age': '11', 'location': 'Amsterdam'},
 15: {'name': 'Ruben', 'age': '90', 'location': 'Amsterdam'},
 16: {'name': 'Mick', 'age': '1', 'location': 'Amsterdam'},
 17: {'name': 'Tom', 'age': '23', 'location': 'Groningen'},
 18: {'name': 'Berta', 'age': '54', 'location': 'Groningen'}
}
# Code for assignment 6.3
import json
import csv

def assignment_6_3():
    csv_input = 'static/names_age_location.csv'
    json_output = 'output/names_age_location.json'
    # YOUR CODE HERE
    raise NotImplementedError()
# Test for assignment 6.3

# Press Ctrl-Enter (PC) or Command-Enter (Mac) after changing the function 
# above and pressing Ctrl-Enter (PC) or Command-Enter (Mac)

test_cell_6_3(assignment_6_3)
# Assertion for assignment 6.3

check_assertion(assignment_6_3)
Back to top