Skip to content

Commit

Permalink
Sync epi_judge_solutions
Browse files Browse the repository at this point in the history
Lots of changes that were not to be seen public
Co-authored-by: Tsung-Hsien Lee <[email protected]>
Co-authored-by: adnanaziz <[email protected]>
  • Loading branch information
metopa committed Aug 28, 2019
1 parent 54a3276 commit 92f55af
Show file tree
Hide file tree
Showing 146 changed files with 18,350 additions and 2,172 deletions.
6 changes: 5 additions & 1 deletion epi_judge_cpp_solutions/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ add_executable(element_appearing_once element_appearing_once.cc)
add_executable(enumerate_balanced_parentheses enumerate_balanced_parentheses.cc)
add_executable(enumerate_palindromic_decompositions enumerate_palindromic_decompositions.cc)
add_executable(enumerate_trees enumerate_trees.cc)
add_executable(euclidean_gcd euclidean_gcd.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)
Expand Down Expand Up @@ -81,6 +82,7 @@ add_executable(is_list_palindromic is_list_palindromic.cc)
add_executable(is_number_palindromic is_number_palindromic.cc)
add_executable(is_string_decomposable_into_words is_string_decomposable_into_words.cc)
add_executable(is_string_in_matrix is_string_in_matrix.cc)
add_executable(is_string_palindromic is_string_palindromic.cc)
add_executable(is_string_palindromic_punctuation is_string_palindromic_punctuation.cc)
add_executable(is_string_permutable_to_palindrome is_string_permutable_to_palindrome.cc)
add_executable(is_tree_a_bst is_tree_a_bst.cc)
Expand Down Expand Up @@ -114,6 +116,7 @@ add_executable(lowest_common_ancestor_in_bst lowest_common_ancestor_in_bst.cc)
add_executable(lowest_common_ancestor_with_parent lowest_common_ancestor_with_parent.cc)
add_executable(lru_cache lru_cache.cc)
add_executable(majority_element majority_element.cc)
add_executable(making_change making_change.cc)
add_executable(matrix_connected_regions matrix_connected_regions.cc)
add_executable(matrix_enclosed_regions matrix_enclosed_regions.cc)
add_executable(matrix_rotation matrix_rotation.cc)
Expand Down Expand Up @@ -181,6 +184,7 @@ add_executable(search_first_key search_first_key.cc)
add_executable(search_for_min_max_in_array search_for_min_max_in_array.cc)
add_executable(search_for_missing_element search_for_missing_element.cc)
add_executable(search_frequent_items search_frequent_items.cc)
add_executable(search_in_bst search_in_bst.cc)
add_executable(search_in_list search_in_list.cc)
add_executable(search_maze search_maze.cc)
add_executable(search_row_col_sorted_matrix search_row_col_sorted_matrix.cc)
Expand All @@ -197,7 +201,7 @@ add_executable(sorted_array_remove_dups sorted_array_remove_dups.cc)
add_executable(sorted_arrays_merge sorted_arrays_merge.cc)
add_executable(sorted_list_to_bst sorted_list_to_bst.cc)
add_executable(sorted_lists_merge sorted_lists_merge.cc)
add_executable(spiral_ordering_segments spiral_ordering_segments.cc)
add_executable(spiral_ordering spiral_ordering.cc)
add_executable(spreadsheet_encoding spreadsheet_encoding.cc)
add_executable(stack_with_max stack_with_max.cc)
add_executable(string_decompositions_into_dictionary_words string_decompositions_into_dictionary_words.cc)
Expand Down
56 changes: 50 additions & 6 deletions epi_judge_cpp_solutions/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ SRC_DIR := .
TEST_DATA_DIR := ../test_data

# Modify this line if you want to pass custom arguments
PROGRAM_ARGS := --test-data-dir $(TEST_DATA_DIR)
PROGRAM_ARGS := --test-data-dir $(TEST_DATA_DIR)

CXX_COMPILER := g++
CXX_FLAGS := -std=c++17 -pthread -Werror -Wno-unused-variable -Wno-sign-compare
Expand Down Expand Up @@ -431,6 +431,16 @@ enumerate_trees: $(BUILD_DIR)/enumerate_trees
.PHONY: enumerate_trees


# euclidean_gcd.cc

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

.PHONY: euclidean_gcd


# evaluate_rpn.cc

$(BUILD_DIR)/evaluate_rpn: $(SRC_DIR)/evaluate_rpn.cc | $(BUILD_DIR)
Expand Down Expand Up @@ -731,6 +741,16 @@ is_string_in_matrix: $(BUILD_DIR)/is_string_in_matrix
.PHONY: is_string_in_matrix


# is_string_palindromic.cc

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

.PHONY: is_string_palindromic


# is_string_palindromic_punctuation.cc

$(BUILD_DIR)/is_string_palindromic_punctuation: $(SRC_DIR)/is_string_palindromic_punctuation.cc | $(BUILD_DIR)
Expand Down Expand Up @@ -1061,6 +1081,16 @@ majority_element: $(BUILD_DIR)/majority_element
.PHONY: majority_element


# making_change.cc

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

.PHONY: making_change


# matrix_connected_regions.cc

$(BUILD_DIR)/matrix_connected_regions: $(SRC_DIR)/matrix_connected_regions.cc | $(BUILD_DIR)
Expand Down Expand Up @@ -1731,6 +1761,16 @@ search_frequent_items: $(BUILD_DIR)/search_frequent_items
.PHONY: search_frequent_items


# search_in_bst.cc

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

.PHONY: search_in_bst


# search_in_list.cc

$(BUILD_DIR)/search_in_list: $(SRC_DIR)/search_in_list.cc | $(BUILD_DIR)
Expand Down Expand Up @@ -1891,14 +1931,14 @@ sorted_lists_merge: $(BUILD_DIR)/sorted_lists_merge
.PHONY: sorted_lists_merge


# spiral_ordering_segments.cc
# spiral_ordering.cc

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

.PHONY: spiral_ordering_segments
.PHONY: spiral_ordering


# spreadsheet_encoding.cc
Expand Down Expand Up @@ -2222,6 +2262,7 @@ all: \
$(BUILD_DIR)/enumerate_balanced_parentheses \
$(BUILD_DIR)/enumerate_palindromic_decompositions \
$(BUILD_DIR)/enumerate_trees \
$(BUILD_DIR)/euclidean_gcd \
$(BUILD_DIR)/evaluate_rpn \
$(BUILD_DIR)/even_odd_array \
$(BUILD_DIR)/even_odd_list_merge \
Expand Down Expand Up @@ -2252,6 +2293,7 @@ all: \
$(BUILD_DIR)/is_number_palindromic \
$(BUILD_DIR)/is_string_decomposable_into_words \
$(BUILD_DIR)/is_string_in_matrix \
$(BUILD_DIR)/is_string_palindromic \
$(BUILD_DIR)/is_string_palindromic_punctuation \
$(BUILD_DIR)/is_string_permutable_to_palindrome \
$(BUILD_DIR)/is_tree_a_bst \
Expand Down Expand Up @@ -2285,6 +2327,7 @@ all: \
$(BUILD_DIR)/lowest_common_ancestor_with_parent \
$(BUILD_DIR)/lru_cache \
$(BUILD_DIR)/majority_element \
$(BUILD_DIR)/making_change \
$(BUILD_DIR)/matrix_connected_regions \
$(BUILD_DIR)/matrix_enclosed_regions \
$(BUILD_DIR)/matrix_rotation \
Expand Down Expand Up @@ -2352,6 +2395,7 @@ all: \
$(BUILD_DIR)/search_for_min_max_in_array \
$(BUILD_DIR)/search_for_missing_element \
$(BUILD_DIR)/search_frequent_items \
$(BUILD_DIR)/search_in_bst \
$(BUILD_DIR)/search_in_list \
$(BUILD_DIR)/search_maze \
$(BUILD_DIR)/search_row_col_sorted_matrix \
Expand All @@ -2368,7 +2412,7 @@ all: \
$(BUILD_DIR)/sorted_arrays_merge \
$(BUILD_DIR)/sorted_list_to_bst \
$(BUILD_DIR)/sorted_lists_merge \
$(BUILD_DIR)/spiral_ordering_segments \
$(BUILD_DIR)/spiral_ordering \
$(BUILD_DIR)/spreadsheet_encoding \
$(BUILD_DIR)/stack_with_max \
$(BUILD_DIR)/string_decompositions_into_dictionary_words \
Expand Down
2 changes: 1 addition & 1 deletion epi_judge_cpp_solutions/absent_value_array.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ int FindMissingElement(vector<int>::const_iterator stream_begin,

// Look for a bucket that contains less than (1 << 16) elements.
const int kBucketCapacity = 1 << 16;
int candidate_bucket;
int candidate_bucket = 0;
for (int i = 0; i < kNumBucket; ++i) {
if (counter[i] < kBucketCapacity) {
candidate_bucket = i;
Expand Down
23 changes: 15 additions & 8 deletions epi_judge_cpp_solutions/adding_credits.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "test_framework/fmt_print.h"
#include "test_framework/generic_test.h"
#include "test_framework/test_config.h"
#include "test_framework/test_failure.h"

using std::map;
Expand Down Expand Up @@ -72,10 +73,11 @@ std::ostream& operator<<(std::ostream& out, const Operation& op) {
return out << FmtStr("{}({}, {})", op.op, op.s_arg, op.i_arg);
}

namespace test_framework {
template <>
struct SerializationTraits<Operation>
: UserSerTraits<Operation, std::string, std::string, int> {};

struct SerializationTrait<Operation>
: UserSerTrait<Operation, std::string, std::string, int> {};
} // namespace test_framework
void ClientsCreditsInfoTester(const std::vector<Operation>& ops) {
ClientsCreditsInfo cr;
int op_idx = 0;
Expand Down Expand Up @@ -116,10 +118,15 @@ void ClientsCreditsInfoTester(const std::vector<Operation>& ops) {
}
}

void ProgramConfig(TestConfig& config) { config.analyze_complexity = false; }

// clang-format off


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, "adding_credits.cc", "adding_credits.tsv",
&ClientsCreditsInfoTester, DefaultComparator{},
param_names);
std::vector<std::string> args {argv + 1, argv + argc};
std::vector<std::string> param_names {"ops"};
return GenericTestMain(args, "adding_credits.cc", "adding_credits.tsv", &ClientsCreditsInfoTester,
DefaultComparator{}, param_names, &ProgramConfig);
}
// clang-format on
3 changes: 2 additions & 1 deletion epi_judge_cpp_solutions/anagrams.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ vector<vector<string>> FindAnagrams(const vector<string>& dictionary) {
int main(int argc, char* argv[]) {
std::vector<std::string> args {argv + 1, argv + argc};
std::vector<std::string> param_names {"dictionary"};
return GenericTestMain(args, "anagrams.cc", "anagrams.tsv", &FindAnagrams, &UnorderedComparator<std::vector<std::vector<std::string>>>, param_names);
return GenericTestMain(args, "anagrams.cc", "anagrams.tsv", &FindAnagrams,
UnorderedComparator{}, param_names);
}
// clang-format on
24 changes: 7 additions & 17 deletions epi_judge_cpp_solutions/apply_permutation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,18 @@
using std::swap;
using std::vector;

void ApplyPermutation(vector<int>* perm_ptr, vector<int>* A_ptr) {
vector<int>&perm = *perm_ptr, &A = *A_ptr;
void ApplyPermutation(vector<int> perm, vector<int>* A_ptr) {
vector<int>& A = *A_ptr;
for (int i = 0; i < size(A); ++i) {
// Check if the element at index i has not been moved by checking if
// perm[i] is nonnegative.
int next = i;
while (perm[next] >= 0) {
swap(A[i], A[perm[next]]);
int temp = perm[next];
// Subtracts size(perm) from an entry in perm to make it negative,
// which indicates the corresponding move has been performed.
perm[next] -= size(perm);
next = temp;
while (perm[i] != i) {
swap(A[i], A[perm[i]]);
swap(perm[i], perm[perm[i]]);
}
}

// Restore perm.
for_each(begin(perm), end(perm), [&perm](int& x) { x += size(perm); });
}

vector<int> ApplyPermutationWrapper(vector<int> perm, vector<int> A) {
ApplyPermutation(&perm, &A);
vector<int> ApplyPermutationWrapper(const vector<int>& perm, vector<int> A) {
ApplyPermutation(perm, &A);
return A;
}

Expand Down
3 changes: 2 additions & 1 deletion epi_judge_cpp_solutions/binomial_coefficients.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ int ComputeBinomialCoefficientsSpaceEfficient(int n, int k) {
int main(int argc, char* argv[]) {
std::vector<std::string> args {argv + 1, argv + argc};
std::vector<std::string> param_names {"n", "k"};
return GenericTestMain(args, "binomial_coefficients.cc", "binomial_coefficients.tsv", &ComputeBinomialCoefficient, DefaultComparator{}, param_names);
return GenericTestMain(args, "binomial_coefficients.cc", "binomial_coefficients.tsv", &ComputeBinomialCoefficient,
DefaultComparator{}, param_names);
}
// clang-format on
9 changes: 5 additions & 4 deletions epi_judge_cpp_solutions/bst_from_sorted_array.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ unique_ptr<BstNode<int>> BuildMinHeightBSTFromSortedSubarray(

int BuildMinHeightBSTFromSortedArrayWrapper(TimedExecutor& executor,
const vector<int>& A) {
unique_ptr<BstNode<int>> result =
auto result =
executor.Run([&] { return BuildMinHeightBSTFromSortedArray(A); });

if (GenerateInorder(result) != A) {
if (test_framework::GenerateInorder(result) != A) {
throw TestFailure("Result binary tree mismatches input array");
}
return BinaryTreeHeight(result);
return test_framework::BinaryTreeHeight(result);
}

// clang-format off
Expand All @@ -48,6 +48,7 @@ int BuildMinHeightBSTFromSortedArrayWrapper(TimedExecutor& executor,
int main(int argc, char* argv[]) {
std::vector<std::string> args {argv + 1, argv + argc};
std::vector<std::string> param_names {"executor", "A"};
return GenericTestMain(args, "bst_from_sorted_array.cc", "bst_from_sorted_array.tsv", &BuildMinHeightBSTFromSortedArrayWrapper, DefaultComparator{}, param_names);
return GenericTestMain(args, "bst_from_sorted_array.cc", "bst_from_sorted_array.tsv", &BuildMinHeightBSTFromSortedArrayWrapper,
DefaultComparator{}, param_names);
}
// clang-format on
3 changes: 2 additions & 1 deletion epi_judge_cpp_solutions/bst_to_sorted_list.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ std::vector<int> BSTToDoublyLinkedListWrapper(
int main(int argc, char* argv[]) {
std::vector<std::string> args {argv + 1, argv + argc};
std::vector<std::string> param_names {"executor", "tree"};
return GenericTestMain(args, "bst_to_sorted_list.cc", "bst_to_sorted_list.tsv", &BSTToDoublyLinkedListWrapper, DefaultComparator{}, param_names);
return GenericTestMain(args, "bst_to_sorted_list.cc", "bst_to_sorted_list.tsv", &BSTToDoublyLinkedListWrapper,
DefaultComparator{}, param_names);
}
// clang-format on
2 changes: 1 addition & 1 deletion epi_judge_cpp_solutions/buy_and_sell_stock_k_times.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ using std::vector;
double UnlimitedPairsProfits(const vector<double> &);

double BuyAndSellStockKTimes(const vector<double> &prices, int k) {
if (!k) {
if (k == 0) {
return 0.0;
} else if (2 * k >= size(prices)) {
return UnlimitedPairsProfits(prices);
Expand Down
4 changes: 3 additions & 1 deletion epi_judge_cpp_solutions/calendar_rendering.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ int FindMaxSimultaneousEvents(const vector<Event>& A) {
return max_num_simultaneous_events;
}

namespace test_framework {
template <>
struct SerializationTraits<Event> : UserSerTraits<Event, int, int> {};
struct SerializationTrait<Event> : UserSerTrait<Event, int, int> {};
} // namespace test_framework

int main(int argc, char* argv[]) {
std::vector<std::string> args{argv + 1, argv + argc};
Expand Down
23 changes: 14 additions & 9 deletions epi_judge_cpp_solutions/circular_queue.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "test_framework/generic_test.h"
#include "test_framework/serialization_traits.h"
#include "test_framework/test_config.h"
#include "test_framework/test_failure.h"

using std::length_error;
Expand All @@ -30,9 +31,6 @@ class Queue {
}

int Dequeue() {
if (!num_queue_elements) {
throw length_error("empty queue");
}
--num_queue_elements;
int result = entries_[head_];
head_ = (head_ + 1) % size(entries_);
Expand Down Expand Up @@ -93,9 +91,10 @@ struct QueueOp {
}
};

namespace test_framework {
template <>
struct SerializationTraits<QueueOp> : UserSerTraits<QueueOp, std::string, int> {
};
struct SerializationTrait<QueueOp> : UserSerTrait<QueueOp, std::string, int> {};
} // namespace test_framework

void QueueTester(const std::vector<QueueOp>& ops) {
Queue q(0);
Expand All @@ -104,9 +103,15 @@ void QueueTester(const std::vector<QueueOp>& ops) {
}
}

void ProgramConfig(TestConfig& config) { config.analyze_complexity = false; }

// clang-format off


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, "circular_queue.cc", "circular_queue.tsv",
&QueueTester, DefaultComparator{}, param_names);
std::vector<std::string> args {argv + 1, argv + argc};
std::vector<std::string> param_names {"ops"};
return GenericTestMain(args, "circular_queue.cc", "circular_queue.tsv", &QueueTester,
DefaultComparator{}, param_names, &ProgramConfig);
}
// clang-format on
Loading

0 comments on commit 92f55af

Please sign in to comment.