From 62e6283ccb20fce838365beb9c138421307a0b7d Mon Sep 17 00:00:00 2001 From: Tin Serdar Date: Thu, 15 Nov 2018 14:04:13 +0100 Subject: [PATCH 1/3] Rijesenja day4 --- exercise/01/containers.cpp | 62 ++++++++++++++++++++++++++++++++++++++ exercise/01/containers.h | 8 ++++- 2 files changed, 69 insertions(+), 1 deletion(-) 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..d650eb6 --- /dev/null +++ b/exercise/01/containers.cpp @@ -0,0 +1,62 @@ +#include "containers.h" + +void remove_element(std::vector& v, int index) +{ + v.erase(v.cbegin() + index); +} + +void input_element(std::vector& v, int index, const std::wstring & value) +{ + v.insert(v.cbegin() + index, value); +} + +int list_nth_element(const std::list& c, int index) +{ + auto it = c.begin(); + std::advance(it, index); + + return *it; +} + +void list_sort_desc(std::list& c) +{ + c.sort(); + c.reverse(); +} + +int unique_numbers(std::wistream& is) +{ + std::set uniques; + std::wstring temp; + while (is >> temp) + { + uniques.insert(stoi(temp)); + } + return uniques.size(); +} + +word_frequency::word_frequency(std::wistream & is) +{ + split(is); +} + +void word_frequency::split(std::wistream & is) +{ + std::wstring temp; + while (is >> temp) + { + std::transform(temp.begin(), temp.end(), temp.begin(), ::towlower); + m_stringMultiSet.insert(temp); + m_stringSet.insert(temp); + } +} + +int word_frequency::frequency(const std::wstring & s) +{ + return m_stringMultiSet.count(s); +} + +int word_frequency::count() +{ + return m_stringSet.size(); +} diff --git a/exercise/01/containers.h b/exercise/01/containers.h index 8e1b172..0a5ab7a 100644 --- a/exercise/01/containers.h +++ b/exercise/01/containers.h @@ -2,6 +2,8 @@ #include #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,8 +15,12 @@ int unique_numbers(std::wistream&); class word_frequency { +private: + std::multiset m_stringMultiSet; + std::set m_stringSet; public: - word_frequency(std::wistream&); + word_frequency(std::wistream&); + void split(std::wistream& is); int frequency(const std::wstring& s); int count(); }; From cab0f606bacbb4d7c0490c1b724acfa5d307d100 Mon Sep 17 00:00:00 2001 From: Tin Serdar Date: Thu, 15 Nov 2018 19:11:17 +0100 Subject: [PATCH 2/3] Rijesenja exercise02 --- exercise/02/algorithms_test.cpp | 96 +++++++++++++++++---------------- 1 file changed, 51 insertions(+), 45 deletions(-) diff --git a/exercise/02/algorithms_test.cpp b/exercise/02/algorithms_test.cpp index 9df3595..28e6396 100644 --- a/exercise/02/algorithms_test.cpp +++ b/exercise/02/algorithms_test.cpp @@ -7,36 +7,35 @@ using namespace Microsoft::VisualStudio::CppUnitTestFramework; #include #include #include +#include TEST_CLASS(test_standard_algorithms_usage) { public: - TEST_METHOD(test_01) - { - std::wstringstream ss(L"14 -78 22"); - std::vector v; - // TODO: read values from input stream into vector - - Assert::AreEqual(3u, v.size()); - Assert::AreEqual(14, v[0]); - Assert::AreEqual(-78, v[1]); - Assert::AreEqual(22, v[2]); - - } - TEST_METHOD(test_02a) - { - std::vector v(10); - // TODO: fill vector with incremental values - Assert::AreEqual(10u, v.size()); - Assert::IsTrue(std::is_sorted(v.cbegin(), v.cend())); - Assert::AreEqual( 1, v[0]); - Assert::AreEqual(10, v[9]); - } - TEST_METHOD(test_02b) - { - // generate - std::vector v(10); - // TODO: fill vector with incremental values (by 2) + TEST_METHOD(test_01) + { + std::wstringstream ss(L"14 -78 22"); + std::vector v; + std::transform(std::istream_iterator(ss), std::istream_iterator(), std::back_inserter(v), [](auto string) -> int {return std::stoi(string.c_str()); }); + Assert::AreEqual(3u, v.size()); + Assert::AreEqual(14, v[0]); + Assert::AreEqual(-78, v[1]); + Assert::AreEqual(22, v[2]); + } + TEST_METHOD(test_02a) + { + std::vector v(10); + 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]); + Assert::AreEqual(10, v[9]); + } + TEST_METHOD(test_02b) + { + // generate + std::vector v(10); + std::generate(v.begin(), v.end(), []() {static int i = -1; i += 2; return i; }); 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 +45,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::for_each(v.begin(), v.end(), [](int &num) {num = pow(num, 3); }); Assert::AreEqual(3u, v.size()); Assert::AreEqual(1, v[0]); Assert::AreEqual(125, v[1]); @@ -57,8 +56,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 + std::transform(std::begin(x), std::end(x), y.begin(), std::back_inserter(d), [](auto x, auto y) {return std::hypot(x, y); }); Assert::AreEqual(3u, d.size()); Assert::AreEqual(5., d[0]); Assert::AreEqual(13., d[1]); @@ -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 a, auto string) -> double {return a + std::stod(string.c_str()); }); 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(v.begin(), v.end(), std::wstring(L"GO "), [](std::wstring a, std::wstring b) -> std::wstring {return a + b; }); 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 + TEST_METHOD(test_04c) + { + struct person { std::wstring name; int age; }; + std::vector v{ {L"Pero", 33}, {L"Iva", 25} }; + auto total_age = std::accumulate(v.begin(), v.end(), 0, [](int sum, const person& person) -> int {return sum + person.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(v.begin(), v.end(), [](const int& a) {return (a < 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(), [](const double& a) {return (a == 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(v.begin(), v.end(), [](const point& a) {return (a.x > 0 && a.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); + //std::vector v { 33, 16, 24, 41, 25, 19, 9 }; + // auto first_prime = std::find_if(v.begin(), v.end(), [](const int& num) {return true; }); + //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(v.begin(), v.end(), 1e10, -1.); Assert::AreEqual(-1., v[0]); Assert::AreEqual(-1., v[4]); Assert::AreEqual(-1., v[6]); @@ -122,12 +121,14 @@ TEST_CLASS(test_standard_algorithms_usage) { std::wstring s(L"ponedjeljak"); // TODO: change every vowel with x + std::replace_if(s.begin(), s.end(), [](auto c) {std::wstring s(L"aeiou"); return (s.find_first_of(c) != std::wstring::npos); }, L'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(v.begin(), v.end(), 1e10), v.end()); Assert::AreEqual(5u, v.size()); Assert::AreEqual(8., v[0]); Assert::AreEqual(99., v[4]); @@ -137,6 +138,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(), [](auto c) {std::wstring s(L"aeiou"); return (s.find_first_of(c) != std::wstring::npos); }), s.end()); Assert::AreEqual(L"pndjljk", s.c_str()); } TEST_METHOD(test_09) @@ -144,6 +146,7 @@ TEST_CLASS(test_standard_algorithms_usage) 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(v.begin(), v.end(), [](exam a, exam b) {return (a.grade > b.grade || a.points > b.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()); @@ -163,6 +166,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::nth_element(v.begin(), v.begin() + v.size() / 2, v.end()); auto t2 = steady_clock::now(); auto dur = duration_cast(t2 - t1); Assert::AreEqual(1000., v[v.size() / 2]); // median value @@ -179,16 +183,18 @@ 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: + auto smallest_value = *(std::min_element(v.begin(), v.end())); Assert::AreEqual(-97.23, smallest_value); - auto largest_value = // TODO: + 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 - auto smallest_difference = // TODO: + std::vector diffs(atp_points.size()); + std::adjacent_difference(atp_points.rbegin(), atp_points.rend(), diffs.rbegin()); + auto smallest_difference = *(std::min_element(diffs.begin(), diffs.end())); Assert::AreEqual(15, smallest_difference); } }; From c631051492fa4fe840a8e0173cbac372d564c75a Mon Sep 17 00:00:00 2001 From: Tin Serdar Date: Fri, 16 Nov 2018 09:22:49 +0100 Subject: [PATCH 3/3] solution for prime numbers --- exercise/02/algorithms_test.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/exercise/02/algorithms_test.cpp b/exercise/02/algorithms_test.cpp index 28e6396..fb8a022 100644 --- a/exercise/02/algorithms_test.cpp +++ b/exercise/02/algorithms_test.cpp @@ -105,9 +105,14 @@ TEST_CLASS(test_standard_algorithms_usage) TEST_METHOD(test_06) { - //std::vector v { 33, 16, 24, 41, 25, 19, 9 }; - // auto first_prime = std::find_if(v.begin(), v.end(), [](const int& num) {return true; }); - //Assert::AreEqual(41, first_prime); + std::vector v { 33, 16, 24, 41, 25, 19, 9 }; + auto first_prime = *(std::find_if(v.begin(), v.end(), [](const int& num) { int flag = true; for (int i = 2; i <= sqrt(num); i++) { + if (num % i == 0) { + flag = false; + break; + } + } return flag; })); + Assert::AreEqual(41, first_prime); } TEST_METHOD(test_07a) {