Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions exercise/01/containers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#include "containers.h"
#include <set>
#include <cctype>

void remove_element(std::vector<int>& v, int index)
{
auto it = v.cbegin();
std::advance(it, index);
v.erase(it);
}

void input_element(std::vector<std::wstring>& v, int index, const std::wstring& value)
{
auto it = v.begin();
std::advance(it, index);
v.insert(it, value);
}

int list_nth_element(const std::list<int>& c, int index)
{
auto it = c.cbegin();
std::advance(it, index);
return *it;
}

void list_sort_desc(std::list<int>& c)
{
c.sort();
c.reverse();
}

int unique_numbers(std::wistream& stream)
{
int n;
std::set<int> set;

while (stream >> n)
{
set.insert(n);
}
return set.size();
}

std::wstring to_lower(const std::wstring& s)
{
std::wstring lower;
for (auto it = s.cbegin(); it != s.cend(); ++it)
{
lower.push_back(std::tolower(*it));
}
return lower;
}

word_frequency::word_frequency(std::wistream& ss)
{
std::wstring s;
while (ss >> s)
{
map[to_lower(s)]++;
}
}

int word_frequency::frequency(const std::wstring& s)
{
auto it = map.find(to_lower(s));
if (it != map.end())
{
return it->second;
}
return 0;
}

int word_frequency::count()
{
return map.size();
}

3 changes: 3 additions & 0 deletions exercise/01/containers.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <string>
#include <vector>
#include <list>
#include <map>

void remove_element(std::vector<int>& v, int index);
void input_element(std::vector<std::wstring>& v, int index, const std::wstring& value);
Expand All @@ -13,6 +14,8 @@ int unique_numbers(std::wistream&);

class word_frequency
{
private:
std::map<std::wstring, int> map;
public:
word_frequency(std::wistream&);
int frequency(const std::wstring& s);
Expand Down
25 changes: 25 additions & 0 deletions exercise/01/e01.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28010.2050
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 = {814F9C84-5D6C-47CA-9D59-1E6F2FEF04F8}
EndGlobalSection
EndGlobal
69 changes: 44 additions & 25 deletions exercise/02/algorithms_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,29 @@ TEST_CLASS(test_standard_algorithms_usage)
{
std::wstringstream ss(L"14 -78 22");
std::vector<int> v;
// TODO: read values from input stream into vector
std::istream_iterator<int, wchar_t> it(ss);
std::istream_iterator<int, wchar_t> end;
std::copy(it, end, std::back_inserter(v));

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<int> 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]);
Assert::AreEqual(10, v[9]);
}
TEST_METHOD(test_02b)
{
// generate
int i = -1;
std::vector<int> v(10);
// TODO: fill vector with incremental values (by 2)
std::generate(v.begin(), v.end(), [&i]() { return i += 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]);
Expand All @@ -46,7 +47,7 @@ TEST_CLASS(test_standard_algorithms_usage)
TEST_METHOD(test_03a)
{
std::vector<int> v = { 1, 5, 10 } ;
// TODO: change all values in a vector
std::transform(v.begin(), v.end(), v.begin(), [](int i) { return pow(i, 3); });
Assert::AreEqual(3u, v.size());
Assert::AreEqual(1, v[0]);
Assert::AreEqual(125, v[1]);
Expand All @@ -58,7 +59,8 @@ TEST_CLASS(test_standard_algorithms_usage)
std::vector<int> y = { 4, 12, 10 };
std::vector<double> 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(pow(a, 2) + pow(b, 2)); });
Assert::AreEqual(3u, d.size());
Assert::AreEqual(5., d[0]);
Assert::AreEqual(13., d[1]);
Expand All @@ -67,67 +69,81 @@ 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
std::istream_iterator<double, wchar_t> it(ss);
std::istream_iterator<double, wchar_t> end;
auto res = std::accumulate(it, end, 0., std::plus<double>());
Assert::AreEqual(7.5, res);
}
TEST_METHOD(test_04b)
{
std::vector<std::wstring> 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::plus<std::wstring>());
Assert::AreEqual(L"GO AVL!", res.c_str());
}
TEST_METHOD(test_04c)
{
struct person { std::wstring name; int age; };
std::vector<person> v{ {L"Pero", 33}, {L"Iva", 25} };
auto total_age = // TODO: sum of all ages
auto total_age = std::accumulate(v.begin(), v.end(), 0, [](int s, const person& p) { return s + p.age; });
Assert::AreEqual(58, total_age);
}

TEST_METHOD(test_05a)
{
std::vector<int> 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<double> 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 i) { return i == 1e10; });
Assert::AreEqual(3, number_of_invalid);
}
TEST_METHOD(test_05c)
{
struct point { int x, y; };
std::vector<point> 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& p) { return p.x > 0 && p.y > 0; });
Assert::AreEqual(2, number_in_first_quadrant);
}

TEST_METHOD(test_06)
{
std::vector<int> v { 33, 16, 24, 41, 25, 19, 9 };
auto first_prime = // TODO:
auto first_prime = *std::find_if(v.begin(), v.end(), [](int n)
{
for (int i = 2; i <= n / 2; ++i)
{
if (n % i == 0)
{
return false;
break;
}
}
return true;
});
Assert::AreEqual(41, first_prime);
}
TEST_METHOD(test_07a)
{
std::vector<double> 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]);
}
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 c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'; }, 'x');
Assert::AreEqual(L"pxnxdjxljxk", s.c_str());
}
TEST_METHOD(test_08a)
{
std::vector<double> 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]);
Expand All @@ -136,14 +152,16 @@ 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(),
[](wchar_t c) { return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'; }), s.end());
Assert::AreEqual(L"pndjljk", s.c_str());
}
TEST_METHOD(test_09)
{
struct exam { std::wstring name; int points, grade; };
std::vector<exam> 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(),
[](const exam& e1, const exam& e2) { return e1.grade > e2.grade || e1.points > e2.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());
Expand All @@ -162,7 +180,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::sort(v.begin(), v.end());
auto t2 = steady_clock::now();
auto dur = duration_cast<milliseconds>(t2 - t1);
Assert::AreEqual(1000., v[v.size() / 2]); // median value
Expand All @@ -172,23 +190,24 @@ TEST_CLASS(test_standard_algorithms_usage)
{
struct employee { std::wstring name; int salary; };
std::vector<employee> 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(), [](const 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)
{
std::vector<double> 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<int> atp_points { 8445, 7480, 6220, 5300, 5285 };
// the most interesting match is the one with the smallest difference
auto smallest_difference = // TODO:
std::adjacent_difference(atp_points.begin(), atp_points.end(), atp_points.begin());
std::transform(atp_points.begin(), atp_points.end(), atp_points.begin(), [](int x) { return std::abs(x); });
auto smallest_difference = *std::min_element(atp_points.begin(), atp_points.end());
Assert::AreEqual(15, smallest_difference);
}
};
25 changes: 25 additions & 0 deletions exercise/02/e02.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28010.2050
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 = {4239F2E2-6A27-43FC-B851-AB28D1F8CCF5}
EndGlobalSection
EndGlobal