-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathruby2.py
More file actions
49 lines (36 loc) · 941 Bytes
/
ruby2.py
File metadata and controls
49 lines (36 loc) · 941 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# Python program to find union and intersection
# using sets
def printUnion(arr1, arr2, n1, n2):
hs = set()
# Insert the elements of arr1[] to set hs
for i in range(0, n1):
hs.add(arr1[i])
# Insert the elements of arr1[] to set hs
for i in range(0, n2):
hs.add(arr2[i])
print("Union:")
for i in hs:
print(i, end=" ")
print("\n")
# Prints intersection of arr1[0..n1-1] and
# arr2[0..n2-1]
def printIntersection(arr1, arr2, n1, n2):
hs = set()
# Insert the elements of arr1[] to set S
for i in range(0, n1):
hs.add(arr1[i])
print("Intersection:")
for i in range(0, n2):
# If element is present in set then
# push it to vector V
if arr2[i] in hs:
print(arr2[i], end=" ")
# Driver Program
arr1 = [7, 1, 5, 2, 3, 6]
arr2 = [3, 8, 6, 20, 7]
n1 = len(arr1)
n2 = len(arr2)
# Function call
printUnion(arr1, arr2, n1, n2)
printIntersection(arr1, arr2, n1, n2)
# This artice is contributed by Kumar Suman .