0% found this document useful (0 votes)
8 views

Code Assessment 2

Uploaded by

sociallyletsmeet
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Code Assessment 2

Uploaded by

sociallyletsmeet
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

// 1.

check if children_locked->values have the same id as passed in the upgrade


function
unordered_map<Node*,int>::iterator itr;
vector<Node*> children_to_be_unlocked;
for(itr=root->children_locked.begin(); itr!=root->children_locked.end(); itr++)
{
if(itr->second!=id) /// trying to upgrade on someone else's lock - not
allowed
{
return false;
}
children_to_be_unlocked.push_back(itr->first);

// 2. unlock all children


int total_children_unlocked = root->children_locked.size();
for(int k=0; k<children_to_be_unlocked.size(); k++)
{
if(!unlock(id,children_to_be_unlocked[k]))
{
return false;
}
}

// 3. Lock root now


if(!lock(id,root))
{
return false;
}

return true;

int main()
{
// int n,m,q;
// cin>>n>>m>>q;
// vector<string> arr;
// vector<string> queries;
// for(int i=0; i<n; i++)
// {
// string s;
// cin>>s;
// arr.push_back(s);
// }
// for(int i=0; i<q; i++)
// {
// string qu;
// cin>>qu;
// queries.push_back(qu);
// }

////////////////////// HARD CODED INPUT - TO BE DELETED


int n = 7;
int m = 2;
int q = 5;
vector<string> arr;
arr.push_back("World");
arr.push_back("Asia");
arr.push_back("Africa");
arr.push_back("China");
arr.push_back("India");
arr.push_back("Souafrica");
arr.push_back("Egypt");
vector<string> queries;
queries.push_back("1 China 9");
queries.push_back("1 India 9");
queries.push_back("3 Asia 9");
queries.push_back("2 India 9");
queries.push_back("1 Asia 9");

/////////////////////////////////////////////////////////////////////// TREE
AND MAP CREATION

unordered_map<string,Node*> m1;

Node* root = new Node(arr[0], NULL);


m1[arr[0]] = root;

int i = 0;
int j = 1;

while(j<n)
{
for(int k = 0; k<m; k++)
{
if(j<n)
{
Node* child = new Node(arr[j], m1[arr[i]]);
m1[arr[j]] = child;
m1[arr[i]]->children.push_back(child);
j++;
}
else
{
break;
}
}
i++;
}

//////////////////////////////////////////////////////// TREE CREATION


COMPLETED - QUERY RESULTS BEGIN

for(int que=0; que<q; que++)


{
string s = queries[que];
int l = s.length();
char operation = s[0];
int id = s[s.length()-1]-'0';
string valx = s.substr(2,l-4);

if(operation=='1')
{
/// lock
cout<<lock(id,m1[valx])<<"\n";
}
else if(operation=='2')
{
// unlock
cout<<unlock(id,m1[valx])<<"\n";
}
else if(operation=='3')
{
//upgrade
cout<<upgrade(id,m1[valx])<<"\n";
}
else

You might also like