Skip to content
Closed
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
2 changes: 1 addition & 1 deletion homework/transform-containers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ FetchContent_MakeAvailable(googletest)
project(transformContainers)
enable_testing()

add_executable(${PROJECT_NAME}-ut test.cpp) # add your cpp file here after test.cpp
add_executable(${PROJECT_NAME}-ut test.cpp transform.cpp) # add your cpp file here after test.cpp
# if this is problematic take a look into CMakeLists.txt files in other exercises

add_compile_options(${PROJECT_NAME}-ut -Wall -Wextra -Wconversion -pedantic -Werror)
Expand Down
25 changes: 10 additions & 15 deletions homework/transform-containers/test.cpp
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
#include <iterator>
#include "gtest/gtest.h"
#include "transform.hpp"

// TODO: add proper includes

TEST(transformContainerTests, ShouldReturnUniqueMap) {
std::map<int, std::string> expected_result{
{1, "Ala"},
{2, "Kot"},
{3, "Ma"},
{4, "Rysia"},
{5, "Sierotka"}};
std::list<std::string> list{
"Ala", "Kot", "Ma", "Rysia", "Ala",
"Sierotka", "Kot", "Ma", "Ala"};
std::deque<int> deque{
1, 2, 3, 4, 5, 3, 1, 2, 3, 4,
5, 2, 3, 1, 1, 2, 3, 2, 1, 4};
{1, "Ala"}, {2, "Kot"}, {3, "Ma"}, {4, "Rysia"}, {5, "Sierotka"}};
std::list<std::string> list{"Ala", "Kot", "Ma", "Rysia", "Ala",
"Sierotka", "Kot", "Ma", "Ala"};
std::deque<int> deque{1, 2, 3, 4, 5, 3, 1, 2, 3, 4,
5, 2, 3, 1, 1, 2, 3, 2, 1, 4};
auto result = removeDuplicateAndTranformToMap(list, deque);

ASSERT_TRUE(expected_result.size() == result.size());
EXPECT_TRUE(std::equal(begin(result),
end(result),
begin(expected_result),
EXPECT_TRUE(std::equal(begin(result), end(result), begin(expected_result),
[](const auto& lhs, const auto& rhs) {
return lhs.first == rhs.first && lhs.second == rhs.second;
return lhs.first == rhs.first &&
lhs.second == rhs.second;
}));
}
21 changes: 21 additions & 0 deletions homework/transform-containers/transform.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "transform.hpp"

std::map<int, std::string> removeDuplicateAndTranformToMap(
std::list<std::string>& l,
std::deque<int>& d) {
std::sort(d.begin(), d.end());
l.sort([](auto a, auto b) {
return std::lexicographical_compare(
a.begin(), a.end(), b.begin(), b.end(),
[](auto c1, auto c2) { return std::tolower(c1) < std::tolower(c2); });
});
auto it = std::unique(l.begin(), l.end());
auto it2 = std::unique(d.begin(), d.end());
std::map<int, std::string> result;
auto dst1 = std::distance(it, l.begin());
auto dst2 = std::distance(it2, d.begin());
std::transform(l.begin(), it, d.begin(),
std::inserter(result, result.begin()),
[](auto s, auto i) mutable { return std::make_pair(i, s); });
return result;
};
13 changes: 13 additions & 0 deletions homework/transform-containers/transform.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <algorithm>
#include <cctype>
#include <deque>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <string>
#include <utility>

std::map<int, std::string> removeDuplicateAndTranformToMap(
std::list<std::string>& l,
std::deque<int>& d);
Loading