0% found this document useful (0 votes)
73 views

Assignment 1 DSA

The document describes a Python program that implements set operations like union, intersection, difference and subsets on two sets A and B. The program allows the user to choose an operation, add/remove elements and check properties of the sets like size and membership.

Uploaded by

Vaishnavi Kaware
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views

Assignment 1 DSA

The document describes a Python program that implements set operations like union, intersection, difference and subsets on two sets A and B. The program allows the user to choose an operation, add/remove elements and check properties of the sets like size and membership.

Uploaded by

Vaishnavi Kaware
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

'''Assignment no.

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:

7. Difference between two sets:

8. Subsets:

9. Display data:

10. End of program:'''

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:

3. Search elements from Contains 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:

10. End of program:

Enter your choice= 1


Enter elements= 11
**_SET OPERATIONS_**
1.Add new element:
2. Remove element:

3. Search elements from Contains 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:

10. End of program:

Enter your choice= 1


Enter elements= 12
**_SET OPERATIONS_**
1.Add new element:
2. Remove element:

3. Search elements from Contains 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:

10. End of program:

5
Enter your choice= 1
Enter elements= 13
**_SET OPERATIONS_**
1.Add new element:
2. Remove element:

3. Search elements from Contains 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:

10. End of program:

Enter your choice= 1


Enter elements= 14
**_SET OPERATIONS_**
1.Add new element:
2. Remove element:

3. Search elements from Contains 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:

10. End of program:

Enter your choice= 9


Set A is {11, 12, 13, 14}
**_SET OPERATIONS_**
1.Add new element:

6
2. Remove element:

3. Search elements from Contains 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:

10. End of program:

Enter your choice= 2


Enter elementn for remove= 14
Element removed successfully..!!
**_SET OPERATIONS_**
1.Add new element:
2. Remove element:

3. Search elements from Contains 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:

10. End of program:

Enter your choice= 9


Set A is {11, 12, 13}
**_SET OPERATIONS_**
1.Add new element:
2. Remove element:

3. Search elements from Contains element:

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:

10. End of program:

Enter your choice= 3


Eneter element to search= 11
It contains in A..!!
**_SET OPERATIONS_**
1.Add new element:
2. Remove element:

3. Search elements from Contains 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:

10. End of program:

Enter your choice= 4


Size of A is: 3
**_SET OPERATIONS_**
1.Add new element:
2. Remove element:

3. Search elements from Contains element:

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:

10. End of program:

Enter your choice= 9


Set A is {11, 12, 13}
**_SET OPERATIONS_**
1.Add new element:
2. Remove element:

3. Search elements from Contains 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:

10. End of program:

Enter your choice= 5


create set B
Enter total no. of values in set B= 2
Enter value= 11
Enter value= 15
Set A is {11, 12, 13}
Set B is {11, 15}
Intersection of A & B is {11}
**_SET OPERATIONS_**
1.Add new element:
2. Remove element:

3. Search elements from Contains element:

4.size of set:
5. Intersection of two sets:

9
6. Difference between two sets:

7.Union of two sets:


8. Subsets:

9. Display data:

10. End of program:

Enter your choice= 6


Set A is {11, 12, 13}
Set B is {11, 15}
Difference of A & B is {12, 13}
**_SET OPERATIONS_**
1.Add new element:
2. Remove element:

3. Search elements from Contains 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:

10. End of program:

Enter your choice= 7


Set A is {11, 12, 13}
Set B is {11, 15}
Union of A & B is= {11, 12, 13, 15}
**_SET OPERATIONS_**
1.Add new element:
2. Remove element:

3. Search elements from Contains element:

4.size of set:
5. Intersection of two sets:

10
6. Difference between two sets:

7.Union of two sets:


8. Subsets:

9. Display data:

10. End of program:

Enter your choice= 8


Set A is {11, 12, 13}
Set B is {11, 15}
A is not subset
**_SET OPERATIONS_**
1.Add new element:
2. Remove element:

3. Search elements from Contains 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:
10.End of program:
Enter your choice= 10
EXIT..!!

11

You might also like