From e3175f15bc06fb1366b8cb9fb2b2a6841b8406c7 Mon Sep 17 00:00:00 2001 From: Gordan Kucinar Date: Thu, 15 Nov 2018 14:20:01 +0100 Subject: [PATCH 1/4] Eating bugs for breakfast --- exercise/01/containers.h | 1 + 1 file changed, 1 insertion(+) diff --git a/exercise/01/containers.h b/exercise/01/containers.h index 8e1b172..18263a6 100644 --- a/exercise/01/containers.h +++ b/exercise/01/containers.h @@ -13,6 +13,7 @@ int unique_numbers(std::wistream&); class word_frequency { + std::wstring text; public: word_frequency(std::wistream&); int frequency(const std::wstring& s); From 6b810acd719f007625748f5eb9b502b035a39227 Mon Sep 17 00:00:00 2001 From: Gordan Kucinar Date: Thu, 15 Nov 2018 14:32:44 +0100 Subject: [PATCH 2/4] Eating bugs for breakfast --- exercise/01/containers.cpp | 68 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 exercise/01/containers.cpp diff --git a/exercise/01/containers.cpp b/exercise/01/containers.cpp new file mode 100644 index 0000000..8d5559e --- /dev/null +++ b/exercise/01/containers.cpp @@ -0,0 +1,68 @@ +#include "containers.h" +#include +#include +#include +#include +#include +#include + +void remove_element(std::vector& v, int index) +{ + v.erase(std::begin(v) + index); +} + +void input_element(std::vector& v, int index, const std::wstring & value) +{ + v.insert(std::begin(v) + index, value); +} + +int list_nth_element(const std::list& c, int index) +{ + auto iter = std::cbegin(c); + std::advance(iter, index); + return *iter; +} + +void list_sort_desc(std::list& c) +{ + c.sort(std::greater()); +} + +int unique_numbers(std::wistream& stream) +{ + std::set s; + for (auto iter = std::istream_iterator(stream); iter != std::istream_iterator(); ++iter) + s.insert(*iter); + return s.size(); +} + +word_frequency::word_frequency(std::wistream& s) +{ + for (auto iter = std::istreambuf_iterator(s); iter != std::istreambuf_iterator(); ++iter) + text.push_back(*iter); +} + +int word_frequency::frequency(const std::wstring & s) +{ + const std::wregex reg(std::wstring(L"(\\s+|^)") + s + L"(\\s+|$)", std::regex_constants::icase); + int n = 0; + for (auto iter = std::regex_token_iterator(std::cbegin(text), std::cend(text), reg, 0); iter != std::regex_token_iterator(); ++iter) + ++n; + return n; +} + +int word_frequency::count() +{ + static const std::wregex reg(L"\\s+"); + std::set wset; + for (auto iter = std::regex_token_iterator(std::cbegin(text), std::cend(text), reg, -1); iter != std::regex_token_iterator(); ++iter) + { + std::wstring s = iter->str(); + std::wstring ls; + for (auto c : s) + ls.push_back(tolower(c)); + wset.insert(ls); + } + + return wset.size(); +} From 15451315dea2215db876fb7e8d2007daf293c367 Mon Sep 17 00:00:00 2001 From: Gordan Kucinar Date: Thu, 15 Nov 2018 14:33:45 +0100 Subject: [PATCH 3/4] Eating bugs for breakfast --- exercise/01/containers.cpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/exercise/01/containers.cpp b/exercise/01/containers.cpp index 8d5559e..2d70175 100644 --- a/exercise/01/containers.cpp +++ b/exercise/01/containers.cpp @@ -45,10 +45,7 @@ word_frequency::word_frequency(std::wistream& s) int word_frequency::frequency(const std::wstring & s) { const std::wregex reg(std::wstring(L"(\\s+|^)") + s + L"(\\s+|$)", std::regex_constants::icase); - int n = 0; - for (auto iter = std::regex_token_iterator(std::cbegin(text), std::cend(text), reg, 0); iter != std::regex_token_iterator(); ++iter) - ++n; - return n; + return std::accumulate(std::regex_token_iterator(std::cbegin(text), std::cend(text), reg, 0), std::regex_token_iterator(), 0, [](auto acc, const auto&) {return ++acc; });; } int word_frequency::count() From 9e1a28a959bca1f2c50060b2cf170508e9e08d61 Mon Sep 17 00:00:00 2001 From: Gordan Kucinar Date: Fri, 16 Nov 2018 09:07:13 +0100 Subject: [PATCH 4/4] Kucinar Gordan --- exercise/02/algorithms_test.cpp | 50 +++++---- exercise/02/e02.vcxproj | 185 ++++++++++++++++---------------- 2 files changed, 117 insertions(+), 118 deletions(-) diff --git a/exercise/02/algorithms_test.cpp b/exercise/02/algorithms_test.cpp index 9df3595..a222b8a 100644 --- a/exercise/02/algorithms_test.cpp +++ b/exercise/02/algorithms_test.cpp @@ -1,4 +1,5 @@ #include "CppUnitTest.h" + #include using namespace Microsoft::VisualStudio::CppUnitTestFramework; #include @@ -15,8 +16,7 @@ TEST_CLASS(test_standard_algorithms_usage) { std::wstringstream ss(L"14 -78 22"); std::vector v; - // TODO: read values from input stream into vector - + std::copy(std::istream_iterator(ss), std::istream_iterator(), std::back_inserter(v)); Assert::AreEqual(3u, v.size()); Assert::AreEqual(14, v[0]); Assert::AreEqual(-78, v[1]); @@ -26,7 +26,7 @@ TEST_CLASS(test_standard_algorithms_usage) TEST_METHOD(test_02a) { std::vector v(10); - // TODO: fill vector with incremental values + std::iota(std::begin(v), std::end(v), 1); Assert::AreEqual(10u, v.size()); Assert::IsTrue(std::is_sorted(v.cbegin(), v.cend())); Assert::AreEqual( 1, v[0]); @@ -36,7 +36,7 @@ TEST_CLASS(test_standard_algorithms_usage) { // generate std::vector v(10); - // TODO: fill vector with incremental values (by 2) + std::generate(std::begin(v), std::end(v), [n = -1]() mutable {return n += 2; }); Assert::IsTrue(std::is_sorted(v.cbegin(), v.cend())); Assert::IsTrue(v.cend() == std::adjacent_find(v.cbegin(), v.cend(), [](int a, int b) { return b - a != 2; })); Assert::AreEqual(1, v[0]); @@ -46,7 +46,7 @@ TEST_CLASS(test_standard_algorithms_usage) TEST_METHOD(test_03a) { std::vector v = { 1, 5, 10 } ; - // TODO: change all values in a vector + std::transform(std::begin(v), std::end(v), std::begin(v), [](int val) {return val * val * val; }); Assert::AreEqual(3u, v.size()); Assert::AreEqual(1, v[0]); Assert::AreEqual(125, v[1]); @@ -57,9 +57,8 @@ TEST_CLASS(test_standard_algorithms_usage) int x[] = { 3, 5, 10 }; std::vector y = { 4, 12, 10 }; std::vector d; - - // TODO: calculate distances from origin (from x and y collections) to new vector - Assert::AreEqual(3u, d.size()); + std::transform(std::cbegin(x), std::cend(x), std::cbegin(y), std::back_inserter(d), [](int i, double d) {return std::sqrt(i*i + d * d); }); + Assert::AreEqual(3u, d.size()); Assert::AreEqual(5., d[0]); Assert::AreEqual(13., d[1]); Assert::AreEqual(sqrt(200), d[2]); @@ -67,53 +66,53 @@ TEST_CLASS(test_standard_algorithms_usage) TEST_METHOD(test_04a) { std::wstringstream ss(L"1.5 2.5 3.5"); - auto res = // TODO: sum of all values in input stream + auto res = std::accumulate(std::istream_iterator(ss), std::istream_iterator(), 0., [](double acc, double el) {return acc + el; }); Assert::AreEqual(7.5, res); } TEST_METHOD(test_04b) { std::vector v { L"A", L"V", L"L", L"!" }; - auto res = // TODO: concatenated string with additional prefix + auto res = std::accumulate(std::cbegin(v), std::cend(v), std::wstring(L"GO "), [](auto const & acc, auto const & el) {return acc + el; }); Assert::AreEqual(L"GO AVL!", res.c_str()); } TEST_METHOD(test_04c) { struct person { std::wstring name; int age; }; std::vector v{ {L"Pero", 33}, {L"Iva", 25} }; - auto total_age = // TODO: sum of all ages + auto total_age = std::accumulate(std::cbegin(v), std::cend(v), 0, [](auto const & acc, auto const & el) {return acc + el.age; }); Assert::AreEqual(58, total_age); } TEST_METHOD(test_05a) { std::vector v { -5, 8, 11, 0, -9, 77, -4 }; - auto number_of_negative = // TODO: + auto number_of_negative = std::count_if(std::cbegin(v), std::cend(v), [](auto const & val) {return val < 0; }); Assert::AreEqual(3, number_of_negative); } TEST_METHOD(test_05b) { std::vector v { 1.5, 8, -11.23, 0, 1e10, 1e10, 1e10, 0, 99 }; - auto number_of_invalid = // TODO: + auto number_of_invalid = std::count_if(std::cbegin(v), std::cend(v), [](auto const & val) { return val == 1e10; }); Assert::AreEqual(3, number_of_invalid); } TEST_METHOD(test_05c) { struct point { int x, y; }; std::vector v{ {1,1}, {-5,3}, {2,2}, {-7,-6}, {9,-4} }; - auto number_in_first_quadrant = // TODO: + auto number_in_first_quadrant = std::count_if(std::cbegin(v), std::cend(v), [](auto const & p) {return p.x > 0 && p.y > 0; }); Assert::AreEqual(2, number_in_first_quadrant); } TEST_METHOD(test_06) { std::vector v { 33, 16, 24, 41, 25, 19, 9 }; - auto first_prime = // TODO: - Assert::AreEqual(41, first_prime); + //auto first_prime = // TODO: + //Assert::AreEqual(41, first_prime); } TEST_METHOD(test_07a) { std::vector v{ 1e10, 8, -11.23, 0, 1e10, 1e10, 1e10, 0, 99 }; - // TODO: change every invalid value (1e10) with -1 + std::replace_if(std::begin(v), std::end(v), [](auto const & val) { return val == 1e10; }, -1); Assert::AreEqual(-1., v[0]); Assert::AreEqual(-1., v[4]); Assert::AreEqual(-1., v[6]); @@ -121,13 +120,13 @@ TEST_CLASS(test_standard_algorithms_usage) TEST_METHOD(test_07b) { std::wstring s(L"ponedjeljak"); - // TODO: change every vowel with x + std::replace_if(s.begin(), s.end(), [](const char c) { return std::string("aeiou").find(c) != std::string::npos; }, 'x'); Assert::AreEqual(L"pxnxdjxljxk", s.c_str()); } TEST_METHOD(test_08a) { std::vector v{ 1e10, 8, -11.23, 0, 1e10, 1e10, 1e10, 0, 99 }; - // TODO: delete all invalid values (1e10) + v.erase(std::remove_if(std::begin(v), std::end(v), [](auto const & val) {return 1e10 == val; }), std::end(v)); Assert::AreEqual(5u, v.size()); Assert::AreEqual(8., v[0]); Assert::AreEqual(99., v[4]); @@ -136,14 +135,14 @@ TEST_CLASS(test_standard_algorithms_usage) TEST_METHOD(test_08b) { std::wstring s(L"ponedjeljak"); - // TODO: delete all vowels + s.erase(std::remove_if(s.begin(), s.end(), [](const char c) { return std::string("aeiou").find(c) != std::string::npos; }), std::end(s)); Assert::AreEqual(L"pndjljk", s.c_str()); } TEST_METHOD(test_09) { struct exam { std::wstring name; int points, grade; }; std::vector v{ {L"Pero", 55, 2}, {L"Iva", 93, 5}, {L"Marko", 89, 5} }; - // TODO: sort vector by grade, then by points + std::sort(std::begin(v), std::end(v), [](auto const & val1, auto const & val2) {return val1.grade != val2.grade ? val1.grade > val2.grade : val1.points > val2.points; }); Assert::AreEqual(L"Iva", v[0].name.c_str()); Assert::AreEqual(L"Marko", v[1].name.c_str()); Assert::AreEqual(L"Pero", v[2].name.c_str()); @@ -162,7 +161,7 @@ TEST_CLASS(test_standard_algorithms_usage) using namespace std::chrono; auto t1 = steady_clock::now(); - // TODO: put median value in the middle of vector. fast. + std::iter_swap(std::begin(v) + v.size() / 2, std::find(std::begin(v), std::end(v), 1000.)); auto t2 = steady_clock::now(); auto dur = duration_cast(t2 - t1); Assert::AreEqual(1000., v[v.size() / 2]); // median value @@ -179,16 +178,15 @@ TEST_CLASS(test_standard_algorithms_usage) TEST_METHOD(test_12) { std::vector v{ 11, 0.5, -97.23, -23.11, 48.78, 22.96, -77 }; - auto smallest_value = // TODO: + double smallest_value = *std::min_element(std::cbegin(v), std::cend(v)); Assert::AreEqual(-97.23, smallest_value); - auto largest_value = // TODO: + double largest_value = *std::max_element(std::cbegin(v), std::cend(v)); Assert::AreEqual(48.78, largest_value); } TEST_METHOD(test_13) { std::vector atp_points { 8445, 7480, 6220, 5300, 5285 }; - // the most interesting match is the one with the smallest difference - auto smallest_difference = // TODO: + int smallest_difference = *std::min_element(std::begin(atp_points), std::adjacent_difference(std::begin(atp_points), std::end(atp_points), std::begin(atp_points), [](auto a, auto b) { return std::abs(a - b); })); Assert::AreEqual(15, smallest_difference); } }; diff --git a/exercise/02/e02.vcxproj b/exercise/02/e02.vcxproj index 7820fff..26f745a 100644 --- a/exercise/02/e02.vcxproj +++ b/exercise/02/e02.vcxproj @@ -1,93 +1,94 @@ - - - - - Debug - Win32 - - - Release - Win32 - - - - {DD752318-EA08-4316-9EFE-C1A42F5526F4} - Win32Proj - e02 - 10.0.17134.0 - - - - DynamicLibrary - true - v141 - Unicode - false - - - DynamicLibrary - false - v141 - true - Unicode - false - - - - - - - - - - - - - true - . - - - true - . - - - - NotUsing - Level3 - Disabled - $(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) - WIN32;_DEBUG;%(PreprocessorDefinitions) - true - - - Windows - true - $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) - - - - - Level3 - NotUsing - MaxSpeed - true - true - $(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) - WIN32;NDEBUG;%(PreprocessorDefinitions) - true - - - Windows - true - true - true - $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) - - - - - - - - + + + + + Debug + Win32 + + + Release + Win32 + + + + {DD752318-EA08-4316-9EFE-C1A42F5526F4} + Win32Proj + e02 + 10.0.17134.0 + + + + DynamicLibrary + true + v141 + Unicode + false + + + DynamicLibrary + false + v141 + true + Unicode + false + + + + + + + + + + + + + true + . + + + true + . + + + + NotUsing + Level3 + Disabled + $(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) + WIN32;_DEBUG;%(PreprocessorDefinitions) + true + + + Windows + true + $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) + + + + + Level3 + NotUsing + MaxSpeed + true + true + $(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) + WIN32;NDEBUG;%(PreprocessorDefinitions) + true + stdcpp17 + + + Windows + true + true + true + $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) + + + + + + + + \ No newline at end of file