Skip to content

Homework all done#20

Open
salexkon wants to merge 23 commits intoDafeCpp:mainfrom
salexkon:homework
Open

Homework all done#20
salexkon wants to merge 23 commits intoDafeCpp:mainfrom
salexkon:homework

Conversation

@salexkon
Copy link
Copy Markdown

All tasks done with tests

Copy link
Copy Markdown

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

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

clang-tidy made some suggestions

There were too many comments to post at once. Showing the first 15 out of 16. Check the log or trigger a new build to see more.

Comment thread task_01/src/task_01.cpp Outdated
std::vector<int> find_two_nums(int target, std::vector<int> arr) {
int left = 0;
int right = arr.size() - 1;
int sum = arr[right] + arr[left];
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

warning: Value stored to 'sum' during its initialization is never read [clang-analyzer-deadcode.DeadStores]

  int sum = arr[right] + arr[left];
      ^
Additional context

task_01/src/task_01.cpp:5: Value stored to 'sum' during its initialization is never read

  int sum = arr[right] + arr[left];
      ^

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

поправь это замечание от clang-tidy

и лучше поправить все замечания от clang-tidy

Comment thread task_04/src/test.cpp

#include "buy_fish.hpp"

int totalFishBought(const std::vector<int>& purchases) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

warning: function 'totalFishBought' can be made static or moved into an anonymous namespace to enforce internal linkage [misc-use-internal-linkage]

Suggested change
int totalFishBought(const std::vector<int>& purchases) {
static int totalFishBought(const std::vector<int>& purchases) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

это не обязательно исправлять, но лучше исправить

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

namespace {

int totalFishBought(const std::vector<int>& purchases) {
...
}

bool isValidSchedule(const std::vector<int>& purchases, int k) {
...
}

}

Comment thread task_04/src/test.cpp
return total;
}

bool isValidSchedule(const std::vector<int>& purchases, int k) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

warning: function 'isValidSchedule' can be made static or moved into an anonymous namespace to enforce internal linkage [misc-use-internal-linkage]

Suggested change
bool isValidSchedule(const std::vector<int>& purchases, int k) {
static bool isValidSchedule(const std::vector<int>& purchases, int k) {

Comment thread task_05/src/test.cpp

#include "QuickSort.hpp"

bool isSorted(const std::vector<int>& arr) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

warning: function 'isSorted' can be made static or moved into an anonymous namespace to enforce internal linkage [misc-use-internal-linkage]

Suggested change
bool isSorted(const std::vector<int>& arr) {
static bool isSorted(const std::vector<int>& arr) {

Comment thread task_08/src/HashTable.hpp Outdated
for (const auto& entry : table[i]) {
std::cout << "(" << entry.first << ": " << entry.second << ") ";
}
std::cout << std::endl;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl]

Suggested change
std::cout << std::endl;
std::cout << '\n';

Comment thread task_08/src/test.cpp

auto res = ht.find("apple");
ASSERT_TRUE(res.has_value());
EXPECT_EQ(res.value(), 10);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

warning: unchecked access to optional value [bugprone-unchecked-optional-access]

  EXPECT_EQ(res.value(), 10);
            ^

Comment thread task_08/src/test.cpp

res = ht.find("banana");
ASSERT_TRUE(res.has_value());
EXPECT_EQ(res.value(), 20);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

warning: unchecked access to optional value [bugprone-unchecked-optional-access]

  EXPECT_EQ(res.value(), 20);
            ^

Comment thread task_08/src/test.cpp

res = ht.find("orange");
ASSERT_TRUE(res.has_value());
EXPECT_EQ(res.value(), 30);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

warning: unchecked access to optional value [bugprone-unchecked-optional-access]

  EXPECT_EQ(res.value(), 30);
            ^

Comment thread task_08/src/test.cpp

auto res = ht.find("apple");
ASSERT_TRUE(res.has_value());
EXPECT_EQ(res.value(), 50);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

warning: unchecked access to optional value [bugprone-unchecked-optional-access]

  EXPECT_EQ(res.value(), 50);
            ^

Comment thread task_08/src/test.cpp

res = ht.find("banana");
ASSERT_TRUE(res.has_value());
EXPECT_EQ(res.value(), 20);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

warning: unchecked access to optional value [bugprone-unchecked-optional-access]

  EXPECT_EQ(res.value(), 20);
            ^

Copy link
Copy Markdown

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

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

clang-tidy made some suggestions

Comment thread task_08/src/test.cpp
for (int i = 0; i < 1000; ++i) {
auto val = ht.find("key" + std::to_string(i));
ASSERT_TRUE(val.has_value());
EXPECT_EQ(val.value(), i);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

warning: unchecked access to optional value [bugprone-unchecked-optional-access]

    EXPECT_EQ(val.value(), i);
              ^

Comment thread task_01/src/task_01.hpp Outdated
#pragma once
#include <vector>

std::vector<int> find_two_nums(int target, std::vector<int> arr); No newline at end of file
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

FindTwoNums

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

Comment thread task_02/src/stack.hpp
#include <vector>

template <typename T>
class Stack {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

давай хотя бы еще метод Size добавим, а лучше и ещё IsEpty (можно и просто Empty)

Comment thread task_02/src/stack.hpp Outdated
return result;
}

T getmax() {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

GetMax

Comment thread task_02/src/stack.hpp Outdated
private:
std::vector<int> data_;
std::vector<T> data_;
std::vector<T> data_mins;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

data_mins_ для data_ добавили подчеркивание что бы обозначить что это private поле, а для data_mins не добавили, чем оно хуже)

Comment thread task_02/src/test.cpp
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

добавь своих тестов, например с пустым стэком

Comment thread task_03/src/topology_sort.cpp Outdated
@@ -1 +1,18 @@
#include "topology_sort.hpp"

std::vector<int> days_until_warmer(const std::vector<int>& temperatures) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

DaysUntilWarmer

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

переименуй файл плиз) название из прошлого семестра осталось) (алгоритмы 2 семетр)

Comment thread task_03/src/test.cpp
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

добавь тестов на пустой массив

Comment thread task_05/src/QuickSort.cpp
@@ -0,0 +1,26 @@
#include "QuickSort.hpp"

void quickSortwrap(std::vector<int>& arr, int low, int high) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Функция quickSortwrap не предназначена для внешнего использования, поместить в анонимное пространство имён (и убрать из hpp)

Comment thread task_06/src/ord_stat.cpp Outdated
int right = arr.size() - 1;

while (left <= right) {
int pivotIndex = left + rand() % (right - left + 1);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

pivot_index по кодстайлу)

Comment thread task_07/src/AVLTree.hpp Outdated

#include <iostream>

struct Node {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

лучше сделать внутреннем классом AVLTree (в private). это часть реализации, ее лучше скрывать

@DafeCpp DafeCpp deleted a comment from github-actions Bot Jun 22, 2025
@DafeCpp DafeCpp deleted a comment from github-actions Bot Jun 22, 2025
@DafeCpp DafeCpp deleted a comment from github-actions Bot Jun 22, 2025
Comment thread task_07/src/AVLTree.hpp
Node(int k) : key(k), left(nullptr), right(nullptr), height(1) {}
};

class AVLTree {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

выделение памяти через new есть, а деструктора нет, значит есть утечка памяти

@DafeCpp DafeCpp deleted a comment from github-actions Bot Jun 22, 2025
@DafeCpp DafeCpp deleted a comment from github-actions Bot Jun 22, 2025
Comment thread task_08/src/HashTable.hpp
#include <vector>

template <typename T>
class HashTable {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

давай добавим методы Size и IsEmpty

Copy link
Copy Markdown

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

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

clang-tidy made some suggestions

Comment thread task_07/src/AVLTree.hpp
Node* left{nullptr};
Node* right{nullptr};
int height;
Node(int k) : key(k), left(nullptr), right(nullptr), height(1) {}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

warning: member initializer for 'left' is redundant [cppcoreguidelines-use-default-member-init]

Suggested change
Node(int k) : key(k), left(nullptr), right(nullptr), height(1) {}
Node(int k) : key(k), , right(nullptr), height(1) {}

Comment thread task_07/src/AVLTree.hpp
Node* left{nullptr};
Node* right{nullptr};
int height;
Node(int k) : key(k), left(nullptr), right(nullptr), height(1) {}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

warning: member initializer for 'right' is redundant [cppcoreguidelines-use-default-member-init]

Suggested change
Node(int k) : key(k), left(nullptr), right(nullptr), height(1) {}
Node(int k) : key(k), left(nullptr), , height(1) {}

Comment thread task_07/src/AVLTree.hpp
Node* left{nullptr};
Node* right{nullptr};
int height;
Node(int k) : key(k), left(nullptr), right(nullptr), height(1) {}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

warning: use default member initializer for 'height' [cppcoreguidelines-use-default-member-init]

Suggested change
Node(int k) : key(k), left(nullptr), right(nullptr), height(1) {}
int height{1};
Node(int k) : key(k), left(nullptr), right(nullptr), {}

Comment thread task_07/src/AVLTree.hpp

Node* root = nullptr;

int height(Node* n) { return n ? n->height : 0; }
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

warning: Use of memory after it is freed [clang-analyzer-cplusplus.NewDelete]

  int height(Node* n) { return n ? n->height : 0; }
                                   ^
Additional context

task_07/src/test.cpp:63: Calling 'AVLTree::erase'

  tree.erase(15);
  ^

task_07/src/AVLTree.hpp:144: Calling 'AVLTree::erase'

  void erase(int key) { root = erase(root, key); }
                               ^

task_07/src/AVLTree.hpp:97: 'node' is non-null

    if (!node) return node;
         ^

task_07/src/AVLTree.hpp:97: Taking false branch

    if (!node) return node;
    ^

task_07/src/AVLTree.hpp:99: Assuming 'key' is >= field 'key'

    if (key < node->key)
        ^

task_07/src/AVLTree.hpp:99: Taking false branch

    if (key < node->key)
    ^

task_07/src/AVLTree.hpp:101: Assuming 'key' is <= field 'key'

    else if (key > node->key)
             ^

task_07/src/AVLTree.hpp:101: Taking false branch

    else if (key > node->key)
         ^

task_07/src/AVLTree.hpp:104: Field 'left' is null

      if (!node->left || !node->right) {
                 ^

task_07/src/AVLTree.hpp:104: Left side of '||' is true

      if (!node->left || !node->right) {
                      ^

task_07/src/AVLTree.hpp:105: Field 'left' is null

        Node* temp = node->left ? node->left : node->right;
                           ^

task_07/src/AVLTree.hpp:105: '?' condition is false

        Node* temp = node->left ? node->left : node->right;
                     ^

task_07/src/AVLTree.hpp:106: 'temp' is non-null

        if (!temp) {
             ^

task_07/src/AVLTree.hpp:106: Taking false branch

        if (!temp) {
        ^

task_07/src/AVLTree.hpp:112: Calling '~Node'

        delete temp;
        ^

task_07/src/AVLTree.hpp:13: Memory is released

      delete left;
      ^

task_07/src/AVLTree.hpp:112: Returning from '~Node'

        delete temp;
        ^

task_07/src/AVLTree.hpp:120: 'node' is non-null

    if (!node) return node;
         ^

task_07/src/AVLTree.hpp:120: Taking false branch

    if (!node) return node;
    ^

task_07/src/AVLTree.hpp:122: Calling 'AVLTree::balance'

    return balance(node);
           ^

task_07/src/AVLTree.hpp:57: Calling 'AVLTree::updateHeight'

    updateHeight(n);
    ^

task_07/src/AVLTree.hpp:27: Calling 'AVLTree::height'

    n->height = 1 + std::max(height(n->left), height(n->right));
                             ^

task_07/src/AVLTree.hpp:20: 'n' is non-null

  int height(Node* n) { return n ? n->height : 0; }
                               ^

task_07/src/AVLTree.hpp:20: '?' condition is true

  int height(Node* n) { return n ? n->height : 0; }
                               ^

task_07/src/AVLTree.hpp:20: Use of memory after it is freed

  int height(Node* n) { return n ? n->height : 0; }
                                   ^

Comment thread task_07/src/AVLTree.hpp
}

void updateHeight(Node* n) {
n->height = 1 + std::max(height(n->left), height(n->right));
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

warning: Use of memory after it is freed [clang-analyzer-cplusplus.NewDelete]

    n->height = 1 + std::max(height(n->left), height(n->right));
                             ^
Additional context

task_07/src/AVLTree.hpp:144: Calling 'AVLTree::erase'

  void erase(int key) { root = erase(root, key); }
                               ^

task_07/src/AVLTree.hpp:97: Assuming 'node' is non-null

    if (!node) return node;
        ^

task_07/src/AVLTree.hpp:97: Taking false branch

    if (!node) return node;
    ^

task_07/src/AVLTree.hpp:99: Assuming 'key' is >= field 'key'

    if (key < node->key)
        ^

task_07/src/AVLTree.hpp:99: Taking false branch

    if (key < node->key)
    ^

task_07/src/AVLTree.hpp:101: Assuming 'key' is > field 'key'

    else if (key > node->key)
             ^

task_07/src/AVLTree.hpp:101: Taking true branch

    else if (key > node->key)
         ^

task_07/src/AVLTree.hpp:102: Calling 'AVLTree::erase'

      node->right = erase(node->right, key);
                    ^

task_07/src/AVLTree.hpp:97: Assuming 'node' is non-null

    if (!node) return node;
        ^

task_07/src/AVLTree.hpp:97: Taking false branch

    if (!node) return node;
    ^

task_07/src/AVLTree.hpp:99: Assuming 'key' is < field 'key'

    if (key < node->key)
        ^

task_07/src/AVLTree.hpp:99: Taking true branch

    if (key < node->key)
    ^

task_07/src/AVLTree.hpp:100: Calling 'AVLTree::erase'

      node->left = erase(node->left, key);
                   ^

task_07/src/AVLTree.hpp:97: Assuming 'node' is non-null

    if (!node) return node;
        ^

task_07/src/AVLTree.hpp:97: Taking false branch

    if (!node) return node;
    ^

task_07/src/AVLTree.hpp:99: Assuming 'key' is < field 'key'

    if (key < node->key)
        ^

task_07/src/AVLTree.hpp:99: Taking true branch

    if (key < node->key)
    ^

task_07/src/AVLTree.hpp:100: Calling 'AVLTree::erase'

      node->left = erase(node->left, key);
                   ^

task_07/src/AVLTree.hpp:97: Assuming 'node' is non-null

    if (!node) return node;
        ^

task_07/src/AVLTree.hpp:97: Taking false branch

    if (!node) return node;
    ^

task_07/src/AVLTree.hpp:99: Assuming 'key' is >= field 'key'

    if (key < node->key)
        ^

task_07/src/AVLTree.hpp:99: Taking false branch

    if (key < node->key)
    ^

task_07/src/AVLTree.hpp:101: Assuming 'key' is <= field 'key'

    else if (key > node->key)
             ^

task_07/src/AVLTree.hpp:101: Taking false branch

    else if (key > node->key)
         ^

task_07/src/AVLTree.hpp:104: Assuming field 'left' is null

      if (!node->left || !node->right) {
          ^

task_07/src/AVLTree.hpp:104: Left side of '||' is true

      if (!node->left || !node->right) {
                      ^

task_07/src/AVLTree.hpp:105: Field 'left' is null

        Node* temp = node->left ? node->left : node->right;
                           ^

task_07/src/AVLTree.hpp:105: '?' condition is false

        Node* temp = node->left ? node->left : node->right;
                     ^

task_07/src/AVLTree.hpp:106: Assuming 'temp' is non-null

        if (!temp) {
            ^

task_07/src/AVLTree.hpp:106: Taking false branch

        if (!temp) {
        ^

task_07/src/AVLTree.hpp:112: Calling '~Node'

        delete temp;
        ^

task_07/src/AVLTree.hpp:13: Memory is released

      delete left;
      ^

task_07/src/AVLTree.hpp:112: Returning from '~Node'

        delete temp;
        ^

task_07/src/AVLTree.hpp:120: 'node' is non-null

    if (!node) return node;
         ^

task_07/src/AVLTree.hpp:120: Taking false branch

    if (!node) return node;
    ^

task_07/src/AVLTree.hpp:122: Calling 'AVLTree::balance'

    return balance(node);
           ^

task_07/src/AVLTree.hpp:57: Calling 'AVLTree::updateHeight'

    updateHeight(n);
    ^

task_07/src/AVLTree.hpp:27: Use of memory after it is freed

    n->height = 1 + std::max(height(n->left), height(n->right));
                             ^

Comment thread task_07/src/test.cpp

class AVLTreeTest : public ::testing::Test {
protected:
AVLTree tree;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

warning: member variable 'tree' has protected visibility [cppcoreguidelines-non-private-member-variables-in-classes]

  AVLTree tree;
          ^

Comment thread task_08/src/HashTable.hpp
private:
std::vector<std::list<std::pair<std::string, T>>> table;
int capacity;
int size;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

warning: use default member initializer for 'size' [cppcoreguidelines-use-default-member-init]

Suggested change
int size;
int size{0};

task_08/src/HashTable.hpp:41:

-   HashTable(int init_capacity = 8) : capacity(init_capacity), size(0) {
+   HashTable(int init_capacity = 8) : capacity(init_capacity), {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants