Lab 10
Lab 10
2023/24 Lab10
Laboratory 10
Topics
1. Creating and using sets and dictionaries
2. Union, intersection and difference between sets
3. Accessing and scanning the dictionary elements
Discussion
A. Processing data using the most common set operations
B. Using a dictionary to search a table
C. Use of complex structures for data storage
Exercises
Part 1 – Set and dictionaries elaboration
Task: For each of the following exercises, write a program in Python that meets the given requirements.
Complete at least two exercises during the laboratory session, and the remaining ones at home.
10.1.1 Most common words. A) Write a program that counts the occurrences of each word present in a text
file, whose name is entered from the keyboard. Assume the file contains only alphabetic or whitespace
characters. B) Next, improve the program so that it displays the 10 most common words (in case of a tie at
position 10, it does not matter which words are printed).
[PEER REVIEW] 10.1.2 Payment between roommates – (exam simulation). Two roommates have the
problem of splitting the check when one of them goes shopping. In the rules they have given themselves,
whoever goes shopping pays for both and then asks the roommate for reimbursement. The reimbursement
is computed considering the list of products that they needed and that were available. The balance is made
considering the receipt and the list of products requested by the roommate.
Write a program that helps the roommates calculate how much the roommate who went shopping should
get from the other. The program will receive two files as input: a check and a product list.
The check is a text file that contains, in each line, two fields separated by a comma:
The unit price is reported to two decimal digits. Moreover, the same product can appear several times on
the receipt if several pieces have been purchased (example: if you buy 3 packs of milk, the receipt will have
3 lines for the milk).
The product list requested by the roommate is a text file that contains in each line two fields separated by
a comma:
the name of the product, the desired quantity
1
07JCJLI Computer Sciences, A.Y. 2023/24 Lab10
Example of check.txt file (in input) Example of product_list.txt file (in input)
Milk,1.59 Beer,2
Milk,1.59 Bread,1
Egg,1.99 Oil,3
Milk,1.59 Rum,2
Egg,1.99 Milk,2
Bread,2.20 Tomatos,1
Tomatos,5.60 Mozzarella,4
Soap,2.99 Water,1
Soap,2.99 AAA Battery,2
Beer,1.29
Beer,1.29
Beer,1.29
Beer,1.29
Beer,1.29
Bananas,3.50
Mozzarella,1.80
Rum,12.59
Oil,6.99
Oil,6.99
Rice,2.50
Mozzarella,1.80
Flour,0.80
Flour,0.80
Water,2.40
Water,2.40
Water,2.40
Water,2.40
Chewing gum,1.20
Razors,4.99
Example of execution (in output)
Total spent: 82.54 euro
Money from roommate: 46.13 euro
Item not purchased:
1 Oil
1 Rum
2 Mozzarella
2 AAA Battery
2
07JCJLI Computer Sciences, A.Y. 2023/24 Lab10
10.1.3 Compare strings. Write a program that asks the user to provide two strings, and then display (avoiding
repetitions of characters in the printout):
10.1.4 Sparse array. A sparse array is a sequence of numbers, most of which are zero. An efficient way to
store a sparse array is a dictionary, in which the keys are the locations where non-zero values occur, and the
values are the correspoding values in the sequence. For example, the sequence 0 0 0 0 0 4 0 0 0 2 9
would be represented by the dictionary {5:4, 9:2, 10:9}. Write a program that calculates the array
sum: its value at position i is the sum of the values of a and b at position i. The sum array is again a sparse
array. The calling program's dictionaries must not be modified.
10.1.5 Set comparison. Given the set definitions below, answer the following questions:
set1 = { 1, 2, 3, 4, 5, 10 }
set2 = { 2, 4, 6, 8, 10 }
set3 = { 1, 5, 9, 13, 17, 10 }
10.1.6 Set Operation. Given three sets, set1, set2, and set3, write Python statement(s) to perform the
following actions:
a. Create a new set of all elements that are in set1 or set2, but not both.
b. Create a new set of all elements that are in only one of the three sets set1, set2, and set3.
c. Create a new set of all elements that are in exactly two of the sets set1, set2, and set3.
Using:
set1 = { 1, 2, 3, 4, 5, 10 }
set2 = { 2, 4, 6, 8, 10 }
set3 = { 1, 5, 9, 13, 17, 10 }
3
07JCJLI Computer Sciences, A.Y. 2023/24 Lab10
10.2.1 Eratosthenes’sieve. Build the sieve of Eratosthenes: a function that calculates prime numbers, also
known in Ancient Greece. Choose an integer, n: this function will calculate all prime numbers up to n. First,
create a set and fill it with all integers from 2 to n, then delete all multiples of 2 except 2 from the set (i.e. 4,
6, 8, 10, 12, …), then delete all multiples of 3, except 3 (ie 6, 9, 12, 15, …) and continue like this,
canceling each time the multiples of the minimum value present in the set, up to the number √n. The
numbers left in the set are the required ones.
10.2.2 Hide the names Write a program that reads a file (names.txt) containing a list of people's names,
one per line, and puts them into a set. Then read another text file: the program must rewrite the second file
read, generating a third in which all the letters of each name have been replaced by a number of asterisks
equal to its length.
10.2.3 Annual Capital incomes. Write a program that reads the data from the rawdata_2004.txt text file
and puts it into a dictionary whose keys are country names and whose values are annual per capita incomes.
Note that in the file the fields are separated by a tab character '\t'. Then, the program has to ask the user
to provide country names, to display the corresponding values. The program terminates when the user types
quit. The rawdata_2004.txt file can be downloaded from
https://drive.google.com/file/d/1SVN7K4B7Bq_zN5lBUJMlvBieF_syxXzO/view?usp=shar
ing
10.2.4 Text common words. Write a program that reads in two text files and prints out, in sorted order, all
words that are common to both of them. Finally computing the similarity figure of the two texts as: (number
of common words)/(numbers of words text one + numbers of words text two)*100