Skip to content

Commit fdc98ac

Browse files
authored
Week 16: Задание 14.04.25 : 13.01 - 13.11 / 150 баллов / 20.04.25 23:59:59 (#19)
* task 13.02 * fix bug * task 13.01
1 parent 415ea7c commit fdc98ac

11 files changed

Lines changed: 187 additions & 2 deletions

File tree

.clang-tidy

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ Checks: '
66
-readability-identifier-length,
77
-readability-else-after-return,
88
-readability-qualified-auto,
9-
-readability-math-missing-parentheses
9+
-readability-math-missing-parentheses,
10+
-readability-magic-numbers
1011
'
1112
CheckOptions:
1213
- key: readability-identifier-naming.NamespaceCase

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ include_directories(shared)
6262

6363

6464
##### subdirectories #####
65-
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-12 week-13 week-14 week-15)
65+
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-12 week-13 week-14 week-15 week-16)
6666
foreach(DIR ${DIRS})
6767
add_subdirectory(${DIR})
6868
endforeach()

week-16/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
set(DIRS task-13-01 task-13-02)
2+
foreach(DIR ${DIRS})
3+
add_subdirectory(${DIR})
4+
endforeach()

week-16/task-13-01/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
include_directories(include)
2+
3+
add_subdirectory(test)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#pragma once
2+
3+
#include <cstdint>
4+
#include <iomanip>
5+
#include <sstream>
6+
#include <string>
7+
#include <vector>
8+
9+
std::string bytes_to_hex_string(const std::vector<uint8_t>& bytes) {
10+
std::ostringstream oss;
11+
oss << std::hex << std::uppercase << std::setfill('0');
12+
for (uint8_t const byte : bytes) {
13+
oss << std::setw(2) << static_cast<int>(byte);
14+
}
15+
return oss.str();
16+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cpp_test(test-13-01.cpp)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include <cstdint>
2+
#include <vector>
3+
4+
#include "bytes-to-hex.hpp"
5+
#include "gtest/gtest.h"
6+
7+
TEST(BytesToHexTest, EmptyVector) {
8+
const std::vector<uint8_t> input;
9+
EXPECT_EQ(bytes_to_hex_string(input), "");
10+
}
11+
12+
TEST(BytesToHexTest, SingleByte) {
13+
EXPECT_EQ(bytes_to_hex_string({0x00}), "00");
14+
EXPECT_EQ(bytes_to_hex_string({0xFF}), "FF");
15+
EXPECT_EQ(bytes_to_hex_string({0x0A}), "0A");
16+
EXPECT_EQ(bytes_to_hex_string({0xA0}), "A0");
17+
}
18+
19+
TEST(BytesToHexTest, CommonCases) {
20+
EXPECT_EQ(bytes_to_hex_string({0xBA, 0xAD}), "BAAD");
21+
EXPECT_EQ(bytes_to_hex_string({0xDE, 0xAD, 0xBE, 0xEF}), "DEADBEEF");
22+
EXPECT_EQ(bytes_to_hex_string({0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF}),
23+
"0123456789ABCDEF");
24+
}
25+
26+
TEST(BytesToHexTest, AllPossibleBytes) {
27+
std::vector<uint8_t> all_bytes(256);
28+
for (int i = 0; i < 256; ++i) {
29+
all_bytes[i] = static_cast<uint8_t>(i);
30+
}
31+
const std::string result = bytes_to_hex_string(all_bytes);
32+
ASSERT_EQ(result.size(), 512); // 256 bytes * 2 chars
33+
for (int i = 0; i < 256; ++i) {
34+
const std::string byte_str = result.substr(i * 2, 2);
35+
const std::string expected = std::string(1, "0123456789ABCDEF"[i >> 4]) +
36+
std::string(1, "0123456789ABCDEF"[i & 0x0F]);
37+
EXPECT_EQ(byte_str, expected);
38+
}
39+
}
40+
41+
TEST(BytesToHexTest, CorrectFormatting) {
42+
const std::vector<uint8_t> input = {0x1, 0x2, 0x03, 0x10, 0xFF};
43+
const std::string result = bytes_to_hex_string(input);
44+
EXPECT_EQ(result, "01020310FF");
45+
EXPECT_TRUE(result.find("01") != std::string::npos);
46+
EXPECT_TRUE(result.find("02") != std::string::npos);
47+
}
48+
49+
TEST(BytesToHexTest, LargeInput) {
50+
const std::vector<uint8_t> large_input(10000, 0xAB);
51+
const std::string result = bytes_to_hex_string(large_input);
52+
EXPECT_EQ(result.size(), 20000);
53+
for (size_t i = 0; i < result.size(); i += 2) {
54+
EXPECT_EQ(result.substr(i, 2), "AB");
55+
}
56+
}

week-16/task-13-02/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
include_directories(include)
2+
3+
add_subdirectory(test)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#pragma once
2+
3+
#include <iomanip>
4+
5+
class PascalsTriangle {
6+
public:
7+
explicit PascalsTriangle(size_t n) : n_(n) {}
8+
9+
void print(std::ostream& out) {
10+
if (n_ == 0) {
11+
return;
12+
}
13+
if (!calculated_) {
14+
triangle_ = calculate(n_);
15+
calculated_ = true;
16+
}
17+
const size_t max_width = num_digits(max_value(triangle_));
18+
for (int i = 0; i < n_; ++i) {
19+
out << std::string((n_ - i - 1) * (max_width + 1) / 2, ' ');
20+
for (const int num : triangle_[i]) {
21+
out << std::setw(max_width) << num << ' ';
22+
}
23+
out << '\n';
24+
}
25+
}
26+
27+
private:
28+
static std::vector<std::vector<int>> calculate(size_t n) {
29+
std::vector<std::vector<int>> triangle(n);
30+
for (int i = 0; i < n; ++i) {
31+
triangle[i].resize(i + 1);
32+
triangle[i][0] = triangle[i][i] = 1;
33+
for (int j = 1; j < i; ++j) {
34+
triangle[i][j] = triangle[i - 1][j - 1] + triangle[i - 1][j];
35+
}
36+
}
37+
return triangle;
38+
}
39+
static int max_value(const std::vector<std::vector<int>>& triangle) {
40+
return triangle.back()[triangle.back().size() / 2];
41+
}
42+
static size_t num_digits(int num) {
43+
size_t res = 0;
44+
while (num > 0) {
45+
num /= 10;
46+
res += 1;
47+
}
48+
return res;
49+
}
50+
51+
size_t n_;
52+
bool calculated_ = false;
53+
std::vector<std::vector<int>> triangle_;
54+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cpp_test(test-13-02.cpp)

0 commit comments

Comments
 (0)