From 6ab678a213f168d40fdae25cc5411e413093304f Mon Sep 17 00:00:00 2001 From: Vahagn Khachatryan Date: Tue, 15 May 2018 23:02:22 +0100 Subject: [PATCH 1/5] simple_wildcard_match.cpp --- .../training/simple_wildcard_match.cpp | 94 +++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 puzzles/interviews/training/simple_wildcard_match.cpp diff --git a/puzzles/interviews/training/simple_wildcard_match.cpp b/puzzles/interviews/training/simple_wildcard_match.cpp new file mode 100644 index 0000000..2571ec5 --- /dev/null +++ b/puzzles/interviews/training/simple_wildcard_match.cpp @@ -0,0 +1,94 @@ +/* +VIM: let b:lcppflags="-std=c++14 -O2 -pthread -I." +VIM: let b:wcppflags="/O2 /EHsc /DWIN32" +VIM-: let b:cppflags=g:Iboost.g:Itbb +VIM-: let b:ldflags=g:Lboost.g:Ltbb +VIM-: let b:ldlibpath=g:Bboost.g:Btbb +VIM-: let b:argv="" +*/ +#include +#include +#include +#include + +/* +Simple wildcard match. + +*/ + +bool wildcard_match(const std::string& input, const std::string& pattern) +{ + int j = 0; + int i = 0; + int restart_idx = -1; + while ( i < input.size() ) + { + while ( j < pattern.size() && pattern[j] == '*' ){ + restart_idx = ++j; + } + if ( j= 0 && pattern[j-1] == '*' ) { + ++i; + } + else if ( restart_idx > 0 ) { + i -= j - restart_idx -1; + j = restart_idx; + } + else { + return false; + } + } + while ( j < pattern.size() && pattern[j] == '*' ){ + ++j; + } + return j == pattern.size(); +} + +void test(const std::string& input, const std::string& pattern) +{ + std::cout << input << " x " << pattern + << " == " << wildcard_match(input,pattern) + << std::endl; +} + + +int main ( void ) +{try{ + auto begin = std::chrono::high_resolution_clock::now(); + + test("zebra", "dog"); + test("zebra", "zebra"); + test("zebra", "*br*"); + test("zebra", "*bi*"); + test("zebra", "*br*a"); + test("zebra", "*br*b"); + test("zebra", "z**a"); + test("zebra", "z*a"); + test("zebra", "x**a"); + test("zebrabrb", "*brb*"); + test("zebra", "*bra*"); + test("zebrazebrazebra", "*bra*zeb*zeb*"); + test("zebrbzebrazebra", "*bra*zeb*zeb*"); + test("zebrbzebrazebra", "*bra*zeb*"); + + auto end = std::chrono::high_resolution_clock::now(); + std::chrono::duration seconds = end - begin; + std::cout << "Time: " << seconds.count() << std::endl; + return 0; +} +catch ( const std::exception& e ) +{ + std::cerr << std::endl + << "std::exception(\"" << e.what() << "\")." << std::endl; + return 2; +} +catch ( ... ) +{ + std::cerr << std::endl + << "unknown exception." << std::endl; + return 1; +}} + From cc53adcb1f0035db08a7d9d5a9ab3a8cefcb0cf4 Mon Sep 17 00:00:00 2001 From: Vahagn Khachatryan Date: Thu, 31 May 2018 19:25:29 +0100 Subject: [PATCH 2/5] Squarepoint Capital --- .../a_bit_more_complex_wildcard_match.cpp | 121 ++++++++++++++++++ .../training/simple_wildcard_match.cpp | 1 + 2 files changed, 122 insertions(+) create mode 100644 puzzles/interviews/training/a_bit_more_complex_wildcard_match.cpp diff --git a/puzzles/interviews/training/a_bit_more_complex_wildcard_match.cpp b/puzzles/interviews/training/a_bit_more_complex_wildcard_match.cpp new file mode 100644 index 0000000..b22b618 --- /dev/null +++ b/puzzles/interviews/training/a_bit_more_complex_wildcard_match.cpp @@ -0,0 +1,121 @@ +/* +VIM: let b:lcppflags="-std=c++14 -O2 -pthread -I." +VIM: let b:wcppflags="/O2 /EHsc /DWIN32" +VIM-: let b:argv="" +*/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +/* + * Complete the function below. + */ +bool match(const string& pattern, const string& string) { + + auto s = string.begin(); + auto p = pattern.begin(); + + const std::string::const_iterator empty; + auto prev_p = empty; + auto prev_s = s; + while (s != string.end()) { + while ( p != pattern.end() && *p == '*' ) { + p++; + prev_p = p; + prev_s = s; + } + if ( p == pattern.end() ) { + if ( prev_p == p ){ + return true; + } else if ( prev_p != empty ) { + p = prev_p; + s = ++prev_s; + } else { + return false; + } + } + else if ( *s == *p ){ + s++; + p++; + } + else if ( *p == '?' ){ + s++; + p++; + } + else if ( prev_p != empty ) { + p = prev_p; + s = ++prev_s; + } + else { + return false; + } + } + while ( *p == '*' ) { + p++; + } + return p == pattern.end(); +} + + +void test( const std::string& str, const std::string& pattern ){ + bool res = match(pattern, str); + std::cout << str << " - " << pattern << " -> " << res < seconds = end - begin; From db8b53504733bdc70decb7263387eb33b0cde60a Mon Sep 17 00:00:00 2001 From: Vahagn Khachatryan Date: Tue, 5 Jun 2018 09:53:58 -0400 Subject: [PATCH 3/5] compile_time_power_of_number.cpp --- .../training/compile_time_power_of_number.cpp | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 puzzles/interviews/training/compile_time_power_of_number.cpp diff --git a/puzzles/interviews/training/compile_time_power_of_number.cpp b/puzzles/interviews/training/compile_time_power_of_number.cpp new file mode 100644 index 0000000..1617f1a --- /dev/null +++ b/puzzles/interviews/training/compile_time_power_of_number.cpp @@ -0,0 +1,89 @@ +/* +VIM: let b:lcppflags="-std=c++14 -O2 -pthread -I." +VIM: let b:wcppflags="/O2 /EHsc /DWIN32" +VIM-: let b:cppflags=g:Iboost.g:Itbb +VIM-: let b:ldflags=g:Lboost.g:Ltbb +VIM-: let b:ldlibpath=g:Bboost.g:Btbb +VIM-: let b:argv="" +*/ +#include +#include +#include + + +template +struct power_of +{ + enum { + value = power_of<(N+1)/2>::value * 2 + }; +}; + +template <> +struct power_of<2> +{ + enum { + value = 2 + }; +}; + +template <> +struct power_of<1> +{ + enum { + value = 1 + }; +}; + +template <> +struct power_of<0> +{ + enum { + value = 0 + }; +}; + +template +void test() +{ + std::cout << N << "=> " << power_of::value <(); + test<1>(); + test<2>(); + test<3>(); + test<4>(); + test<5>(); + test<6>(); + test<7>(); + test<8>(); + test<9>(); + test<127>(); + test<128>(); + test<129>(); + //...... + + auto end = std::chrono::high_resolution_clock::now(); + std::chrono::duration seconds = end - begin; + std::cout << "Time: " << seconds.count() << std::endl; + return 0; +} +catch ( const std::exception& e ) +{ + std::cerr << std::endl + << "std::exception(\"" << e.what() << "\")." << std::endl; + return 2; +} +catch ( ... ) +{ + std::cerr << std::endl + << "unknown exception." << std::endl; + return 1; +}} + From 74342319e3533e6f0ccd1747838a28360655b6be Mon Sep 17 00:00:00 2001 From: Vahagn Khachatryan Date: Fri, 14 Sep 2018 13:38:22 -0400 Subject: [PATCH 4/5] multiple_shared_future_get.cpp --- cpp/stl/multiple_shared_future_get.cpp | 54 ++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 cpp/stl/multiple_shared_future_get.cpp diff --git a/cpp/stl/multiple_shared_future_get.cpp b/cpp/stl/multiple_shared_future_get.cpp new file mode 100644 index 0000000..fd08061 --- /dev/null +++ b/cpp/stl/multiple_shared_future_get.cpp @@ -0,0 +1,54 @@ +/* +VIM: let b:lcppflags="-std=c++14 -O2 -pthread -I." +VIM: let b:wcppflags="/O2 /EHsc /DWIN32" +VIM-: let b:cppflags=g:Iboost.g:Itbb +VIM-: let b:ldflags=g:Lboost.g:Ltbb +VIM-: let b:ldlibpath=g:Bboost.g:Btbb +VIM-: let b:argv="" +*/ +#include +#include +#include +#include + +int main ( void ) +{try{ + auto begin = std::chrono::high_resolution_clock::now(); + + std::promise p; + auto f = p.get_future().share(); + + std::thread t1([f](){ + std::cout << ' ' << f.get() << std::endl; + }); + + std::thread t2([f](){ + std::cout << ' ' << f.get() << std::endl; + }); + + + p.set_value(55); + + t1.join(); + t2.join(); + + //...... + + auto end = std::chrono::high_resolution_clock::now(); + std::chrono::duration seconds = end - begin; + std::cout << "Time: " << seconds.count() << std::endl; + return 0; +} +catch ( const std::exception& e ) +{ + std::cerr << std::endl + << "std::exception(\"" << e.what() << "\")." << std::endl; + return 2; +} +catch ( ... ) +{ + std::cerr << std::endl + << "unknown exception." << std::endl; + return 1; +}} + From b0a3dd24602320baabb45a075c00a4fb50056188 Mon Sep 17 00:00:00 2001 From: Vahagn Khachatryan Date: Wed, 17 Oct 2018 12:56:45 -0400 Subject: [PATCH 5/5] exception_context.cpp --- cpp/cpp11/exception_context.cpp | 57 +++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 cpp/cpp11/exception_context.cpp diff --git a/cpp/cpp11/exception_context.cpp b/cpp/cpp11/exception_context.cpp new file mode 100644 index 0000000..31e3a2e --- /dev/null +++ b/cpp/cpp11/exception_context.cpp @@ -0,0 +1,57 @@ +/* +VIM: let b:lcppflags="-std=c++14 -O2 -pthread -I." +VIM: let b:wcppflags="/O2 /EHsc /DWIN32" +VIM-: let b:cppflags=g:Iboost.g:Itbb +VIM-: let b:ldflags=g:Lboost.g:Ltbb +VIM-: let b:ldlibpath=g:Bboost.g:Btbb +VIM-: let b:argv="" +*/ +#include +#include +#include +#include + +void print_exception() +{ + try { + std::rethrow_exception(std::current_exception()); + } catch ( const std::exception& e ) { + std::cout << e.what() << std::endl; + } +} + +int main ( void ) +{try{ + auto begin = std::chrono::high_resolution_clock::now(); + + try { + throw std::runtime_error("first exception"); + } catch(...) { + try { + throw std::runtime_error("second exception"); + } catch (...) { + print_exception(); + } + print_exception(); + } + + //...... + + auto end = std::chrono::high_resolution_clock::now(); + std::chrono::duration seconds = end - begin; + std::cout << "Time: " << seconds.count() << std::endl; + return 0; +} +catch ( const std::exception& e ) +{ + std::cerr << std::endl + << "std::exception(\"" << e.what() << "\")." << std::endl; + return 2; +} +catch ( ... ) +{ + std::cerr << std::endl + << "unknown exception." << std::endl; + return 1; +}} +