Skip to content

Задание 17.03.25 : 11.10 - 11.18 / 75 баллов / 23.03.25 23:59:59 #16

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 3 commits into
base: master
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 CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ include_directories(shared)


##### subdirectories #####
set(DIRS week-W week-2 week-3 week-4 week-5 week-6 week-7 week-8 week-9 week-10 week-11)
set(DIRS week-W week-2 week-3 week-4 week-5 week-6 week-7 week-8 week-9 week-10 week-11 week-13)
foreach(DIR ${DIRS})
add_subdirectory(${DIR})
endforeach()
Expand Down
4 changes: 4 additions & 0 deletions week-13/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
set(DIRS task-11-10 task-11-16 task-11-11)
foreach(DIR ${DIRS})
add_subdirectory(${DIR})
endforeach()
3 changes: 3 additions & 0 deletions week-13/task-11-10/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
include_directories(include)

add_subdirectory(test)
30 changes: 30 additions & 0 deletions week-13/task-11-10/include/smth_of.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once
#include <ranges>

namespace my_algorithm {

template <std::ranges::range Range, typename T>
requires std::convertible_to<T, typename Range::value_type>
bool has_one(const Range& range, T&& value) {
return std::ranges::find(range, std::forward<T>(value)) != range.end();
}

template <std::ranges::range Range, typename... Args>
requires(std::convertible_to<Args, typename Range::size_type> && ...)
bool any_of(const Range& range, Args&&... args) {
return (has_one(range, std::forward<Args>(args)) || ...);
}

template <std::ranges::range Range, typename... Args>
requires(std::convertible_to<Args, typename Range::size_type> && ...)
bool all_of(const Range& range, Args&&... args) {
return (has_one(range, std::forward<Args>(args)) && ...);
}

template <std::ranges::range Range, typename... Args>
requires(std::convertible_to<Args, typename Range::size_type> && ...)
bool none_of(const Range& range, Args&&... args) {
return (!has_one(range, std::forward<Args>(args)) && ...);
}

} // namespace my_algorithm
1 change: 1 addition & 0 deletions week-13/task-11-10/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cpp_test(test-11-10.cpp)
68 changes: 68 additions & 0 deletions week-13/task-11-10/test/test-11-10.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <gtest/gtest.h>

#include <list>

#include "smth_of.hpp"

using my_algorithm::all_of;
using my_algorithm::any_of;
using my_algorithm::has_one;
using my_algorithm::none_of;

TEST(HasOne, Array) {
const std::array<std::string, 5> a{"one", "two", "three", "four", "five"};
EXPECT_TRUE(has_one(a, "two"));
EXPECT_FALSE(has_one(a, "six"));
}

TEST(AnyOfAllOfNoneOf, Vector) {
const std::vector<int> vec = {1, 2, 3, 4, 5}; // NOLINT

EXPECT_TRUE(any_of(vec, 2, 6));
EXPECT_FALSE(any_of(vec, 6, 7));

EXPECT_TRUE(all_of(vec, 1, 2, 3));
EXPECT_FALSE(all_of(vec, 1, 2, 6));

EXPECT_TRUE(none_of(vec, 6, 7, 8));
EXPECT_FALSE(none_of(vec, 1, 6, 7));
}

TEST(AnyOfAllOfNoneOf, Set) {
const std::set<int> s = {10, 20, 30, 40}; // NOLINT

EXPECT_TRUE(any_of(s, 20, 50));
EXPECT_FALSE(any_of(s, 50, 60));

EXPECT_TRUE(all_of(s, 10, 20));
EXPECT_FALSE(all_of(s, 10, 50));

EXPECT_TRUE(none_of(s, 50, 60));
EXPECT_FALSE(none_of(s, 20, 60));
}

TEST(AnyOfAllOfNoneOf, List) {
const std::list<int> lst = {5, 10, 15, 20}; // NOLINT

EXPECT_TRUE(any_of(lst, 10, 25));
EXPECT_FALSE(any_of(lst, 30, 35));

EXPECT_TRUE(all_of(lst, 5, 10));
EXPECT_FALSE(all_of(lst, 5, 30));

EXPECT_TRUE(none_of(lst, 25, 30));
EXPECT_FALSE(none_of(lst, 10, 30));
}

TEST(AnyOfAllOfNoneOf, String) {
const std::string str = "hello"; // NOLINT

EXPECT_TRUE(any_of(str, 'h', 'x'));
EXPECT_FALSE(any_of(str, 'x', 'y'));

EXPECT_TRUE(all_of(str, 'h', 'e'));
EXPECT_FALSE(all_of(str, 'h', 'z'));

EXPECT_TRUE(none_of(str, 'x', 'y'));
EXPECT_FALSE(none_of(str, 'h', 'y'));
}
3 changes: 3 additions & 0 deletions week-13/task-11-11/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
include_directories(include)

add_subdirectory(test)
15 changes: 15 additions & 0 deletions week-13/task-11-11/include/transform_if.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

namespace my_algorithm {

template <class InputIt, class OutputIt, class Fct, class Pred>
void transform_if(InputIt first, InputIt last, OutputIt dest, Fct transform, Pred pred) {
while (first != last) {
if (pred(*first)) {
*(dest++) = transform(*first);
}
++first;
}
}

} // namespace my_algorithm
1 change: 1 addition & 0 deletions week-13/task-11-11/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cpp_test(test-11-11.cpp)
83 changes: 83 additions & 0 deletions week-13/task-11-11/test/test-11-11.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include <gtest/gtest.h>

#include <list>

#include "transform_if.hpp"

using my_algorithm::transform_if;

TEST(TransformIfTest, SimpleTransformAndFilter) {
const std::vector<int> input = {1, 2, 3, 4, 5};
std::vector<int> output;

transform_if(
input.begin(), input.end(), std::back_inserter(output), [](int x) { return x * x; },
[](int x) { return x % 2 == 0; });

const std::vector<int> expected = {4, 16};
EXPECT_EQ(output, expected);
}

TEST(TransformIfTest, StringTransformAndFilter) {
const std::vector<std::string> input = {"apple", "banana", "cherry", "date"};
std::vector<std::string> output;

transform_if(
input.begin(), input.end(), std::back_inserter(output),
[](const std::string& s) {
std::string result = s;
std::transform(result.begin(), result.end(), result.begin(),
[](auto&& e) { return std::toupper(e); });
return result;
},
[](const std::string& s) { return s.length() > 5; }); // NOLINT

const std::vector<std::string> expected = {"BANANA", "CHERRY"};
EXPECT_EQ(output, expected);
}

TEST(TransformIfTest, EmptyInputRange) {
constexpr std::vector<int> input;
std::vector<int> output;

transform_if(
input.begin(), input.end(), std::back_inserter(output), [](int x) { return x * 2; },
[](int x) { return x > 0; });

EXPECT_TRUE(output.empty());
}

TEST(TransformIfTest, NoElementsSatisfyPredicate) {
const std::vector<int> input = {1, 3, 5, 7};
std::vector<int> output;

transform_if(
input.begin(), input.end(), std::back_inserter(output), [](int x) { return x * 2; },
[](int x) { return x % 2 == 0; });

EXPECT_TRUE(output.empty());
}

TEST(TransformIfTest, AllElementsSatisfyPredicate) {
const std::vector<int> input = {2, 4, 6, 8};
std::vector<int> output;

transform_if(
input.begin(), input.end(), std::back_inserter(output), [](int x) { return x / 2; },
[](int x) { return x % 2 == 0; });

const std::vector<int> expected = {1, 2, 3, 4};
EXPECT_EQ(output, expected);
}

TEST(TransformIfTest, ListInput) {
const std::list<int> input = {10, 20, 30, 40};
std::vector<int> output;

transform_if(
input.begin(), input.end(), std::back_inserter(output), [](int x) { return x + 1; },
[](int x) { return x > 15; }); // NOLINT

const std::vector<int> expected = {21, 31, 41};
EXPECT_EQ(output, expected);
}
1 change: 1 addition & 0 deletions week-13/task-11-16/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_subdirectory(test)
1 change: 1 addition & 0 deletions week-13/task-11-16/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cpp_test(test-11-16.cpp)
64 changes: 64 additions & 0 deletions week-13/task-11-16/test/test-11-16.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include <gtest/gtest.h>

#include <ranges>
#include <sstream>
#include <vector>

TEST(Views, Istream) {
std::istringstream input("1 2 3 4 5");
auto stream_view = std::ranges::views::istream<int>(input);

std::vector<int> result;
std::ranges::copy(stream_view, std::back_inserter(result));
const std::vector<int> expected = {1, 2, 3, 4, 5};

EXPECT_EQ(result, expected);
}

TEST(Views, Counted) {
const std::vector<int> vec = {1, 2, 3, 4, 5, 6, 7};
auto counted_view = std::views::counted(std::ranges::next(vec.begin(), 2), 3);

const std::vector<int> result(counted_view.begin(), counted_view.end());

EXPECT_EQ(result, (std::vector<int>{3, 4, 5}));
}

TEST(Views, JoinStrings) {
const std::vector<std::string> words = {"hello", " ", "world"};
const std::string result =
std::string(std::views::join(words).begin(), std::views::join(words).end());

EXPECT_EQ(result, "hello world");
}

TEST(Views, JoinNestedVecs) {
const std::vector<std::vector<int>> nested = {{1, 2}, {3, 4}, {5, 6}};
auto join_view = nested | std::ranges::views::join;

const std::vector<int> result(join_view.begin(), join_view.end());
const std::vector<int> expected = {1, 2, 3, 4, 5, 6};

EXPECT_EQ(result, expected);
}

TEST(Views, Stride) {
const std::vector<int> vec = {1, 2, 3, 4, 5, 6, 7};
auto stride_view = vec | std::views::stride(2);

const std::vector<int> result(stride_view.begin(), stride_view.end());

EXPECT_EQ(result, (std::vector<int>{1, 3, 5, 7}));
}

TEST(Views, JoinAndStride) {
const std::vector<std::vector<int>> nested = {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
auto join_view = nested | std::ranges::views::join;
auto stride_view = join_view | std::ranges::views::stride(2);

std::vector<int> result;
std::ranges::copy(stride_view, std::back_inserter(result));
const std::vector<int> expected = {1, 3, 5, 7};

EXPECT_EQ(result, expected);
}