From 0038ff5f04b272004897ffa0ffb81b46ca200f50 Mon Sep 17 00:00:00 2001 From: Marina Krcek Date: Thu, 15 Nov 2018 14:12:26 +0100 Subject: [PATCH 1/6] Day 04, Exercise 01, containers - make unit tests pass :) --- exercise/01/containers.cpp | 65 ++++++++++++++++++++++++++++++++++++++ exercise/01/containers.h | 2 ++ 2 files changed, 67 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..4a007fe --- /dev/null +++ b/exercise/01/containers.cpp @@ -0,0 +1,65 @@ +#include "containers.h" +#include +#include +//#include + +void remove_element(std::vector& v, int index) +{ + v.erase(v.begin()+index); +} + +void input_element(std::vector& v, int index, const std::wstring& value) +{ + v.insert(v.begin() + index, value); +} + +int list_nth_element(const std::list& c, int index) +{ + auto it = c.begin(); + std::advance(it, index); + return it != c.end() ? *it : 0; +} + +void list_sort_desc(std::list& c) +{ + c.sort(std::greater()); +} + +int unique_numbers(std::wistream& wis) +{ + std::unordered_set s; + int n; + while (wis >> n) + s.insert(n); + return s.size(); +} + +namespace +{ + std::wstring to_lower(const std::wstring& wstr) + { + std::wstring w(wstr); + for (wchar_t& wc : w) + wc = ::towlower(wc); + //std::transform(w.begin(), w.end(), w.begin(), ::towlower); + return w; + } +} + +word_frequency::word_frequency(std::wistream& wis) +{ + std::wstring w; + while (wis >> w) + word_freq[to_lower(w)]++; +} + +int word_frequency::frequency(const std::wstring & s) +{ + auto ws = to_lower(s); + return word_freq.find(ws) != word_freq.end() ? word_freq[ws] : 0; +} + +int word_frequency::count() +{ + return word_freq.size(); +} diff --git a/exercise/01/containers.h b/exercise/01/containers.h index 8e1b172..f04fb17 100644 --- a/exercise/01/containers.h +++ b/exercise/01/containers.h @@ -2,6 +2,7 @@ #include #include #include +#include void remove_element(std::vector& v, int index); void input_element(std::vector& v, int index, const std::wstring& value); @@ -13,6 +14,7 @@ int unique_numbers(std::wistream&); class word_frequency { + std::map word_freq; public: word_frequency(std::wistream&); int frequency(const std::wstring& s); From 89c849e8914b64b6800909b4cb292591d9db91a1 Mon Sep 17 00:00:00 2001 From: Marina Krcek Date: Thu, 15 Nov 2018 17:04:27 +0100 Subject: [PATCH 2/6] Day 4, Exercise 02, stl algorithms_test --- exercise/02/algorithms_test.cpp | 201 +++++++++++++++++--------------- 1 file changed, 108 insertions(+), 93 deletions(-) diff --git a/exercise/02/algorithms_test.cpp b/exercise/02/algorithms_test.cpp index 9df3595..aeea11a 100644 --- a/exercise/02/algorithms_test.cpp +++ b/exercise/02/algorithms_test.cpp @@ -16,7 +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]); @@ -27,6 +27,7 @@ TEST_CLASS(test_standard_algorithms_usage) { std::vector v(10); // TODO: fill vector with incremental values + std::iota(v.begin(), v.end(), 1); Assert::AreEqual(10u, v.size()); Assert::IsTrue(std::is_sorted(v.cbegin(), v.cend())); Assert::AreEqual( 1, v[0]); @@ -37,28 +38,29 @@ TEST_CLASS(test_standard_algorithms_usage) // generate std::vector v(10); // TODO: fill vector with incremental values (by 2) + std::generate(v.begin(), v.end(), [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]); Assert::AreEqual(19, v[9]); } - TEST_METHOD(test_03a) - { - std::vector v = { 1, 5, 10 } ; - // TODO: change all values in a vector - Assert::AreEqual(3u, v.size()); - Assert::AreEqual(1, v[0]); - Assert::AreEqual(125, v[1]); - Assert::AreEqual(1000, v[2]); - } + //TEST_METHOD(test_03a) + //{ + // std::vector v = { 1, 5, 10 } ; + // // TODO: change all values in a vector + // Assert::AreEqual(3u, v.size()); + // Assert::AreEqual(1, v[0]); + // Assert::AreEqual(125, v[1]); + // Assert::AreEqual(1000, v[2]); + //} TEST_METHOD(test_03b) { 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 + std::transform(std::begin(x), std::end(x), y.begin(), std::back_inserter(d), [](int a, int b) { return sqrt(a*a + b * b); }); Assert::AreEqual(3u, d.size()); Assert::AreEqual(5., d[0]); Assert::AreEqual(13., d[1]); @@ -67,128 +69,141 @@ 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 + // TODO: sum of all values in input stream + auto res = std::accumulate(std::istream_iterator(ss), std::istream_iterator(), 0.0); 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 + // TODO: concatenated string with additional prefix + auto res = std::accumulate(v.begin(), v.end(), std::wstring(L"GO ")); 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 - Assert::AreEqual(58, total_age); - } + //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 + // 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(v.begin(), v.end(), [](int v) {return isless(v, 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(v.begin(), v.end(), [](double v) {return isgreater(v, 100.0); }); // ??? 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(v.begin(), v.end(), [](point 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); - } + //TEST_METHOD(test_06) + //{ + // std::vector v { 33, 16, 24, 41, 25, 19, 9 }; + // 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(v.begin(), v.end(), [](double v) {return isgreater(v, 100.0); }, -1.0); Assert::AreEqual(-1., v[0]); Assert::AreEqual(-1., v[4]); Assert::AreEqual(-1., v[6]); } + bool is_vowel(const wchar_t c) + { + return (c == 'a' || c == 'A' || + c == 'e' || c == 'E' || + c == 'i' || c == 'I' || + c == 'o' || c == 'O' || + c == 'u' || c == 'U'); + } + TEST_METHOD(test_07b) { std::wstring s(L"ponedjeljak"); // TODO: change every vowel with x + std::replace_if(s.begin(), s.end(), [&](wchar_t c) { return is_vowel(c); }, '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) - Assert::AreEqual(5u, v.size()); - Assert::AreEqual(8., v[0]); - Assert::AreEqual(99., v[4]); - } + //TEST_METHOD(test_08a) + //{ + // std::vector v{ 1e10, 8, -11.23, 0, 1e10, 1e10, 1e10, 0, 99 }; + // // TODO: delete all invalid values (1e10) + // Assert::AreEqual(5u, v.size()); + // Assert::AreEqual(8., v[0]); + // Assert::AreEqual(99., v[4]); + //} - TEST_METHOD(test_08b) - { - std::wstring s(L"ponedjeljak"); - // TODO: delete all vowels - 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 - 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()); + //TEST_METHOD(test_08b) + //{ + // std::wstring s(L"ponedjeljak"); + // // TODO: delete all vowels + // 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 + // 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()); - } - TEST_METHOD(test_10a) - { - // nth_element - std::vector v(2e7); - // half of the values less than 1000 - std::generate(v.begin(), v.begin() + v.size() / 2, []() { return rand() % 1000; }); - // other half of the values greater than 1000 - std::generate(v.begin() + v.size() / 2, v.end(), []() { return 1001 + rand() % 1000; }); - v.push_back(1000); // to be median - std::random_shuffle(v.begin(), v.end()); + //} + //TEST_METHOD(test_10a) + //{ + // // nth_element + // std::vector v(2e7); + // // half of the values less than 1000 + // std::generate(v.begin(), v.begin() + v.size() / 2, []() { return rand() % 1000; }); + // // other half of the values greater than 1000 + // std::generate(v.begin() + v.size() / 2, v.end(), []() { return 1001 + rand() % 1000; }); + // v.push_back(1000); // to be median + // std::random_shuffle(v.begin(), v.end()); - using namespace std::chrono; - auto t1 = steady_clock::now(); - // TODO: put median value in the middle of vector. fast. - auto t2 = steady_clock::now(); - auto dur = duration_cast(t2 - t1); - Assert::AreEqual(1000., v[v.size() / 2]); // median value - Assert::IsTrue(dur.count() < 1000, std::to_wstring(dur.count()).c_str()); // faster than 1 second - } - TEST_METHOD(test_10b) - { - struct employee { std::wstring name; int salary; }; - std::vector v{ {L"Iva", 2000}, {L"Pero", 1000}, {L"Marko", 10000} }; - // TODO: put employee with median salary in the middle of vector - std::nth_element(v.begin(), v.begin() + v.size() / 2, v.end(), [](const employee& a, const employee& b) { return a.salary < b.salary; }); - Assert::AreEqual(L"Iva", v[v.size() / 2].name.c_str()); // median_salary - } - TEST_METHOD(test_12) - { - std::vector v{ 11, 0.5, -97.23, -23.11, 48.78, 22.96, -77 }; - auto smallest_value = // TODO: - Assert::AreEqual(-97.23, smallest_value); - auto largest_value = // TODO: - 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: - Assert::AreEqual(15, smallest_difference); - } + // using namespace std::chrono; + // auto t1 = steady_clock::now(); + // // TODO: put median value in the middle of vector. fast. + // auto t2 = steady_clock::now(); + // auto dur = duration_cast(t2 - t1); + // Assert::AreEqual(1000., v[v.size() / 2]); // median value + // Assert::IsTrue(dur.count() < 1000, std::to_wstring(dur.count()).c_str()); // faster than 1 second + //} + //TEST_METHOD(test_10b) + //{ + // struct employee { std::wstring name; int salary; }; + // std::vector v{ {L"Iva", 2000}, {L"Pero", 1000}, {L"Marko", 10000} }; + // // TODO: put employee with median salary in the middle of vector + // std::nth_element(v.begin(), v.begin() + v.size() / 2, v.end(), [](const employee& a, const employee& b) { return a.salary < b.salary; }); + // Assert::AreEqual(L"Iva", v[v.size() / 2].name.c_str()); // median_salary + //} + //TEST_METHOD(test_12) + //{ + // std::vector v{ 11, 0.5, -97.23, -23.11, 48.78, 22.96, -77 }; + // auto smallest_value = // TODO: + // Assert::AreEqual(-97.23, smallest_value); + // auto largest_value = // TODO: + // 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: + // Assert::AreEqual(15, smallest_difference); + //} }; From bed17cec3e3a030824f950d29a58d00dd27b301a Mon Sep 17 00:00:00 2001 From: Marina Krcek Date: Fri, 16 Nov 2018 11:31:12 +0100 Subject: [PATCH 3/6] Day 4, Exercise 02, stl algorithms_test - make unit tests pass --- exercise/02/algorithms_test.cpp | 211 +++++++++++++++++++------------- 1 file changed, 123 insertions(+), 88 deletions(-) diff --git a/exercise/02/algorithms_test.cpp b/exercise/02/algorithms_test.cpp index aeea11a..67c36d6 100644 --- a/exercise/02/algorithms_test.cpp +++ b/exercise/02/algorithms_test.cpp @@ -45,15 +45,16 @@ TEST_CLASS(test_standard_algorithms_usage) Assert::AreEqual(19, v[9]); } - //TEST_METHOD(test_03a) - //{ - // std::vector v = { 1, 5, 10 } ; - // // TODO: change all values in a vector - // Assert::AreEqual(3u, v.size()); - // Assert::AreEqual(1, v[0]); - // Assert::AreEqual(125, v[1]); - // Assert::AreEqual(1000, v[2]); - //} + TEST_METHOD(test_03a) + { + std::vector v = { 1, 5, 10 } ; + // TODO: change all values in a vector + std::transform(v.begin(), v.end(), v.begin(), [](int val) { return val * val * val; }); + Assert::AreEqual(3u, v.size()); + Assert::AreEqual(1, v[0]); + Assert::AreEqual(125, v[1]); + Assert::AreEqual(1000, v[2]); + } TEST_METHOD(test_03b) { int x[] = { 3, 5, 10 }; @@ -80,13 +81,14 @@ TEST_CLASS(test_standard_algorithms_usage) auto res = std::accumulate(v.begin(), v.end(), std::wstring(L"GO ")); 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 - // Assert::AreEqual(58, total_age); - //} + TEST_METHOD(test_04c) + { + struct person { std::wstring name; int age; }; + std::vector v{ {L"Pero", 33}, {L"Iva", 25} }; + // TODO: sum of all ages + auto total_age = std::accumulate(v.begin(), v.end(), 0, [](const int& a, const person& p) { return a + p.age; }); + Assert::AreEqual(58, total_age); + } TEST_METHOD(test_05a) { @@ -97,7 +99,7 @@ TEST_CLASS(test_standard_algorithms_usage) TEST_METHOD(test_05b) { std::vector v { 1.5, 8, -11.23, 0, 1e10, 1e10, 1e10, 0, 99 }; - auto number_of_invalid = std::count_if(v.begin(), v.end(), [](double v) {return isgreater(v, 100.0); }); // ??? + auto number_of_invalid = std::count(v.begin(), v.end(), 1e10); Assert::AreEqual(3, number_of_invalid); } TEST_METHOD(test_05c) @@ -108,17 +110,27 @@ TEST_CLASS(test_standard_algorithms_usage) 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); - //} + bool is_prime(int n) + { + for (int i = 2; i <= n / 2; ++i) + { + if (n%i == 0) + return false; + } + return true; + } + + TEST_METHOD(test_06) + { + std::vector v { 33, 16, 24, 41, 25, 19, 9 }; + auto first_prime = *std::find_if(v.begin(), v.end(), [&](int val) {return is_prime(val); }); + 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(v.begin(), v.end(), [](double v) {return isgreater(v, 100.0); }, -1.0); + std::replace(v.begin(), v.end(), 1e10, -1.0); Assert::AreEqual(-1., v[0]); Assert::AreEqual(-1., v[4]); Assert::AreEqual(-1., v[6]); @@ -139,71 +151,94 @@ TEST_CLASS(test_standard_algorithms_usage) std::replace_if(s.begin(), s.end(), [&](wchar_t c) { return is_vowel(c); }, '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) - // Assert::AreEqual(5u, v.size()); - // Assert::AreEqual(8., v[0]); - // Assert::AreEqual(99., v[4]); - //} + 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(v.begin(), v.end(), 1e10), v.end()); + Assert::AreEqual(5u, v.size()); + Assert::AreEqual(8., v[0]); + Assert::AreEqual(99., v[4]); + } + + TEST_METHOD(test_08b) + { + std::wstring s(L"ponedjeljak"); + // TODO: delete all vowels + s.erase(std::remove_if(s.begin(), s.end(), [&](wchar_t c) { return is_vowel(c); }), s.end()); + Assert::AreEqual(L"pndjljk", s.c_str()); + } + + TEST_METHOD(test_09) + { + struct exam + { + std::wstring name; int points, grade; + bool operator<(const exam& other) + { + if (grade < other.grade) return true; + if (grade > other.grade) return false; + if (points < other.points) return true; + if (points > other.points) return false; + return false; + } + }; - //TEST_METHOD(test_08b) - //{ - // std::wstring s(L"ponedjeljak"); - // // TODO: delete all vowels - // 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 - // 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()); + 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(v.begin(), v.end()); + std::reverse(v.begin(), v.end()); + 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()); + } - //} - //TEST_METHOD(test_10a) - //{ - // // nth_element - // std::vector v(2e7); - // // half of the values less than 1000 - // std::generate(v.begin(), v.begin() + v.size() / 2, []() { return rand() % 1000; }); - // // other half of the values greater than 1000 - // std::generate(v.begin() + v.size() / 2, v.end(), []() { return 1001 + rand() % 1000; }); - // v.push_back(1000); // to be median - // std::random_shuffle(v.begin(), v.end()); + TEST_METHOD(test_10a) + { + // nth_element + std::vector v(2e7); + // half of the values less than 1000 + std::generate(v.begin(), v.begin() + v.size() / 2, []() { return rand() % 1000; }); + // other half of the values greater than 1000 + std::generate(v.begin() + v.size() / 2, v.end(), []() { return 1001 + rand() % 1000; }); + v.push_back(1000); // to be median + std::random_shuffle(v.begin(), v.end()); - // using namespace std::chrono; - // auto t1 = steady_clock::now(); - // // TODO: put median value in the middle of vector. fast. - // auto t2 = steady_clock::now(); - // auto dur = duration_cast(t2 - t1); - // Assert::AreEqual(1000., v[v.size() / 2]); // median value - // Assert::IsTrue(dur.count() < 1000, std::to_wstring(dur.count()).c_str()); // faster than 1 second - //} - //TEST_METHOD(test_10b) - //{ - // struct employee { std::wstring name; int salary; }; - // std::vector v{ {L"Iva", 2000}, {L"Pero", 1000}, {L"Marko", 10000} }; - // // TODO: put employee with median salary in the middle of vector - // std::nth_element(v.begin(), v.begin() + v.size() / 2, v.end(), [](const employee& a, const employee& b) { return a.salary < b.salary; }); - // Assert::AreEqual(L"Iva", v[v.size() / 2].name.c_str()); // median_salary - //} - //TEST_METHOD(test_12) - //{ - // std::vector v{ 11, 0.5, -97.23, -23.11, 48.78, 22.96, -77 }; - // auto smallest_value = // TODO: - // Assert::AreEqual(-97.23, smallest_value); - // auto largest_value = // TODO: - // 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: - // Assert::AreEqual(15, smallest_difference); - //} + using namespace std::chrono; + int half_size = v.size() / 2; + auto t1 = steady_clock::now(); + // TODO: put median value in the middle of vector. fast. + std::nth_element(v.begin(), v.begin() + half_size, v.end()); + auto t2 = steady_clock::now(); + auto dur = duration_cast(t2 - t1); + Assert::AreEqual(1000., v[v.size() / 2]); // median value + Assert::IsTrue(dur.count() < 1000, std::to_wstring(dur.count()).c_str()); // faster than 1 second + } + TEST_METHOD(test_10b) + { + struct employee { std::wstring name; int salary; }; + std::vector v{ {L"Iva", 2000}, {L"Pero", 1000}, {L"Marko", 10000} }; + // TODO: put employee with median salary in the middle of vector + + std::nth_element(v.begin(), v.begin() + v.size() / 2, v.end(), [](const employee& a, const employee& b) { return a.salary < b.salary; }); + Assert::AreEqual(L"Iva", v[v.size() / 2].name.c_str()); // median_salary + } + TEST_METHOD(test_12) + { + std::vector v{ 11, 0.5, -97.23, -23.11, 48.78, 22.96, -77 }; + auto smallest_value = *std::min_element(v.begin(), v.end()); + Assert::AreEqual(-97.23, smallest_value); + auto largest_value = *std::max_element(v.begin(), v.end()); + 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 + std::sort(atp_points.begin(), atp_points.end()); + std::vector diff; + std::adjacent_difference(atp_points.begin(), atp_points.end(), std::back_inserter(diff)); + auto smallest_difference = *std::min_element(diff.begin(), diff.end()); + Assert::AreEqual(15, smallest_difference); + } }; From 7b4c841f3f2b8dde843d8a8f08d40b0dc30cd8ec Mon Sep 17 00:00:00 2001 From: Marina Krcek Date: Fri, 16 Nov 2018 11:34:23 +0100 Subject: [PATCH 4/6] Day 4, Exercise 02, stl algorithms_test - improve difference test --- exercise/02/algorithms_test.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/exercise/02/algorithms_test.cpp b/exercise/02/algorithms_test.cpp index 67c36d6..d9da366 100644 --- a/exercise/02/algorithms_test.cpp +++ b/exercise/02/algorithms_test.cpp @@ -235,10 +235,8 @@ TEST_CLASS(test_standard_algorithms_usage) { std::vector atp_points { 8445, 7480, 6220, 5300, 5285 }; // the most interesting match is the one with the smallest difference - std::sort(atp_points.begin(), atp_points.end()); - std::vector diff; - std::adjacent_difference(atp_points.begin(), atp_points.end(), std::back_inserter(diff)); - auto smallest_difference = *std::min_element(diff.begin(), diff.end()); + std::adjacent_difference(atp_points.begin(), atp_points.end(), atp_points.begin(), [](const int x, const int y) {return abs(x - y); }); + auto smallest_difference = *std::min_element(atp_points.begin(), atp_points.end()); Assert::AreEqual(15, smallest_difference); } }; From 08333494bd757610ebc132547efa153b56da4b6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marina=20Kr=C4=8Dek?= Date: Mon, 19 Nov 2018 22:52:23 +0100 Subject: [PATCH 5/6] Day 4, fixes according to comments - find and operator[] searched through map two times, use what find found if it did - use towlower to avoid 10 comparisons - 4 if statements replaced with ternary operator - replace sort+reverse with sort using reverse iterator - add another option with lambda for opposite comparison (if there is no overloaded operator<) --- exercise/01/containers.cpp | 5 +++-- exercise/02/algorithms_test.cpp | 19 ++++++------------- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/exercise/01/containers.cpp b/exercise/01/containers.cpp index 4a007fe..a43355e 100644 --- a/exercise/01/containers.cpp +++ b/exercise/01/containers.cpp @@ -55,8 +55,9 @@ word_frequency::word_frequency(std::wistream& wis) int word_frequency::frequency(const std::wstring & s) { - auto ws = to_lower(s); - return word_freq.find(ws) != word_freq.end() ? word_freq[ws] : 0; + const auto ws = to_lower(s); + const auto it = word_freq.find(ws); + return it != word_freq.end() ? it->second : 0; } int word_frequency::count() diff --git a/exercise/02/algorithms_test.cpp b/exercise/02/algorithms_test.cpp index d9da366..fd7e210 100644 --- a/exercise/02/algorithms_test.cpp +++ b/exercise/02/algorithms_test.cpp @@ -135,13 +135,10 @@ TEST_CLASS(test_standard_algorithms_usage) Assert::AreEqual(-1., v[4]); Assert::AreEqual(-1., v[6]); } - bool is_vowel(const wchar_t c) + bool is_vowel(wchar_t c) { - return (c == 'a' || c == 'A' || - c == 'e' || c == 'E' || - c == 'i' || c == 'I' || - c == 'o' || c == 'O' || - c == 'u' || c == 'U'); + c = towlower(c); + return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' ); } TEST_METHOD(test_07b) @@ -176,18 +173,14 @@ TEST_CLASS(test_standard_algorithms_usage) std::wstring name; int points, grade; bool operator<(const exam& other) { - if (grade < other.grade) return true; - if (grade > other.grade) return false; - if (points < other.points) return true; - if (points > other.points) return false; - return false; + return grade != other.grade ? grade < other.grade : points < other.points; } }; 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(v.begin(), v.end()); - std::reverse(v.begin(), v.end()); + std::sort(v.rbegin(), v.rend()); + //std::sort(v.begin(), v.end(), [](const exam& l, const exam& r) { return l.grade != r.grade ? l.grade > r.grade : l.points > r.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()); From 34355c0932c8309365af953136f6fe3e49b80659 Mon Sep 17 00:00:00 2001 From: Marina Krcek Date: Tue, 20 Nov 2018 16:30:08 +0100 Subject: [PATCH 6/6] Day 4 - don't use lambdas for is_prime and is_vowel --- exercise/02/algorithms_test.cpp | 39 ++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/exercise/02/algorithms_test.cpp b/exercise/02/algorithms_test.cpp index fd7e210..fc43154 100644 --- a/exercise/02/algorithms_test.cpp +++ b/exercise/02/algorithms_test.cpp @@ -8,6 +8,24 @@ using namespace Microsoft::VisualStudio::CppUnitTestFramework; #include #include +namespace +{ +bool is_vowel(wchar_t c) +{ + c = towlower(c); + return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'); +} + +bool is_prime(int n) +{ + for (int i = 2; i <= n / 2; ++i) + { + if (n%i == 0) + return false; + } + return true; +} +} TEST_CLASS(test_standard_algorithms_usage) { public: @@ -110,20 +128,10 @@ TEST_CLASS(test_standard_algorithms_usage) Assert::AreEqual(2, number_in_first_quadrant); } - bool is_prime(int n) - { - for (int i = 2; i <= n / 2; ++i) - { - if (n%i == 0) - return false; - } - return true; - } - TEST_METHOD(test_06) { std::vector v { 33, 16, 24, 41, 25, 19, 9 }; - auto first_prime = *std::find_if(v.begin(), v.end(), [&](int val) {return is_prime(val); }); + auto first_prime = *std::find_if(v.begin(), v.end(), is_prime); Assert::AreEqual(41, first_prime); } TEST_METHOD(test_07a) @@ -135,17 +143,12 @@ TEST_CLASS(test_standard_algorithms_usage) Assert::AreEqual(-1., v[4]); Assert::AreEqual(-1., v[6]); } - bool is_vowel(wchar_t c) - { - c = towlower(c); - return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' ); - } TEST_METHOD(test_07b) { std::wstring s(L"ponedjeljak"); // TODO: change every vowel with x - std::replace_if(s.begin(), s.end(), [&](wchar_t c) { return is_vowel(c); }, 'x'); + std::replace_if(s.begin(), s.end(), is_vowel, 'x'); Assert::AreEqual(L"pxnxdjxljxk", s.c_str()); } TEST_METHOD(test_08a) @@ -162,7 +165,7 @@ TEST_CLASS(test_standard_algorithms_usage) { std::wstring s(L"ponedjeljak"); // TODO: delete all vowels - s.erase(std::remove_if(s.begin(), s.end(), [&](wchar_t c) { return is_vowel(c); }), s.end()); + s.erase(std::remove_if(s.begin(), s.end(), is_vowel), s.end()); Assert::AreEqual(L"pndjljk", s.c_str()); }