updated A4 to python
This commit is contained in:
parent
4a3c14afef
commit
1bec1716df
@ -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.
|
Problem Statement: To create ADT that implement the "set" concept.
|
||||||
a. Add (newElement) -Place a value into the set
|
a. Add (newElement) -Place a value into the set
|
||||||
b. Remove (element) Remove the value
|
b. Remove (element) Remove the value
|
||||||
@ -15,499 +10,107 @@ g. Difference between two sets
|
|||||||
h. Subset
|
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/
|
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
|
# BEGINNING OF CODE
|
||||||
#include<iostream>
|
setOne=[]
|
||||||
using namespace std;
|
setTwo=[]
|
||||||
template<class T>class Set;
|
|
||||||
template<class T>class Iterator;
|
def addVal(Set):
|
||||||
template<class T>
|
val = int(input("Value to add:\t"))
|
||||||
class Node
|
Set.append(val)
|
||||||
{
|
print(f"Set is:\t{Set}")
|
||||||
T data;
|
|
||||||
Node<T> *next;
|
def delVal(Set):
|
||||||
public:
|
val = int(input("Value to remove:\t"))
|
||||||
Node()
|
if(val not in Set):
|
||||||
{
|
print(f"{val} is not present in the set.")
|
||||||
next=NULL;
|
else:
|
||||||
}
|
Set.remove(val)
|
||||||
Node(T x)
|
print(f"Set is:\t{Set}")
|
||||||
{
|
|
||||||
data=x;
|
def searchVal(Set):
|
||||||
next=NULL;
|
val = int(input("Value to search:\t"))
|
||||||
}
|
if(val in Set):
|
||||||
friend class Set<T>;
|
print(f"{val} is present in the set.")
|
||||||
friend class Iterator<T>;
|
else:
|
||||||
};
|
print(f"{val} is not present in the set.")
|
||||||
template<class T>
|
|
||||||
class Iterator
|
def size(Set):
|
||||||
{
|
p = iter(Set)
|
||||||
Node<T> *ptr;
|
for i in p:
|
||||||
friend class Set<T>;
|
print(i)
|
||||||
public:
|
print("Size of set is:\t{len(Set)}")
|
||||||
Iterator()
|
|
||||||
{ ptr=NULL; }
|
def intersection():
|
||||||
void set(Node<T> *&t)
|
intersectionSet = []
|
||||||
{ ptr=t; }
|
for i in setOne:
|
||||||
void operator++(int)
|
if i in setTwo:
|
||||||
{ ptr=ptr->next; }
|
intersectionSet.append(i)
|
||||||
void next()
|
print(f"Intersection set is {intersectionSet}")
|
||||||
{ ptr=ptr->next; }
|
|
||||||
bool operator!=(Iterator<T> a)
|
def union():
|
||||||
{ if(a.ptr!=NULL&&ptr!=NULL)
|
unionSet = []
|
||||||
{
|
for i in setOne:
|
||||||
if((a.ptr)->data==(ptr)->data)
|
unionSet.append(i)
|
||||||
return false;
|
for j in setTwo:
|
||||||
return true;
|
if j not in setOne:
|
||||||
}
|
unionSet.append(j)
|
||||||
if(a.ptr==NULL&&ptr==NULL)
|
print("Union of set one and set two is:\t{union}")
|
||||||
return false;
|
|
||||||
return true;
|
def difference():
|
||||||
}
|
differenceSet = []
|
||||||
void del()
|
for i in setOne:
|
||||||
{ delete ptr; }
|
if i not in setTwo:
|
||||||
};
|
differenceSet.append(i)
|
||||||
template<class T>
|
print("Difference of set one and set two is:\t{differenceSet}")
|
||||||
class Set
|
|
||||||
{
|
def subsetCheck(a,b):
|
||||||
Node<T> *head;
|
for i in b:
|
||||||
public:
|
if i not in a:
|
||||||
Set()
|
return False
|
||||||
{
|
return True
|
||||||
head=NULL;
|
|
||||||
}
|
def subset():
|
||||||
Iterator<T> begin()
|
if subsetCheck(setOne,setTwo):
|
||||||
{
|
print("Set two is a subset of set one.")
|
||||||
Iterator<T> a;
|
else:
|
||||||
a.ptr=head;
|
print("Set two is not a subset of set one.")
|
||||||
return a;
|
|
||||||
}
|
def menu():
|
||||||
Iterator<T> end()
|
while (True):
|
||||||
{
|
print("--- MAIN MENU ---")
|
||||||
Iterator<T> a;
|
print("1. Add value to set")
|
||||||
a.ptr=NULL;
|
print("2. Remove value from set")
|
||||||
return a;
|
print("3. Search value in set")
|
||||||
}
|
print("4. Show size of set")
|
||||||
int size();
|
optn = int(input("Choose an option (1-):\t"))
|
||||||
void display();
|
if (optn == 1):
|
||||||
void insert(T);
|
setSel = int(input("Which set to operate on?\n1. Set one\n2. Set two\nSet 1/2:\t"))
|
||||||
bool remove(T);
|
if (setSel == 1):
|
||||||
bool contain(T);
|
addVal(setOne)
|
||||||
bool subSet(Set&);
|
elif (setSel == 2):
|
||||||
Set unionOf(Set&);
|
addVal(setTwo)
|
||||||
Set intersectionOf(Set&);
|
else:
|
||||||
Set differenceOf(Set&);
|
print("Please choose a valid option.")
|
||||||
};
|
elif (optn == 2):
|
||||||
template<class T>
|
setSel = int(input("Which set to operate on?\n1. Set one\n2. Set two\nSet 1/2:\t"))
|
||||||
int Set<T>::size()
|
if (setSel == 1):
|
||||||
{
|
delVal(setOne)
|
||||||
Iterator<T> it;
|
elif (setSel == 2):
|
||||||
it.set(head);
|
delVal(setTwo)
|
||||||
int cnt=0;
|
else:
|
||||||
while(it!=end())
|
print("Please choose a valid option.")
|
||||||
{
|
elif (optn == 3):
|
||||||
cnt++;
|
setSel = int(input("Which set to operate on?\n1. Set one\n2. Set two\nSet 1/2:\t"))
|
||||||
it++;
|
if (setSel == 1):
|
||||||
}
|
searchVal(setOne)
|
||||||
return cnt;
|
elif (setSel == 2):
|
||||||
}
|
searchVal(setTwo)
|
||||||
template<class T>
|
else:
|
||||||
void Set<T>::insert(T x)
|
print("Please choose a valid option.")
|
||||||
{
|
## PENDING
|
||||||
Node<T> *curr=head,*prev=NULL;
|
|
||||||
while(curr!=NULL&&curr->data<x)
|
|
||||||
{
|
menu()
|
||||||
prev=curr;
|
# END OF CODE
|
||||||
curr=curr->next;
|
|
||||||
}
|
|
||||||
if(curr!=NULL&&curr->data==x)
|
|
||||||
{
|
|
||||||
cout<<x<<" is already present, duplicate entry not allowed\n";
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if(prev==NULL)
|
|
||||||
{
|
|
||||||
head=new Node<T>(x);
|
|
||||||
head->next=curr;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
prev->next=new Node<T>(x);
|
|
||||||
prev->next->next=curr;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
template<class T>
|
|
||||||
void Set<T>::display()
|
|
||||||
{
|
|
||||||
Iterator<T> it;
|
|
||||||
it.set(head);
|
|
||||||
while(it!=end())
|
|
||||||
{
|
|
||||||
cout<<it.ptr->data<<" ";
|
|
||||||
it.next();
|
|
||||||
}
|
|
||||||
cout<<"\n";
|
|
||||||
}
|
|
||||||
template<class T>
|
|
||||||
bool Set<T>::contain(T x)
|
|
||||||
{
|
|
||||||
Iterator<T> it;
|
|
||||||
it.set(head);
|
|
||||||
while(it!=end())
|
|
||||||
{
|
|
||||||
if(it.ptr->data==x)
|
|
||||||
return true;
|
|
||||||
it++;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
template<class T>
|
|
||||||
bool Set<T>::remove(T x)
|
|
||||||
{
|
|
||||||
if(contain(x))
|
|
||||||
{
|
|
||||||
Iterator<T> 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<class T>
|
|
||||||
bool Set<T>::subSet(Set &S2)
|
|
||||||
{
|
|
||||||
if(head==NULL)
|
|
||||||
return true;
|
|
||||||
if(S2.head==NULL)
|
|
||||||
return false;
|
|
||||||
Iterator<T> 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<class T>
|
|
||||||
Set<T> Set<T>::unionOf(Set &S2)
|
|
||||||
{
|
|
||||||
Set<T> U;
|
|
||||||
Iterator<T> p=begin(),q=S2.begin();
|
|
||||||
Node<T> *r=NULL;
|
|
||||||
if(p!=end()&&q!=S2.end())
|
|
||||||
{
|
|
||||||
if(p.ptr->data<q.ptr->data)
|
|
||||||
{
|
|
||||||
U.head=new Node<T>(p.ptr->data);
|
|
||||||
p++;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
U.head=new Node<T>(q.ptr->data);
|
|
||||||
if(p.ptr->data==q.ptr->data)
|
|
||||||
p++;
|
|
||||||
q++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
r=U.head;
|
|
||||||
while(p!=end()&&q!=S2.end())
|
|
||||||
{
|
|
||||||
if(p.ptr->data<q.ptr->data)
|
|
||||||
{
|
|
||||||
r->next=new Node<T>(p.ptr->data);
|
|
||||||
p++;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
r->next=new Node<T>(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<T>(q.ptr->data);
|
|
||||||
q++;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
U.head=new Node<T>(p.ptr->data);
|
|
||||||
p++;
|
|
||||||
}
|
|
||||||
r=U.head;
|
|
||||||
}
|
|
||||||
while(p!=end())
|
|
||||||
{
|
|
||||||
r->next=new Node<T>(p.ptr->data);
|
|
||||||
r=r->next;
|
|
||||||
p++;
|
|
||||||
}
|
|
||||||
while(q!=S2.end())
|
|
||||||
{
|
|
||||||
r->next=new Node<T>(q.ptr->data);
|
|
||||||
r=r->next;
|
|
||||||
q++;
|
|
||||||
}
|
|
||||||
return U;
|
|
||||||
}
|
|
||||||
template<class T>
|
|
||||||
Set<T> Set<T>::intersectionOf(Set &S2)
|
|
||||||
{
|
|
||||||
Set<T> I;
|
|
||||||
Iterator<T> p=begin(),q=S2.begin();
|
|
||||||
Node<T> *r=NULL;
|
|
||||||
while(p!=end()&&q!=S2.end())
|
|
||||||
{
|
|
||||||
if(p.ptr->data==q.ptr->data)
|
|
||||||
{
|
|
||||||
if(I.head==NULL)
|
|
||||||
{
|
|
||||||
I.head=new Node<T>(p.ptr->data);
|
|
||||||
r=I.head;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
r->next=new Node<T>(p.ptr->data);
|
|
||||||
q++;
|
|
||||||
p++;
|
|
||||||
}
|
|
||||||
else if(p.ptr->data>q.ptr->data)
|
|
||||||
q++;
|
|
||||||
else
|
|
||||||
p++;
|
|
||||||
}
|
|
||||||
return I;
|
|
||||||
}
|
|
||||||
template<class T>
|
|
||||||
Set<T> Set<T>::differenceOf(Set &S2)
|
|
||||||
{
|
|
||||||
Node<T> *r;
|
|
||||||
Iterator<T> p,q;
|
|
||||||
if(head==NULL)
|
|
||||||
{
|
|
||||||
Set<T> D;
|
|
||||||
return D;
|
|
||||||
}
|
|
||||||
Set<T> 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<T>(p.ptr->data);
|
|
||||||
r=D.head;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
r->next=new Node<T>(p.ptr->data);
|
|
||||||
r=r->next;
|
|
||||||
}
|
|
||||||
p++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(D.head==NULL&&p!=end())
|
|
||||||
{
|
|
||||||
D.head=new Node<T>(p.ptr->data);
|
|
||||||
p.next();
|
|
||||||
r=D.head;
|
|
||||||
}
|
|
||||||
while(p!=end())
|
|
||||||
{
|
|
||||||
r->next=new Node<T>(p.ptr->data);
|
|
||||||
p.next();
|
|
||||||
r=r->next;
|
|
||||||
}
|
|
||||||
return D;
|
|
||||||
}
|
|
||||||
template<class T>
|
|
||||||
void operations(T var)
|
|
||||||
{
|
|
||||||
Set <T>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 "<<S1.size()<<"\n";
|
|
||||||
break;
|
|
||||||
case 5:
|
|
||||||
if(S1.subSet(S2))
|
|
||||||
cout<<"Set 1 is SubSet of Set 2\n";
|
|
||||||
else
|
|
||||||
cout<<"Not a SubSet\n";
|
|
||||||
break;
|
|
||||||
case 6:
|
|
||||||
Res=S1.unionOf(S2);
|
|
||||||
cout<<"Union of 2 Sets is\n";
|
|
||||||
Res.display();
|
|
||||||
break;
|
|
||||||
case 7:
|
|
||||||
cout<<"Intersection of 2 Sets is\n";
|
|
||||||
Res=S1.intersectionOf(S2);
|
|
||||||
Res.display();
|
|
||||||
break;
|
|
||||||
case 8:
|
|
||||||
cout<<"Difference of 2 Sets is\n";
|
|
||||||
Res=S1.differenceOf(S2);
|
|
||||||
Res.display();
|
|
||||||
break;
|
|
||||||
case 0:
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
cout<<"Invalid Choice\n";
|
|
||||||
}
|
|
||||||
}while(choice);
|
|
||||||
break;
|
|
||||||
case 2:
|
|
||||||
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;
|
|
||||||
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 "<<S2.size()<<"\n";
|
|
||||||
break;
|
|
||||||
case 5:
|
|
||||||
if(S2.subSet(S1))
|
|
||||||
cout<<"Set 2 is SubSet of Set 1\n";
|
|
||||||
else
|
|
||||||
cout<<"Not a SubSet\n";
|
|
||||||
break;
|
|
||||||
case 6:
|
|
||||||
Res=S2.unionOf(S1);
|
|
||||||
cout<<"Union of 2 Sets is\n";
|
|
||||||
Res.display();
|
|
||||||
break;
|
|
||||||
case 7:
|
|
||||||
cout<<"Intersection of 2 Sets is\n";
|
|
||||||
Res=S2.intersectionOf(S1);
|
|
||||||
Res.display();
|
|
||||||
break;
|
|
||||||
case 8:
|
|
||||||
cout<<"Difference of 2 Sets is\n";
|
|
||||||
Res=S2.differenceOf(S1);
|
|
||||||
Res.display();
|
|
||||||
break;
|
|
||||||
case 0:
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
cout<<"Invalid Choice\n";
|
|
||||||
}
|
|
||||||
}while(choice);
|
|
||||||
break;
|
|
||||||
case 0:
|
|
||||||
cout<<"You have opted to EXIT\n";
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
cout<<"Invalid Choice\n";
|
|
||||||
}
|
|
||||||
}while(work);
|
|
||||||
}
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
int ch;
|
|
||||||
cout<<"Enter Choice of Datatype\n1 For Integer\n2 For Float\n3 For Character\n";
|
|
||||||
cin>>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
|
|
||||||
|
Loading…
Reference in New Issue
Block a user