forked from adnanaziz/EPIJudge
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
44 changed files
with
1,561 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#include "test_framework/generic_test.h" | ||
|
||
int Fibonacci(int n) { | ||
// TODO - you fill in here. | ||
return -1; | ||
} | ||
|
||
int main(int argc, char* argv[]) { | ||
std::vector<std::string> args{argv + 1, argv + argc}; | ||
std::vector<std::string> param_names{"n"}; | ||
return GenericTestMain(args, "fibonacci.cc", "fibonacci.tsv", &Fibonacci, | ||
DefaultComparator{}, param_names); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#include <vector> | ||
#include "test_framework/generic_test.h" | ||
|
||
using std::vector; | ||
|
||
int FindMaximumSubarray(const vector<int>& A) { | ||
// TODO - you fill in here. | ||
return -1; | ||
} | ||
|
||
int main(int argc, char* argv[]) { | ||
std::vector<std::string> args{argv + 1, argv + argc}; | ||
std::vector<std::string> param_names{"A"}; | ||
return GenericTestMain(args, "max_sum_subarray.cc", "max_sum_subarray.tsv", | ||
&FindMaximumSubarray, DefaultComparator{}, | ||
param_names); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
|
||
#include <algorithm> | ||
#include <deque> | ||
#include <queue> | ||
#include <stdexcept> | ||
#include <string> | ||
|
||
#include "test_framework/generic_test.h" | ||
#include "test_framework/serialization_traits.h" | ||
#include "test_framework/test_failure.h" | ||
|
||
using std::deque; | ||
using std::length_error; | ||
using std::queue; | ||
|
||
template <typename T> | ||
class QueueWithMax { | ||
public: | ||
void Enqueue(const T& x) { | ||
entries_.emplace(x); | ||
// Eliminate dominated elements in candidates_for_max_. | ||
while (!empty(candidates_for_max_) && candidates_for_max_.back() < x) { | ||
candidates_for_max_.pop_back(); | ||
} | ||
candidates_for_max_.emplace_back(x); | ||
} | ||
|
||
T Dequeue() { | ||
if (!empty(entries_)) { | ||
T result = entries_.front(); | ||
if (result == candidates_for_max_.front()) { | ||
candidates_for_max_.pop_front(); | ||
} | ||
entries_.pop(); | ||
return result; | ||
} | ||
throw length_error("empty queue"); | ||
} | ||
|
||
const T& Max() const { | ||
if (!empty(candidates_for_max_)) { | ||
return candidates_for_max_.front(); | ||
} | ||
throw length_error("empty queue"); | ||
} | ||
|
||
T& Head() { return entries_.front(); } | ||
|
||
const T& Head() const { return entries_.front(); } | ||
|
||
private: | ||
queue<T> entries_; | ||
deque<T> candidates_for_max_; | ||
}; | ||
|
||
struct QueueOp { | ||
enum { kConstruct, kDequeue, kEnqueue, kMax } op; | ||
int argument; | ||
|
||
QueueOp(const std::string& op_string, int arg) : argument(arg) { | ||
if (op_string == "QueueWithMax") { | ||
op = kConstruct; | ||
} else if (op_string == "dequeue") { | ||
op = kDequeue; | ||
} else if (op_string == "enqueue") { | ||
op = kEnqueue; | ||
} else if (op_string == "max") { | ||
op = kMax; | ||
} else { | ||
throw std::runtime_error("Unsupported queue operation: " + op_string); | ||
} | ||
} | ||
}; | ||
|
||
template <> | ||
struct SerializationTraits<QueueOp> : UserSerTraits<QueueOp, std::string, int> { | ||
}; | ||
|
||
void QueueTester(const std::vector<QueueOp>& ops) { | ||
try { | ||
QueueWithMax<int> q; | ||
for (auto& x : ops) { | ||
switch (x.op) { | ||
case QueueOp::kConstruct: | ||
break; | ||
case QueueOp::kDequeue: { | ||
int result = q.Dequeue(); | ||
if (result != x.argument) { | ||
throw TestFailure("Dequeue: expected " + | ||
std::to_string(x.argument) + ", got " + | ||
std::to_string(result)); | ||
} | ||
} break; | ||
case QueueOp::kEnqueue: | ||
q.Enqueue(x.argument); | ||
break; | ||
case QueueOp::kMax: { | ||
int s = q.Max(); | ||
if (s != x.argument) { | ||
throw TestFailure("Max: expected " + std::to_string(x.argument) + | ||
", got " + std::to_string(s)); | ||
} | ||
} break; | ||
} | ||
} | ||
} catch (length_error&) { | ||
throw TestFailure("Unexpected length_error exception"); | ||
} | ||
} | ||
|
||
int main(int argc, char* argv[]) { | ||
std::vector<std::string> args{argv + 1, argv + argc}; | ||
std::vector<std::string> param_names{"ops"}; | ||
return GenericTestMain(args, "queue_with_max_using_deque.cc", | ||
"queue_with_max.tsv", &QueueTester, | ||
DefaultComparator{}, param_names); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include <memory> | ||
#include <vector> | ||
|
||
#include "bst_node.h" | ||
#include "test_framework/generic_test.h" | ||
#include "test_framework/serialization_traits.h" | ||
|
||
using std::unique_ptr; | ||
using std::vector; | ||
|
||
struct Interval { | ||
int left, right; | ||
}; | ||
|
||
vector<int> RangeLookupInBST(const unique_ptr<BstNode<int>>& tree, | ||
const Interval& interval) { | ||
// TODO - you fill in here. | ||
return {}; | ||
} | ||
|
||
void RangeLookupInBSTHelper(const unique_ptr<BstNode<int>>& tree, | ||
const Interval& interval, vector<int>* result) {} | ||
|
||
template <> | ||
struct SerializationTraits<Interval> : UserSerTraits<Interval, int, int> { | ||
static std::vector<std::string> GetMetricNames(const std::string& arg_name) { | ||
return {FmtStr("length({})", arg_name)}; | ||
} | ||
|
||
static std::vector<int> GetMetrics(const Interval& x) { | ||
return {x.right - x.left}; | ||
} | ||
}; | ||
|
||
int main(int argc, char* argv[]) { | ||
std::vector<std::string> args{argv + 1, argv + argc}; | ||
std::vector<std::string> param_names{"tree", "interval"}; | ||
return GenericTestMain(args, "range_lookup_in_bst.cc", | ||
"range_lookup_in_bst.tsv", &RangeLookupInBST, | ||
DefaultComparator{}, param_names); | ||
} |
Oops, something went wrong.