Arvutiteaduse instituut
  1. Kursused
  2. 2018/19 sügis
  3. Programmeerimine (Narva Kolledž) (SVNC.00.054)
EN
Logi sisse

Programmeerimine (Narva Kolledž) 2018/19 sügis

  • Home
  • Grading
  • Links

Before session 12

Before the next class session, watch the video about tuples and read the text material about sets.

1. Tuples

Slides in English and in Russian

Textbook in English

Textbook in Estonian (ennikud), in Estonian (hulgad)

2. Sets

Python also has a data type for sets. A set is an unordered collection of unique elements. A set does not have duplicate elements. Every element is unique (no duplicates) and immutable (cannot be changed). The elements of a set can be of any data type: integers, floats, strings, even tuples, and so on.

Reminder: a set cannot have mutable elements (like lists or dictionaries have). However, a set itself is mutable – we can add or remove elements from it.

Creating sets

A set is created by placing all the elements inside the curly brackets {}, separated by commas, or by using the built-in function set(). Note: to create an empty set you have to use set(), not {}; the latter creates an empty dictionary.

s1 = {8, 2, 3, 6, 7}
s2 = set([6, 4, 5])
s3 = set('Good morning')
s4 = set()

Go through the following examples as well.

>>> basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
>>> print(basket)    # show that duplicates have been removed
{'orange', 'pear', 'banana', 'apple'}
>>> a = set('abracadabra')
>>> a    # unique letters in a
{'a', 'r', 'b', 'c', 'd'}

Set operations

Sets have a large number of built-in methods. In the table below, only some methods are tabulated. More information about sets and their methods can be found in Python documentation on sets.

OperationDescription
s.add(el)adds element el to set s
s.remove(el)removes element el from set s if the element is in the set. If el is not in the set, a KeyError is raised.
s.discard(el)removes element el from set s, if the element is in the set. If el is not in the set, nothing is done.
s.update(s1)updates set s by adding the elements from another set s1
s.pop()removes an arbitrary element from set s, the method returns the removed element
s.clear()removes all elements from set s
s.copy()returns a new set with a shallow copy of set s

Sets also support mathematical operations like union, intersection, difference, and symmetric difference.

OperationDescription
A & B
A.intersection(B)
a new set with elements common to A and B
A | B
A.union(B)
a new set with all elements from both A and B
A - B
A.difference(B)
difference of A and B, a set of elements that are only in A, but not in B
A ^ B
A.symmetric_difference(B)
symmetric difference of A and B, a set with elements in either A or B, but not in both
A <= B
A.issubset(B)
tests whether every element in set A is in set B
A >= B
A.issuperset(B)
tests whether every element in set B is in set A

Try yourself how all these operations work!

Iterating through a set

Using a for loop, we can iterate though a set.

for letter in set("apple"):
    print(letter, end=" ")

In the output, the duplicates are removed and the order is not preserved:

 p a e l

Test

Go to Moodle and take the test on tuples and sets.

3. Examples

Example 1. Meetings

The following program is a modified example from last week – The meetings program. The file meetings.txt contains records of meetings. Each number in the file indicates the day of the week when an appointment takes place. The following program reads the data from the file and creates a dictionary (the days of the week are the keys, and the numbers from the file are the values). Then the program creates a list of tuples and sorts it in descending order. Finally, the program outputs three days which have the most meetings.

def day_of_week(n):
    days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
    return days[n-1]

ffile = open("meetings.txt")

meetings = dict()

for line in ffile:
    try:
        number = int(line)
        day = day_of_week(number)
        meetings[day] = meetings.get(day,0) + 1
    except:
        print("Invalid value")

ffile.close()

lst = list()
for key, val in meetings.items():
    lst.append( (val, key) )

lst.sort(reverse=True)

for val, key in lst[:3]:
    print(key, val)

Example 2. Names

This program prompts the user for first and last names of three people and outputs two names which have the most number of common letters. Pay attention to function names which returns several values (actually one tuple).

def names(number):
    first = input("Please enter the first name of person number "+str(number)+": ")
    last = input("Please enter the last name of person number "+str(number)+": ")
    return first.lower(), last.lower()

def common(firstX, lastX, firstY, lastY):
    firsts = set(firstX) & set(firstY)
    lasts = set(lastX) & set(lastY)
    return len(firsts) + len(lasts)

first1, last1 = names(1)
first2, last2 = names(2)
first3, last3 = names(3)

common12 = common(first1, last1, first2, last2)
common23 = common(first2, last2, first3, last3)
common13 = common(first1, last1, first3, last3)

if common12 >= common13 and common12 >= common23:
    print("Names of persons 1 and 2 are most similar to each other.")
if common13 >= common12 and common13 >= common23:
    print("Names of persons 1 and 3 are most similar to each other.")
if common23 >= common12 and common23 >= common13:
    print("Names of persons 2 and 3 are most similar to each other.")

4. Solve and submit

Exercise 1. Birthdays

Update the Birthday program in such a way that it constructs a dictionary where keys are years and values are numbers of people who have been born on that year, and then outputs top 3 years that have the most birthdays.

Exercise 2. Distances between points

Write a function coordinates that prompts the user for x and y coordinates of the point and returns its coordinates as a tuple.

Write a function distance that takes four coordinates (two coordinates for each point) as its arguments and returns the distance between two points. Hint:

Write a program that first asks the user for the number of points, and then for each point asks for its x and y coordinates. Using the function coordinates it finds and outputs two points that the closest to each other (use the function distance). If there are several pairs of points with the same distance, any of such pairs can be taken.

Here is an example of program output:

 Please enter the number of points: 4
 Please enter x coordinate for 1. point: 8
 Please enter y coordinate for 1. point: 9
 Please enter x coordinate for 2. point: 3
 Please enter y coordinate for 2. point: 2
 Please enter x coordinate for 3. point: 16
 Please enter y coordinate for 3. point: 20
 Please enter x coordinate for 4. point: 0
 Please enter y coordinate for 4. point: 0
 Points 2 and 4 are the closest to each other.

Go to Moodle and upload your solutions under Session 12 homework.

  • Arvutiteaduse instituut
  • Loodus- ja täppisteaduste valdkond
  • Tartu Ülikool
Tehniliste probleemide või küsimuste korral kirjuta:

Kursuse sisu ja korralduslike küsimustega pöörduge kursuse korraldajate poole.
Õppematerjalide varalised autoriõigused kuuluvad Tartu Ülikoolile. Õppematerjalide kasutamine on lubatud autoriõiguse seaduses ettenähtud teose vaba kasutamise eesmärkidel ja tingimustel. Õppematerjalide kasutamisel on kasutaja kohustatud viitama õppematerjalide autorile.
Õppematerjalide kasutamine muudel eesmärkidel on lubatud ainult Tartu Ülikooli eelneval kirjalikul nõusolekul.
Courses’i keskkonna kasutustingimused