Skip to content

Commit

Permalink
Add new programs
Browse files Browse the repository at this point in the history
  • Loading branch information
metopa committed May 2, 2018
1 parent 007d350 commit 3a3133c
Show file tree
Hide file tree
Showing 44 changed files with 1,561 additions and 4 deletions.
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@
## [Beta 4] - 2018-05-??

### Added
- This changelog
- This changelog
- New programs
- Fibonacci
- KthLargestElementInLongArray
- MaxSumSubarray
- QueueWithMaxUsingDeque
- RangeLookupInBst
- ReverseLinkedListIterative
- TwoSortedArraysMerge

### Changed
- [IMPORTANT] Java project now requires at least Java 9 JDK
Expand Down
5 changes: 5 additions & 0 deletions epi_judge_cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ add_executable(enumerate_trees enumerate_trees.cc)
add_executable(evaluate_rpn evaluate_rpn.cc)
add_executable(even_odd_array even_odd_array.cc)
add_executable(even_odd_list_merge even_odd_list_merge.cc)
add_executable(fibonacci fibonacci.cc)
add_executable(find_salary_threshold find_salary_threshold.cc)
add_executable(first_missing_positive_entry first_missing_positive_entry.cc)
add_executable(gcd gcd.cc)
Expand Down Expand Up @@ -92,6 +93,7 @@ add_executable(k_closest_stars k_closest_stars.cc)
add_executable(k_largest_in_heap k_largest_in_heap.cc)
add_executable(k_largest_values_in_bst k_largest_values_in_bst.cc)
add_executable(knapsack knapsack.cc)
add_executable(kth_largest_element_in_long_array kth_largest_element_in_long_array.cc)
add_executable(kth_largest_element_in_two_sorted_arrays kth_largest_element_in_two_sorted_arrays.cc)
add_executable(kth_largest_in_array kth_largest_in_array.cc)
add_executable(kth_node_in_tree kth_node_in_tree.cc)
Expand Down Expand Up @@ -121,6 +123,7 @@ add_executable(max_product_all_but_one max_product_all_but_one.cc)
add_executable(max_safe_height max_safe_height.cc)
add_executable(max_square_submatrix max_square_submatrix.cc)
add_executable(max_submatrix max_submatrix.cc)
add_executable(max_sum_subarray max_sum_subarray.cc)
add_executable(max_teams_in_photograph max_teams_in_photograph.cc)
add_executable(max_trapped_water max_trapped_water.cc)
add_executable(max_water_trappable max_water_trappable.cc)
Expand Down Expand Up @@ -156,6 +159,7 @@ add_executable(queue_from_stacks queue_from_stacks.cc)
add_executable(queue_with_max queue_with_max.cc)
add_executable(random_permutation random_permutation.cc)
add_executable(random_subset random_subset.cc)
add_executable(range_lookup_in_bst range_lookup_in_bst.cc)
add_executable(real_square_root real_square_root.cc)
add_executable(rectangle_intersection rectangle_intersection.cc)
add_executable(refueling_schedule refueling_schedule.cc)
Expand Down Expand Up @@ -218,6 +222,7 @@ add_executable(tree_postorder tree_postorder.cc)
add_executable(tree_preorder tree_preorder.cc)
add_executable(tree_right_sibling tree_right_sibling.cc)
add_executable(tree_with_parent_inorder tree_with_parent_inorder.cc)
add_executable(two_sorted_arrays_merge two_sorted_arrays_merge.cc)
add_executable(two_sum two_sum.cc)
add_executable(uniform_random_number uniform_random_number.cc)
add_executable(valid_ip_addresses valid_ip_addresses.cc)
Expand Down
55 changes: 55 additions & 0 deletions epi_judge_cpp/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,16 @@ even_odd_list_merge: $(BUILD_DIR)/even_odd_list_merge
.PHONY: even_odd_list_merge


# fibonacci.cc

$(BUILD_DIR)/fibonacci: $(SRC_DIR)/fibonacci.cc | $(BUILD_DIR)
$(CXX_COMPILER) $(CXX_FLAGS_DEBUG) -o $@ $<
fibonacci: $(BUILD_DIR)/fibonacci
$< $(PROGRAM_ARGS)

.PHONY: fibonacci


# find_salary_threshold.cc

$(BUILD_DIR)/find_salary_threshold: $(SRC_DIR)/find_salary_threshold.cc | $(BUILD_DIR)
Expand Down Expand Up @@ -831,6 +841,16 @@ knapsack: $(BUILD_DIR)/knapsack
.PHONY: knapsack


# kth_largest_element_in_long_array.cc

$(BUILD_DIR)/kth_largest_element_in_long_array: $(SRC_DIR)/kth_largest_element_in_long_array.cc | $(BUILD_DIR)
$(CXX_COMPILER) $(CXX_FLAGS_DEBUG) -o $@ $<
kth_largest_element_in_long_array: $(BUILD_DIR)/kth_largest_element_in_long_array
$< $(PROGRAM_ARGS)

.PHONY: kth_largest_element_in_long_array


# kth_largest_element_in_two_sorted_arrays.cc

$(BUILD_DIR)/kth_largest_element_in_two_sorted_arrays: $(SRC_DIR)/kth_largest_element_in_two_sorted_arrays.cc | $(BUILD_DIR)
Expand Down Expand Up @@ -1121,6 +1141,16 @@ max_submatrix: $(BUILD_DIR)/max_submatrix
.PHONY: max_submatrix


# max_sum_subarray.cc

$(BUILD_DIR)/max_sum_subarray: $(SRC_DIR)/max_sum_subarray.cc | $(BUILD_DIR)
$(CXX_COMPILER) $(CXX_FLAGS_DEBUG) -o $@ $<
max_sum_subarray: $(BUILD_DIR)/max_sum_subarray
$< $(PROGRAM_ARGS)

.PHONY: max_sum_subarray


# max_teams_in_photograph.cc

$(BUILD_DIR)/max_teams_in_photograph: $(SRC_DIR)/max_teams_in_photograph.cc | $(BUILD_DIR)
Expand Down Expand Up @@ -1471,6 +1501,16 @@ random_subset: $(BUILD_DIR)/random_subset
.PHONY: random_subset


# range_lookup_in_bst.cc

$(BUILD_DIR)/range_lookup_in_bst: $(SRC_DIR)/range_lookup_in_bst.cc | $(BUILD_DIR)
$(CXX_COMPILER) $(CXX_FLAGS_DEBUG) -o $@ $<
range_lookup_in_bst: $(BUILD_DIR)/range_lookup_in_bst
$< $(PROGRAM_ARGS)

.PHONY: range_lookup_in_bst


# real_square_root.cc

$(BUILD_DIR)/real_square_root: $(SRC_DIR)/real_square_root.cc | $(BUILD_DIR)
Expand Down Expand Up @@ -2091,6 +2131,16 @@ tree_with_parent_inorder: $(BUILD_DIR)/tree_with_parent_inorder
.PHONY: tree_with_parent_inorder


# two_sorted_arrays_merge.cc

$(BUILD_DIR)/two_sorted_arrays_merge: $(SRC_DIR)/two_sorted_arrays_merge.cc | $(BUILD_DIR)
$(CXX_COMPILER) $(CXX_FLAGS_DEBUG) -o $@ $<
two_sorted_arrays_merge: $(BUILD_DIR)/two_sorted_arrays_merge
$< $(PROGRAM_ARGS)

.PHONY: two_sorted_arrays_merge


# two_sum.cc

$(BUILD_DIR)/two_sum: $(SRC_DIR)/two_sum.cc | $(BUILD_DIR)
Expand Down Expand Up @@ -2175,6 +2225,7 @@ all: \
$(BUILD_DIR)/evaluate_rpn \
$(BUILD_DIR)/even_odd_array \
$(BUILD_DIR)/even_odd_list_merge \
$(BUILD_DIR)/fibonacci \
$(BUILD_DIR)/find_salary_threshold \
$(BUILD_DIR)/first_missing_positive_entry \
$(BUILD_DIR)/gcd \
Expand Down Expand Up @@ -2212,6 +2263,7 @@ all: \
$(BUILD_DIR)/k_largest_in_heap \
$(BUILD_DIR)/k_largest_values_in_bst \
$(BUILD_DIR)/knapsack \
$(BUILD_DIR)/kth_largest_element_in_long_array \
$(BUILD_DIR)/kth_largest_element_in_two_sorted_arrays \
$(BUILD_DIR)/kth_largest_in_array \
$(BUILD_DIR)/kth_node_in_tree \
Expand Down Expand Up @@ -2241,6 +2293,7 @@ all: \
$(BUILD_DIR)/max_safe_height \
$(BUILD_DIR)/max_square_submatrix \
$(BUILD_DIR)/max_submatrix \
$(BUILD_DIR)/max_sum_subarray \
$(BUILD_DIR)/max_teams_in_photograph \
$(BUILD_DIR)/max_trapped_water \
$(BUILD_DIR)/max_water_trappable \
Expand Down Expand Up @@ -2276,6 +2329,7 @@ all: \
$(BUILD_DIR)/queue_with_max \
$(BUILD_DIR)/random_permutation \
$(BUILD_DIR)/random_subset \
$(BUILD_DIR)/range_lookup_in_bst \
$(BUILD_DIR)/real_square_root \
$(BUILD_DIR)/rectangle_intersection \
$(BUILD_DIR)/refueling_schedule \
Expand Down Expand Up @@ -2338,6 +2392,7 @@ all: \
$(BUILD_DIR)/tree_preorder \
$(BUILD_DIR)/tree_right_sibling \
$(BUILD_DIR)/tree_with_parent_inorder \
$(BUILD_DIR)/two_sorted_arrays_merge \
$(BUILD_DIR)/two_sum \
$(BUILD_DIR)/uniform_random_number \
$(BUILD_DIR)/valid_ip_addresses \
Expand Down
13 changes: 13 additions & 0 deletions epi_judge_cpp/fibonacci.cc
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);
}
17 changes: 17 additions & 0 deletions epi_judge_cpp/max_sum_subarray.cc
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);
}
117 changes: 117 additions & 0 deletions epi_judge_cpp/queue_with_max_using_deque.cc
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);
}
41 changes: 41 additions & 0 deletions epi_judge_cpp/range_lookup_in_bst.cc
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);
}
Loading

0 comments on commit 3a3133c

Please sign in to comment.