Docsity

Ejercicio python, Ejercicios de Ingeniería Biomédica

Toggle sidebar
bg1
Unit 6. Dictionaries
Goal
•Learn to create and use Python dictionaries.
Exercise 1: histogram
Write a function that gets a list of integers and generates a histogram, preferably with
their values sorted ascendingly. A histogram is a graph that shows how many times each
value appears on the list.
Example:
[1,2,1,3,4,2,4,2,3,4,1,2,4,4,5,2,3,2,4,2]
The output should be:
1: ***
2: *******
3: ***
4: ******
5: *
Exercise 2: marks
Make a dictionary of marks, where the keys are the names of the students and the values
are lists with the marks of each student, as for example:
marks = {}
marks['Samir'] = [2,3,1,2]
marks['Anna'] = [6,4,7,3]
marks['Jordi'] = [5,4,6,2,5,4,6,7,3]
Write the following functions. All of them should receive the dictionary of marks; some
functions also need other parameters as detailed below:
a) Given a name of a student, return the average mark of that student.
b) Given a name of a student, return the number of failed marks (<5) of that student.
c) Return the average mark of all students (average of the averages obtained by the
function in a).
d) Given a number n, return a list with the names of the students with n or more failed
marks.
e) Return the name of the student with the highest average mark.
f) Return True if there is at least one student with all his/her marks failed.
Exercise 3: collectors
We have to make a program to manage our club of collectors. For now we have a
dictionary like the following one (make it on your program; you can add more data):
members = {}
members[1] = ['Joan', 'Coins', 654898]
members[2] = ['Carla', 'Stones', 1234443]
Unit 6 Computer Science Laboratory EEBE - UPC 1
Samir Kanaan Izquierdo 2016
pf2

Vista previa parcial del texto

¡Descarga Ejercicio python y más Ejercicios en PDF de Ingeniería Biomédica solo en Docsity!

Unit 6. Dictionaries

Goal

  • Learn to create and use Python dictionaries.

Exercise 1: histogram

Write a function that gets a list of integers and generates a histogram, preferably with their values sorted ascendingly. A histogram is a graph that shows how many times each value appears on the list. Example: [1,2,1,3,4,2,4,2,3,4,1,2,4,4,5,2,3,2,4,2] The output should be: 1: *** 2: ******* 3: *** 4: ****** 5: *

Exercise 2: marks

Make a dictionary of marks, where the keys are the names of the students and the values are lists with the marks of each student, as for example: marks = {} marks['Samir'] = [2,3,1,2] marks['Anna'] = [6,4,7,3] marks['Jordi'] = [5,4,6,2,5,4,6,7,3] Write the following functions. All of them should receive the dictionary of marks; some functions also need other parameters as detailed below: a) Given a name of a student, return the average mark of that student. b) Given a name of a student, return the number of failed marks (<5) of that student. c) Return the average mark of all students (average of the averages obtained by the function in a). d) Given a number n, return a list with the names of the students with n or more failed marks. e) Return the name of the student with the highest average mark. f) Return True if there is at least one student with all his/her marks failed.

Exercise 3: collectors

We have to make a program to manage our club of collectors. For now we have a dictionary like the following one (make it on your program; you can add more data): members = {} members[1] = ['Joan', 'Coins', 654898] members[2] = ['Carla', 'Stones', 1234443] Unit 6 Computer Science Laboratory EEBE - UPC 1 Samir Kanaan Izquierdo 2016

So we have a dictionary where the key is the member number, and the value is a list with the name of the member, his or her hobby and the telephone number. Write the following functions. All of them receive the dictionary of members, plus additional parameters if specified: a) Gets a hobby and returns a list with all the telephone numbers of the members with that hobby. b) Returns a new dictionary where each hobby is associated with the members that have that hobby, for example (imagine the data existed): {'Stamps': ['Joan','Jordi','Jose'], 'Stones':['Carla', 'Mònica']} c) Returns an equivalent dictionary but using the member name as key and a list with member number, hobby and telephone as value: {'Joan':[1,'Coins', 654898], 'Carla':[2,'Stones', 1234443]}

Exercise 4: list to dictionary

A hospital has performed a clinical study but has given us the resulting data without structure, everything on a single list with the following format: [name1, age1, illness1, name2, age2, illness2, ...] Write a function that converts the previous list to a dictionary like this: {name1: [age1, illness1], name2: [age2, illness2], ...}

Exercise 5: flight reservations

The flight company Bryanair has requested us to make a software to manage their flight reservations. There are three dictionaries, one for passengers, another for flights and a third one to list the passengers of each flight. They have the following format: passengers = {pass_id1:[name1,phone1,age1], pass_id2:[name2,phone2,age2],…} flights = {flight_id1:[origin1, destination1, seats1], flight_id2:[origin2, dest2, seats2], …} reservations = {flight_id1:[pass_id1_1, pass_id1_2, …], flight_id2:[pass_id2_1, pass_id2_2, …], …} First create all three dictionaries on your Python code. Then write the following functions: a) Receive the passengers, the reservations, and a flight id and return how many minors (age<18) are there in the flight. b) Flight cancelled! Receive the passengers, the reservations and a flight id and return a list with the telephone numbers of all the passengers of the flight. c) Receive the flights and a destination and return all the ids of the flights to that destination. d) Receive the flights, the reservations and a destination and return how many passengers will fly to that destination. Use the previous function. e) Receive the flights and the reservations and return the favourite destination (the one with most reservations). Use the previous functions (c and d). Hint: make a new dictionary to annotate the destinations and the associated reservations. Unit 6 Computer Science Laboratory EEBE - UPC 2 Samir Kanaan Izquierdo 2016