PROGRAMMING WITH DATA S2 – 2025/26
Class exercises
S3 - Data structures & functions
[PWD 2026] S2 – Exercises 2
Recursive functions (1 exercise)
Exercise 1 - Recursive sum
Write a recursive function named recursive_sum(number: int) which calculates the sum 1 + 2 + ... + number.
Lists (4 exercises)
Exercise 1 – Move 0 to the end
Write a Python program to move all zero digits to the end of a given list of numbers
Exercise 2 - Reverse everything
Write a function named everything_reversed, which takes a list of strings as its argument. The function returns a new
list with all the items on the original list reversed. Also, the order of items should be reversed on the new list. Example:
Input = [“Hello”, “This”, “is”, 24]
Output = [42, “si”, “sihT”, “olleH”]
Exercise 3 – Most common character
Write a function named most_common_character, which takes a string argument. The function returns the character
which has the most occurrences within the string. If there are many characters with equally many occurrences, the one
which appears first in the string should be returned.
Exercise 4 – Longest series of neighbors
Given a list of integers, let's decide that two consecutive items in the list are neighbours if their difference is 1. So, items 1
and 2 would be neighbours, and so would items 56 and 55.
Write a function named longest_series_of_neighbours, which looks for the longest series of neighbours within the
list, and returns its length.
For example, in the list [1, 2, 5, 4, 3, 4] the longest list of neighbours would be [5, 4, 3, 4], with a length of 4.
Tuples (4 exercises)
Exercise 1 – Multiplying
Write a Python program to calculate the product, multiplying all the numbers in a given tuple.
Exercise 2 – Older people
Write a function named older_people(people: list, year: int), given a list of tuples with all the name and the
year of birth of different persons return the list of people older than the given year.
Example:
[PWD 2026] S2 – Exercises 3
Exercise 3 – Find person
Write a function named find_person(people: list, city: string) which takes a list of tuples in the format (name,
city, age) and a city name and returns a list of names of all people who live in that city.
Exercise 4 – Greater
Write a code that compare two tuples and find out which one is “greater” and prints it.
Dictionaries (3 exercises)
Exercise 1 – Lists to dictionaries
Write a function named combining(keys_list: list, values_list: list) that given two lists, it combines them
into a dictionary. The elements of the first one serve as keys and the elements of the second one serve as values. Each
item in the first list must be unique.
Exercise 2 – Histogram
Write a function named histogram(word: string). The function should print out a histogram representing the number
of times each letter occurs in the string. Each occurrence of a letter should be represented by a star (*) on the specific line
for that letter.
Exercise 3 – Inverting dictionaries
Write a function named invert(dictionary: dict), which takes a dictionary as its argument. The dictionary should
be inverted in place so that values become keys and keys become values.
Sets (3 exercises)
Exercise 1 – Duplicates in a list
Write a function to check if a list has duplicates using a set.
Exercise 2 – Common element
Write a function to check if two lists have at-least one element common using sets
Exercise 3 – Subset Check
Write a function named is_subset(a: list, b: list) which returns True if set a is a subset of set b, and False
otherwise. In case that is not a subset, print what’s missing to complete the subset.
Matrix (2 exercises)
Exercise 1 – Printing a matrix
Write a Python code to create a matrix. Giving 3 inputs:
1. The number of rows
2. The number of columns
3. The row values (as many times as rows defined in input number 1)
After everything is defined, print the matrix without lists. As the example:
[PWD 2026] S2 – Exercises 4
Exercise 2 – Adding two matrices
Write a function named matrix_add(a: list, b: list) which takes two matrices of the same size and returns a
new matrix which is their element-wise sum. If the matrices are not the same size, print “This matrix cannot be added”.