Skip to content

Commit 78a1145

Browse files
authored
Merge pull request #4 from jk-007/master
Add exam solutions.
2 parents 332bb1f + d64a219 commit 78a1145

File tree

5 files changed

+286
-0
lines changed

5 files changed

+286
-0
lines changed

Data Structures Exam/81374-1.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <iostream>
2+
#include <assert.h>
3+
4+
using namespace std;
5+
6+
struct Node
7+
{
8+
int data;
9+
Node* left;
10+
Node* right;
11+
Node (Node* l, Node* r, int d): left(l), right(r), data(d) {}
12+
};
13+
14+
int getMinElementInBST(Node* root)
15+
{
16+
assert(root != NULL);
17+
while (root -> left != NULL)
18+
root = root -> left;
19+
return root -> data;
20+
}
21+
22+
int getMaxElementInBST(Node* root)
23+
{
24+
assert(root != NULL);
25+
while (root -> right != NULL)
26+
root = root -> right;
27+
return root -> data;
28+
}
29+
30+
int main()
31+
{
32+
return 0;
33+
}

Data Structures Exam/81374-2.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include <algorithm>
2+
#include <iostream>
3+
#include <vector>
4+
5+
using namespace std;
6+
7+
struct Node
8+
{
9+
int data;
10+
Node* left;
11+
Node* right;
12+
Node (Node* l, Node* r, int d): left(l), right(r), data(d) {}
13+
};
14+
15+
void printBSTIntersections(Node* firstRoot, Node* secondRoot)
16+
{
17+
if ((firstRoot == NULL) || (secondRoot == NULL)) return;
18+
if (firstRoot -> data == secondRoot -> data)
19+
{
20+
cout << firstRoot -> data << endl;
21+
printBSTIntersections(firstRoot -> left, secondRoot -> left);
22+
printBSTIntersections(firstRoot -> right, secondRoot -> right);
23+
return;
24+
}
25+
if (firstRoot -> data > secondRoot -> data) swap(firstRoot, secondRoot);
26+
printBSTIntersections(firstRoot -> right, secondRoot);
27+
printBSTIntersections(firstRoot, secondRoot -> left);
28+
}
29+
30+
void getSortedElementsOfTree(Node* root, vector <int> &elements)
31+
{
32+
if (root == NULL)
33+
return;
34+
getSortedElementsOfTree(root -> left, elements);
35+
elements.push_back(root -> data);
36+
getSortedElementsOfTree(root -> left, elements);
37+
}
38+
39+
void printBSTIntersectionsUsingExtraMemory(Node* firstRoot, Node* secondRoot)
40+
{
41+
vector <int> firstTreeElements, secondTreeElements;
42+
getSortedElementsOfTree(firstRoot, firstTreeElements);
43+
getSortedElementsOfTree(secondRoot, secondTreeElements);
44+
int ind1 = 0, ind2 = 0;
45+
while ((ind1 < firstTreeElements.size()) && (ind2 < secondTreeElements.size()))
46+
{
47+
if (firstTreeElements[ind1] == secondTreeElements[ind2])
48+
{
49+
cout << firstTreeElements[ind1] << endl;
50+
++ind1;
51+
++ind2;
52+
}
53+
else if (firstTreeElements[ind1] < secondTreeElements[ind2]) ind1++;
54+
else ind2++;
55+
}
56+
}
57+
58+
int main()
59+
{
60+
return 0;
61+
}

Data Structures Exam/81374-3.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <assert.h>
4+
5+
using namespace std;
6+
7+
struct DoubleStack
8+
{
9+
private:
10+
vector <int> stacks;
11+
size_t numberOfElementsInFirstStack;
12+
size_t numberOfElementsInSecondStack;
13+
14+
public:
15+
DoubleStack(): numberOfElementsInFirstStack(0), numberOfElementsInSecondStack(0) {}
16+
17+
void push1(int x) /// O(stacks.size())
18+
{
19+
++numberOfElementsInFirstStack;
20+
stacks.insert(stacks.begin(), x);
21+
}
22+
23+
void push2(int x) /// O(1)
24+
{
25+
++numberOfElementsInSecondStack;
26+
stacks.push_back(x);
27+
}
28+
29+
int pop1() /// O(stacks.size())
30+
{
31+
assert(numberOfElementsInFirstStack);
32+
--numberOfElementsInFirstStack;
33+
int ret = stacks[0];
34+
stacks.erase(stacks.begin());
35+
return ret;
36+
}
37+
38+
int pop2() /// O(1)
39+
{
40+
assert(numberOfElementsInSecondStack);
41+
--numberOfElementsInSecondStack;
42+
int ret = stacks.back();
43+
stacks.pop_back();
44+
return ret;
45+
}
46+
};
47+
48+
void testDoubleStack()
49+
{
50+
DoubleStack ds;
51+
//ds.pop1();
52+
//ds.pop2();
53+
ds.push1(5);
54+
ds.push2(-3);
55+
ds.push1(16);
56+
ds.push1(-35);
57+
ds.push2(23);
58+
ds.push2(0);
59+
ds.push2(5);
60+
ds.push1(-69);
61+
ds.push2(31);
62+
for (int i = 0; i < 4; i++)
63+
cout << ds.pop1() << endl;
64+
cout << endl;
65+
for (int i = 0; i < 5; i++)
66+
cout << ds.pop2() << endl;
67+
//ds.pop1();
68+
//ds.pop2();
69+
}
70+
71+
int main()
72+
{
73+
testDoubleStack();
74+
return 0;
75+
}

Data Structures Exam/81374-4.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include <iostream>
2+
#include <fstream>
3+
#include <string>
4+
#include <vector>
5+
#include <algorithm>
6+
#include <map>
7+
8+
using namespace std;
9+
10+
bool isEnd(char c)
11+
{
12+
int numberOfChars = 12;
13+
char endCharacters[] = {',', ' ', '.', '!', '?', '\t', '\n', '\v', '\r', ';', '\\', '/'};
14+
for (int i = 0; i < numberOfChars; ++i)
15+
if (c == endCharacters[i])
16+
return true;
17+
return false;
18+
}
19+
20+
bool comparePairs(pair <int, string> first, pair <int, string> second)
21+
{
22+
return first.first > second.first;
23+
}
24+
25+
int main()
26+
{
27+
ifstream fin("input.txt");
28+
string word = "";
29+
map <string, int> wordToIndex;
30+
map <int, string> indexToWord;
31+
vector <int> numberOfOccurences;
32+
char input;
33+
while (fin.good())
34+
{
35+
input = fin.get();
36+
if (isEnd(input))
37+
{
38+
if (word != "")
39+
{
40+
if (wordToIndex.count(word)) ++numberOfOccurences[wordToIndex[word]];
41+
else
42+
{
43+
wordToIndex[word] = numberOfOccurences.size();
44+
indexToWord[numberOfOccurences.size()] = word;
45+
numberOfOccurences.push_back(1);
46+
}
47+
word = "";
48+
}
49+
}
50+
else word += input;
51+
}
52+
53+
vector < pair <int, string> > words;
54+
55+
for (int ind = 0; ind < numberOfOccurences.size(); ind++)
56+
words.push_back(pair <int, string> (numberOfOccurences[ind], indexToWord[ind]));
57+
58+
sort(words.begin(), words.end(), comparePairs);
59+
cout << words.size() << endl;
60+
int endOfIteration = (10 > words.size()) ? words.size() : 10;
61+
for (int ind = 0; ind < endOfIteration; ++ind)
62+
cout << words[ind].second << endl;
63+
64+
return 0;
65+
}
66+

Data Structures Exam/input.txt

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
adscfvbhnjmk,l./sdgo;nar 'pzsdG
2+
KL
3+
SD SG ]
4+
SR{HdfhgzsfjGhslzfh'l; S
5+
fg zsgfz;h,sfhsFRH
6+
hSH
7+
hs
8+
ghsd
9+
10+
11+
d
12+
13+
SF
14+
/f
15+
16+
F/QE'F D'.FG'
17+
D.G;S F,LSFGH
18+
S; SF;GH
19+
[DFHL[SDHL\ET;E]H;L[PGH
20+
ASD ASD ASD ASD ASD ASD ASD ASD ASD ASD ASD ??!?!#???$?r?fa?dgGASg?ASFGas?gt? $?^ ^?@#$%^!$?#^$
21+
DASDAS
22+
23+
SDFG
24+
AS
25+
SG
26+
ASG
27+
SG
28+
SG
29+
F
30+
G
31+
32+
33+
FG
34+
35+
G
36+
GGG
37+
38+
39+
A
40+
A
41+
A
42+
A
43+
A
44+
A
45+
A
46+
A
47+
A
48+
49+
A
50+
51+
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."

0 commit comments

Comments
 (0)