Sets

Introduction to Sets

def average(array):
    distinct = set(array)
    return sum(distinct) / len(distinct)

if __name__ == '__main__':
    n = int(input())
    arr = list(map(int, input().split()))
    result = average(arr)
    print(result)

Set .add()

distinct = set()

for _ in range(int(input())):
    distinct.add(input())
    
print(len(distinct))

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

n = int(input())
s = set(map(int, input().split()))

for _ in range(int(input())):
    command = input().split()
    
    if command[0] == "discard":
        s.discard(int(command[1]))
    elif command[0] == "remove":
        s.remove(int(command[1]))
    elif command[0] == "pop":
        s.pop()
        
print(sum(s))

Set .union() Operation

n = int(input())
english_newspaper = set(map(int, input().split()))
m = int(input())
french_newspaper = set(map(int, input().split()))

print(len(english_newspaper.union(french_newspaper)))

Set .intersection() Operation

n = int(input())
english_newspaper = set(map(int, input().split()))
m = int(input())
french_newspaper = set(map(int, input().split()))

print(len(english_newspaper.intersection(french_newspaper)))

Set .difference() Operation

n = int(input())
english_newspaper = set(map(int, input().split()))
m = int(input())
french_newspaper = set(map(int, input().split()))

print(len(english_newspaper.difference(french_newspaper)))

Set .symmetric_difference() Operation

n = int(input())
english_newspaper = set(map(int, input().split()))
m = int(input())
french_newspaper = set(map(int, input().split()))

print(len(english_newspaper.symmetric_difference(french_newspaper)))

Symmetric Difference

m = int(input())
M = set(map(int, input().split()))
n = int(input())
N = set(map(int, input().split()))

for each in sorted(M.symmetric_difference(N)):
    print(each)

Set Mutations

n = int(input())
set_a = set(map(int, input().strip().split()))

for _ in range(int(input())):
    command = input().strip().split()
    set_b = set(map(int, input().strip().split()))
    if command[0] == "intersection_update":
        set_a.intersection_update(set_b)
    if command[0] == "symmetric_difference_update":
        set_a.symmetric_difference_update(set_b)
    if command[0] == "difference_update":
        set_a.difference_update(set_b)
    if command[0] == "update":
        set_a.update(set_b)
        
print(sum(set_a))

The Captain's Room

K = int(input())
room_numbers = list(map(int, input().split()))
distinct_room_numbers = set(room_numbers)

for each in distinct_room_numbers:
    room_numbers.remove(each)

print(distinct_room_numbers.difference(set(room_numbers)).pop())

Check Subset

for _ in range(int(input())):
    a = int(input())
    A = set(map(int, input().split()))
    b = int(input())
    B = set(map(int, input().split()))
    
    if A.issubset(B):
        print(True)
    else:
        print(False)


Check Strict Superset

A = set(map(int, input().split()))

for _ in range(int(input())):
    if A.issuperset(set(map(int, input().split()))):
        pass
    else:
        print(False)
        exit()
    
print(True)

No Idea!

happiness = 0

n, m = map(int, input().split())
arr = list(map(int, input().split()))
A = set(map(int, input().split()))
B = set(map(int, input().split()))

for each in arr:
    if each in A:
        happiness += 1
    if each in B:
        happiness -= 1
        
print(happiness)

Last updated