Skip to content

Commit

Permalink
Update on 01/19/2018
Browse files Browse the repository at this point in the history
  • Loading branch information
tsunghsienlee committed Jan 20, 2018
1 parent 1c08756 commit 07ddf29
Show file tree
Hide file tree
Showing 266 changed files with 1,208 additions and 429 deletions.
2 changes: 1 addition & 1 deletion epi_judge_cpp/bst_from_preorder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using std::unique_ptr;
using std::vector;

unique_ptr<BSTNode<int>> RebuildBSTFromPreorder(
unique_ptr<BstNode<int>> RebuildBSTFromPreorder(
const vector<int>& preorder_sequence) {
// Implement this placeholder.
return nullptr;
Expand Down
18 changes: 16 additions & 2 deletions epi_judge_cpp/bst_from_sorted_array.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,34 @@
#include <vector>

#include "bst_node.h"
#include "test_framework/binary_tree_utils.h"
#include "test_framework/test_failure_exception.h"

using std::unique_ptr;
using std::vector;

unique_ptr<BSTNode<int>> BuildMinHeightBSTFromSortedArray(
unique_ptr<BstNode<int>> BuildMinHeightBSTFromSortedArray(
const vector<int>& A) {
// Implement this placeholder.
return nullptr;
}

int BuildMinHeightBSTFromSortedArrayWrapper(TestTimer& timer,
const vector<int>& A) {
timer.Start();
unique_ptr<BstNode<int>> result = BuildMinHeightBSTFromSortedArray(A);
timer.Stop();

if (GenerateInorder(result) != A) {
throw TestFailureException("Result binay tree mismatches input array");
}
return BinaryTreeHeight(result);
}

#include "test_framework/test_utils_generic_main.h"

int main(int argc, char* argv[]) {
generic_test_main(argc, argv, "bst_from_sorted_array.tsv",
&BuildMinHeightBSTFromSortedArray);
&BuildMinHeightBSTFromSortedArrayWrapper);
return 0;
}
4 changes: 2 additions & 2 deletions epi_judge_cpp/bst_merge.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

using std::shared_ptr;

shared_ptr<BSTNode<int>> MergeTwoBSTs(shared_ptr<BSTNode<int>> A,
shared_ptr<BSTNode<int>> B) {
shared_ptr<BstNode<int>> MergeTwoBSTs(shared_ptr<BstNode<int>> A,
shared_ptr<BstNode<int>> B) {
// Implement this placeholder.
return nullptr;
}
Expand Down
12 changes: 6 additions & 6 deletions epi_judge_cpp/bst_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@
using std::unique_ptr;

template <typename T>
struct BSTNode {
struct BstNode {
T data;
unique_ptr<BSTNode<T>> left, right;
unique_ptr<BstNode<T>> left, right;

explicit BSTNode(const T& data) : data(data) {}
explicit BstNode(const T& data) : data(data) {}

BSTNode(T data, unique_ptr<BSTNode<T>> left, unique_ptr<BSTNode<T>> right)
BstNode(T data, unique_ptr<BstNode<T>> left, unique_ptr<BstNode<T>> right)
: data(data), left(std::move(left)), right(std::move(right)) {}
};

template <typename KeyT>
struct SerializationTraits<std::unique_ptr<BSTNode<KeyT>>>
: BinaryTreeSerializationTraits<std::unique_ptr<BSTNode<KeyT>>, false> {};
struct SerializationTraits<std::unique_ptr<BstNode<KeyT>>>
: BinaryTreeSerializationTraits<std::unique_ptr<BstNode<KeyT>>, false> {};
10 changes: 5 additions & 5 deletions epi_judge_cpp/bst_prototype_shared_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
using std::shared_ptr;

template <typename T>
struct BSTNode {
struct BstNode {
T data;
shared_ptr<BSTNode<T>> left, right;
shared_ptr<BstNode<T>> left, right;

explicit BSTNode(const T& data) : data(data) {}
explicit BstNode(const T& data) : data(data) {}
};

template <typename KeyT>
struct SerializationTraits<std::shared_ptr<BSTNode<KeyT>>>
: BinaryTreeSerializationTraits<std::shared_ptr<BSTNode<KeyT>>, false> {};
struct SerializationTraits<std::shared_ptr<BstNode<KeyT>>>
: BinaryTreeSerializationTraits<std::shared_ptr<BstNode<KeyT>>, false> {};
6 changes: 3 additions & 3 deletions epi_judge_cpp/bst_to_sorted_list.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@

using std::shared_ptr;

shared_ptr<BSTNode<int>> BSTToDoublyLinkedList(
const shared_ptr<BSTNode<int>>& tree) {
shared_ptr<BstNode<int>> BSTToDoublyLinkedList(
const shared_ptr<BstNode<int>>& tree) {
// Implement this placeholder.
return nullptr;
}

std::vector<int> BSTToDoublyLinkedListWrapper(
TestTimer& timer, const std::shared_ptr<BSTNode<int>>& tree) {
TestTimer& timer, const std::shared_ptr<BstNode<int>>& tree) {
timer.Start();
auto list = BSTToDoublyLinkedList(tree);
timer.Stop();
Expand Down
8 changes: 4 additions & 4 deletions epi_judge_cpp/descendant_and_ancestor_in_bst.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
using std::unique_ptr;

bool PairIncludesAncestorAndDescendantOfM(
const unique_ptr<BSTNode<int>>& possible_anc_or_desc_0,
const unique_ptr<BSTNode<int>>& possible_anc_or_desc_1,
const unique_ptr<BSTNode<int>>& middle) {
const unique_ptr<BstNode<int>>& possible_anc_or_desc_0,
const unique_ptr<BstNode<int>>& possible_anc_or_desc_1,
const unique_ptr<BstNode<int>>& middle) {
// Implement this placeholder.
return true;
}

bool PairIncludesAncestorAndDescendantOfMWrapper(
TestTimer& timer, const unique_ptr<BSTNode<int>>& tree, int candidate1idx,
TestTimer& timer, const unique_ptr<BstNode<int>>& tree, int candidate1idx,
int candidate2idx, int middle_idx) {
auto& candidate1 = MustFindNode(tree, candidate1idx);
auto& candidate2 = MustFindNode(tree, candidate2idx);
Expand Down
2 changes: 1 addition & 1 deletion epi_judge_cpp/k_largest_values_in_bst.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using std::unique_ptr;
using std::vector;

vector<int> FindKLargestInBST(const unique_ptr<BSTNode<int>>& tree, int k) {
vector<int> FindKLargestInBST(const unique_ptr<BstNode<int>>& tree, int k) {
// Implement this placeholder.
return {};
}
Expand Down
8 changes: 4 additions & 4 deletions epi_judge_cpp/lowest_common_ancestor_in_bst.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ using std::unique_ptr;

// Input nodes are nonempty and the key at s is less than or equal to that at
// b.
BSTNode<int>* FindLCA(const unique_ptr<BSTNode<int>>& tree,
const unique_ptr<BSTNode<int>>& s,
const unique_ptr<BSTNode<int>>& b) {
BstNode<int>* FindLCA(const unique_ptr<BstNode<int>>& tree,
const unique_ptr<BstNode<int>>& s,
const unique_ptr<BstNode<int>>& b) {
// Implement this placeholder.
return nullptr;
}

int LcaWrapper(TestTimer& timer, const std::unique_ptr<BSTNode<int>>& root,
int LcaWrapper(TestTimer& timer, const std::unique_ptr<BstNode<int>>& root,
int key1, int key2) {
auto& node1 = MustFindNode(root, key1);
auto& node2 = MustFindNode(root, key2);
Expand Down
4 changes: 2 additions & 2 deletions epi_judge_cpp/search_first_greater_value_in_bst.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

using std::unique_ptr;

BSTNode<int>* FindFirstGreaterThanK(const unique_ptr<BSTNode<int>>& tree,
BstNode<int>* FindFirstGreaterThanK(const unique_ptr<BstNode<int>>& tree,
int k) {
// Implement this placeholder.
return nullptr;
}

int FindFirstGreaterThanKWrapper(const unique_ptr<BSTNode<int>>& tree, int k) {
int FindFirstGreaterThanKWrapper(const unique_ptr<BstNode<int>>& tree, int k) {
auto result = FindFirstGreaterThanK(tree, k);
return result ? result->data : -1;
}
Expand Down
10 changes: 10 additions & 0 deletions epi_judge_cpp/test_framework/binary_tree_utils.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// @library
#pragma once

#include <algorithm>
#include <memory>
#include <stdexcept>
#include <vector>
Expand Down Expand Up @@ -102,3 +103,12 @@ void DeleteBinaryTree(Node* tree) {
tree->reset(nullptr);
}
}

template <typename Node>
int BinaryTreeHeight(const Node& tree) {
if (!tree) {
return -1;
}
return 1 + std::max(BinaryTreeHeight(tree->left),
BinaryTreeHeight(tree->right));
}
65 changes: 65 additions & 0 deletions epi_judge_cpp/test_framework/console_color.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// @library
#pragma once

#include <functional>
#include <iostream>

#ifdef _WINDOWS
#define NOMINMAX
#include <Windows.h>
#endif

#include "platform.h"

enum class ConsoleColor { FG_RED, FG_GREEN, FG_BLUE, FG_DEFAULT };

short GetColorCodeWin(ConsoleColor color) {
switch (color) {
case ConsoleColor::FG_RED:
return 4 | 8;
case ConsoleColor::FG_GREEN:
return 2 | 8;
case ConsoleColor::FG_BLUE:
return 1 | 8;
case ConsoleColor::FG_DEFAULT:
return 7;
}
}

const char* GetColorCodeUnix(ConsoleColor color) {
switch (color) {
case ConsoleColor::FG_RED:
return "\033[31m";
case ConsoleColor::FG_GREEN:
return "\033[32m";
case ConsoleColor::FG_BLUE:
return "\033[34m";
case ConsoleColor::FG_DEFAULT:
return "\033[39m";
}
}

template <typename T>
void PrintStdOutColored(ConsoleColor color, const T& value) {
if (!platform::UseColorOutput()) {
std::cout << value;
return;
}
#ifdef _WINDOWS
const HANDLE stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO buffer_info;
GetConsoleScreenBufferInfo(stdout_handle, &buffer_info);
const WORD old_color_attrs = buffer_info.wAttributes;
fflush(stdout);

SetConsoleTextAttribute(stdout_handle, GetColorCodeWin(color));

std::cout << value;
fflush(stdout);

SetConsoleTextAttribute(stdout_handle, old_color_attrs);
#else
std::cout << GetColorCodeUnix(color) << value
<< GetColorCodeUnix(ConsoleColor::FG_DEFAULT);
#endif
}
5 changes: 5 additions & 0 deletions epi_judge_cpp/test_framework/generic_test_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ class GenericTestHandler {
return std::is_same<expected_tag, ExpectedIsVoidTag>::value;
}

static constexpr size_t ArgumentCount() {
return std::tuple_size<
typename FunctionalTraits<Function>::arg_tuple_t>::value;
}

private:
/**
* This method parses expected value (if return type is not void),
Expand Down
21 changes: 15 additions & 6 deletions epi_judge_cpp/test_framework/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,32 @@

namespace platform {

inline int StdOutFd() {
bool ENABLE_COLOR_OUTPUT = true;

int StdOutFd() {
#ifdef _WINDOWS
return _fileno(stdout);
#else
return STDOUT_FILENO;
#endif
}

inline int IsATty(int fd) {
int IsATty(int fd) {
#ifdef _WINDOWS
return ::_isatty(fd);
return _isatty(fd);
#else
return ::isatty(fd);
return isatty(fd);
#endif
}

inline bool IsDir(const char* path) {
bool UseTtyOutput() {
static int cached = IsATty(StdOutFd());
return static_cast<bool>(cached);
}

bool UseColorOutput() { return UseTtyOutput() && ENABLE_COLOR_OUTPUT; }

bool IsDir(const char* path) {
#ifdef _WINDOWS
struct _stat buf {};
_stat(path, &buf);
Expand All @@ -46,7 +55,7 @@ inline bool IsDir(const char* path) {
#endif
}

inline char PathSep() {
char PathSep() {
#ifdef _WINDOWS
return '\\';
#else
Expand Down
58 changes: 10 additions & 48 deletions epi_judge_cpp/test_framework/test_result.h
Original file line number Diff line number Diff line change
@@ -1,51 +1,13 @@
// @library
#pragma once

#include <ostream>
#include "platform.h"

//We can't detect STACK_OVERFLOW in C++
enum TestResult { PASSED, FAILED, TIMEOUT, UNKNOWN_EXCEPTION, /*STACK_OVERFLOW*/ };

namespace console_color {
const char* FG_RED = "\033[31m";
const char* FG_GREEN = "\033[32m";
const char* FG_BLUE = "\033[34m";
const char* FG_DEFAULT = "\033[39m";
}; // namespace console_color

bool UseTtyOutput() {
static int cached = platform::IsATty(platform::StdOutFd());
return static_cast<bool>(cached);
}

inline bool PlatformSupportsColor() {
#ifdef _WINDOWS
return false; // Windows doesn't support coloring through escape codes
#else
return true;
#endif
}

std::string Colored(std::string text, const char* color) {
if (UseTtyOutput() && PlatformSupportsColor()) {
return color + std::move(text) + console_color::FG_DEFAULT;
} else {
return std::move(text);
}
}

std::ostream& operator<<(std::ostream& out, const TestResult& tr) {
switch (tr) {
case PASSED:
return out << Colored("PASSED", console_color::FG_GREEN);
case FAILED:
return out << Colored("FAILED", console_color::FG_RED);
case TIMEOUT:
return out << Colored("TIMEOUT", console_color::FG_BLUE);
case UNKNOWN_EXCEPTION:
return out << Colored("UNHANDLED EXCEPTION", console_color::FG_RED);
default:
throw std::runtime_error("Unknown TestResult");
}
}
#include "console_color.h"

// We may not detect STACK_OVERFLOW in C++ (Java and Python detect that).
enum TestResult {
PASSED,
FAILED,
TIMEOUT,
UNKNOWN_EXCEPTION,
/*STACK_OVERFLOW*/
};
Loading

0 comments on commit 07ddf29

Please sign in to comment.