Assignment 1 DSA
Assignment 1 DSA
1
TO create ADT that implement the set concept:
1.Add new(element)-place a value into the set
2.Remove(element)-remove the value:
3.Contains(element)-return the value:
4.size()-returns number of values in collection:
5.Intersection of two sets:
6. Union of two sets:
8. Subsets:
9. Display data:
def main():
A=set()
B=set()
while True:
print("**_SET OPERATIONS_**")
print("1.Add new element:")
print("2.Remove element:")
print("3.Search elements from Contains element:")
print("4.size of set:")
print("5.Intersection of two sets:")
print("6.Difference between two sets:")
print("7.Union of two sets:")
print("8.Subsets:")
print("9.Display data:")
print("10.End of program:")
choice=int(input("Enter your choice= "))
if(choice==10):
1
print("EXIT..!!")
break
if(choice==1):
x=int(input("Enter elements= "))
A.add(x)
if(choice==9):
print("Set A is",A)
if(choice==2):
X=int(input("Enter elementn for remove= "))
A=remove_element(x,A)
print("Element removed successfully..!!")
if(choice==3):
x=int(input("Eneter element to search= "))
if(contain(x,A)):
print("It contains in A..!!")
else:
print("Not contains in A..!!")
if(choice==4):
print("Size of A is:",size(A))
if(choice==5):
print("create set B")
n=int(input("Enter total no. of values in set B= "))
for i in range(n):
B.add(int(input("Enter value= ")))
print("Set A is",A)
print("Set B is",B)
intersection(A,B)
if(choice==6):
print("Set A is",A)
print("Set B is",B)
2
difference(A,B)
if(choice==7):
print("Set A is",A)
print("Set B is",B)
union(A,B)
if(choice==8):
print("Set A is",A)
print("Set B is",B)
subset(A,B)
def contain(x,A):
if x in A:
return True
else:
return False
def remove_element(x,A):
z=set()
for i in A:
if(x!=i):
z.add(i)
return(z)
def size(A):
counter=0
for i in A:
counter=counter+1
return(counter)
def intersection(A,B):
R=set()
for i in A:
if i in B:
R.add(i)
3
print("Intersection of A & B is ",R)
def difference(A,B):
R=set()
for i in A:
if i not in B:
R.add(i)
print("Difference of A & B is ",R)
def union(A,B):
R=set()
for i in A:
if i in B:
print("Union of A & B is= ",A.union(B))
def subset(A,B):
flag=True
for i in A:
if i not in B:
flag=False
print("A is not subset")
return
if flag==True :
print('it is subset')
main()
Output:-
**_SET OPERATIONS_**
1.Add new element:
2. Remove element:
4.size of set:
5.Intersection of two sets:
6.Difference between two sets:
4
7. Union of two sets:
8. Subsets:
9. Display data:
4.size of set:
5.Intersection of two sets:
6.Difference between two sets:
7.Union of two sets:
8. Subsets:
9. Display data:
4.size of set:
5.Intersection of two sets:
6.Difference between two sets:
7.Union of two sets:
8.Subsets:
9. Display data:
5
Enter your choice= 1
Enter elements= 13
**_SET OPERATIONS_**
1.Add new element:
2. Remove element:
4.size of set:
5.Intersection of two sets:
6.Difference between two sets:
7.Union of two sets:
8. Subsets:
9. Display data:
4.size of set:
5.Intersection of two sets:
6.Difference between two sets:
7.Union of two sets:
8.Subsets:
9. Display data:
6
2. Remove element:
4.size of set:
5.Intersection of two sets:
6.Difference between two sets:
7.Union of two sets:
8. Subsets:
9. Display data:
4.size of set:
5.Intersection of two sets:
6.Difference between two sets:
7.Union of two sets:
8. Subsets:
9. Display data:
4.size of set:
7
5.Intersection of two sets:
6.Difference between two sets:
7.Union of two sets:
8. Subsets:
9. Display data:
4.size of set:
5.Intersection of two sets:
6.Difference between two sets:
7.Union of two sets:
8. Subsets:
9. Display data:
4.size of set:
5.Intersection of two sets:
6.Difference between two sets:
7.Union of two sets:
8
8. Subsets:
9. Display data:
4.size of set:
5.Intersection of two sets:
6.Difference between two sets:
7.Union of two sets:
8. Subsets:
9. Display data:
4.size of set:
5. Intersection of two sets:
9
6. Difference between two sets:
9. Display data:
4.size of set:
5.Intersection of two sets:
6.Difference between two sets:
7.Union of two sets:
8. Subsets:
9. Display data:
4.size of set:
5. Intersection of two sets:
10
6. Difference between two sets:
9. Display data:
4.size of set:
5.Intersection of two sets:
6.Difference between two sets:
7.Union of two sets:
8.Subsets:
9.Display data:
10.End of program:
Enter your choice= 10
EXIT..!!
11