-
Notifications
You must be signed in to change notification settings - Fork 0
/
a4.py
159 lines (137 loc) · 4.56 KB
/
a4.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
150
151
152
153
154
155
156
157
158
159
def createSet():
n=int(input("Enter number of Elements in set :- "))
s = []
for i in range(n) :
e = int(input("Enter Element->"))
s.append(e)
return s
#----------------------------------------------------------------------------------------------
def add( s, e ):
if e not in s :
s.append( e )
#-------------------------------------------
def remove( s, e ):
if e in s:
s.remove(e)
#-------------------------------------------
def intersect( s1, s2 ):
newSet = []
for i in range(len(s1)) :
for j in range(len(s2)) :
if s1[i] == s2[j] :
add(newSet, s1[i])
return newSet
#-------------------------------------------
def union( s1, s2 ):
newSet = s1
for e in s2 :
if e not in s1 :
add(newSet,e)
return newSet
#-------------------------------------------
def isSubsetOf( s1, s2 ):
for e in s2 :
if e not in s1 :
return False
return True
#-------------------------------------------
def isProperSubset( s1, s2 ):
if isSubsetOf(s1,s2) and not isSubsetOf(s2,s1):
return True
return False
#-------------------------------------------
def difference( s1, s2 ):
newSet = []
for e in s1 :
if e not in s2:
add(newSet,e)
return newSet
#--------------------------------------------------------------------------------------------
def display(s):
string = "\n{ "
for i in range(len(s)):
string = string + str(s[i])
if i != len(s)-1:
string = string + " , "
string = string + " }\n"
return string
#---------------------------------------------------------------------------------------------
choice = 0
print("Create Set A")
s1 = createSet()
print(display(s1))
while choice != 10:
print("Menu")
print("1.Add")
print("2.Remove")
print("3.Contains")
print("4.Size")
print("5.Intersection")
print("6.Union")
print("7.Difference")
print("8.Subset")
print("9.Proper Subset")
print("10.Exit")
choice = int(input("Enter Choice :- "))
#-------------------------------------------
if choice==1:
e = int(input("Enter Number to Add :- "))
add(s1,e)
print(display(s1))
elif choice==2:
e = int(input("Enter Number to Remove :- "))
remove(s1,e)
print(display(s1))
elif choice==3:
e = int(input("Enter Number to Search : "))
if e in s1:
print("Number Present in Set.")
else:
print("Number is not Present in Set.")
print(display(s1))
elif choice==4:
print("Set Contains {} elements".format(len(s1),"\n"))
elif choice==5:
print("Create a Set B for doing Intersection Operation.")
s2 = createSet()
s3 = intersect(s1,s2)
print("Set A = ",display(s1))
print("Set B = ",display(s2))
print("Intersection = ",display(s3),"\n")
elif choice==6:
print("Create a Set B for doing Union Operation.")
s2 = createSet()
s3 = union(s1,s2)
print("Set A = "+display(s1))
print("Set B = "+display(s2))
print("Union = "+display(s3),"\n")
elif choice==7:
print("Create a Set B for calculating Set Difference.")
s2 = createSet()
s3 = difference(s1,s2)
print("Set A = "+display(s1))
print("Set B = "+display(s2))
print("Difference = "+display(s3),"\n")
elif choice==8:
print("Create a Set B for checking Subset or not.")
s2 = createSet()
isSubset = isSubsetOf(s1,s2)
print("Set A = "+display(s1))
print("Set B = "+display(s2))
if isSubset:
print("Set B is the Subset of Set A.")
else:
print("Set B is not a Subset of Set A.")
elif choice==9:
print("Create a Set B for checking ProperSubset or not.")
s2 = createSet()
isProperSub = isProperSubset(s1,s2)
print("Set A = "+display(s1))
print("Set B = "+display(s2))
if isProperSub:
print("Set B is the Proper Subset of Set A.")
else:
print("Set B is not a Proper Subset of Set A.")
elif choice==10:
break
#----------------------------------------------------------------------------------------------