From 1bec1716dffe77cf2a35699f8d149630acd6a1d1 Mon Sep 17 00:00:00 2001 From: Kshitij Date: Thu, 15 Feb 2024 09:47:56 +0530 Subject: [PATCH] updated A4 to python --- Codes/Practical-A4.cpp | 605 +++++++---------------------------------- 1 file changed, 104 insertions(+), 501 deletions(-) diff --git a/Codes/Practical-A4.cpp b/Codes/Practical-A4.cpp index 3a568c2..9f416b5 100644 --- a/Codes/Practical-A4.cpp +++ b/Codes/Practical-A4.cpp @@ -1,9 +1,4 @@ -// WARNING: THIS CODE HAS NOT BEEN TESTED. -// IT WILL TAKE 1-2 MONTHS TO PERFECT THE CODE. -// IF YOU FACE ANY ERRORS, UNEXPECTED BEHAVIOUR OR HAVE ANY SUGGESTIONS, -// LET US KNOW BY CREATING AN ISSUE ON OUR KSKA GIT REPOSITORY. - -/* +''' 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 @@ -15,499 +10,107 @@ 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 -#include -using namespace std; -templateclass Set; -templateclass Iterator; -template -class Node -{ - T data; - Node *next; - public: - Node() - { - next=NULL; - } - Node(T x) - { - data=x; - next=NULL; - } - friend class Set; - friend class Iterator; -}; -template -class Iterator -{ - Node *ptr; - friend class Set; - public: - Iterator() - { ptr=NULL; } - void set(Node *&t) - { ptr=t; } - void operator++(int) - { ptr=ptr->next; } - void next() - { ptr=ptr->next; } - bool operator!=(Iterator a) - { if(a.ptr!=NULL&&ptr!=NULL) - { - if((a.ptr)->data==(ptr)->data) - return false; - return true; - } - if(a.ptr==NULL&&ptr==NULL) - return false; - return true; - } - void del() - { delete ptr; } -}; -template -class Set -{ - Node *head; - public: - Set() - { - head=NULL; - } - Iterator begin() - { - Iterator a; - a.ptr=head; - return a; - } - Iterator end() - { - Iterator a; - a.ptr=NULL; - return a; - } - int size(); - void display(); - void insert(T); - bool remove(T); - bool contain(T); - bool subSet(Set&); - Set unionOf(Set&); - Set intersectionOf(Set&); - Set differenceOf(Set&); -}; -template -int Set::size() -{ - Iterator it; - it.set(head); - int cnt=0; - while(it!=end()) - { - cnt++; - it++; - } - return cnt; -} -template -void Set::insert(T x) -{ - Node *curr=head,*prev=NULL; - while(curr!=NULL&&curr->datanext; - } - if(curr!=NULL&&curr->data==x) - { - cout<(x); - head->next=curr; - } - else - { - prev->next=new Node(x); - prev->next->next=curr; - } -} -template -void Set::display() -{ - Iterator it; - it.set(head); - while(it!=end()) - { - cout<data<<" "; - it.next(); - } - cout<<"\n"; -} -template -bool Set::contain(T x) -{ - Iterator it; - it.set(head); - while(it!=end()) - { - if(it.ptr->data==x) - return true; - it++; - } - return false; -} -template -bool Set::remove(T x) -{ - if(contain(x)) - { - Iterator curr,prev; - curr=begin(); - while(curr.ptr->data!=x) - { - prev=curr; - curr.next(); - } - if(prev.ptr==NULL) - head=head->next; - else - prev.ptr->next=curr.ptr->next; - curr.del(); - return true; - } - else - return false; -} -template -bool Set::subSet(Set &S2) -{ - if(head==NULL) - return true; - if(S2.head==NULL) - return false; - Iterator p,q; - p=begin(); - q=S2.begin(); - int cnt=0; - while(q!=S2.end()&&p!=end()) - { - if(p.ptr->data==q.ptr->data) - { - cnt++; - p++; - } - q.next(); - } - if(cnt==size()) - return true; - return false; -} -template -Set Set::unionOf(Set &S2) -{ - Set U; - Iterator p=begin(),q=S2.begin(); - Node *r=NULL; - if(p!=end()&&q!=S2.end()) - { - if(p.ptr->datadata) - { - U.head=new Node(p.ptr->data); - p++; - } - else - { - U.head=new Node(q.ptr->data); - if(p.ptr->data==q.ptr->data) - p++; - q++; - } - } - r=U.head; - while(p!=end()&&q!=S2.end()) - { - if(p.ptr->datadata) - { - r->next=new Node(p.ptr->data); - p++; - } - else - { - r->next=new Node(q.ptr->data); - if(p.ptr->data==q.ptr->data) - p++; - q++; - } - r=r->next; - } - if(r==NULL) - { - if(p.ptr==NULL) - { - U.head=new Node(q.ptr->data); - q++; - } - else - { - U.head=new Node(p.ptr->data); - p++; - } - r=U.head; - } - while(p!=end()) - { - r->next=new Node(p.ptr->data); - r=r->next; - p++; - } - while(q!=S2.end()) - { - r->next=new Node(q.ptr->data); - r=r->next; - q++; - } - return U; -} -template -Set Set::intersectionOf(Set &S2) -{ - Set I; - Iterator p=begin(),q=S2.begin(); - Node *r=NULL; - while(p!=end()&&q!=S2.end()) - { - if(p.ptr->data==q.ptr->data) - { - if(I.head==NULL) - { - I.head=new Node(p.ptr->data); - r=I.head; - } - else - r->next=new Node(p.ptr->data); - q++; - p++; - } - else if(p.ptr->data>q.ptr->data) - q++; - else - p++; - } - return I; -} -template -Set Set::differenceOf(Set &S2) -{ - Node *r; - Iterator p,q; - if(head==NULL) - { - Set D; - return D; - } - Set D; - p=begin(); - q=S2.begin(); - while(p!=end()&&q!=S2.end()) - { - if(p.ptr->data==q.ptr->data) - { - p.next(); - q.next(); - } - else - { - if(D.head==NULL) - { - D.head=new Node(p.ptr->data); - r=D.head; - } - else - { - r->next=new Node(p.ptr->data); - r=r->next; - } - p++; - } - } - if(D.head==NULL&&p!=end()) - { - D.head=new Node(p.ptr->data); - p.next(); - r=D.head; - } - while(p!=end()) - { - r->next=new Node(p.ptr->data); - p.next(); - r=r->next; - } - return D; -} -template -void operations(T var) -{ - Set S1,S2,Res; - T ele; - int work,choice; - do - { - cout<<"Enter\n1 To Work with Set 1\n2 To Work with Set 2\n0 To EXIT\n"; - cin>>work; - switch(work) - { - case 1: - do - { - cout<<"Enter\n1 To Insert\n2 To Remove\n3 To Search\n4 To Print Size\n5 To Check if Subset\n"; - cout<<"6 To Find Union\n7 To Find Intersection\n8 To Find Difference\n0 To Previous Menu\n"; - cin>>choice; - switch(choice) - { - case 1: - cout<<"Enter the Element to Insert\n"; - cin>>ele; - S1.insert(ele); - S1.display(); - break; - case 2: - cout<<"Enter the Element to Remove\n"; - cin>>ele; - if(S1.remove(ele)) - cout<<"Element Removed\n"; - else - cout<<"Element Not Present\n"; - S1.display(); - break; - case 3: - cout<<"Enter the Element to search\n"; - cin>>ele; - if(S1.contain(ele)) - cout<<"Element Present\n"; - else - cout<<"Element Not Present\n"; - break; - case 4: - cout<<"Set Size is "<>choice; - switch(choice) - { - case 1: - cout<<"Enter the Element to Insert\n"; - cin>>ele; - S2.insert(ele); - S2.display(); - break; - case 2: - cout<<"Enter the Element to Remove\n"; - cin>>ele; - if(S2.remove(ele)) - cout<<"Element Removed\n"; - else - cout<<"Element Not Present\n"; - S2.display(); - break; - case 3: - cout<<"Enter the Element to search\n"; - cin>>ele; - if(S2.contain(ele)) - cout<<"Element Present\n"; - else - cout<<"Element Not Present\n"; - break; - case 4: - cout<<"Set Size is "<>ch; - if(ch==1) - operations((int)1); - else if(ch==2) - operations((float)1.0f); - else if(ch==3) - operations('a'); - else - cout<<"Invalid Choice\n"; - return 0; -} -// END OF CODE -// EXPERIMENTAL CODE \ No newline at end of file +# 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