-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA4.py
More file actions
175 lines (144 loc) · 4.52 KB
/
A4.py
File metadata and controls
175 lines (144 loc) · 4.52 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# To create ADT that implement the "set" concept.
# a. Add (newElement) -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. Subset
set1 = []
set2 = []
def insertElement(n, set3):
if n not in set3:
set3.append(n)
return
def removeElement(n):
if n in set1:
set1.remove(n)
else:
print("Number does not exist in the set")
return
def containElement(n):
if n in set1:
return True
else:
return False
def checkSize(set3):
print("The size of the set is", len(set3))
return
def iterateSet(set3):
for ele in set3:
print(ele)
# return iter(set3)
return
def createSet(set3):
n = int(input("Enter number of elements of the set 2 : "))
for i in range(n):
ele = int(input())
insertElement(ele, set3)
return
def intersectSet(set1, set2):
intersect = []
for ele in set1:
if ele in set2:
intersect.append(ele)
return intersect
def unionSet(set1, set2):
union = []
for ele in set1:
union.append(ele)
for ele in set2:
if ele not in union:
union.append(ele)
return union
def differenceSet(set1, set2):
# set1 - set2
difference = []
for ele in set1:
if ele not in set2:
difference.append(ele)
return difference
def checkSubset(set1, set2):
if(set1 == set2):
print("Both sets are subsets of each other")
return
else:
count1 = 0
count2 = 0
for ele in set1:
if ele in set2:
count1+=1
if len(set2) == count1:
print("Set 2 is a subset of Set 1")
return
for ele in set2:
if ele in set1:
count2+=1
if len(set1) == count2:
print("Set 1 is a subset of Set 2")
return
print("No set is a subset of each other")
return
def main():
while True:
print("\nSet Operations Menu:")
print("1. Add an element to the set")
print("2. Remove an element from the set")
print("3. Check if an element exists in the set")
print("4. Get the size of the set")
print("5. Display all elements in the set")
print("6. Add elements to set2")
print("7. Find intersection with another set")
print("8. Find union with another set")
print("9. Find difference with another set")
print("10. Check if current set is a subset of another set")
print("0. Exit")
choice = input("Enter your choice: ")
if choice == "1":
addNum = int(input("Enter the number to add : "))
insertElement(addNum, set1)
elif choice == "2":
removeNum = int(input("Enter the number to remove : "))
removeElement(removeNum)
elif choice == "3":
checkNum = int(input("Enter element to be checked if the set contains it"))
contains = containElement(checkNum)
if contains == True:
print("Number exists in the set")
else:
print("Number does not exist in the set")
elif choice == "4":
checkSize(set1)
elif choice == "5":
print("Executing iterator")
iterateSet(set1)
elif choice == "6":
createSet(set2)
elif choice == "7":
intersectingElements = intersectSet(set1, set2)
print(intersectingElements)
elif choice == "8":
unionElements = unionSet(set1, set2)
print(unionElements)
elif choice == "9":
print("Difference between two sets : ")
print("1. A - B")
print("2. B - A")
choice = int(input())
if(choice == 1):
print("The difference A - B is : ",differenceSet(set1, set2))
elif(choice == 2):
print("The difference B - A is : ", differenceSet(set2, set1))
else:
print("Invalid Choice")
continue
elif choice == "10":
checkSubset(set1, set2)
elif choice == "0":
print("Exiting program.")
break
else:
print("Invalid choice. Please try again.")
main()