forked from ShetkariisLive/Data-structure-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
set.py
149 lines (122 loc) · 3.21 KB
/
set.py
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
""" To create ADT that implement the "set" concept.
a. Add (new Element) -Place a value into the set , b. Remove (element) Remove the
value
c. Contains (element) Return true if element is in collection, d. Size () Return number of
values in collection Iterator () Return an iterator used to loop over collection, e.
Intersection of two sets , f. Union of two sets, g. Difference between two sets, h. Subse
"""
class setExample:
set1=set()
set2=set()
def create(self):
set1=self.set1
num=int(input("Enter the number do you want insert in set 1 : "))
for i in range(0,num):
val=input("Enter the value : ")
set1.add(val)
def insert(self):
set1=self.set1
val=input("Enter the value to insert in Set 1 : ")
set1.add(val)
def remove(self):
set1=self.set1
val=input('Enter the value to remove from Set 1 : ')
if val not in set1:
print("Value not found in Set 1 !!!")
else:
set1.discard(val)
print("Value removed successfully ")
def search(self):
set1=self.set1
val=input('Enter the value to search in Set 1 : ')
key=None
for i in set1:
if i==val:
key=True
if key==True:
print("Value is found in Set")
else:
print("Value is not found is Set !!!")
def set_union(self):
set1=self.set1
set2=self.set2
num=int(input("Enter the number do you want insert in set 2 : "))
for i in range(0,num):
val=input("Enter the value : ")
set2.add(val)
set3=set1.union(set2)
print("Union of set1 and set2 is : ",set3)
def set_intersection(self):
set1=self.set1
set2=self.set2
num=int(input("Enter the number do you want insert in set 2 : "))
for i in range(0,num):
val=input("Enter the value : ")
set2.add(val)
set3=set1.intersection(set2)
print("Intersection of set1 and set2 is : ",set3)
def set_difference(self):
set1=self.set1
set2=self.set2
num=int(input("Enter the number do you want insert in set 2 : "))
for i in range(0,num):
val=input("Enter the value : ")
set2.add(val)
set3=set1.difference(set2)
print("Difference of set1 and set2 is : ",set3)
def is_subset(self):
set1=self.set1
val=[val for val in input("Enter the subset : ").split()]
sets=set(val)
res =sets.issubset(set1)
if res==True:
print("This is subset of set1")
else:
print("This is not subset of set1")
def display(self):
set1=self.set1
print("set1: ",set1)
def size_of_set(self):
print("Size of set 1 is : ",self.set1.__len__())
s=setExample()
print("------------- Welcome ------------")
def choice(ch):
if ch==1:
s.create()
elif ch==2:
s.insert()
elif ch==3:
s.remove()
elif ch==4:
s.search()
elif ch==5:
s.display()
elif ch==6:
s.set_union()
elif ch==7:
s.set_intersection()
elif ch==8:
s.set_difference()
elif ch==9:
s.is_subset()
elif ch==10:
s.size_of_set()
elif ch==11:
exit("Thank You !!!")
else:
print("Please enter right choice !!!")
while True:
ch=int(input("""********* Set Operations ********
1. Create
2. Insert value in Set 1
3. Remove element from Set 1
4. Search element in Set 1
5. Display Set 1
6. Union
7. Intersection
8. Difference
9. is subset
10. print size of set 1
11. Exit
Please enter your choice : """))
choice(ch)