fixed the file extension for Practical-A4 (in README too) and updated the code

This commit is contained in:
K 2024-02-16 17:57:07 +05:30
parent 2312eb22eb
commit 27069b283c
Signed by: notkshitij
GPG Key ID: C5B8BC7530F8F43F
3 changed files with 156 additions and 117 deletions

View File

@ -1,116 +0,0 @@
'''
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 Data Structures and Algorithms (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-content/DataStructuresAndAlgorithms/
'''
# BEGINNING OF CODE
setOne=[]
setTwo=[]
def addVal(Set):
val = int(input("Value to add:\t"))
Set.append(val)
print(f"Set is:\t{Set}")
def delVal(Set):
val = int(input("Value to remove:\t"))
if(val not in Set):
print(f"{val} is not present in the set.")
else:
Set.remove(val)
print(f"Set is:\t{Set}")
def searchVal(Set):
val = int(input("Value to search:\t"))
if(val in Set):
print(f"{val} is present in the set.")
else:
print(f"{val} is not present in the set.")
def size(Set):
p = iter(Set)
for i in p:
print(i)
print("Size of set is:\t{len(Set)}")
def intersection():
intersectionSet = []
for i in setOne:
if i in setTwo:
intersectionSet.append(i)
print(f"Intersection set is {intersectionSet}")
def union():
unionSet = []
for i in setOne:
unionSet.append(i)
for j in setTwo:
if j not in setOne:
unionSet.append(j)
print("Union of set one and set two is:\t{union}")
def difference():
differenceSet = []
for i in setOne:
if i not in setTwo:
differenceSet.append(i)
print("Difference of set one and set two is:\t{differenceSet}")
def subsetCheck(a,b):
for i in b:
if i not in a:
return False
return True
def subset():
if subsetCheck(setOne,setTwo):
print("Set two is a subset of set one.")
else:
print("Set two is not a subset of set one.")
def menu():
while (True):
print("--- MAIN MENU ---")
print("1. Add value to set")
print("2. Remove value from set")
print("3. Search value in set")
print("4. Show size of set")
optn = int(input("Choose an option (1-):\t"))
if (optn == 1):
setSel = int(input("Which set to operate on?\n1. Set one\n2. Set two\nSet 1/2:\t"))
if (setSel == 1):
addVal(setOne)
elif (setSel == 2):
addVal(setTwo)
else:
print("Please choose a valid option.")
elif (optn == 2):
setSel = int(input("Which set to operate on?\n1. Set one\n2. Set two\nSet 1/2:\t"))
if (setSel == 1):
delVal(setOne)
elif (setSel == 2):
delVal(setTwo)
else:
print("Please choose a valid option.")
elif (optn == 3):
setSel = int(input("Which set to operate on?\n1. Set one\n2. Set two\nSet 1/2:\t"))
if (setSel == 1):
searchVal(setOne)
elif (setSel == 2):
searchVal(setTwo)
else:
print("Please choose a valid option.")
## PENDING
menu()
# END OF CODE

155
Codes/Practical-A4.py Normal file
View File

@ -0,0 +1,155 @@
'''
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 Data Structures and Algorithms (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-content/DataStructuresAndAlgorithms/
'''
# BEGINNING OF CODE
setOne=[]
setTwo=[]
def addVal(Set):
# Function to add value to set
val = int(input("Value to add:\t"))
if (val in Set): # Checking if value already exists in set
print(f"{val} already exists in the set.")
else: # Adding value if does not exist
Set.append(val)
print(f"Set is:\t{Set}")
def delVal(Set):
# Function to delete value from set
val = int(input("Value to remove:\t"))
if(val not in Set): # Checking if value is not there in set
print(f"{val} is not present in the set.")
else: # Deleting value if it exists in set
Set.remove(val)
print(f"Set is:\t{Set}")
def searchVal(Set):
# Function to search value in set
val = int(input("Value to search:\t"))
if(val in Set): # Check if value is present in set
print(f"{val} is present in the set.")
else: # Print if value not present in set
print(f"{val} is not present in the set.")
def size(Set):
# Function to print size (length) of set
print(f"Size of set is:\t{len(Set)}")
def intersection(setA, setB):
# Function to perform intersection of two sets
intersectionSet = []
for i in setA:
if i in setB:
intersectionSet.append(i)
print(f"Intersection is:\t{intersectionSet}")
def union(setA, setB):
# Function to perform union of two sets
unionSet = []
for i in setA:
unionSet.append(i)
for j in setB:
if j not in setA:
unionSet.append(j)
print(f"Union is:\t{unionSet}")
def difference(setA, setB):
# Function to perform difference of two sets
differenceSet = []
for i in setA:
if i not in setB:
differenceSet.append(i)
print(f"Difference is:\t{differenceSet}")
def subsetCheck(setA, setB):
# Function to check if two sets are subsets, called in subset()
for i in setB:
if i not in setA:
return False
return True
def subset(setA, setB):
# Function to print if two sets are subsets
if subsetCheck(setA,setB):
print("Set two is a subset of set one.")
else:
print("Set two is not a subset of set one.")
def main():
# Function for main menu
while (True):
print("--- MAIN MENU ---")
print("1 -> Add value to set")
print("2 -> Remove value from set")
print("3 -> Search value in set")
print("4 -> Show size of set")
print("5 -> Iterator (implementation pending)")
print("6 -> Intersection of two sets")
print("7 -> Union of two sets")
print("8 -> Difference of two sets")
print("9 -> Subset of two sets")
print("10 -> Exit")
optn = int(input("Choose an option (1-10):\t"))
if (optn == 1):
setSel = int(input("Which set to operate on?\n1. Set one\n2. Set two\nSet 1/2:\t"))
total = int(input("Total values to add:\t"))
for i in range(total):
if (setSel == 1):
addVal(setOne)
elif (setSel == 2):
addVal(setTwo)
else:
print("\nPlease choose a valid option.\n")
elif (optn == 2):
setSel = int(input("Which set to operate on?\n1. Set one\n2. Set two\nSet 1/2:\t"))
if (setSel == 1):
delVal(setOne)
elif (setSel == 2):
delVal(setTwo)
else:
print("\nPlease choose a valid option.\n")
elif (optn == 3):
setSel = int(input("Which set to operate on?\n1. Set one\n2. Set two\nSet 1/2:\t"))
if (setSel == 1):
searchVal(setOne)
elif (setSel == 2):
searchVal(setTwo)
else:
print("\nPlease choose a valid option.\n")
elif (optn == 4):
setSel = int(input("Which set to operate on?\n1. Set one\n2. Set two\nSet 1/2:\t"))
if (setSel == 1):
size(setOne)
elif (setSel == 2):
size(setTwo)
else:
print("\nPlease choose a valid option.\n")
elif (optn == 5):
print("\nIMPLEMENTATION PENDING\n")
elif (optn == 6):
intersection(setOne, setTwo)
elif (optn == 7):
union(setOne, setTwo)
elif (optn == 8):
difference(setOne, setTwo)
elif (optn == 9):
subset(setOne, setTwo)
elif (optn == 10):
print("\n\n## END OF CODE\n\n")
exit(1)
else:
print("Please choose a valid option (1-10).")
main() # Calling the main function
# END OF CODE

View File

@ -12,7 +12,7 @@ Delve into the realm of Data Structures and Algorithms (DSA) with our Git reposi
### Codes
1. [Practical A1 - Telephone book database](https://git.kska.io/sppu-se-comp-content/DataStructuresAndAlgorithms/src/branch/main/Codes/Practical-A1.cpp)
2. [Practical A4 - Set operations](https://git.kska.io/sppu-se-comp-content/DataStructuresAndAlgorithms/src/branch/main/Codes/Practical-A4.cpp)
2. [Practical A4 - Set operations](https://git.kska.io/sppu-se-comp-content/DataStructuresAndAlgorithms/src/branch/main/Codes/Practical-A4.py)
3. [Practical B5 - Tree data structure](https://git.kska.io/sppu-se-comp-content/DataStructuresAndAlgorithms/src/branch/main/Codes/Practical-B5.cpp)
4. [Practical B7 - Binary tree functions](https://git.kska.io/sppu-se-comp-content/DataStructuresAndAlgorithms/src/branch/main/Codes/Practical-B7.cpp)
5. [Practical B11 - Dictionary using BST](https://git.kska.io/sppu-se-comp-content/DataStructuresAndAlgorithms/src/branch/main/Codes/Practical-B11.cpp)