Skip to content

Задание 24.03.25 : 12.01 - 12.13 / 75 баллов / 01.04.25 23:59:59 #17

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: 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-14)
foreach(DIR ${DIRS})
add_subdirectory(${DIR})
endforeach()
Expand Down
4 changes: 4 additions & 0 deletions week-14/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
set(DIRS task-12-06 task-12-07 task-12-08)
foreach(DIR ${DIRS})
add_subdirectory(${DIR})
endforeach()
3 changes: 3 additions & 0 deletions week-14/task-12-06/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
include_directories(include)

add_subdirectory(test)
18 changes: 18 additions & 0 deletions week-14/task-12-06/include/title_case.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include <algorithm>
#include <iterator>
#include <ranges>
#include <string>

namespace my_algorithms {

void title_case(std::string& str) {
std::ranges::transform(
std::views::zip(str, std::views::iota(0)), str.begin(), [&](auto&& pair) {
auto [c, idx] = pair;
return (idx == 0 || std::isspace(str[idx - 1])) ? std::toupper(c) : std::tolower(c);
});
}

} // namespace my_algorithms
1 change: 1 addition & 0 deletions week-14/task-12-06/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cpp_test(test-12-06.cpp)
40 changes: 40 additions & 0 deletions week-14/task-12-06/test/test-12-06.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "gtest/gtest.h"
#include "title_case.hpp"

using my_algorithms::title_case;

TEST(TitleCaseTest, HandlesEmptyString) {
std::string str;
title_case(str);
EXPECT_EQ(str, "");
}

TEST(TitleCaseTest, HandlesSingleWord) {
std::string str = "hello";
title_case(str);
EXPECT_EQ(str, "Hello");
}

TEST(TitleCaseTest, HandlesMultipleWords) {
std::string str = "heLLo, woRLD";
title_case(str);
EXPECT_EQ(str, "Hello, World");
}

TEST(TitleCaseTest, HandlesLeadingAndTrailingSpaces) {
std::string str = " test CASE ";
title_case(str);
EXPECT_EQ(str, " Test Case ");
}

TEST(TitleCaseTest, HandlesMixedCaseWithNumbers) {
std::string str = "1st plaCE";
title_case(str);
EXPECT_EQ(str, "1st Place");
}

TEST(TitleCaseTest, HandlesAllUppercase) {
std::string str = "ALL UPPER";
title_case(str);
EXPECT_EQ(str, "All Upper");
}
3 changes: 3 additions & 0 deletions week-14/task-12-07/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
include_directories(include)

add_subdirectory(test)
20 changes: 20 additions & 0 deletions week-14/task-12-07/include/join.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

namespace my_algorithm {

template <std::ranges::forward_range Range>
requires requires {
requires std::convertible_to<std::ranges::range_value_t<Range>, std::string_view>;
}
std::string join(const Range& strings, const std::string& delimiter) {
std::string res;
for (auto it = std::begin(strings); it != std::end(strings); ++it) {
if (it != std::begin(strings)) {
res += delimiter;
}
res += *it;
}
return res;
}

} // namespace my_algorithm
1 change: 1 addition & 0 deletions week-14/task-12-07/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cpp_test(test-12-07.cpp)
37 changes: 37 additions & 0 deletions week-14/task-12-07/test/test-12-07.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <list>
#include <vector>

#include "gtest/gtest.h"
#include "join.hpp"

using my_algorithm::join;

TEST(JoinTest, HandlesEmptyRange) {
const std::vector<std::string> empty_vec;
EXPECT_EQ(join(empty_vec, ","), "");
}

TEST(JoinTest, HandlesSingleString) {
const std::list<std::string> single = {"hello"};
EXPECT_EQ(join(single, " "), "hello");
}

TEST(JoinTest, JoinsMultipleStrings) {
const std::vector<std::string> strings = {"a", "b", "c"};
EXPECT_EQ(join(strings, ","), "a,b,c");
}

TEST(JoinTest, WorksWithStringView) {
const std::vector<std::string_view> sv = {"x", "y", "z"};
EXPECT_EQ(join(sv, "->"), "x->y->z");
}

TEST(JoinTest, HandlesMultiCharDelimiter) {
const std::vector<std::string> strings = {"1", "2", "3"};
EXPECT_EQ(join(strings, "###"), "1###2###3");
}

TEST(JoinTest, WorksWithNonOwningTypes) {
const char* cstrings[] = {"quick", "brown", "fox"};
EXPECT_EQ(join(cstrings, "..."), "quick...brown...fox");
}
3 changes: 3 additions & 0 deletions week-14/task-12-08/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
include_directories(include)

add_subdirectory(test)
28 changes: 28 additions & 0 deletions week-14/task-12-08/include/tokenize.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once

#include <unordered_set>

namespace my_algorithm {

std::vector<std::string> tokenize(const std::string& str,
const std::unordered_set<char>& delimiters) {
std::vector<std::string> tokens;
auto start = str.begin();
auto end = str.end();
while (start != end) {
while (start != end && delimiters.contains(*start)) {
++start;
}
auto token_end = start;
while (token_end != end && !delimiters.contains(*token_end)) {
++token_end;
}
if (start != token_end) {
tokens.emplace_back(start, token_end);
}
start = token_end;
}
return tokens;
}

} // namespace my_algorithm
1 change: 1 addition & 0 deletions week-14/task-12-08/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cpp_test(test-12-08.cpp)
39 changes: 39 additions & 0 deletions week-14/task-12-08/test/test-12-08.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include "gtest/gtest.h"
#include "tokenize.hpp"

using my_algorithm::tokenize;

TEST(TokenizeTest, HandlesEmptyString) {
EXPECT_EQ(tokenize("", {' ', ','}), std::vector<std::string>({}));
}

TEST(TokenizeTest, HandlesNoDelimiters) {
EXPECT_EQ(tokenize("hello", {}), std::vector<std::string>({"hello"}));
}

TEST(TokenizeTest, SingleDelimiterBetweenTokens) {
EXPECT_EQ(tokenize("a,b,c", {','}), std::vector<std::string>({"a", "b", "c"}));
}

TEST(TokenizeTest, MultipleDelimitersBetweenTokens) {
EXPECT_EQ(tokenize("hello, world!how?are.you", {' ', ',', '!', '?', '.'}),
std::vector<std::string>({"hello", "world", "how", "are", "you"}));
}

TEST(TokenizeTest, LeadingAndTrailingDelimiters) {
EXPECT_EQ(tokenize(" ,,test,,case,, ", {' ', ','}),
std::vector<std::string>({"test", "case"}));
}

TEST(TokenizeTest, ConsecutiveDelimiters) {
EXPECT_EQ(tokenize("a,, ,b", {',', ' '}), std::vector<std::string>({"a", "b"}));
}

TEST(TokenizeTest, MixedDelimitersAndNoTokens) {
EXPECT_EQ(tokenize(", , ,", {',', ' '}), std::vector<std::string>({}));
}

TEST(TokenizeTest, ComplexRealWorldCase) {
EXPECT_EQ(tokenize(" John;Doe,42;New York;USA ", {' ', ',', ';'}),
std::vector<std::string>({"John", "Doe", "42", "New", "York", "USA"}));
}