From d64a2193da23aed9b6979b82d8f89358bd572edb Mon Sep 17 00:00:00 2001 From: Ivan Kamburov Date: Thu, 19 Jan 2017 14:12:27 +0200 Subject: [PATCH] Add exam solutions. --- Data Structures Exam/81374-1.cpp | 33 ++++++++++++++ Data Structures Exam/81374-2.cpp | 61 ++++++++++++++++++++++++++ Data Structures Exam/81374-3.cpp | 75 ++++++++++++++++++++++++++++++++ Data Structures Exam/81374-4.cpp | 66 ++++++++++++++++++++++++++++ Data Structures Exam/input.txt | 51 ++++++++++++++++++++++ 5 files changed, 286 insertions(+) create mode 100644 Data Structures Exam/81374-1.cpp create mode 100644 Data Structures Exam/81374-2.cpp create mode 100644 Data Structures Exam/81374-3.cpp create mode 100644 Data Structures Exam/81374-4.cpp create mode 100644 Data Structures Exam/input.txt diff --git a/Data Structures Exam/81374-1.cpp b/Data Structures Exam/81374-1.cpp new file mode 100644 index 0000000..3f5a84f --- /dev/null +++ b/Data Structures Exam/81374-1.cpp @@ -0,0 +1,33 @@ +#include +#include + +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; +} diff --git a/Data Structures Exam/81374-2.cpp b/Data Structures Exam/81374-2.cpp new file mode 100644 index 0000000..b92ce9d --- /dev/null +++ b/Data Structures Exam/81374-2.cpp @@ -0,0 +1,61 @@ +#include +#include +#include + +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 &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 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; +} diff --git a/Data Structures Exam/81374-3.cpp b/Data Structures Exam/81374-3.cpp new file mode 100644 index 0000000..5cf908c --- /dev/null +++ b/Data Structures Exam/81374-3.cpp @@ -0,0 +1,75 @@ +#include +#include +#include + +using namespace std; + +struct DoubleStack +{ + private: + vector 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; +} diff --git a/Data Structures Exam/81374-4.cpp b/Data Structures Exam/81374-4.cpp new file mode 100644 index 0000000..8ec0de5 --- /dev/null +++ b/Data Structures Exam/81374-4.cpp @@ -0,0 +1,66 @@ +#include +#include +#include +#include +#include +#include + +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 first, pair second) +{ + return first.first > second.first; +} + +int main() +{ + ifstream fin("input.txt"); + string word = ""; + map wordToIndex; + map indexToWord; + vector 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 > words; + + for (int ind = 0; ind < numberOfOccurences.size(); ind++) + words.push_back(pair (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; +} + diff --git a/Data Structures Exam/input.txt b/Data Structures Exam/input.txt new file mode 100644 index 0000000..8d0764b --- /dev/null +++ b/Data Structures Exam/input.txt @@ -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." \ No newline at end of file