Skip to content

Commit

Permalink
Misc code fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
metopa committed Nov 13, 2019
1 parent 0ea8881 commit d1b3822
Show file tree
Hide file tree
Showing 162 changed files with 16,273 additions and 21,246 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ __pycache__/
cpp_build

epi_judge_java/java_build/
*.class

# OS X specific files
.DS_Store
7 changes: 1 addition & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,20 @@
- Run each test case with a timeout


## [Beta 5] - 2019-07-??
## [Beta 5] - 2018-??-??

### Added
- Automatically determine solution asymptotic complexity
- Please, let us know if complexity analyzer produces strange results
- Some types of problems (like graph problems) are not yet supported
- Namespace for C++ test framework
- Python type hints
- New programs
- Euclidean GCD
- Is string palindromic
- Making change

### Changed
- Changed test data format
- Now every field is encoded in pure JSON format

### Fixed
- Fixed various naming issues

### Removed

Expand Down
5 changes: 5 additions & 0 deletions epi_judge_cpp/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# Compile and run by "make <program_name>"
# For example, typing "make parity" in your terminal will compile and run parity.cc.
#
# You could try "make last" (or simply "make"), this command will compile and run the last modified file.
# This comes pretty handy while you are working a problem, and then you want to see the result soon without thinking program name.
BUILD_DIR := cpp_build
SRC_DIR := .
TEST_DATA_DIR := ../test_data
Expand All @@ -13,6 +17,7 @@ CXX_FLAGS_DEBUG := $(CXX_FLAGS) -g -O0
LAST_MODIFIED_CXX_FILE = $(shell ls -rt $(SRC_DIR)/*\.cc | tail -1)


# Compile and run the last modified file.
last: $(notdir $(basename $(LAST_MODIFIED_CXX_FILE)))

.PHONY: last
Expand Down
9 changes: 6 additions & 3 deletions epi_judge_cpp/absent_value_array.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ int FindMissingElement(vector<int>::const_iterator stream_begin,
// TODO - you fill in here.
return 0;
}
int FindMissingElementWrapper(const vector<int>& stream) {
void FindMissingElementWrapper(const vector<int>& stream) {
try {
return FindMissingElement(cbegin(stream), cend(stream));
int res = FindMissingElement(cbegin(stream), cend(stream));
if (std::find(stream.begin(), stream.end(), res) != stream.end()) {
throw TestFailure(std::to_string(res) + " appears in stream");
}
} catch (invalid_argument&) {
throw TestFailure("Unexpected no_missing_element exception");
throw TestFailure("Unexpected no missing element exception");
}
}

Expand Down
2 changes: 1 addition & 1 deletion epi_judge_cpp/drawing_skyline.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ using std::vector;
struct Rect {
int left, right, height;
};
typedef vector<Rect> Skyline;
using Skyline = vector<Rect>;

Skyline ComputeSkyline(const vector<Rect>& buildings) {
// TODO - you fill in here.
Expand Down
2 changes: 1 addition & 1 deletion epi_judge_cpp/dutch_national_flag.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "test_framework/test_failure.h"
#include "test_framework/timed_executor.h"
using std::vector;
typedef enum { kRed, kWhite, kBlue } Color;
using Color = enum { kRed, kWhite, kBlue };

void DutchFlagPartition(int pivot_index, vector<Color>* A_ptr) {
// TODO - you fill in here.
Expand Down
2 changes: 1 addition & 1 deletion epi_judge_cpp/search_maze.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "test_framework/test_failure.h"
#include "test_framework/timed_executor.h"
using std::vector;
typedef enum { kWhite, kBlack } Color;
using Color = enum { kWhite, kBlack };
struct Coordinate {
bool operator==(const Coordinate& that) const {
return x == that.x && y == that.y;
Expand Down
2 changes: 1 addition & 1 deletion epi_judge_cpp/string_integer_interconversion.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ int StringToInt(const string& s) {
return 0;
}
void Wrapper(int x, const string& s) {
if (IntToString(x) != s) {
if (stoi(IntToString(x)) != x) {
throw TestFailure("Int to string conversion failed");
}

Expand Down
1 change: 0 additions & 1 deletion epi_judge_cpp/string_transformability.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using std::string;
using std::unordered_set;

// Uses BFS to find the least steps of transformation.
int TransformString(unordered_set<string> D, const string& s, const string& t) {
// TODO - you fill in here.
return 0;
Expand Down
10 changes: 6 additions & 4 deletions epi_judge_cpp/test_framework/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -3570,9 +3570,9 @@ class parser {
JSON_THROW(parse_error::create(101, m_lexer.get_position(), error_msg));
}

private :
/// current level of recursion
int depth = 0;
private:
/// current level of recursion
int depth = 0;
/// callback function
const parser_callback_t callback = nullptr;
/// the type of the last read token
Expand Down Expand Up @@ -9964,7 +9964,9 @@ class basic_json {
break;
}

default: { break; }
default: {
break;
}
}
}
};
Expand Down
28 changes: 13 additions & 15 deletions epi_judge_cpp/test_framework/test_complexity.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,29 +55,27 @@ double Factorial(int n) {
}

// Internal function to calculate the different scalability forms.
std::function<double(const double&)> FittingCurve(BigO complexity) {
std::function<double(double)> FittingCurve(BigO complexity) {
switch (complexity) {
case N:
return [](const double& n) -> double { return n; };
return [](double n) -> double { return n; };
case N_SQUARED:
return [](const double& n) -> double { return n * n; };
return [](double n) -> double { return n * n; };
case N_CUBED:
return [](const double& n) -> double { return n * n * n; };
return [](double n) -> double { return n * n * n; };
case LOGN:
return [](const double& n) { return std::log2(n); };
return [](double n) { return std::log2(n); };
case N_LOGN:
return [](const double& n) { return n * std::log2(n); };
return [](double n) { return n * std::log2(n); };
case N_SQUARED_LOGN:
return [](const double& n) { return n * n * std::log2(n); };
return [](double n) { return n * n * std::log2(n); };
case TWO_POWER_N:
return [](const double& n) { return pow(2, n); };
return [](double n) { return pow(2, n); };
case N_FACTORIAL:
return [](const double& n) -> double {
return Factorial(static_cast<int>(n));
};
return [](double n) { return Factorial(static_cast<int>(n)); };
case ONE:
default:
return [](const double&) { return 1.0; };
return [](double) { return 1.0; };
}
}

Expand Down Expand Up @@ -149,7 +147,7 @@ LeastSq MinimalLeastSqWithCurve(const std::vector<double>& n,
return result;
}

int FindMaximumPower(const double& max_n) {
int FindMaximumPower(double max_n) {
if (max_n > 250000000.0) {
return static_cast<int>(N);
} else if (max_n > 2500000.0) {
Expand Down Expand Up @@ -239,7 +237,7 @@ void GenerateExpression(const std::vector<int> max_n_for_params,
}
}

BigO PowerToComplexity(const double& power) {
BigO PowerToComplexity(double power) {
return static_cast<BigO>(2.0 * power);
}

Expand Down Expand Up @@ -346,7 +344,7 @@ std::string MinimalLeastSqMultipleParams(
n.emplace_back(
test_complexity::ReduceAccordingOper(metric, powers[i], opers[i]));
}
if (std::any_of(n.begin(), n.end(), [](const double& d) {
if (std::any_of(n.begin(), n.end(), [](double d) {
return std::isinf(d) || std::isnan(d);
})) {
continue;
Expand Down
5 changes: 5 additions & 0 deletions epi_judge_cpp_solutions/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# Compile and run by "make <program_name>"
# For example, typing "make parity" in your terminal will compile and run parity.cc.
#
# You could try "make last" (or simply "make"), this command will compile and run the last modified file.
# This comes pretty handy while you are working a problem, and then you want to see the result soon without thinking program name.
BUILD_DIR := cpp_build
SRC_DIR := .
TEST_DATA_DIR := ../test_data
Expand All @@ -13,6 +17,7 @@ CXX_FLAGS_DEBUG := $(CXX_FLAGS) -g -O0
LAST_MODIFIED_CXX_FILE = $(shell ls -rt $(SRC_DIR)/*\.cc | tail -1)


# Compile and run the last modified file.
last: $(notdir $(basename $(LAST_MODIFIED_CXX_FILE)))

.PHONY: last
Expand Down
5 changes: 2 additions & 3 deletions epi_judge_cpp_solutions/a_b_sqrt2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ struct Number {
};

vector<double> GenerateFirstKABSqrt2(int k) {
set<Number, function<bool(Number, Number)>> candidates(
[](const Number &a, const Number &b) { return a.val < b.val; });
// Initial for 0 + 0 * sqrt(2).
candidates.emplace(0, 0);
set<Number, function<bool(Number, Number)>> candidates(
{{0, 0}}, [](const Number &a, const Number &b) { return a.val < b.val; });

vector<double> result;
while (size(result) < k) {
Expand Down
9 changes: 6 additions & 3 deletions epi_judge_cpp_solutions/absent_value_array.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,14 @@ int FindMissingElement(vector<int>::const_iterator stream_begin,
throw invalid_argument("no missing element");
}

int FindMissingElementWrapper(const vector<int>& stream) {
void FindMissingElementWrapper(const vector<int>& stream) {
try {
return FindMissingElement(cbegin(stream), cend(stream));
int res = FindMissingElement(cbegin(stream), cend(stream));
if (std::find(stream.begin(), stream.end(), res) != stream.end()) {
throw TestFailure(std::to_string(res) + " appears in stream");
}
} catch (invalid_argument&) {
throw TestFailure("Unexpected no_missing_element exception");
throw TestFailure("Unexpected no missing element exception");
}
}

Expand Down
3 changes: 2 additions & 1 deletion epi_judge_cpp_solutions/adding_credits.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class ClientsCreditsInfo {
? ""
: *cbegin(iter->second);
}

friend std::ostream& operator<<(std::ostream& os,
const ClientsCreditsInfo& info) {
PrintTo(os, info.credit_to_clients_);
Expand Down Expand Up @@ -126,7 +127,7 @@ void ProgramConfig(TestConfig& config) { config.analyze_complexity = false; }
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,
return GenericTestMain(args, "adding_credits.cc", "adding_credits.tsv", &ClientsCreditsInfoTester,
DefaultComparator{}, param_names, &ProgramConfig);
}
// clang-format on
2 changes: 1 addition & 1 deletion epi_judge_cpp_solutions/anagrams.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +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,
return GenericTestMain(args, "anagrams.cc", "anagrams.tsv", &FindAnagrams,
UnorderedComparator{}, param_names);
}
// clang-format on
6 changes: 3 additions & 3 deletions epi_judge_cpp_solutions/arbitrage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ bool IsArbitrageExist(vector<vector<double>> graph) {
}

bool BellmanFord(const vector<vector<double>>& graph, int source) {
vector<double> dis_to_source(size(graph), numeric_limits<double>::max());
vector<double> dis_to_source(size(graph), numeric_limits<double>::infinity());
dis_to_source[source] = 0;

for (int times = 1; times < size(graph); ++times) {
bool have_update = false;
for (int i = 0; i < size(graph); ++i) {
for (int j = 0; j < size(graph[i]); ++j) {
if (dis_to_source[i] != numeric_limits<double>::max() &&
if (dis_to_source[i] != numeric_limits<double>::infinity() &&
dis_to_source[j] > dis_to_source[i] + graph[i][j]) {
have_update = true;
dis_to_source[j] = dis_to_source[i] + graph[i][j];
Expand All @@ -45,7 +45,7 @@ bool BellmanFord(const vector<vector<double>>& graph, int source) {
// Detects cycle if there is any further update.
for (int i = 0; i < size(graph); ++i) {
for (int j = 0; j < size(graph[i]); ++j) {
if (dis_to_source[i] != numeric_limits<double>::max() &&
if (dis_to_source[i] != numeric_limits<double>::infinity() &&
dis_to_source[j] > dis_to_source[i] + graph[i][j]) {
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion epi_judge_cpp_solutions/binomial_coefficients.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +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,
return GenericTestMain(args, "binomial_coefficients.cc", "binomial_coefficients.tsv", &ComputeBinomialCoefficient,
DefaultComparator{}, param_names);
}
// clang-format on
4 changes: 2 additions & 2 deletions epi_judge_cpp_solutions/bst_from_preorder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ unique_ptr<BstNode<int>> RebuildBSTFromPreorderHelper(
cbegin(preorder_sequence),
find_if_not(cbegin(preorder_sequence) + start, cend(preorder_sequence),
[&](int a) { return a <= preorder_sequence[start]; }));
return make_unique<BstNode<int>>(BstNode<int>{
return make_unique<BstNode<int>>(
preorder_sequence[start],
RebuildBSTFromPreorderHelper(preorder_sequence, start + 1,
transition_point),
RebuildBSTFromPreorderHelper(preorder_sequence, transition_point, end)});
RebuildBSTFromPreorderHelper(preorder_sequence, transition_point, end));
}

int main(int argc, char* argv[]) {
Expand Down
6 changes: 3 additions & 3 deletions epi_judge_cpp_solutions/bst_from_sorted_array.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ unique_ptr<BstNode<int>> BuildMinHeightBSTFromSortedSubarray(
}
int mid = start + ((end - start) / 2);
return make_unique<BstNode<int>>(
BstNode<int>{A[mid], BuildMinHeightBSTFromSortedSubarray(A, start, mid),
BuildMinHeightBSTFromSortedSubarray(A, mid + 1, end)});
A[mid], BuildMinHeightBSTFromSortedSubarray(A, start, mid),
BuildMinHeightBSTFromSortedSubarray(A, mid + 1, end));
}

int BuildMinHeightBSTFromSortedArrayWrapper(TimedExecutor& executor,
Expand All @@ -48,7 +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,
return GenericTestMain(args, "bst_from_sorted_array.cc", "bst_from_sorted_array.tsv", &BuildMinHeightBSTFromSortedArrayWrapper,
DefaultComparator{}, param_names);
}
// clang-format on
2 changes: 1 addition & 1 deletion epi_judge_cpp_solutions/bst_to_sorted_list.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +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,
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.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ using std::numeric_limits;
using std::vector;

double BuyAndSellStockOnce(const vector<double>& prices) {
double min_price_so_far = numeric_limits<double>::max(), max_profit = 0;
double min_price_so_far = numeric_limits<double>::infinity(), max_profit = 0;
for (double price : prices) {
double max_profit_sell_today = price - min_price_so_far;
max_profit = max(max_profit, max_profit_sell_today);
Expand Down
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 @@ -16,7 +16,7 @@ double BuyAndSellStockKTimes(const vector<double> &prices, int k) {
} else if (2 * k >= size(prices)) {
return UnlimitedPairsProfits(prices);
}
vector<double> min_prices(k, numeric_limits<double>::max()),
vector<double> min_prices(k, numeric_limits<double>::infinity()),
max_profits(k, 0.0);
for (double price : prices) {
for (int i = k - 1; i >= 0; --i) {
Expand Down
6 changes: 3 additions & 3 deletions epi_judge_cpp_solutions/buy_and_sell_stock_twice.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ using std::vector;
double BuyAndSellStockTwice(const vector<double>& prices) {
double max_total_profit = 0;
vector<double> first_buy_sell_profits(size(prices), 0);
double min_price_so_far = numeric_limits<double>::max();
double min_price_so_far = numeric_limits<double>::infinity();

// Forward phase. For each day, we record maximum profit if we
// sell on that day.
Expand All @@ -37,8 +37,8 @@ double BuyAndSellStockTwice(const vector<double>& prices) {
}

double BuyAndSellStockTwiceConstantSpace(const vector<double>& prices) {
array<double, 2> min_prices = {numeric_limits<double>::max(),
numeric_limits<double>::max()},
array<double, 2> min_prices = {numeric_limits<double>::infinity(),
numeric_limits<double>::infinity()},
max_profits = {0.0, 0.0};
for (double price : prices) {
for (int i = 1; i >= 0; --i) {
Expand Down
Loading

0 comments on commit d1b3822

Please sign in to comment.