Skip to content

Added removeDuplicateAndTranformToMap() implementation #877

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: transform-containers
Choose a base branch
from
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
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
1 change: 1 addition & 0 deletions homework/transform-containers/test.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "gtest/gtest.h"

// TODO: add proper includes
#include "transform.hpp"

TEST(transformContainerTests, ShouldReturnUniqueMap) {
std::map<int, std::string> expected_result{
Expand Down
20 changes: 20 additions & 0 deletions homework/transform-containers/transform.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

#include "transform.hpp"
#include <algorithm>
#include <iostream>
#include <iterator>

std::map<int, std::string> removeDuplicateAndTranformToMap(std::list<std::string>& l, std::deque<int>& d) {
l.sort();
auto it_l = std::unique(l.begin(), l.end());
l.erase(it_l, l.end());

std::sort(d.begin(), d.end());
auto it_d = std::unique(d.begin(), d.end());
d.erase(it_d, d.end());

std::map<int, std::string> m;
std::transform(d.begin(), d.end(), l.begin(), std::inserter(m, m.end()), [](const auto& d_, const auto& l_) { return std::make_pair(d_, l_); });

return m;
}
8 changes: 8 additions & 0 deletions homework/transform-containers/transform.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

#include <deque>
#include <list>
#include <map>
#include <string>

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