diff --git a/homework/transform-containers/CMakeLists.txt b/homework/transform-containers/CMakeLists.txt index 1c924a028..70898b2a3 100644 --- a/homework/transform-containers/CMakeLists.txt +++ b/homework/transform-containers/CMakeLists.txt @@ -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) diff --git a/homework/transform-containers/test.cpp b/homework/transform-containers/test.cpp index 3f6e9c0c8..f8764e5c4 100644 --- a/homework/transform-containers/test.cpp +++ b/homework/transform-containers/test.cpp @@ -1,27 +1,22 @@ +#include #include "gtest/gtest.h" +#include "transform.hpp" // TODO: add proper includes TEST(transformContainerTests, ShouldReturnUniqueMap) { std::map expected_result{ - {1, "Ala"}, - {2, "Kot"}, - {3, "Ma"}, - {4, "Rysia"}, - {5, "Sierotka"}}; - std::list list{ - "Ala", "Kot", "Ma", "Rysia", "Ala", - "Sierotka", "Kot", "Ma", "Ala"}; - std::deque 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 list{"Ala", "Kot", "Ma", "Rysia", "Ala", + "Sierotka", "Kot", "Ma", "Ala"}; + std::deque 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; })); } diff --git a/homework/transform-containers/transform.cpp b/homework/transform-containers/transform.cpp new file mode 100644 index 000000000..b200ce8ab --- /dev/null +++ b/homework/transform-containers/transform.cpp @@ -0,0 +1,21 @@ +#include "transform.hpp" + +std::map removeDuplicateAndTranformToMap( + std::list& l, + std::deque& 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 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; +}; diff --git a/homework/transform-containers/transform.hpp b/homework/transform-containers/transform.hpp new file mode 100644 index 000000000..327e98a7b --- /dev/null +++ b/homework/transform-containers/transform.hpp @@ -0,0 +1,13 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +std::map removeDuplicateAndTranformToMap( + std::list& l, + std::deque& d);