Sets

Sets


Symmetric Difference

Task
Given sets of integers, and , print their symmetric difference in ascending order. The term symmetric difference indicates those values that exist in either or but do not exist in both.

Input Format
The first line of input contains an integer, .
The second line contains space-separated integers.
The third line contains an integer, .
The fourth line contains space-separated integers.

Output Format
Output the symmetric difference integers in ascending order, one per line.

Sample Input
4
2 4 5 9
4
2 4 11 12

Sample Output
5
9
11
12

My solution

m, n = [set(input().split()) for i in range(4)][1::2]
result = m.difference(n)
result.update(n.difference(m))
print(*sorted(map(int, result)), sep = '\n')

Set .add()

Task
Apply your knowledge of the .add() operation to help your friend Rupal.Rupal has a huge collection of country stamps. She decided to count the total number of distinct country stamps in her collection. She asked for your help. You pick the stamps one by one from a stack of country stamps.
Find the total number of distinct country stamps.

Input Format
The first line contains an integer , the total number of country stamps.
The next lines contains the name of the country where the stamp is from.

Output Format
Output the total number of distinct country stamps on a single line.

Sample Input
7
UK
China
USA
France
New Zealand
UK
France

Sample Output
5

My solution

n, stamps = int(input()), set()
[stamps.add(input()) for i in range(n)]
print(len(stamps))

Set .discard(), .remove() & .pop()

Task
You have a non-empty set , and you have to execute commands given in lines.
The commands will be pop, remove and discard.

Input Format

The first line contains integer , the number of elements in the set .
The second line contains space separated elements of set . All of the elements are non-negative integers, less than or equal to 9.
The third line contains integer , the number of commands.
The next lines contains either pop, remove and/or discard commands followed by their associated value.

Output Format
Print the sum of the elements of set on a single line.

Sample Input
9
1 2 3 4 5 6 7 8 9
10
pop
remove 9
discard 9
discard 8
remove 7
pop
discard 6
remove 5
pop
discard 5
Sample Output

4

My solution

code

Adapted solution

code


You'll only receive email when understanding publishes a new post

More from understanding