From f90bd1c150c891b8329803d4ddee5cdc7928358e Mon Sep 17 00:00:00 2001 From: jadranko lucic Date: Thu, 15 Nov 2018 13:46:50 +0100 Subject: [PATCH 1/3] =?UTF-8?q?Jadranko=20Lu=C4=8Di=C4=87,=20day=204=20e?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- exercise/01/containers.cpp | 76 +++++++++++++++ exercise/01/containers.h | 4 + exercise/01/e01.sln | 25 +++++ exercise/01/e01.vcxproj | 192 ++++++++++++++++++------------------- 4 files changed, 201 insertions(+), 96 deletions(-) create mode 100644 exercise/01/containers.cpp create mode 100644 exercise/01/e01.sln diff --git a/exercise/01/containers.cpp b/exercise/01/containers.cpp new file mode 100644 index 0000000..515f34a --- /dev/null +++ b/exercise/01/containers.cpp @@ -0,0 +1,76 @@ +#include +#include +#include "containers.h" +#include +#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) +{ + std::list::const_iterator it = c.begin(); + std::advance(it, index); + return *it; +} +void list_sort_desc(std::list& c) +{ + return c.sort(std::greater()); +} + +int unique_numbers(std::wistream& is) +{ + std::set s; + int v; + while (is >> v) + { + s.insert(v); + } + return s.size(); + +} + +word_frequency::word_frequency(std::wistream& is) +{ + std::wstring word; + std::locale loc(""); + while (is >> word) + { + std::transform(word.begin(), word.end(), word.begin(), ::towlower); + ++_m[word]; + //if (_m.find(word) == _m.end()) { + // // not found + // _m[word] = 1; + //} + //else { + // ++_m[word]; + //} + } +}; + +int word_frequency::frequency(const std::wstring& s) +{ + std::map::const_iterator i = _m.find(s); + + if (i == _m.end()) { + return 0; + } + else + { + return _m[s]; + } +} +int word_frequency::count() +{ + return _m.size(); +} \ No newline at end of file diff --git a/exercise/01/containers.h b/exercise/01/containers.h index 8e1b172..da95c9d 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,9 @@ int unique_numbers(std::wistream&); class word_frequency { +private: + std::map _m; + public: word_frequency(std::wistream&); int frequency(const std::wstring& s); diff --git a/exercise/01/e01.sln b/exercise/01/e01.sln new file mode 100644 index 0000000..ea19bb8 --- /dev/null +++ b/exercise/01/e01.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.27703.2026 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "e01", "e01.vcxproj", "{DD752318-EA08-4316-9EFE-C1A42F5526F4}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x86 = Debug|x86 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {DD752318-EA08-4316-9EFE-C1A42F5526F4}.Debug|x86.ActiveCfg = Debug|Win32 + {DD752318-EA08-4316-9EFE-C1A42F5526F4}.Debug|x86.Build.0 = Debug|Win32 + {DD752318-EA08-4316-9EFE-C1A42F5526F4}.Release|x86.ActiveCfg = Release|Win32 + {DD752318-EA08-4316-9EFE-C1A42F5526F4}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {71A2FFEB-3CF5-4512-AC29-28C2110C8EF5} + EndGlobalSection +EndGlobal diff --git a/exercise/01/e01.vcxproj b/exercise/01/e01.vcxproj index 8db291c..5b9acd7 100644 --- a/exercise/01/e01.vcxproj +++ b/exercise/01/e01.vcxproj @@ -1,97 +1,97 @@ - - - - - Debug - Win32 - - - Release - Win32 - - - - {DD752318-EA08-4316-9EFE-C1A42F5526F4} - Win32Proj - e01 - 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 + e01 + 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) + + + + + + + + + + + + \ No newline at end of file From c4af1f251cbae1f7b0f03a48d5896c956d190487 Mon Sep 17 00:00:00 2001 From: jadranko lucic Date: Thu, 15 Nov 2018 23:02:45 +0100 Subject: [PATCH 2/3] =?UTF-8?q?Hrvatska:=C5=A0panjolska=203:2=20!!!!!!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/01/persistence/persistence.cpp | Bin 1232 -> 1622 bytes exercise/02/algorithms_test.cpp | 146 +++++++++++++++++++++---- exercise/02/e02.sln | 25 +++++ 3 files changed, 149 insertions(+), 22 deletions(-) create mode 100644 exercise/02/e02.sln diff --git a/example/01/persistence/persistence.cpp b/example/01/persistence/persistence.cpp index 8ef2d8d4b5545d0a5a169be8f70037f6954c6619..5bd8281c2a7dc44fb75506a14b96be8c8362c729 100644 GIT binary patch delta 367 zcmXw#&27R!5QSglLli`M*gKJOPYWpmE#3w3XCvEOP!JX5Gvfh_`^^$mCDq*O6!n;PG!#CCb6+=Y_ Vcf*|QoJ(%|j>v1;tjp}^_Z>fdLTCU0 delta 22 ecmcb{bAfY13iISo%pH^eu-s#-W#DDtVgLYck_YYp diff --git a/exercise/02/algorithms_test.cpp b/exercise/02/algorithms_test.cpp index 9df3595..6d30b7c 100644 --- a/exercise/02/algorithms_test.cpp +++ b/exercise/02/algorithms_test.cpp @@ -1,4 +1,6 @@ #include "CppUnitTest.h" +#include +#include using namespace Microsoft::VisualStudio::CppUnitTestFramework; #include @@ -16,6 +18,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]); @@ -27,6 +30,8 @@ TEST_CLASS(test_standard_algorithms_usage) { 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,18 +41,30 @@ TEST_CLASS(test_standard_algorithms_usage) { // generate std::vector v(10); - // TODO: fill vector with incremental values (by 2) + + // TODO: fill vector with incremental values (by 2) + class IncrementatorlOfValues{ + int current_; + public: + IncrementatorlOfValues(int bya) : current_(bya) {} + int operator() () { return current_ += 2; } + }; + + std::generate(v.begin(), v.end(), IncrementatorlOfValues(-1)); + 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) + TEST_METHOD(test_03a) { std::vector v = { 1, 5, 10 } ; + // TODO: change all values in a vector - Assert::AreEqual(3u, v.size()); + std::transform(v.begin(), v.end(), v.begin(), [](auto value) { return pow(value, 3); }); + + Assert::AreEqual(3u, v.size()); Assert::AreEqual(1, v[0]); Assert::AreEqual(125, v[1]); Assert::AreEqual(1000, v[2]); @@ -59,6 +76,8 @@ TEST_CLASS(test_standard_algorithms_usage) std::vector d; // TODO: calculate distances from origin (from x and y collections) to new vector + std::transform(y.begin(), y.end(), std::begin(x), std::back_inserter(d), [](int value1, int value2) { return sqrt(pow(value1, 2) + pow(value2 ,2)); }); + Assert::AreEqual(3u, d.size()); Assert::AreEqual(5., d[0]); Assert::AreEqual(13., d[1]); @@ -67,67 +86,114 @@ 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 - Assert::AreEqual(7.5, res); + + // TODO: sum of all values in input stream + auto res = std::accumulate(std::istream_iterator(ss), std::istream_iterator(), 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 - Assert::AreEqual(L"GO AVL!", res.c_str()); + + //TODO: concatenated string with additional prefix + std::wstring result = L"GO "; + auto res = std::accumulate(v.begin(), v.end(), result); + + Assert::AreEqual(L"GO AVL!", res.c_str()); } TEST_METHOD(test_04c) { + //acumm struct person { std::wstring name; int age; }; std::vector v{ {L"Pero", 33}, {L"Iva", 25} }; - auto total_age = // TODO: sum of all ages + // TODO: sum of all ages + auto total_age = std::accumulate(v.begin(), v.end(), 0, [](int i, const person& p) { return p.age + i; }); 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 i) {return i < 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 d) {return d == 1e10; }); Assert::AreEqual(3, number_of_invalid); } - TEST_METHOD(test_05c) + + TEST_METHOD(test_05c) { + //count5 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) { + struct IsPrime { + int operator()(int x) + { + int i; + bool is_prime = true; + for (i = 2; i <= x / 2; ++i) + { + if (x % i == 0) + { + is_prime = false; + break; + } + } + return is_prime; + } + } isPrime; + std::vector v { 33, 16, 24, 41, 25, 19, 9 }; - auto first_prime = // TODO: + auto first_prime = *std::find_if(v.begin(), v.end(), isPrime); + Assert::AreEqual(41, first_prime); } TEST_METHOD(test_07a) { + //replace std::vector v{ 1e10, 8, -11.23, 0, 1e10, 1e10, 1e10, 0, 99 }; - // TODO: change every invalid value (1e10) with -1 + + // TODO: change every invalid value (1e10) with -1 + std::replace_if(v.begin(), v.end(), [](double d) {return d == 1e10; }, -1); + Assert::AreEqual(-1., v[0]); Assert::AreEqual(-1., v[4]); Assert::AreEqual(-1., v[6]); } TEST_METHOD(test_07b) { + //replace, pro,jeni svaki samoglasnik std::wstring s(L"ponedjeljak"); // TODO: change every vowel with x - Assert::AreEqual(L"pxnxdjxljxk", s.c_str()); + std::replace_if(s.begin(), s.end(), + [](wchar_t letter) + { + std::wstring vowels(L"aoeiu"); + return std::any_of(vowels.cbegin(), vowels.cend(), [letter](wchar_t vowel) {return letter == vowel; }); + }, + 'x'); + + Assert::AreEqual(L"pxnxdjxljxk", s.c_str()); } TEST_METHOD(test_08a) { + // erase(remove range of elements std::vector v{ 1e10, 8, -11.23, 0, 1e10, 1e10, 1e10, 0, 99 }; + // TODO: delete all invalid values (1e10) + v.erase(std::remove_if(v.begin(), v.end(), [](double d) {return d == 1e10; }), v.end()); + Assert::AreEqual(5u, v.size()); Assert::AreEqual(8., v[0]); Assert::AreEqual(99., v[4]); @@ -137,12 +203,32 @@ 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 letter) + { + std::wstring vowels(L"aoeiu"); + return std::any_of(vowels.cbegin(), vowels.cend(), [letter](wchar_t vowel) {return letter == vowel; }); + }), + s.end()); + Assert::AreEqual(L"pndjljk", s.c_str()); } TEST_METHOD(test_09) { + //sort struct exam { std::wstring name; int points, grade; }; std::vector v{ {L"Pero", 55, 2}, {L"Iva", 93, 5}, {L"Marko", 89, 5} }; + + std::sort(v.begin(), v.end(), [](exam& e1, const exam& e2) + { + if (e1.grade > e2.grade) + return true; + if (e1.grade == e2.grade) + return e1.points > e2.points; + return false; + }); + // 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()); @@ -162,8 +248,15 @@ 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. - auto t2 = steady_clock::now(); + + // TODO: put median value in the middle of vector. fast. + //std::sort(v.begin(), v.end(), [](double const d1, double const d2) {return d1 < d2; }); + //auto it1 = std:: partition(v.begin(), v.end(), [](double d) { return d < 1000; }); + //auto it2 = std::partition(it1, v.end(), [](double d) { return d < 1001; }); + + std::partition(std::partition(v.begin(), v.end(), [](double d) { return d < 1000; }), v.end(), [](double d) { return d < 1001; }); + + 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 @@ -173,22 +266,31 @@ TEST_CLASS(test_standard_algorithms_usage) 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::sort(v.begin(), v.end(), [](employee& e1, const employee& e2) + { + return (e1.salary > e2.salary); + }); + 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) { + //min/max 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: + int size = atp_points.size() - 1; + int result[5]; + std::adjacent_difference(atp_points.begin(), atp_points.end(), result, [](int x, int y) {return y - x; }); + auto smallest_difference = *std::min_element(std::begin(result), std::end(result)); Assert::AreEqual(15, smallest_difference); } }; diff --git a/exercise/02/e02.sln b/exercise/02/e02.sln new file mode 100644 index 0000000..688ddea --- /dev/null +++ b/exercise/02/e02.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.27703.2026 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "e02", "e02.vcxproj", "{DD752318-EA08-4316-9EFE-C1A42F5526F4}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x86 = Debug|x86 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {DD752318-EA08-4316-9EFE-C1A42F5526F4}.Debug|x86.ActiveCfg = Debug|Win32 + {DD752318-EA08-4316-9EFE-C1A42F5526F4}.Debug|x86.Build.0 = Debug|Win32 + {DD752318-EA08-4316-9EFE-C1A42F5526F4}.Release|x86.ActiveCfg = Release|Win32 + {DD752318-EA08-4316-9EFE-C1A42F5526F4}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {E5A26C2B-0F0B-4CB1-B69A-749E821228E7} + EndGlobalSection +EndGlobal From 643cbeaca00f63031d8cef3a341af5c1db60e600 Mon Sep 17 00:00:00 2001 From: jadranko lucic Date: Mon, 19 Nov 2018 10:44:37 +0100 Subject: [PATCH 3/3] nth_element --- exercise/02/algorithms_test.cpp | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/exercise/02/algorithms_test.cpp b/exercise/02/algorithms_test.cpp index 6d30b7c..9ac2d6d 100644 --- a/exercise/02/algorithms_test.cpp +++ b/exercise/02/algorithms_test.cpp @@ -250,12 +250,9 @@ TEST_CLASS(test_standard_algorithms_usage) auto t1 = steady_clock::now(); // TODO: put median value in the middle of vector. fast. - //std::sort(v.begin(), v.end(), [](double const d1, double const d2) {return d1 < d2; }); - //auto it1 = std:: partition(v.begin(), v.end(), [](double d) { return d < 1000; }); - //auto it2 = std::partition(it1, v.end(), [](double d) { return d < 1001; }); - std::partition(std::partition(v.begin(), v.end(), [](double d) { return d < 1000; }), v.end(), [](double d) { return d < 1001; }); - + std::nth_element(v.begin(), v.begin() + v.size() / 2, v.end()); + //std::partition(std::partition(v.begin(), v.end(), [](double d) { return d < 1000; }), v.end(), [](double d) { return d < 1001; }); auto t2 = steady_clock::now(); auto dur = duration_cast(t2 - t1); Assert::AreEqual(1000., v[v.size() / 2]); // median value