Solved: 31 - Transform and Reduce
This commit is contained in:
@@ -1,9 +1,20 @@
|
|||||||
#include "transform_reduce.hpp"
|
#include "transform_reduce.hpp"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
namespace stl_algorithms {
|
namespace stl_algorithms {
|
||||||
|
|
||||||
auto square_all(const std::vector<int>& data) -> std::vector<int> { (void)data; return {}; }
|
auto square_all(const std::vector<int>& data) -> std::vector<int> {
|
||||||
auto sum_all(const std::vector<int>& data) -> int { (void)data; return 0; }
|
auto result = std::vector<int>{};
|
||||||
auto product_positive(const std::vector<int>& data) -> long long { (void)data; return 0; }
|
result.reserve(data.size());
|
||||||
|
std::ranges::transform(data, std::back_inserter(result), [](const int value) { return value * value; });
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
auto sum_all(const std::vector<int>& data) -> int {
|
||||||
|
return std::ranges::fold_left(data, 0, std::plus<int>{});
|
||||||
|
}
|
||||||
|
auto product_positive(const std::vector<int>& data) -> long long {
|
||||||
|
return std::ranges::fold_left(data, 1, [](const int accumulator, const int value) { return value > 0 ? value * accumulator : accumulator; });
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace stl_algorithms
|
} // namespace stl_algorithms
|
||||||
|
|||||||
Reference in New Issue
Block a user