Skip to content

Commit 900a0c9

Browse files
authored
Create ruby2.py
added union and intersection in python
1 parent 8ec5a7a commit 900a0c9

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

ruby2.py

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Python program to find union and intersection
2+
# using sets
3+
4+
5+
def printUnion(arr1, arr2, n1, n2):
6+
hs = set()
7+
8+
# Insert the elements of arr1[] to set hs
9+
for i in range(0, n1):
10+
hs.add(arr1[i])
11+
12+
# Insert the elements of arr1[] to set hs
13+
for i in range(0, n2):
14+
hs.add(arr2[i])
15+
print("Union:")
16+
for i in hs:
17+
print(i, end=" ")
18+
print("\n")
19+
20+
# Prints intersection of arr1[0..n1-1] and
21+
# arr2[0..n2-1]
22+
23+
24+
def printIntersection(arr1, arr2, n1, n2):
25+
hs = set()
26+
27+
# Insert the elements of arr1[] to set S
28+
for i in range(0, n1):
29+
hs.add(arr1[i])
30+
print("Intersection:")
31+
for i in range(0, n2):
32+
33+
# If element is present in set then
34+
# push it to vector V
35+
if arr2[i] in hs:
36+
print(arr2[i], end=" ")
37+
38+
39+
# Driver Program
40+
arr1 = [7, 1, 5, 2, 3, 6]
41+
arr2 = [3, 8, 6, 20, 7]
42+
n1 = len(arr1)
43+
n2 = len(arr2)
44+
45+
# Function call
46+
printUnion(arr1, arr2, n1, n2)
47+
printIntersection(arr1, arr2, n1, n2)
48+
49+
# This artice is contributed by Kumar Suman .

0 commit comments

Comments
 (0)