Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
11 views5 pages

Dsa 2

The document defines a SetADT class that implements a basic set data structure with methods for adding, deleting, finding elements, displaying sets, calculating size, and performing intersection and union operations. The main program loop allows users to interact with the set through a menu-driven interface. The sets can hold up to 10 unique integer elements, and invalid inputs are handled with error messages.

Uploaded by

abhinavsargar1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views5 pages

Dsa 2

The document defines a SetADT class that implements a basic set data structure with methods for adding, deleting, finding elements, displaying sets, calculating size, and performing intersection and union operations. The main program loop allows users to interact with the set through a menu-driven interface. The sets can hold up to 10 unique integer elements, and invalid inputs are handled with error messages.

Uploaded by

abhinavsargar1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

class SetADT:

def __init__(self):

self.set1, self.set2, self.set3 = [-1] * 10, [-1] * 10, [-1] * 10

def add(self, ch):

set_list = self.set1 if ch == 1 else self.set2

while sum(1 for x in set_list if x != -1) < 10:

for element in input("\n\tEnter elements (space separated): ").split():

try:

element = int(element)

if element not in set_list and -1 in set_list:

set_list[set_list.index(-1)] = element

except ValueError:

print(f"\n\tInvalid element '{element}'!")

if input("\n\tAdd more? (y/n): ").lower() != 'y': break

self.display()

def display(self):

for idx, set_list in enumerate([self.set1, self.set2], 1):

print(f"\n\tSet {idx} = {{", ", ".join(map(str, filter(lambda x: x != -1, set_list))), "}}")

def find(self, ch, s):

set_list = self.set1 if s == 1 else self.set2

return set_list.index(ch) if ch in set_list else -1

def delete(self, ch, s):

index = self.find(ch, s)

if index != -1: (self.set1 if s == 1 else self.set2)[index] = -1

return index

def size(self):

print(f"\n\tSet 1 size: {sum(1 for i in self.set1 if i != -1)}")

print(f"\n\tSet 2 size: {sum(1 for i in self.set2 if i != -1)}")

def intersection(self):
self.set3 = [x for x in self.set1 if x != -1 and x in self.set2]

print("\n\tIntersection = {", ", ".join(map(str, self.set3)), "}")

def union(self):

union_result = list(set(x for x in self.set1 + self.set2 if x != -1))

print("\n\tUnion = {", ", ".join(map(str, union_result)), "}")

# Main program loop

obj = SetADT()

while True:

print("\n\t****** Set ADT ******\n\t1. Add\n\t2. Remove\n\t3. Find\n\t4. Size\n\t5. Intersection\n\t6. Union\
n\t7. Exit")

ch = int(input("\n\tEnter choice: "))

if ch == 1:

obj.add(int(input("\n\tAdd to Set 1 (1) or Set 2 (2): ")))

elif ch == 2:

ch, s = int(input("\n\tElement to delete: ")), int(input("\n\tDelete from Set 1 (1) or Set 2 (2): "))

index = obj.delete(ch, s)

print(f"\n\tElement {ch} deleted from index {index + 1}" if index != -1 else "\n\tElement not found")

obj.display()

elif ch == 3:

ch, s = int(input("\n\tElement to search: ")), int(input("\n\tSearch in Set 1 (1) or Set 2 (2): "))

index = obj.find(ch, s)

print(f"\n\tElement {ch} found at index {index + 1}" if index != -1 else "\n\tElement not found")

elif ch == 4:

obj.display(); obj.size()

elif ch == 5:

obj.intersection()

elif ch == 6:

obj.union()

elif ch == 7:

break
OUTPUT:

You might also like