Skip to content
5 changes: 3 additions & 2 deletions 01_week/tasks/addition/addition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@


int64_t Addition(int a, int b) {
throw std::runtime_error{"Not implemented"};
}

return static_cast<int64_t>(a) + static_cast<int64_t>(b);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Лишний каст, после приведения одной переменной, вторая приведется неявно, необходимо понимать и использовать данные возможности языка для лаконичности

}
59 changes: 56 additions & 3 deletions 01_week/tasks/char_changer/char_changer.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,60 @@
#include <cstddef>
#include <stdexcept>

size_t CharChanger(char* array, size_t size, char delimiter) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Не нужно изменять сигнатуру функции если не просят. В данном случае это некорректное изменение, так как теряется часть функциональности

if (size == 0) return 0;

size_t CharChanger(char array[], size_t size, char delimiter = ' ') {
throw std::runtime_error{"Not implemented"};
}
size_t writePos = 0; // куда пишем результат
size_t readPos = 0; // откуда читаем исходные символы

while (readPos < size - 1) { // последний символ — '\0'
char currentChar = array[readPos];

// Приведения текущего символа к определенным типам: пробел, число или ASCII символ
bool isSpace = std::isspace(static_cast<unsigned char>(currentChar)) != 0;
bool isDigit = std::isdigit(static_cast<unsigned char>(currentChar)) != 0;
bool isAlpha = std::isalpha(static_cast<unsigned char>(currentChar)) != 0;
Copy link
Contributor Author

@18thday 18thday Dec 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Зачем создавать переменные если переменная:

  1. Используется единожды
  2. Если используется, то другие не используются (на каждом шаге выполняются все проверки)


// Подсчёт длины последовательности одинаковых символов
size_t nextPos = readPos + 1;
while (nextPos < size - 1 && array[nextPos] == currentChar) {
nextPos++;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Постфиксный инкремент создает ненужную копию

}
size_t runLength = nextPos - readPos;

// Обработка пробелов
if (isSpace) {
array[writePos++] = delimiter;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

более выразительно переписать с использованием continue и следовательно убрать вложенность и непосредственно следом идущий else, так как иначе приходится листать весь блок кода else чтобы понять, будет ли ещё что-то изменяться

} else {
char outChar;

if (isDigit) {
outChar = '*';
} else if (isAlpha) {
if (std::islower(static_cast<unsigned char>(currentChar))) {
outChar = std::toupper(static_cast<unsigned char>(currentChar));
} else {
outChar = currentChar; // буква и так прописная
}
} else {
outChar = '_';
}

array[writePos++] = outChar;

// Добавление количества повторений
if (runLength > 1) {
if (runLength >= 10) {
array[writePos++] = '0';
} else {
array[writePos++] = char('0' + runLength);
}
}
}

readPos = nextPos;
}

array[writePos] = '\0';
return writePos;
}
38 changes: 36 additions & 2 deletions 01_week/tasks/check_flags/check_flags.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,39 @@ enum class CheckFlags : uint8_t {
};

void PrintCheckFlags(CheckFlags flags) {
throw std::runtime_error{"Not implemented"};
}
uint8_t value = static_cast<uint8_t>(flags);
uint8_t all_bits = static_cast<uint8_t>(CheckFlags::ALL);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

единственное место использования


// выход за пределы возможных значений
if (value & ~all_bits) {
return;
}

// не установлено никаких флагов
if (value == 0) {
std::cout << "[]";
return;
}

const std::pair<CheckFlags, const char*> flag_names[] = {
Copy link
Contributor Author

@18thday 18thday Dec 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Это могло бы быть уместно в случае статической переменной, но в данном случае при каждом вызове функции мы создаем массив из шести пар, которые можем даже не использовать, очень неэффективный поход.

{CheckFlags::TIME, "TIME"},
{CheckFlags::DATE, "DATE"},
{CheckFlags::USER, "USER"},
{CheckFlags::CERT, "CERT"},
{CheckFlags::KEYS, "KEYS"},
{CheckFlags::DEST, "DEST"}
};

bool first = true;
std::cout << "[";
for (const auto& flag : flag_names) {
if (value & static_cast<uint8_t>(flag.first)) {
if (!first) {
std::cout << ",";
}
std::cout << flag.second;
first = false;
}
}
std::cout << "]";
}
68 changes: 68 additions & 0 deletions 01_week/tasks/length_lit/length_lit.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
constexpr long double FT_TO_M = 0.3048L;
constexpr long double M_TO_FT = 1.0L / FT_TO_M;
constexpr long double FT_TO_IN = 12.0L;
constexpr long double IN_TO_FT = 1.0L / FT_TO_IN;
constexpr long double FT_TO_CM = FT_TO_M * 100.0L;
constexpr long double CM_TO_FT = 1.0L / FT_TO_CM;

constexpr long double IN_TO_M = 0.0254L;
constexpr long double M_TO_IN = 1.0L / IN_TO_M;
constexpr long double IN_TO_CM = IN_TO_M * 100.0L;
constexpr long double CM_TO_IN = 1.0L / IN_TO_CM;


Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

лишняя строка

constexpr long double CM_TO_M = 0.01L;
constexpr long double M_TO_CM = 1.0L / CM_TO_M;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Как будто объявлено лишнее количество констант и удобнее объявить меньшее количество а часть выражений перенести в код операторов



// ft ->
constexpr double operator"" _ft_to_m(long double value) {
return static_cast<double>(value * FT_TO_M);
}

constexpr double operator"" _ft_to_in(long double value) {
return static_cast<double>(value * FT_TO_IN);
}

constexpr double operator"" _ft_to_cm(long double value) {
return static_cast<double>(value * FT_TO_CM);
}

// in ->
constexpr double operator"" _in_to_m(long double value) {
return static_cast<double>(value * IN_TO_M);
}

constexpr double operator"" _in_to_ft(long double value) {
return static_cast<double>(value * IN_TO_FT);
}

constexpr double operator"" _in_to_cm(long double value) {
return static_cast<double>(value * IN_TO_CM);
}

// cm ->
constexpr double operator"" _cm_to_m(long double value) {
return static_cast<double>(value * CM_TO_M);
}

constexpr double operator"" _cm_to_ft(long double value) {
return static_cast<double>(value * CM_TO_FT);
}

constexpr double operator"" _cm_to_in(long double value) {
return static_cast<double>(value * CM_TO_IN);
}

// m ->
constexpr double operator"" _m_to_ft(long double value) {
return static_cast<double>(value * M_TO_FT);
}

constexpr double operator"" _m_to_in(long double value) {
return static_cast<double>(value * M_TO_IN);
}

constexpr double operator"" _m_to_cm(long double value) {
return static_cast<double>(value * M_TO_CM);
}
29 changes: 27 additions & 2 deletions 01_week/tasks/print_bits/print_bits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,31 @@
#include <stdexcept>


void PrintBits(long long value, size_t bytes) {
throw std::runtime_error{"Not implemented"};
void PrintBits(long long value, size_t bytes) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

лишний пробел перед void

using std::cout;
using ULL = unsigned long long;
constexpr char BITS_IN_BYTE = 8;
constexpr char BYTES_IN_LONG_LONG = sizeof(long long);

size_t bits = bytes * BITS_IN_BYTE;

// Сдвиг, чтобы напечатать младшие байты
ULL mask = (bytes == BYTES_IN_LONG_LONG)
? ~0ULL
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Как-то неожиданно отступ стал 2 пробела вместо принятых 4. Обычно пишут в одном стиле, когда сами пишут

: ((1ULL << (bits)) - 1);

ULL valueWithMask = static_cast<ULL>(value) & mask;
cout << "0b";

for(size_t i = 0; i < bits; ++i) {
size_t bitIndex = bits - i - 1;
bool bit = (valueWithMask >> bitIndex) & 1; // определение бита на позиции bitIndex
cout << (bit ? '1' : '0');

if(bitIndex % 4 == 0 && bitIndex != 0) {
cout << '\'';
}
}

cout << '\n';
}
36 changes: 33 additions & 3 deletions 01_week/tasks/quadratic/quadratic.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,36 @@
#include <stdexcept>

#include <iostream>
#include <iomanip>
#include <cmath>

void SolveQuadratic(int a, int b, int c) {
throw std::runtime_error{"Not implemented"};
}
using std::cout, std::setprecision;
if(a == 0 && b == 0 && c == 0) {
cout << "infinite solutions";
return;
}
if(a == 0 && b == 0 && c != 0) {
cout << "no solutions";
return;
}

if(a != 0) { // two roots
int discriminant = b * b - 4 * a * c;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

то 2, то 4 пробела


cout << setprecision(6);
if(discriminant > 0) {
double discriminantRoot = sqrt(discriminant);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

лучше использовать std::sqrt

double x1 = (-b - discriminantRoot) / (2.0*a);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

пробелы вокруг *

double x2 = (-b + discriminantRoot) / (2.0*a);
cout << x1 << ' ' << x2;
} else if(discriminant == 0){
double x1 = (-b) / (2.0*a);
cout << x1;
} else {
cout << "no solutions";
}
} else { // only one root
double x1 = static_cast<double>(-c) / b;
cout << x1;
}
}
17 changes: 13 additions & 4 deletions 01_week/tasks/rms/rms.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
#include <cstdef>
#include <stdexcept>
#include <cmath>


double CalculateRMS(double values[], size_t size) {
throw std::runtime_error{"Not implemented"};
}
if(size == 0) return 0.0;
if(values == nullptr) return 0.0;

long double sumOfSquares = 0;

for(size_t i = 0; i < size; ++i) {
double &value = values[i];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const double&

sumOfSquares += value * value;
}

return sqrt(sumOfSquares / size);
}
16 changes: 13 additions & 3 deletions 02_week/tasks/func_array/func_array.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
#include <stdexcept>


double ApplyOperations(double a, double b /* other arguments */) {
throw std::runtime_error{"Not implemented"};
}
double ApplyOperations(double a, double b, double (**ops)(double, double), size_t size) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Для наглядности хотелось, чтобы это был массив укзателей на функцию

if (ops == nullptr || size == 0) return 0.0;

double sum = 0.0;
for (size_t i = 0; i < size; ++i) {
if (ops[i] == nullptr) {
continue; // �� ��������� ��������, ���� ��������� ������� ����� nullptr
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Вероятно нужно поправить кодировку на UTF-8

}
sum += ops[i](a, b);
}

return sum;
}
13 changes: 9 additions & 4 deletions 02_week/tasks/last_of_us/last_of_us.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#include <stdexcept>
template <typename Predicate>
const int* FindLastElement(const int* begin, const int* end, Predicate pred) {
if (begin == nullptr || end == nullptr || begin > end) return end;


/* return_type */ FindLastElement(/* ptr_type */ begin, /* ptr_type */ end, /* func_type */ predicate) {
throw std::runtime_error{"Not implemented"};
for (const int* ptr = end - 1; ptr >= begin; --ptr) {
if (pred(*ptr)) {
return ptr;
}
}
return end;
}
50 changes: 44 additions & 6 deletions 02_week/tasks/little_big/little_big.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,48 @@
#include <stdexcept>
#include <iomanip>

void PrintMemory(int value, bool reverse = false) {
using std::cout, std::hex, std::uppercase, std::setw, std::setfill;
// Представляем как массив байт
const uint8_t* bytes = reinterpret_cast<const uint8_t*>(&value);

void PrintMemory(int /* write arguments here */) {
throw std::runtime_error{"Not implemented"};
cout << "0x";
cout << uppercase << hex;
if (reverse) {
// Печатаем от старших к младшим
for (int i = sizeof(int) - 1; i >= 0; --i) {
cout << setw(2)
<< setfill('0') << static_cast<int>(bytes[i]);
}
}
else {
// Печатаем от младших к старшим
for (size_t i = 0; i < sizeof(int); ++i) {
cout << setw(2)
<< setfill('0') << static_cast<int>(bytes[i]);
}
}
cout << "\n";
}

void PrintMemory(double /* write arguments here */) {
throw std::runtime_error{"Not implemented"};
}
void PrintMemory(double value, bool reverse = false) {
using std::cout, std::hex, std::uppercase, std::setw, std::setfill;
const uint8_t* bytes = reinterpret_cast<const uint8_t*>(&value);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Большое дублирование кода после того как мы получили указатель и зная sizeof(value) далее можно было вызывать одну общую функцию, и переиспользовать код


cout << "0x";
cout << uppercase << hex;
if (reverse) {
// Печатаем от старших к младшим
for (int i = sizeof(double) - 1; i >= 0; --i) {
cout << setw(2)
<< setfill('0') << static_cast<int>(bytes[i]);
}
}
else {
// Печатаем от младших к старшим
for (size_t i = 0; i < sizeof(double); ++i) {
cout << setw(2)
<< setfill('0') << static_cast<int>(bytes[i]);
}
}
cout << "\n";
}
Loading