Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions Data Structures Exam/81374-1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <iostream>
#include <assert.h>

using namespace std;

struct Node
{
int data;
Node* left;
Node* right;
Node (Node* l, Node* r, int d): left(l), right(r), data(d) {}
};

int getMinElementInBST(Node* root)
{
assert(root != NULL);
while (root -> left != NULL)
root = root -> left;
return root -> data;
}

int getMaxElementInBST(Node* root)
{
assert(root != NULL);
while (root -> right != NULL)
root = root -> right;
return root -> data;
}

int main()
{
return 0;
}
61 changes: 61 additions & 0 deletions Data Structures Exam/81374-2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

struct Node
{
int data;
Node* left;
Node* right;
Node (Node* l, Node* r, int d): left(l), right(r), data(d) {}
};

void printBSTIntersections(Node* firstRoot, Node* secondRoot)
{
if ((firstRoot == NULL) || (secondRoot == NULL)) return;
if (firstRoot -> data == secondRoot -> data)
{
cout << firstRoot -> data << endl;
printBSTIntersections(firstRoot -> left, secondRoot -> left);
printBSTIntersections(firstRoot -> right, secondRoot -> right);
return;
}
if (firstRoot -> data > secondRoot -> data) swap(firstRoot, secondRoot);
printBSTIntersections(firstRoot -> right, secondRoot);
printBSTIntersections(firstRoot, secondRoot -> left);
}

void getSortedElementsOfTree(Node* root, vector <int> &elements)
{
if (root == NULL)
return;
getSortedElementsOfTree(root -> left, elements);
elements.push_back(root -> data);
getSortedElementsOfTree(root -> left, elements);
}

void printBSTIntersectionsUsingExtraMemory(Node* firstRoot, Node* secondRoot)
{
vector <int> firstTreeElements, secondTreeElements;
getSortedElementsOfTree(firstRoot, firstTreeElements);
getSortedElementsOfTree(secondRoot, secondTreeElements);
int ind1 = 0, ind2 = 0;
while ((ind1 < firstTreeElements.size()) && (ind2 < secondTreeElements.size()))
{
if (firstTreeElements[ind1] == secondTreeElements[ind2])
{
cout << firstTreeElements[ind1] << endl;
++ind1;
++ind2;
}
else if (firstTreeElements[ind1] < secondTreeElements[ind2]) ind1++;
else ind2++;
}
}

int main()
{
return 0;
}
75 changes: 75 additions & 0 deletions Data Structures Exam/81374-3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include <iostream>
#include <vector>
#include <assert.h>

using namespace std;

struct DoubleStack
{
private:
vector <int> stacks;
size_t numberOfElementsInFirstStack;
size_t numberOfElementsInSecondStack;

public:
DoubleStack(): numberOfElementsInFirstStack(0), numberOfElementsInSecondStack(0) {}

void push1(int x) /// O(stacks.size())
{
++numberOfElementsInFirstStack;
stacks.insert(stacks.begin(), x);
}

void push2(int x) /// O(1)
{
++numberOfElementsInSecondStack;
stacks.push_back(x);
}

int pop1() /// O(stacks.size())
{
assert(numberOfElementsInFirstStack);
--numberOfElementsInFirstStack;
int ret = stacks[0];
stacks.erase(stacks.begin());
return ret;
}

int pop2() /// O(1)
{
assert(numberOfElementsInSecondStack);
--numberOfElementsInSecondStack;
int ret = stacks.back();
stacks.pop_back();
return ret;
}
};

void testDoubleStack()
{
DoubleStack ds;
//ds.pop1();
//ds.pop2();
ds.push1(5);
ds.push2(-3);
ds.push1(16);
ds.push1(-35);
ds.push2(23);
ds.push2(0);
ds.push2(5);
ds.push1(-69);
ds.push2(31);
for (int i = 0; i < 4; i++)
cout << ds.pop1() << endl;
cout << endl;
for (int i = 0; i < 5; i++)
cout << ds.pop2() << endl;
//ds.pop1();
//ds.pop2();
}

int main()
{
testDoubleStack();
return 0;
}
66 changes: 66 additions & 0 deletions Data Structures Exam/81374-4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>

using namespace std;

bool isEnd(char c)
{
int numberOfChars = 12;
char endCharacters[] = {',', ' ', '.', '!', '?', '\t', '\n', '\v', '\r', ';', '\\', '/'};
for (int i = 0; i < numberOfChars; ++i)
if (c == endCharacters[i])
return true;
return false;
}

bool comparePairs(pair <int, string> first, pair <int, string> second)
{
return first.first > second.first;
}

int main()
{
ifstream fin("input.txt");
string word = "";
map <string, int> wordToIndex;
map <int, string> indexToWord;
vector <int> numberOfOccurences;
char input;
while (fin.good())
{
input = fin.get();
if (isEnd(input))
{
if (word != "")
{
if (wordToIndex.count(word)) ++numberOfOccurences[wordToIndex[word]];
else
{
wordToIndex[word] = numberOfOccurences.size();
indexToWord[numberOfOccurences.size()] = word;
numberOfOccurences.push_back(1);
}
word = "";
}
}
else word += input;
}

vector < pair <int, string> > words;

for (int ind = 0; ind < numberOfOccurences.size(); ind++)
words.push_back(pair <int, string> (numberOfOccurences[ind], indexToWord[ind]));

sort(words.begin(), words.end(), comparePairs);
cout << words.size() << endl;
int endOfIteration = (10 > words.size()) ? words.size() : 10;
for (int ind = 0; ind < endOfIteration; ++ind)
cout << words[ind].second << endl;

return 0;
}

51 changes: 51 additions & 0 deletions Data Structures Exam/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
adscfvbhnjmk,l./sdgo;nar 'pzsdG
KL
SD SG ]
SR{HdfhgzsfjGhslzfh'l; S
fg zsgfz;h,sfhsFRH
hSH
hs
ghsd


d

SF
/f

F/QE'F D'.FG'
D.G;S F,LSFGH
S; SF;GH
[DFHL[SDHL\ET;E]H;L[PGH
ASD ASD ASD ASD ASD ASD ASD ASD ASD ASD ASD ??!?!#???$?r?fa?dgGASg?ASFGas?gt? $?^ ^?@#$%^!$?#^$
DASDAS

SDFG
AS
SG
ASG
SG
SG
F
G


FG

G
GGG


A
A
A
A
A
A
A
A
A

A

"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."