Skip to content

check these sick programs out #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 19, 2017
Merged
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
130 changes: 130 additions & 0 deletions Data Structures Exam/81248_GLL/01_lists.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
#include <unordered_map>

using std::unordered_map;

struct Node
{
int data;
Node* next;

Node() : next(nullptr) {}
Node(int data) : data(data), next(nullptr) {}
Node(int data, Node* sNext) : data(data), next(sNext) {}
};

void pushBack(Node* first, int elemToAdd)
{
while(first->next != nullptr)
{
first = first->next;
}
first->next = new Node(elemToAdd);
}

void copyList(const Node* from, Node* to)
{
to = new Node(from->data);
while(from->next != nullptr)
{
pushBack(to, from->data);
from = from->next;
}
pushBack(to, from->data);
}

void initialiseIntBoolMapFromList(const Node* first, unordered_map<int, bool> &map)
{
while(first->next != nullptr)
{
map[first->data] = false;
first = first->next;
}
map[first->data] = false;
}

Node* listUnion(const Node* first, const Node* second)
{
Node* newList = new Node(first->data); //hack
unordered_map<int, bool> unionElems;

if(first == nullptr && second == nullptr)
{
return newList;
}
if(first == nullptr)
{
copyList(second, newList);
return newList;
}
else if (second == nullptr)
{
copyList(second, newList);
return newList;
}
initialiseIntBoolMapFromList(first, unionElems);
initialiseIntBoolMapFromList(second, unionElems);
unionElems[first->data] = true; // hack

while(first != nullptr)
{
if(!unionElems[first->data])
{
unionElems[first->data] = true;
pushBack(newList, first->data);
}
first = first->next;
}
while(second != nullptr)
{
if(!unionElems[second->data])
{
unionElems[second->data] = true;
pushBack(newList, second->data);
}
second = second->next;
}
return newList;
}

Node* listIntersection(const Node* first, const Node* second)
{
Node* newList = nullptr; //hack
unordered_map<int, bool> intersectionElems;

if(first == nullptr || second == nullptr)
{
return newList;
}

initialiseIntBoolMapFromList(first, intersectionElems);
initialiseIntBoolMapFromList(second, intersectionElems);

while(first != nullptr)
{
if(!intersectionElems[first->data])
{
intersectionElems[first->data] = true;
}
first = first->next;
}
while(second != nullptr)
{
if(intersectionElems[second->data])
{
if(newList == nullptr) //stupid hack, sorry
{
newList = new Node(second->data);
}
else
{
pushBack(newList, second->data);
}
}
second = second->next;
}
if(newList == nullptr)
{
newList = new Node(); //stupid hack
}
return newList;
}
58 changes: 58 additions & 0 deletions Data Structures Exam/81248_GLL/02_tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@

struct Node
{
int data;
Node* left;
Node* right;
Node(const int& sData) : data(sData), left(nullptr), right(nullptr) {}
Node(const int& sData, Node* sLeft, Node* sRight) : data(sData), left(sLeft), right(sRight) {}
};

bool hasLeft(const Node* root)
{
return (root->left != nullptr);
}

bool hasRight(const Node* root)
{
return (root->right != nullptr);
}

void insertInTree(Node* &root, const int& elem)
{
if(root == nullptr)
{
root = new Node(elem);
}
if(elem <= root->data)
{
insertInTree(root->left, elem);
}
else
{
insertInTree(root->right, elem);
}
}

void reinsertElements(Node* root, Node* newRoot)
{
insertInTree(newRoot, root->data);
if(hasLeft(root))
{
reinsertElements(root->left, newRoot);
}
if(hasRight(root))
{
reinsertElements(root->right, newRoot);
}
}

Node* fixBrokenTree(Node* root)
{
Node* newRoot = nullptr;
if(root != nullptr)
{
reinsertElements(root, newRoot);
}
return newRoot;
}
77 changes: 77 additions & 0 deletions Data Structures Exam/81248_GLL/04_words_in_file.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#include <fstream>
#include <string>
#include <map>
#include <iostream>
#include <vector>
#include <algorithm>

using std::ifstream;
using std::string;
using std::map;
using std::cout;
using std::endl;
using std::pair;
using std::vector;
using std::sort;


string placeFileIntoString(const char* inputFilename)
{
ifstream inputFile;
string temp;
string input;
inputFile.open(inputFilename);
while(!inputFile.eof())
{
getline(inputFile, temp);
input += temp;
input.push_back('\n');
temp.clear();
}
input.erase(--input.end());
inputFile.close();
return input;
}

map<string, int> countWords(const string& input)
{
string currentWord;
string copy = input;
map<string, int> occurences;
int position;

while(!copy.empty())
{
position = copy.find_first_not_of("\t .,!?");
copy.erase(0, position);
position = copy.find_first_of("\t .,!?");
currentWord.insert(0, copy, 0, position);
copy.erase(0, position);
occurences[currentWord]++;
currentWord.clear();
}
return occurences;
}

void countWordsInFile(const char* inputFilename)
{
string contents = placeFileIntoString(inputFilename);
map<string, int> occurences = countWords(contents);
vector<pair<int, string> > dirtyHacksYo;
for(map<string, int>::iterator it = occurences.begin(); it != occurences.end(); ++it)
{
dirtyHacksYo.push_back(pair<int, string>((*it).second, (*it).first));
}
sort(dirtyHacksYo.rbegin(), dirtyHacksYo.rend());
for(int i = 0; i < 10; i++)
{
cout<<(i + 1)<<". "<<dirtyHacksYo[i].second<<" with "<<dirtyHacksYo[i].first<<" occurences."<<endl;
}
// LOL KEK
}

int main(int argc, char** argv)
{
countWordsInFile(argv[1]);
return 0;
}
6 changes: 6 additions & 0 deletions Data Structures Exam/81248_GLL/lorem_ipsum.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"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."