165 lines
4.8 KiB
Python
165 lines
4.8 KiB
Python
|
"""
|
||
|
THIS CODE HAS BEEN TESTED AND IS FULLY OPERATIONAL.
|
||
|
|
||
|
Problem Statement: 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
|
||
|
|
||
|
Code from DataStructuresAndAlgorithms (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-content/DataStructuresAndAlgorithms/
|
||
|
"""
|
||
|
|
||
|
# BEGINNING OF CODE
|
||
|
class sets:
|
||
|
"""Implementation using list"""
|
||
|
def __init__(self) -> None:
|
||
|
self.data = []
|
||
|
|
||
|
def get_iter(self) -> iter:
|
||
|
return iter(self.data)
|
||
|
|
||
|
def add(self, elmt: any) -> None:
|
||
|
if (elmt not in self.data):
|
||
|
self.data.append(elmt)
|
||
|
|
||
|
def remove(self, elmt: any) -> bool:
|
||
|
if (elmt in self.data):
|
||
|
self.data.remove(elmt)
|
||
|
return True
|
||
|
else:
|
||
|
return False
|
||
|
|
||
|
def contains(self, elmt: any) -> bool:
|
||
|
return (elmt in self.data)
|
||
|
|
||
|
def size(self) -> int:
|
||
|
l = 0
|
||
|
iter1 = self.get_iter()
|
||
|
for elmt in iter1:
|
||
|
l += 1
|
||
|
return l
|
||
|
|
||
|
def intersection(self, input_set: any) -> list:
|
||
|
ans = []
|
||
|
set1 = list(input_set)
|
||
|
for elmt in set1:
|
||
|
if (elmt in self.data):
|
||
|
ans.append(elmt)
|
||
|
return ans
|
||
|
|
||
|
def union(self, input_set: any) -> list:
|
||
|
ans = self.data.copy()
|
||
|
set1 = list(input_set)
|
||
|
for elmt in set1:
|
||
|
if (elmt not in self.data):
|
||
|
ans.append(elmt)
|
||
|
return ans
|
||
|
|
||
|
def difference(self, input_set: any) -> list:
|
||
|
ans = self.data.copy()
|
||
|
set1 = list(input_set)
|
||
|
for elmt in set1:
|
||
|
if elmt in ans:
|
||
|
ans.remove(elmt)
|
||
|
return ans
|
||
|
|
||
|
def subset(self, input_set: any) -> bool:
|
||
|
for e in input_set:
|
||
|
if e not in self.data:
|
||
|
return False
|
||
|
return True
|
||
|
|
||
|
def display(self) -> None:
|
||
|
str_set = str(self.data)
|
||
|
print(str_set.replace('[', '{').replace(']', '}'))
|
||
|
|
||
|
|
||
|
def input_set() -> set:
|
||
|
ans = set()
|
||
|
n = int(input("Total elements in set are:\t"))
|
||
|
for i in range(n):
|
||
|
ans.add(int(input(f"Enter element {i+1}:\t")))
|
||
|
return ans
|
||
|
|
||
|
|
||
|
def print_set(lst: list) -> None:
|
||
|
str_set = str(lst)
|
||
|
print(str_set.replace('[', '{').replace(']', '}'))
|
||
|
|
||
|
|
||
|
def menu():
|
||
|
print("----- MAIN MENU -----")
|
||
|
print("0 -> Display main menu")
|
||
|
print("1 -> Add new element")
|
||
|
print("2 -> Remove an element")
|
||
|
print("3 -> Search for an element")
|
||
|
print("4 -> Display size of set")
|
||
|
print("5 -> Intersection of sets")
|
||
|
print("6 -> Union of sets")
|
||
|
print("7 -> Difference of sets")
|
||
|
print("8 -> Check if subset of a set")
|
||
|
print("9 -> Display set")
|
||
|
print("10 -> Exit")
|
||
|
|
||
|
|
||
|
def main() -> None:
|
||
|
s1 = sets()
|
||
|
choice = 1
|
||
|
menu()
|
||
|
while (choice != 10):
|
||
|
choice = int(input("Choose an option (0-10):\t"))
|
||
|
match (choice):
|
||
|
case (1):
|
||
|
n = int(input("Number to add:\t"))
|
||
|
s1.add(n)
|
||
|
print("Element added sucessfully.")
|
||
|
case (2):
|
||
|
n = int(input("Number to remove:\t"))
|
||
|
if (s1.remove(n)):
|
||
|
print("Element removed sucessfully.")
|
||
|
else:
|
||
|
print("Element not present in set.")
|
||
|
case (3):
|
||
|
n = int(input("Number to search:\t"))
|
||
|
if (s1.contains(n)):
|
||
|
print("Element is present in the set.")
|
||
|
else:
|
||
|
print("Element does not exist in the set.")
|
||
|
case (4):
|
||
|
print("Size of the set is: ", s1.size())
|
||
|
case (5):
|
||
|
s = input_set()
|
||
|
print("Intersection of sets is: ", end="")
|
||
|
print_set(s1.intersection(s))
|
||
|
case (6):
|
||
|
s = input_set()
|
||
|
print("Union of sets is: ", end="")
|
||
|
print_set(s1.union(s))
|
||
|
case (7):
|
||
|
s = input_set()
|
||
|
print("Difference of sets is: ", end="")
|
||
|
print_set(s1.difference(s))
|
||
|
case (8):
|
||
|
s = input_set()
|
||
|
if (s1.subset(s)):
|
||
|
print("Set is s subset.")
|
||
|
else:
|
||
|
print("Set is NOT a subset.")
|
||
|
case (9):
|
||
|
s1.display()
|
||
|
case (10):
|
||
|
print("\n## END OF CODE\n")
|
||
|
case (0):
|
||
|
menu()
|
||
|
case default:
|
||
|
print("Please choose a valid option (0-10)")
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
main()
|
||
|
# END OF CODE
|