Skip to content
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
4 changes: 3 additions & 1 deletion 01_week/tasks/addition/addition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@


int64_t Addition(int a, int b) {
throw std::runtime_error{"Not implemented"};
int64_t x1 = a;
int64_t x2 = b;
return x1 + x2;
Copy link
Contributor

Choose a reason for hiding this comment

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

две новые переменные создаватьизлишне, достаточно в return написать выражение, используя для одного из операндов static_cast

}
69 changes: 67 additions & 2 deletions 01_week/tasks/char_changer/char_changer.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,72 @@
#include <cstddef>
#include <stdexcept>

#include <cctype>

size_t CharChanger(char array[], size_t size, char delimiter = ' ') {
throw std::runtime_error{"Not implemented"};
if (size == 0) return 0;

size_t pos_read = 0; // позиция чтения
size_t pos_write = 0; // позиция записи

while (array[pos_read] != '\0') { // проверка на последний символ
char current_char = array[pos_read]; // изменяемый символ
size_t count = 1;

while (pos_read + count < size && array[pos_read + count] == current_char) { // подсчет повторающихся символов
++count;
}

if (isdigit(current_char)) {// проверка на цифры
current_char = '*';
}
else if (islower(current_char)) { // проверка на строчные буквы
current_char = toupper(current_char);
}
else if (!isupper(current_char) && current_char != ' ') { //остальные символы, кроме пробелов и прописных букв
current_char = '_';
}

if (current_char == ' ') { // оработка пробелов
current_char = delimiter;
if (pos_write < size) {
array[pos_write] = current_char;
++pos_write;
}
}

else { // обработка остальных символов
if (pos_write < size) { //записываем символ
array[pos_write] = current_char;
++pos_write;
}
if ( count > 1) { ///обрабатываем повторения
if (count >= 10) {
if (pos_write < size) {
array[pos_write] = '0';
++pos_write;
}
}
else {
if (pos_write < size) {
array[pos_write] = '0' + count;
++pos_write;
}
}
}
}
pos_read += count;
}



// Добавляем завершающий нуль-символ
if (pos_write < size) {
array[pos_write] = '\0';
}
else if (size > 0) {
array[size - 1] = '\0';
pos_write = size - 1;
}

return pos_write;
}
42 changes: 41 additions & 1 deletion 01_week/tasks/check_flags/check_flags.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <cstdint>
#include <stdexcept>
#include <iostream>


enum class CheckFlags : uint8_t {
Expand All @@ -14,5 +15,44 @@ enum class CheckFlags : uint8_t {
};

void PrintCheckFlags(CheckFlags flags) {
throw std::runtime_error{"Not implemented"};
uint8_t value = static_cast<uint8_t>(flags);

if (value > static_cast<uint8_t>(CheckFlags::ALL)) {
return;
}

// Обработка специального случая NONE
if (flags == CheckFlags::NONE) {
std::cout << "[]";
return;
}

std::cout<< "[";
const char* comma = "";
if ((value & static_cast<uint8_t>(CheckFlags::TIME)) !=0) {
std::cout<< comma << "TIME";
comma = ",";
}
if ((value & static_cast<uint8_t>(CheckFlags::DATE)) !=0) {
std::cout<< comma << "DATE";
comma = ",";
}
if ((value & static_cast<uint8_t>(CheckFlags::USER)) !=0) {
std::cout<< comma << "USER";
comma = ",";
}
if ((value & static_cast<uint8_t>(CheckFlags::CERT)) !=0) {
std::cout<< comma << "CERT";
comma = ",";
}
if ((value & static_cast<uint8_t>(CheckFlags::KEYS)) !=0) {
std::cout<< comma << "KEYS";
comma = ",";
}
if ((value & static_cast<uint8_t>(CheckFlags::DEST)) !=0) {
std::cout<< comma << "DEST";
comma = ",";
}
Copy link
Contributor

Choose a reason for hiding this comment

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

можно было избавиться от дублирования похожего кода


std::cout<<"]";
}
44 changes: 44 additions & 0 deletions 01_week/tasks/length_lit/length_lit.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

namespace length_const {
constexpr double METER = 1.0;
constexpr double CENTIMETER = 0.01;
constexpr double FOOT = 0.3048;
constexpr double INCH = 0.0254;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

это хорошо, METER только лишний в данном случае


constexpr double operator""_ft_to_in(long double value) {
return value * length_const::FOOT / length_const::INCH;
}
constexpr double operator""_ft_to_cm(long double value) {
return value * length_const::FOOT / length_const::CENTIMETER;
}
constexpr double operator""_ft_to_m(long double value) {
return value * length_const::FOOT / length_const::METER;
}
constexpr double operator""_in_to_ft(long double value) {
return value * length_const::INCH / length_const::FOOT;
}
constexpr double operator""_in_to_cm(long double value) {
return value * length_const::INCH / length_const::CENTIMETER;
}
constexpr double operator""_in_to_m(long double value) {
return value * length_const::INCH / length_const::METER;
}
constexpr double operator""_cm_to_ft(long double value) {
return value * length_const::CENTIMETER / length_const::FOOT;
}
constexpr double operator""_cm_to_in(long double value) {
return value * length_const::CENTIMETER / length_const::INCH;
}
constexpr double operator""_cm_to_m(long double value) {
return value * length_const::CENTIMETER / length_const::METER;
}
constexpr double operator""_m_to_ft(long double value) {
return value * length_const::METER / length_const::FOOT;
}
constexpr double operator""_m_to_in(long double value) {
return value * length_const::METER / length_const::INCH;
}
constexpr double operator""_m_to_cm(long double value) {
return value * length_const::METER / length_const:: CENTIMETER;
}
16 changes: 15 additions & 1 deletion 01_week/tasks/print_bits/print_bits.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
#include <cstddef>
#include <stdexcept>
#include <iostream>


void PrintBits(long long value, size_t bytes) {
throw std::runtime_error{"Not implemented"};
if (bytes == 0 || bytes > 8) {
return;
}

std::cout << "0b";
for (int i = static_cast<int>(bytes * 8 - 1); i >= 0; i--) {
Copy link
Contributor

@18thday 18thday Jan 19, 2026

Choose a reason for hiding this comment

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

префиксный декремент должен быть

unsigned long long bit = (static_cast<unsigned long long>(value) >> i) & 1;
std::cout << bit;

if (i > 0 && i % 4 == 0) {
std::cout << "'";
}
}
std::cout << std::endl;
}
50 changes: 47 additions & 3 deletions 01_week/tasks/quadratic/quadratic.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,50 @@
#include <iostream>
#include <iomanip>
#include <cmath>
#include <stdexcept>


void SolveQuadratic(int a, int b, int c) {
throw std::runtime_error{"Not implemented"};
}
std::cout << std::defaultfloat << std::setprecision(6);

if (a == 0 && b == 0 && c == 0) {
std::cout << "infinite solutions";
return;
}

if (a == 0) {
if (b == 0) {
std::cout << "no solutions";
}
else {
Copy link
Contributor

Choose a reason for hiding this comment

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

лучше было сделать две короткие ветви с return без else и ещё оного уровня вложенности

double x = -static_cast<double>(c) / b;
if (x == -0) {
x = 0;
}
std::cout << x;
}
return;
}

double discriminant = static_cast<double>(b) * b - 4.0 * a * c;
if (discriminant < 0) {
std::cout << "no solutions";
Copy link
Contributor

Choose a reason for hiding this comment

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

аналогично, после лучше было поставить return а дальше убрать else, у последнего не было бы вложенности и не нужно было бы пролистать оставшийся код, чтоббы увидеть, что больше ничего в std::cout не добавляется общего для случаев

}
else if (discriminant == 0) {
double x = -static_cast<double>(b) / (2.0 * a);
if (x == -0) {
x = 0;
}
std::cout << x;
}
else {
double sqrt_d = std::sqrt(discriminant);
double x1 = (-static_cast<double>(b) - sqrt_d) / (2.0 * a);
double x2 = (-static_cast<double>(b) + sqrt_d) / (2.0 * a);

if (x1 > x2) {
std::swap(x1, x2);
}

std::cout << x1 << " " << x2;
}
}
19 changes: 15 additions & 4 deletions 01_week/tasks/rms/rms.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
#include <cstdef>
#include <stdexcept>

#include <cmath>

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

Choose a reason for hiding this comment

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

на nullptr часто принято не сравнивать явно, а пользоваться неявным приведением указателей к bool, !values

return 0.0;
}

double sum_of_squares = 0.0;
for (size_t i = 0; i < size; ++i) {
sum_of_squares += values[i] * values[i];
}

double mean_of_squares = sum_of_squares / size;
double rms = std::sqrt(mean_of_squares);

return rms;
Copy link
Contributor

Choose a reason for hiding this comment

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

выше две лишние переменные, которые не используются ещё где-то, достаточно было аписать выражение сразу в return

}
15 changes: 13 additions & 2 deletions 02_week/tasks/func_array/func_array.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
#include <stdexcept>


double ApplyOperations(double a, double b /* other arguments */) {
throw std::runtime_error{"Not implemented"};
double ApplyOperations(double a, double b, double (*operations[])(double, double), size_t size ) {
double sum = 0.0;
Copy link
Contributor

Choose a reason for hiding this comment

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

переменную нужно создать после прохождения условий по выходу из функций, всегда стараемся уменьшить область видимости переменных перемещая их определения ближе к непосредственному использованию


if (operations == nullptr || size == 0) {
return 0.0;
}

for (size_t i = 0; i < size; ++i) {
if (operations[i] != nullptr) {
sum += operations[i](a, b);
}
}
return sum;
}
12 changes: 10 additions & 2 deletions 02_week/tasks/last_of_us/last_of_us.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
#include <stdexcept>


/* return_type */ FindLastElement(/* ptr_type */ begin, /* ptr_type */ end, /* func_type */ predicate) {
throw std::runtime_error{"Not implemented"};
const int* FindLastElement(const int* begin, const int* end, bool (*predicate)(int)) {
if (begin == nullptr || begin == nullptr || begin > end || begin == end) {
Copy link
Contributor

Choose a reason for hiding this comment

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

проверка с неявным приведением к bool была бы компактнее

return end;
}
for (const int* ptr = end - 1; ptr >= begin; --ptr) {
if (predicate(*ptr)) {
return ptr;
}
}
return end;
}
41 changes: 35 additions & 6 deletions 02_week/tasks/little_big/little_big.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,39 @@
#include <stdexcept>
#include <cstring>
#include <iostream>
#include <cstdint>
#include <iomanip>


void PrintMemory(int /* write arguments here */) {
throw std::runtime_error{"Not implemented"};
void PrintMemory(int value, bool reverse = false) {
uint32_t bytes;
std::memcpy(&bytes, &value, sizeof(value));

if (reverse) {
std::cout << "0x" << std::uppercase << std::hex << std::setfill('0') << std::setw(8)
<< bytes << "\n";
} else {
uint32_t reversed = 0;
for (size_t i = 0; i < sizeof(value); ++i) {
reversed = (reversed << 8) | ((bytes >> (i * 8)) & 0xFF);
}
std::cout << "0x" << std::uppercase << std::hex << std::setfill('0') << std::setw(8)
<< reversed << "\n";
}
}

void PrintMemory(double /* write arguments here */) {
throw std::runtime_error{"Not implemented"};
}
void PrintMemory(double value, bool reverse = false) {
uint64_t bytes;
std::memcpy(&bytes, &value, sizeof(value));
Copy link
Contributor

@18thday 18thday Jan 19, 2026

Choose a reason for hiding this comment

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

можно было переписать код чтобы не дублировать, вызывая для double и int третью функцию, куда передавать указатель, размер и флаг


if (reverse) {
std::cout << "0x" << std::uppercase << std::hex << std::setfill('0') << std::setw(16)
<< bytes << "\n";
} else {
uint64_t reversed = 0;
for (size_t i = 0; i < sizeof(value); ++i) {
reversed = (reversed << 8) | ((bytes >> (i * 8)) & 0xFF);
}
std::cout << "0x" << std::uppercase << std::hex << std::setfill('0') << std::setw(16)
<< reversed << "\n";
}
}
Loading