Skip to content

Commit 320d23f

Browse files
authored
Merge pull request #5 from googleson78/master
check these sick programs out
2 parents 78a1145 + 052885f commit 320d23f

File tree

4 files changed

+271
-0
lines changed

4 files changed

+271
-0
lines changed
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
#include <unordered_map>
2+
3+
using std::unordered_map;
4+
5+
struct Node
6+
{
7+
int data;
8+
Node* next;
9+
10+
Node() : next(nullptr) {}
11+
Node(int data) : data(data), next(nullptr) {}
12+
Node(int data, Node* sNext) : data(data), next(sNext) {}
13+
};
14+
15+
void pushBack(Node* first, int elemToAdd)
16+
{
17+
while(first->next != nullptr)
18+
{
19+
first = first->next;
20+
}
21+
first->next = new Node(elemToAdd);
22+
}
23+
24+
void copyList(const Node* from, Node* to)
25+
{
26+
to = new Node(from->data);
27+
while(from->next != nullptr)
28+
{
29+
pushBack(to, from->data);
30+
from = from->next;
31+
}
32+
pushBack(to, from->data);
33+
}
34+
35+
void initialiseIntBoolMapFromList(const Node* first, unordered_map<int, bool> &map)
36+
{
37+
while(first->next != nullptr)
38+
{
39+
map[first->data] = false;
40+
first = first->next;
41+
}
42+
map[first->data] = false;
43+
}
44+
45+
Node* listUnion(const Node* first, const Node* second)
46+
{
47+
Node* newList = new Node(first->data); //hack
48+
unordered_map<int, bool> unionElems;
49+
50+
if(first == nullptr && second == nullptr)
51+
{
52+
return newList;
53+
}
54+
if(first == nullptr)
55+
{
56+
copyList(second, newList);
57+
return newList;
58+
}
59+
else if (second == nullptr)
60+
{
61+
copyList(second, newList);
62+
return newList;
63+
}
64+
initialiseIntBoolMapFromList(first, unionElems);
65+
initialiseIntBoolMapFromList(second, unionElems);
66+
unionElems[first->data] = true; // hack
67+
68+
while(first != nullptr)
69+
{
70+
if(!unionElems[first->data])
71+
{
72+
unionElems[first->data] = true;
73+
pushBack(newList, first->data);
74+
}
75+
first = first->next;
76+
}
77+
while(second != nullptr)
78+
{
79+
if(!unionElems[second->data])
80+
{
81+
unionElems[second->data] = true;
82+
pushBack(newList, second->data);
83+
}
84+
second = second->next;
85+
}
86+
return newList;
87+
}
88+
89+
Node* listIntersection(const Node* first, const Node* second)
90+
{
91+
Node* newList = nullptr; //hack
92+
unordered_map<int, bool> intersectionElems;
93+
94+
if(first == nullptr || second == nullptr)
95+
{
96+
return newList;
97+
}
98+
99+
initialiseIntBoolMapFromList(first, intersectionElems);
100+
initialiseIntBoolMapFromList(second, intersectionElems);
101+
102+
while(first != nullptr)
103+
{
104+
if(!intersectionElems[first->data])
105+
{
106+
intersectionElems[first->data] = true;
107+
}
108+
first = first->next;
109+
}
110+
while(second != nullptr)
111+
{
112+
if(intersectionElems[second->data])
113+
{
114+
if(newList == nullptr) //stupid hack, sorry
115+
{
116+
newList = new Node(second->data);
117+
}
118+
else
119+
{
120+
pushBack(newList, second->data);
121+
}
122+
}
123+
second = second->next;
124+
}
125+
if(newList == nullptr)
126+
{
127+
newList = new Node(); //stupid hack
128+
}
129+
return newList;
130+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
2+
struct Node
3+
{
4+
int data;
5+
Node* left;
6+
Node* right;
7+
Node(const int& sData) : data(sData), left(nullptr), right(nullptr) {}
8+
Node(const int& sData, Node* sLeft, Node* sRight) : data(sData), left(sLeft), right(sRight) {}
9+
};
10+
11+
bool hasLeft(const Node* root)
12+
{
13+
return (root->left != nullptr);
14+
}
15+
16+
bool hasRight(const Node* root)
17+
{
18+
return (root->right != nullptr);
19+
}
20+
21+
void insertInTree(Node* &root, const int& elem)
22+
{
23+
if(root == nullptr)
24+
{
25+
root = new Node(elem);
26+
}
27+
if(elem <= root->data)
28+
{
29+
insertInTree(root->left, elem);
30+
}
31+
else
32+
{
33+
insertInTree(root->right, elem);
34+
}
35+
}
36+
37+
void reinsertElements(Node* root, Node* newRoot)
38+
{
39+
insertInTree(newRoot, root->data);
40+
if(hasLeft(root))
41+
{
42+
reinsertElements(root->left, newRoot);
43+
}
44+
if(hasRight(root))
45+
{
46+
reinsertElements(root->right, newRoot);
47+
}
48+
}
49+
50+
Node* fixBrokenTree(Node* root)
51+
{
52+
Node* newRoot = nullptr;
53+
if(root != nullptr)
54+
{
55+
reinsertElements(root, newRoot);
56+
}
57+
return newRoot;
58+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#include <fstream>
2+
#include <string>
3+
#include <map>
4+
#include <iostream>
5+
#include <vector>
6+
#include <algorithm>
7+
8+
using std::ifstream;
9+
using std::string;
10+
using std::map;
11+
using std::cout;
12+
using std::endl;
13+
using std::pair;
14+
using std::vector;
15+
using std::sort;
16+
17+
18+
string placeFileIntoString(const char* inputFilename)
19+
{
20+
ifstream inputFile;
21+
string temp;
22+
string input;
23+
inputFile.open(inputFilename);
24+
while(!inputFile.eof())
25+
{
26+
getline(inputFile, temp);
27+
input += temp;
28+
input.push_back('\n');
29+
temp.clear();
30+
}
31+
input.erase(--input.end());
32+
inputFile.close();
33+
return input;
34+
}
35+
36+
map<string, int> countWords(const string& input)
37+
{
38+
string currentWord;
39+
string copy = input;
40+
map<string, int> occurences;
41+
int position;
42+
43+
while(!copy.empty())
44+
{
45+
position = copy.find_first_not_of("\t .,!?");
46+
copy.erase(0, position);
47+
position = copy.find_first_of("\t .,!?");
48+
currentWord.insert(0, copy, 0, position);
49+
copy.erase(0, position);
50+
occurences[currentWord]++;
51+
currentWord.clear();
52+
}
53+
return occurences;
54+
}
55+
56+
void countWordsInFile(const char* inputFilename)
57+
{
58+
string contents = placeFileIntoString(inputFilename);
59+
map<string, int> occurences = countWords(contents);
60+
vector<pair<int, string> > dirtyHacksYo;
61+
for(map<string, int>::iterator it = occurences.begin(); it != occurences.end(); ++it)
62+
{
63+
dirtyHacksYo.push_back(pair<int, string>((*it).second, (*it).first));
64+
}
65+
sort(dirtyHacksYo.rbegin(), dirtyHacksYo.rend());
66+
for(int i = 0; i < 10; i++)
67+
{
68+
cout<<(i + 1)<<". "<<dirtyHacksYo[i].second<<" with "<<dirtyHacksYo[i].first<<" occurences."<<endl;
69+
}
70+
// LOL KEK
71+
}
72+
73+
int main(int argc, char** argv)
74+
{
75+
countWordsInFile(argv[1]);
76+
return 0;
77+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
2+
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
3+
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
4+
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
5+
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
6+
proident, sunt in culpa qui officia deserunt mollit anim id est laborum."

0 commit comments

Comments
 (0)