Repository navigation
Expand file tree
/
Copy pathinterview.cpp
More file actions
1333 lines (1158 loc) · 32.5 KB
/
Copy pathinterview.cpp
File metadata and controls
1333 lines (1158 loc) · 32.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cmath>
#include <stdint.h>
#include <queue>
#include <deque>
#include <screen.hpp>
#include <list>
#include <functional>
#include <boost/lexical_cast.hpp>
#include <map>
#include <unordered_map>
#include <sstream>
//#include "print.h"
using namespace std;
//static int i = 3;
//int count[5] = {2, 4, 6, 8, 10} ;
int a = 5;
/*
int& function () { };
namespace { static int n; }
namespace A {
namespace { static int n; }
}
*//*
class A {
public:
A() {}
A(A& a) {}
A& operator=(const A& a)
{ return(*this); }
};
*/
/*
class sc {
int x;
public:
sc(int xx) : x(xx) {}
};
template<typename T> class DynA{
T * contents;
int size;
public:
explicit DynA(int initial_size);
};
template<class T>
struct sum{
static void foo(T op1, T op2) {
cout << " sum j= " << endl;
}
};
*/
/*
void swap(uint32_t& varA_, uint32_t& varB_) {
varA_ = varB_ - varA_;
varB_ -= varA_;
varA_ += varB_;
}
//given that 0 = empty, 1 = 'x', 2 = 'o'. -- amended to n-sized games - only checking horizontal and verticals as is because moving on.
bool winnerTTT(string gameState_) {
// very limited number of winning states. check for each state seperately.
uint32_t streak = 0;
// for each side - traverse through all possible winning starting positions.
uint32_t sideLength = sqrt(gameState_.size());
for (uint32_t i = 1; i < 3; i++) {
// 0 - 3 - 6
for (uint32_t start = 0; start < sideLength; start++) {
// check horizontal
for (uint32_t streakPos = 0; streakPos < sideLength; streakPos++) {
if (gameState_.at(start*sideLength + streakPos) == boost::lexical_cast<char>(i)) {
streak++;
cout << "streak value: " << streak << "after increment" << endl;
}
else { break;} //found a broken link, abandon loop to reduce operation overhead.
}
if (streak == sideLength) { return true;}
streak = 0; //reset tracker for next iteration.
// check vertical.
for (uint32_t streakPos = 0; streakPos < sideLength; streakPos++) {
if ( (gameState_.at(start*sideLength + (streakPos*sideLength)) ) == boost::lexical_cast<char>(i)) {
streak++;
}
else { break;}
}
if ( streak== sideLength ) { return true;}
streak = 0;
}
}
return false;
}
uint32_t calcFact(uint32_t factCand_ ){
return (factCand_ == 1 || factCand_ == 0) ? 1 : calcFact(factCand_ - 1 ) * factCand_;
}
typedef struct PTNode{
char ltr = NULL;
bool terminal = false;
map<char, PTNode> cNodes;
};
string compressString(string word_){
char currentChar = word_.at(0);
uint32_t count = 0;
string cString;
for (uint32_t i = 0; i < word_.size(); i++) {
if (word_.at(i) != currentChar && i > 0) {
cString+=currentChar;
cString+= to_string(count);
count = 1;
currentChar = word_.at(i);
}
else {
count++;
}
}
cString += currentChar;
cString += to_string(count);
return cString;
}
string expandString(string word_){
string cString;
for (uint32_t i = 0; i < word_.size()-1; i+= 2) {
for (uint32_t j = 0; j < boost::lexical_cast<uint32_t>(word_.at(i+1)); j++) {
cString += word_.at(i);
}
}
return cString;
}
// presumes that the head node is properly initialized.
void insertWord(PTNode& head_, string word_) {
if ( word_.size() <= 0) {
return; //robustness measure.
}
char firstChar = word_.at(0);
if (head_.cNodes.find(firstChar) == head_.cNodes.end()) { // no word in our prefix tree exists starting with the current letter
//create new node, insert char and move on through the candidate string.
PTNode tempNode;
tempNode.ltr = firstChar;
head_.cNodes[firstChar] = tempNode;
if ( word_.size() > 1 ) {
insertWord(head_.cNodes[firstChar], word_.substr(1));
}
else {
head_.cNodes[firstChar].terminal = true;
return;
}
}
else { //easy step, letter already exists in tree, just follow along.
if ( word_.size() > 1 ) {
insertWord(head_.cNodes[firstChar], word_.substr(1));
}
else {
head_.cNodes[firstChar].terminal = true;
return;
}
}
}
void traverseTree(PTNode& head, string word_){
// else {
word_ += head.ltr;
if ( head.terminal ) {
cout << word_ << endl;
}
for (auto& node : head.cNodes) {
traverseTree(node.second, word_);
}
// }
}
unordered_map<string, int> createDict(string book[], uint32_t length_){
unordered_map<string, int> * tempMap = new unordered_map<string, int>();
uint32_t temp = 0;
for (uint32_t i = 0 ; i < length_ ; i++ ) {
cout << "at word: " << book[i] << endl;
if ( tempMap->count(book[i]) == 0){
tempMap->insert({book[i], 1});
cout << book[i] << " not in dict, creating key" << endl;
// tempMap->at(book[i]) = 1;
}
else {
temp = tempMap->at(book[i]);
cout << book[i] << " encountered " << temp+1 << " times\n";
tempMap->at(book[i]) = temp + 1;
cout << "count after insert-> command: " << tempMap->at(book[i]) << endl;
}
}
return *(tempMap);
}
*/
/*
struct Foo{
Foo() {cout << "d";}
Foo(int i) {cout << "i";}
Foo(char c) {cout << "c";}
Foo(long l) {cout << "l";}
Foo(float f) {cout << "f";}
};
*/
/*
class foo {
public:
virtual !foo()};
class foobar2 : public bar2{
};
class bar2 : public foo {
};
*/
/*
class base{
protected int b;
};
class derived : base{
friend class Friend;
};
class Friend{
derived der;
};
*/
/*
class professor{
~professor() {cout << "destruct professor\n";
};*/
/*
class Base{};
class derived : Base{};
*/
// maze node structure used to demonstrate bfs traversal and stack tracking of a maze shortest path solution. We have no knowledge of the end point.
// REF: mazeNode
//
/*
typedef struct mazeNode {
uint32_t id;
uint32_t val;
bool end;
};
typedef struct pTNode{
string id; //using char reduces the number of lexical casts from uint32_t to char
map<string, pTNode> cPaths;
};
// upon proper path tracing and return of the new total path, the client protocol can manage a number of paths.
// this was honestly the biggest waste of fucking time ever
string insertPath(pTNode& node, deque<string> cPath, uint32_t cVal) { //using a string bc we need to remember our paths
if (node.id == "root" ) {//is root
if (cPath.size() == 0) { //easy pz, we are the last step in a potential path.
pTNode temp;
temp.id = boost::lexical_cast<string>(cVal);
node.cPaths[boost::lexical_cast<string>(cVal)] = temp;
return boost::lexical_cast<string>(cVal);
}
else{//traverse
//if it exists move accordingly.
if (node.cPaths.find(cPath.at(0)) != node.cPaths.end()) {
// return the current letter plus the next or null letter.
string tempS = cPath.at(0);
cPath.pop_front();
return (tempS + insertPath(node.cPaths[tempS], cPath, cVal));
}
else {
pTNode temp;
temp.id = cPath.at(0);
node.cPaths[cPath.at(0)] = temp;
string tempS = cPath.at(0);
cPath.pop_front();
return (tempS + insertPath(node.cPaths[tempS], cPath, cVal));
}
}
}
else {
if (cPath.size() == 0) { //easy pz, we are the last step in a potential path.
pTNode temp;
temp.id = boost::lexical_cast<string>(cVal);
node.cPaths[boost::lexical_cast<string>(cVal)] = temp;
return boost::lexical_cast<string>(cVal);
}
else{//traverse
//if it exists move accordingly.
if (node.cPaths.find(cPath.at(0)) != node.cPaths.end()) {
// return the current letter plus the next or null letter.
string tempS = cPath.at(0);
cPath.pop_front();
return (tempS + insertPath(node.cPaths[tempS], cPath, cVal));
}
else {
pTNode temp;
temp.id = cPath.at(0);
node.cPaths[cPath.at(0)] = temp;
string tempS = cPath.at(0);
cPath.pop_front();
return (tempS + insertPath(node.cPaths[tempS], cPath, cVal));
}
}
}
}
//construct a trie internal to this function to track the paths in an efficient manner using the parents and children as nodes.
vector<uint32_t> solveMaze(mazeNode maze_[][4], uint32_t goal_) {
vector<uint32_t> * path = new vector<uint32_t>();
pTNode pathRoot;
pathRoot.id = "root";
// now begin a bfs while tracking a stack for EACH path and then identify the first stack that reaches id == goal_;
deque<mazeNode> unprocessed;
unordered_map<uint32_t, bool> processed; //mazeNode.id, false/true
processed[0] = true;
unprocessed.push_back(maze_[0][0]);
uint32_t xInd, yInd, maxX, maxY;
xInd = yInd = 0;
maxX = 7;
maxY = 4;
mazeNode temp;
// accounting variables for efficiently tracking paths.
uint32_t unProcIndex = 0; // used to keep a track record of previously accomplished work. ...
deque<uint32_t> stackNodes;
uint32_t numPaths = 1;
bool firstPass = true;
map<uint32_t, uint32_t> discovered;
while (unprocessed.size() > 0) {
temp = unprocessed.front();
stackNodes.push_back(temp.id);
//currentString = insertPath(pathRoot, currentString[currentPath], temp.id);//find the valid path.
// calculate xInd and yInd based on the temp being processed.
yInd = temp.id / maxX;
xInd = temp.id % maxX;
unprocessed.pop_front();
cout << "processing node id: " << temp.id << " => " << boost::lexical_cast<string>(temp.val) << endl;
//start checking all cardinal directions and act accordingly.
if (xInd > 0 ) {// can look left.
//if its our target...
if ( temp.id == goal_ ) {
//return the corresponding path stack TODO: [ ] determine tracking system for various paths.
cout << "I think the shortest path is [ asdf] : " << endl;
int tDec = 1;
for (uint32_t& sNode: stackNodes) {
cout << sNode << endl;
}
cout << "enough lollygagging\n";
uint32_t tID = temp.id;
while (tID != 0) {
cout << discovered[tID] << endl;
tID = discovered[tID];
}
break;
}
else if (maze_[xInd-1][yInd].val == 1) {
//check if it's been encountered and queue accordingly.
if (!processed[maze_[xInd-1][yInd].id]) {
processed[maze_[xInd-1][yInd].id] = true;
// stackNodes.push_back(maze_[xInd-1][yInd].id);
discovered[maze_[xInd-1][yInd].id] = temp.id;
unprocessed.push_back(maze_[xInd-1][yInd]);
cout << "pushing node: " << boost::lexical_cast<string>(maze_[xInd-1][yInd].id) <<
" from node " << boost::lexical_cast<string>(maze_[xInd][yInd].id) << " left " << endl;
numPaths++;
}
}
}
if (yInd < (maxY-1) ) { // can look down./if its our target...
if ( temp.id == goal_ ) {
//return the corresponding path stack TODO: [ ] determine tracking system for various paths.
cout << "I think the shortest path is [ asdf] : " << endl;
int tDec = 1;
for (uint32_t& sNode: stackNodes) {
cout << sNode << endl;
}
cout << "enough lollygagging\n";
uint32_t tID = temp.id;
while (tID != 0) {
cout << discovered[tID] << endl;
tID = discovered[tID];
}
break;
}
else if (maze_[xInd][yInd+1].val == 1) {
//check if it's been encountered and queue accordingly.
if (!processed[maze_[xInd][yInd+1].id]) {
processed[maze_[xInd][yInd+1].id] = true;
//stackNodes.push_back(maze_[xInd][yInd+1].id);
discovered[maze_[xInd][yInd+1].id] = temp.id;
unprocessed.push_back(maze_[xInd][yInd+1]);
cout << "pushing node: " << boost::lexical_cast<string>(maze_[xInd][yInd+1].id) <<
" from node " << boost::lexical_cast<string>(maze_[xInd][yInd].id) << " down " << endl;
numPaths++;
}
}
}
if (yInd > 0) { // can look up.
if ( temp.id == goal_ ) {
//return the corresponding path stack TODO: [ ] determine tracking system for various paths.
cout << "I think the shortest path is [ asdf] : " << endl;
int tDec = 1;
for (uint32_t& sNode: stackNodes) {
cout << sNode << endl;
}
cout << "enough lollygagging\n";
uint32_t tID = temp.id;
while (tID != 0) {
cout << discovered[tID] << endl;
tID = discovered[tID];
}
break;
}
else if (maze_[xInd][yInd-1].val == 1) {
//check if it's been encountered and queue accordingly.
if (!processed[maze_[xInd][yInd-1].id]) {
processed[maze_[xInd][yInd-1].id] = true;
unprocessed.push_back(maze_[xInd][yInd-1]);
// stackNodes.push_back(maze_[xInd][yInd-1].id);
discovered[maze_[xInd][yInd-1].id] = temp.id;
cout << "pushing node: " << boost::lexical_cast<string>(maze_[xInd][yInd-1].id) << endl;
numPaths++;
}
}
}
if (xInd < (maxX-1) ) { // can look right.
if ( temp.id == goal_ ) {
//return the corresponding path stack TODO: [ ] determine tracking system for various paths.
cout << "I think the shortest path is [ asdf] : " << endl;
int tDec = 1;
for (uint32_t& sNode: stackNodes) {
cout << sNode << endl;
}
cout << "enough lollygagging\n";
uint32_t tID = temp.id;
while (tID != 0) {
cout << discovered[tID] << endl;
tID = discovered[tID];
}
break;
}
else if (maze_[xInd+1][yInd].val == 1) {
//check if it's been encountered and queue accordingly.
if (!processed[maze_[xInd+1][yInd].id]) {
processed[maze_[xInd+1][yInd].id] = true;
unprocessed.push_back(maze_[xInd+1][yInd]);
// stackNodes.push_back(maze_[xInd+1][yInd].id);
discovered[maze_[xInd+1][yInd].id] = temp.id;
cout << "pushing node: " << boost::lexical_cast<string>(maze_[xInd+1][yInd].id) <<
" from node " << boost::lexical_cast<string>(maze_[xInd][yInd].id) << " right " << endl;
numPaths++;
}
}
}
++unProcIndex;
}
return *(path);
}
int binSearch(vector<int> nums_, int target, int lB, int rB) {
int pos;
while ((rB - lB) > 1) {
pos = (rB-lB) / 2 + lB; // the offset
if (nums_[pos] == target ) return pos;
else if ( nums_[pos] > target ) {
rB--;
}
else lB++;
}
}
struct gNode{
int val;
vector<gNode*> eV;
gNode(int i) : val(i) { }
};
void traverseGraph(gNode root) {
map<int, bool> discovered;
deque<gNode> toProcess;
toProcess.push_back(root);
discovered[root.val] = true;
while (toProcess.size() >0) {
root = toProcess.front();
toProcess.pop_front();
cout << "at node: " << root.val << endl;
for (gNode* gN : root.eV) {
if (!discovered[gN->val]) {
toProcess.push_back(*(gN));
discovered[gN->val] = true;
}
}
}
}
void insert(int& count) {
count++;
}
*/
//Kruskal's algorithm 6.1.2 Alg Des Man
//
struct kNode {
int weight;
char vertex;
};
class kGraph {
protected:
map<char, vector<kNode*>> edges;
public:
kGraph();
~kGraph();
void insertEdge(char P, char C, int weight);
void iterateEdges();
vector<char> primAlgo(char spawn);
};
kGraph::kGraph() {
edges = *(new map<char, vector<kNode*>>);
}
kGraph::~kGraph(){
// delete *edges;
}
void kGraph::insertEdge(char P, char C, int weight) {
kNode * temp = new kNode();
temp->weight = weight;
temp->vertex = C;
edges[P].push_back(temp);
temp = new kNode();
temp->weight = weight;
temp->vertex = P;
edges[C].push_back(temp);
}
void kGraph::iterateEdges() {
for (map<char, vector<kNode*>>::iterator it = edges.begin(); it != edges.end(); it++) {
cout << it->first << " connects with these nodes:\n";
for (kNode* n : it->second) {
cout << "Node - " << n->vertex << " Edge weight: " << n->weight << endl;
}
}
}
/*
* The prim algorithm for defining minimum spanning trees is a greedy algorithm
* Traversing a graph, building the tree, each new path is selected by the cheapes local edge leading to an undiscovered edge.
*/
vector<char> kGraph::primAlgo(char spawn) {
//map<char, vector<kNode*>>::iterator it;
vector<char> disc;
disc.push_back(spawn);
int numKeys = edges.size();
int lWeight;// 'local weight'.
char nNode;
int iterations;
int totalW = 0;
// robustness measure, the spawn and subsequent traversal may lead to a dead end, at which point, start from the original spawn and just go the other way.
char start = spawn;
while (numKeys > disc.size()){
lWeight = INT_MAX;
if ( iterations == numKeys ) {
spawn = start; //robustness measure
iterations = 0;//incase it happens again lol.
}
for (kNode* e : edges[spawn]) {
if (e->weight < lWeight &&
find(disc.begin(),disc.end(),e->vertex) == disc.end()) {
nNode = e->vertex;
lWeight = e->weight;
}
}
// robustness measure
if (lWeight!=INT_MAX ) {
disc.push_back(nNode);
cout << spawn << " routes to " << nNode << " with weight " << lWeight << endl;
spawn = nNode;
totalW += lWeight;
}
iterations++;
}
cout << "traversal cost: " << totalW << endl;
return disc;
}
void qSHelper(int arr[], int left, int right){
int temp;
if (right-left <=1) {
return;
}
//select pivot value. [heuristically today, middle of posts.
//move it to the front of this partition.
int pivot = arr[((right-left)/2) + left];
arr[((right-left))/2 + left] = arr[left];
arr[left] = pivot;
int pInd = left;
//iterate through partition and sort according to pivot value.
for (int i = left+1;i<=right; i++) {
if (arr[i] < pivot) {
//swap pivot with neighbor
arr[pInd] = arr[pInd+1];
arr[pInd+1] = pivot;
//now switch the neighbor with the current val
temp = arr[pInd];
arr[pInd] = arr[i];
arr[i] = temp;
//iterate index, move on
pInd++;
}
}
//test
cout << "left values:\n";
for (int i = left; i < pInd; i++) cout << arr[i] << endl;
cout << "pivot value: " << pivot << endl;
cout << "right values:\n";
for (int i = pInd+1; i <= right; i++) cout << arr[i] << endl;
//divide and conquer based on the pivot index.
qSHelper(arr, left, pInd-1);
qSHelper(arr, pInd+1, right);
}
// quick sort, array addition.
void qSort(int arr[], int length) {
for (int i = 0; i < length; i++) cout << arr[i] << endl;
qSHelper(arr, 0, length-1);
}
int atoi(string num_) {
if (num_.size() <1) return 0;
bool neg = false;
if (num_.at(0) == '-') neg = true;
int val = 0;
for (int i = 1; i < num_.size(); i++) {
val *=10;
val += (int)num_.at(i);
}
if (neg) return -1*val;
return val;
}
void reverseWords(string& phrase) {
istringstream myS(phrase);
string temp;
string ans = "";
while(myS) {
myS >> temp;
cout << temp << endl;
ans = temp + " " + ans;
}
phrase = ans;
}
bool isSub(string one, string two) {
if (two.size() < one.size()) return false;
for (int i = 0; i < two.size()-one.size(); i++) {
if (one == two.substr(i,one.size() )) return true;
}
return false;
}
int main() {
if (isSub("bat", "batman")) cout << "bat is a substr of batman" << endl;
else cout << "bat is not a substr of batman" << endl;
if (isSub("tab", "tannery")) cout <<"tab is a substr of tannery" <<endl;
else cout << "tab is not a substr of tannery" << endl;
cout << atoi("-12345") << endl;
string oc = "MC Sucker Has to Pay";
cout << oc << endl;
reverseWords(oc);
cout << oc << endl;
/*
int Arr[10] = {5,26,73,85,12,4,7,10,3,2};
int val;
int l = end(Arr)-begin(Arr);
for (int i = 0; i < l; i++) {
val = Arr[i];
cout << "val: " << val << endl;
}
qSort(Arr, l);
cout << "after qsort" << endl;
for (int i = 0; i < end(Arr)-begin(Arr); i++) {
val = Arr[i];
cout << "val: " << val << endl;
}
*/
/*
kGraph myKG = kGraph();
myKG.insertEdge('a', 'b', 5);
myKG.insertEdge('a', 'd', 4);
myKG.insertEdge('a', 'i', 9);
myKG.insertEdge('b', 'd', 3);
myKG.insertEdge('g', 'd', 6);
myKG.insertEdge('g', 'e', 6);
myKG.insertEdge('d', 'e', 4);
myKG.insertEdge('i', 'g', 2);
myKG.insertEdge('i', 'j', 1);
myKG.insertEdge('j', 'h', 4);
myKG.insertEdge('h', 'g', 3);
myKG.insertEdge('h', 'f', 11);
myKG.insertEdge('f', 'c', 2);
myKG.insertEdge('b', 'c', 3);
myKG.insertEdge('c', 'e', 2);
myKG.insertEdge('e', 'f', 8);
myKG.insertEdge('h', 'e', 7);
myKG.insertEdge('j', 'g', 2);
myKG.insertEdge('e', 'b', 1);
myKG.iterateEdges();
myKG.primAlgo('c');
//another graph rep. paradigm - map<char, vector<nodes> Edges, Nodes retain weight and node name.
*/
/*
int test[10] = {};
for (int i = 0; i < 10; i++) {
cout << test[i] << endl;
}
int count = 0;
insert(count);
cout << count << endl;
insert(count);
cout << count << endl;
gNode A(1);
gNode B(2);
gNode C(3);
gNode D(4);
gNode E(5);
A.eV.push_back(&B);
D.eV.push_back(&E);
B.eV.push_back(&C);
C.eV.push_back(&D);
traverseGraph(A);
*/
/*
vector<int> nums;
nums.push_back(1);
nums.push_back(2);
nums.push_back(3);
nums.push_back(4);
nums.push_back(5);
nums.push_back(6);
nums.push_back(7);
cout << binSearch(nums, 6, 0, nums.size()-1);
*/
//see REF: mazeNode
//given an input of 0's and 1's defining a maze structre and a root of id=0 and GOAL of id=rand, find the shortest path to goal.
/* 1's are passable, 0's are not.
* [1 1 1 0 0 1 0
* 1 1 0 1 1 1 0
* 1 1 1 1 0 1 1
* 1 1 0 0 0 0 1] start = 0, test goal = 28
*
* return the inorder shortest path in any data structure.
* */
/*
mazeNode maze[7][4];
string mazeInit = "1110010110111011110111100001";
uint32_t xInd, yInd, currentVal;
xInd = yInd = currentVal = 0;
uint32_t maxX, maxY;
maxX = 6;
maxY = 4;
while (mazeInit.size() > 0) {
if (xInd > maxX) {
xInd = 0;
++yInd; //preincrement is fastr than postincrement
}
// populate your maze data Structure.
maze[xInd][yInd] = mazeNode();
maze[xInd][yInd].id = currentVal;
maze[xInd][yInd].val = boost::lexical_cast<uint32_t>(mazeInit.at(0));
mazeInit = mazeInit.substr(1);
++currentVal;
++xInd;
}
*/
/*
for (uint32_t x = 0; x < maxX; x++) {
for (uint32_t y = 0; y < maxY; y++) {
cout << boost::lexical_cast<string>(maze[x][y].id) << " => " << boost::lexical_cast<string>(maze[x][y].val) << endl;
}
}
vector<uint32_t> answer = solveMaze(maze, 27);
for (uint32_t& path: answer) {
cout << boost::lexical_cast<string>(path) << endl;
}
*/
/*
try{
derived * der = new derived();
throw der;
}
catch(Base * ) { cout << "caught pBase\n";}
catch(derived * ) { cout << "caught pDere\n";}
*/
/*
int i = 5;
for (int i =0; i < 10; i++) cout << i << endl;
cout << i << endl;
*/
/*
vector<int> numbers;
numbers.push_back(55);
numbers.push_back(37);
numbers.push_back(87);
numbers.push_back(1);
sort(numbers.begin(), numbers.end(), greater<int>());
for(int& number : numbers){
cout << number << endl;
}
*/
//foobar2 * fb3 = new foo;
/*
int i;
try { foo(); }
catch(double e) {i = 3;}
catch(int e) {i = 4;}
catch(bool e) {i = 5;}
cout << i << endl;
*/
/*A: reference to n is ambiguous
n = 5;
return 0;
*/
/*
Foo f1('a');
Foo f2('a' + 1);
Foo f3(1);
Foo f4(0x01);
*/
/*
//implement a dictionary of wordcounts for a given array of strings, to permit rapid lookup O(1) average of any query.
string words[] = {"cat", "dog", "bear", "cat", "beet", "dog", "beaver", "griffon", "dog"};
unordered_map<string, int> dict = createDict(words, 9);
for (auto& row: dict) {
cout << "word: " << row.first << " frequency: " << row.second << endl;
}
*/
// Given stock prices and ability to buy and sell one share at any one time, optimize returns. no commission requirements.
/*
vector<uint32_t> GOOG;
GOOG.push_back(55);
GOOG.push_back(58);
GOOG.push_back(60);
GOOG.push_back(55);
GOOG.push_back(58);
GOOG.push_back(60);
GOOG.push_back(55);
GOOG.push_back(58);
GOOG.push_back(60);
GOOG.push_back(65);
GOOG.push_back(85);
uint32_t currentPoint = 0;
uint32_t scoutPoint = 1;//scout point shoud always be ahead.
bool invested = false;
uint32_t profit = 0;
while (scoutPoint < GOOG.size()) {
if (!invested) {
if ( GOOG[scoutPoint] > GOOG[currentPoint] ) {
//BUY ORDER
currentPoint = scoutPoint-1;
cout << "buy at: " << boost::lexical_cast<string>(GOOG[currentPoint]);
invested = true;
scoutPoint++;
}
else {//price is dropping, do not buy because we are waiting for a buy price.
while (GOOG[scoutPoint] > GOOG[scoutPoint+1] && (scoutPoint + 1) < GOOG.size()) {
scoutPoint++;
//traverse the order until we see a viable place to by.
}
if (scoutPoint < GOOG.size() - 1 ) { // we have a valid buy point.
invested = true;
currentPoint = scoutPoint;
scoutPoint++;
cout << "buy at: " << boost::lexical_cast<string>(GOOG[currentPoint]);
}
}
}
else { //detect the maxima.
while ((scoutPoint + 1) < GOOG.size() && GOOG[scoutPoint] < GOOG[scoutPoint+1]) {
scoutPoint++;
}
profit += (GOOG[scoutPoint] - GOOG[currentPoint]);
cout << "sale at: " << GOOG[scoutPoint] << endl;
cout << "capgains earned this transaction: " << (GOOG[scoutPoint] - GOOG[currentPoint]) << endl;
currentPoint = scoutPoint;
scoutPoint++;
invested = false;
}
}
cout << "total profit earned this cycle: " << profit << endl;
*/
/*
PTNode head;
insertWord(head, "hello");
insertWord(head, "hell");
insertWord(head, "candy");
insertWord(head, "candidate");
traverseTree(head, "");
string cand = "aaaabbbeeeeetttt";
string comCand = compressString(cand);
string dCand = expandString(comCand);
cout << cand + " => " << comCand << " decompresses to => " << dCand << endl;
cand = "aaddaabbbebbghjeeeetttt";
comCand = compressString(cand);
dCand = expandString(comCand);
cout << cand + " => " << comCand << " decompresses to => " << dCand << endl;
*/
/*
// write a function that counts the number of trailing zeros in n-factorial
// yea I over though this, should have just used math from the beginning.
//first, lets calc. n!
uint32_t factCand = 10;
uint32_t fact = calcFact(factCand);
cout << "factorial of " << factCand << " is " << fact << endl;
uint32_t numZeros = 0;