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
122 changes: 122 additions & 0 deletions exercise/01/containers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#include "containers.h"
#include <iterator>

void remove_element(std::vector<int>& v, int index)
{
auto it = v.begin();
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.begin();
std::advance(it, index);

return *it;
}

void list_sort_desc(std::list<int>& c)
{
for (auto it = c.begin(); it != c.end(); ++it)
{
for (auto it2 = c.begin()++; it2 != c.end(); ++it2)
{
if (*it > *it2)
{
auto temp = *it;
*it = *it2;
*it2 = temp;
}
}
}
}

int unique_numbers(std::wistream& is)
{
std::vector<int> uniqueNumbers;

int n;
while (is >> n)
{
bool unique = true;

for (int i = 0; i < uniqueNumbers.size(); i++)
{
if (uniqueNumbers[i] == n)
{
unique = false;
break;
}
}

if (unique)
{
uniqueNumbers.push_back(n);
}
}

return uniqueNumbers.size();
}

word_frequency::word_frequency(std::wistream& in)
{
std::wstring word;

while (in >> word)
{
for (wchar_t& c:word)
{
c = towlower(c);
}
words.push_back(word);
}
}

int word_frequency::frequency(const std::wstring& s)
{
int count = 0;

for (int i = 0; i < words.size(); i++)
{
if (words[i]._Equal(s))
{
count++;
}
}

return count;
}

int word_frequency::count()
{
std::vector<std::wstring> uniqueWords;

for (int i = 0; i < words.size(); i++)
{
bool unique = true;

for (int j = 0; j < uniqueWords.size(); j++)
{
if (words[i]._Equal(uniqueWords[j]))
{
unique = false;
break;
}
}

if (unique)
{
uniqueWords.push_back(words[i]);
}
}

return uniqueWords.size();
}
2 changes: 2 additions & 0 deletions exercise/01/containers.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ class word_frequency
word_frequency(std::wistream&);
int frequency(const std::wstring& s);
int count();
private:
std::vector<std::wstring> words;
};
94 changes: 78 additions & 16 deletions exercise/02/algorithms_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,31 @@ using namespace Microsoft::VisualStudio::CppUnitTestFramework;
#include <numeric>
#include <chrono>

unsigned cubeNumber(int num)
{
return pow(num, 3);
}

double calcDistance(int x, int y)
{
return sqrt(pow(x, 2) + pow(y, 2));
}

bool isNegative(int num)
{
return num < 0;
}

bool isTooBig(float num)
{
return num > std::numeric_limits<int>::max();
}

bool isVowel(wchar_t value)
{
return value == L'a' || value == L'e' || value == L'i' || value == L'o' || value == L'u';
}

TEST_CLASS(test_standard_algorithms_usage)
{
public:
Expand All @@ -16,7 +41,7 @@ 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::copy(std::istream_iterator<int, wchar_t>(ss), std::istream_iterator<int, wchar_t>(), std::back_inserter(v));
Assert::AreEqual(3u, v.size());
Assert::AreEqual(14, v[0]);
Assert::AreEqual(-78, v[1]);
Expand All @@ -27,6 +52,7 @@ TEST_CLASS(test_standard_algorithms_usage)
{
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]);
Expand All @@ -37,6 +63,8 @@ TEST_CLASS(test_standard_algorithms_usage)
// generate
std::vector<int> v(10);
// TODO: fill vector with incremental values (by 2)
int n = -1;
std::generate(v.begin(), v.end(), [&n] {return n = 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]);
Expand All @@ -47,6 +75,15 @@ TEST_CLASS(test_standard_algorithms_usage)
{
std::vector<int> v = { 1, 5, 10 } ;
// TODO: change all values in a vector
/*class power
{
public:
unsigned n;
power(const int initval=0) : n(initval){}
unsigned operator()() const { return pow(n, 3); }
};*/

std::transform(v.begin(), v.end(), v.begin(), cubeNumber);
Assert::AreEqual(3u, v.size());
Assert::AreEqual(1, v[0]);
Assert::AreEqual(125, v[1]);
Expand All @@ -59,6 +96,7 @@ TEST_CLASS(test_standard_algorithms_usage)
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), calcDistance);
Assert::AreEqual(3u, d.size());
Assert::AreEqual(5., d[0]);
Assert::AreEqual(13., d[1]);
Expand All @@ -67,53 +105,65 @@ 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<double, wchar_t>(ss), std::istream_iterator<double, wchar_t>(), 0.0);// TODO: sum of all values in input stream
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 "));// TODO: concatenated string with additional prefix
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
Assert::AreEqual(58, total_age);
auto total_age = std::accumulate(v.begin(), v.end(), 0, [](int sum, const person& p) { return sum+p.age; });// TODO: sum of all ages
//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(), isNegative);// TODO:
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(), isTooBig);// TODO:
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 = count_if(v.begin(), v.end(), [](const point& v) { return (v.x > 0 && v.y > 0); });// TODO:
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 value)
{
for (int i = 2; i < sqrt(value) + 1; ++i)
{
if (value % i == 0)
{
return false;
}
}

return true;
});// TODO:
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.0);
Assert::AreEqual(-1., v[0]);
Assert::AreEqual(-1., v[4]);
Assert::AreEqual(-1., v[6]);
Expand All @@ -122,12 +172,13 @@ TEST_CLASS(test_standard_algorithms_usage)
{
std::wstring s(L"ponedjeljak");
// TODO: change every vowel with x
std::replace_if(s.begin(), s.end(), isVowel, L'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());// TODO: delete all invalid values (1e10)
Assert::AreEqual(5u, v.size());
Assert::AreEqual(8., v[0]);
Assert::AreEqual(99., v[4]);
Expand All @@ -136,14 +187,22 @@ 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(), isVowel), s.end());// 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<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(), [](exam value1, exam value2)
{
if (value1.grade == value2.grade)
{
return value1.points > value2.points;
}

return value1.grade > value2.grade;
});// 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());
Expand All @@ -163,6 +222,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(), [](const double value1, const double value2) { return value1 < value2; });
auto t2 = steady_clock::now();
auto dur = duration_cast<milliseconds>(t2 - t1);
Assert::AreEqual(1000., v[v.size() / 2]); // median value
Expand All @@ -179,16 +239,18 @@ TEST_CLASS(test_standard_algorithms_usage)
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());// TODO:
Assert::AreEqual(-97.23, smallest_value);
auto largest_value = // TODO:
auto largest_value = *std::max_element(v.begin(), v.end());// TODO:
Assert::AreEqual(48.78, largest_value);
}
TEST_METHOD(test_13)
{
std::vector<int> atp_points { 8445, 7480, 6220, 5300, 5285 };
std::vector<int> diffs;
// the most interesting match is the one with the smallest difference
auto smallest_difference = // TODO:
std::adjacent_difference(atp_points.rbegin(), atp_points.rend(), std::back_inserter(diffs));
auto smallest_difference = *std::min_element(diffs.begin(), diffs.end());// TODO:
Assert::AreEqual(15, smallest_difference);
}
};
};