adding cs142 code

This commit is contained in:
dm
2016-04-06 20:45:34 -07:00
parent 552d456d53
commit 8e52ce1982
174 changed files with 14064 additions and 0 deletions
+29
View File
@@ -0,0 +1,29 @@
CXX=g++
CPPFLAGS=-Wall -g -std=c++0x
SOURCES=main.cc util.cc
OBJECTS=$(SOURCES:.cc=.o)
EXE=app
all: $(EXE) test
main.o: main.cc util.o
util.o: util.h util.cc
unittest: util.o unittest.cc
$(EXE): $(OBJECTS)
$(CXX) $(LDFLAGS) $(OBJECTS) -o $@
clean:
@rm -vf *.o
@rm -rvf *.dSYM
@rm -vf $(EXE)
@rm -vf unittest
test: unittest
./unittest
debug: $(EXE)
gdb $(EXE)
valgrind: $(EXE)
valgrind --tool=memcheck --leak-check=yes ./$(EXE)
+22
View File
@@ -0,0 +1,22 @@
#include <string>
#include <iostream>
#include <cstdlib>
using namespace std;
#include "util.h"
const string usage = "usage: app <number of people> <step>";
int main(int argc, char * argv []) {
if(argc != 3) {
cerr << usage << endl;
return 1;
}
int number_of_people = atoi(argv[1]);
int step = atoi(argv[2]);
vector<bool> people(number_of_people, true);
vector<int> loosers = _play_game(people, step);
cout << loosers[loosers.size()-1] << endl;
return 0;
}
+27
View File
@@ -0,0 +1,27 @@
#include <string>
#include <cassert>
using namespace std;
#include "util.h"
int main(int argc, char * argv []) {
bool verbose = false;
if(argc > 1)
verbose = true;
assert(9 == _test_run(10, 1, verbose));
assert(3 == _test_run(10, 3, verbose));
assert(9 == _test_run(10, 12, verbose));
assert(19 == _test_run(20, 3, verbose));
assert(4 == _test_run(15, 7, verbose));
for(int i=0; i >= -1; i--) {
try {
_test_run(1500000, i);
}
catch(string e) {
assert(e == "rule violation: m > 0");
}
}
return 0;
}
+56
View File
@@ -0,0 +1,56 @@
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#include "util.h"
ostream & operator<<(ostream & os, vector<int> & v) {
os << "[";
for(unsigned int i = 0; i < v.size(); i++) {
os << v[i];
if(i != v.size() - 1) {
os << ", ";
}
}
os << "]";
return os;
}
int _increment_to_next_looser(const vector<bool> & players,
int start, const int step) {
int counter = 0;
// the test examples in the pdf have us start by killing the m-1th person
// offset by one for some dumb reason
int attempt = start - 1;
while(counter != step) {
attempt = (attempt + 1) % players.size();
if(players[attempt]) {
counter++;
}
}
return attempt;
}
vector<int> _play_game(vector<bool> & players, int step) {
vector<int> loosers;
int token = 0;
while(loosers.size() < players.size()) {
token = _increment_to_next_looser(players, token, step);
players[token] = false;
loosers.push_back(token);
}
return loosers;
}
int _test_run(int n, int m, bool verbose) {
if(m < 1) {
throw string("rule violation: m > 0");
}
vector<bool> players(n, true);
vector<int> loosers = _play_game(players, m);
if(verbose) {
cout << loosers << endl;
}
return loosers[loosers.size()-1];
}
+16
View File
@@ -0,0 +1,16 @@
#ifndef __UTIL_H__
#define __UTIL_H__
#include <vector>
#include <iostream>
using namespace std;
ostream & operator<<(ostream &, vector<bool> &);
vector<int> _play_game(vector<bool> & players, const int step);
int _increment_to_next_looser(const vector<bool> & players, int start, const int step);
int _test_run(int n, int m, bool verbose=false);
#endif