From 98fe64725ab2e5a2a305ae7556de852c58b09f3b Mon Sep 17 00:00:00 2001 From: sh1buya <92878896+sh1buya@users.noreply.github.com> Date: Sat, 10 Feb 2024 08:45:01 +0000 Subject: [PATCH 01/12] first_commit --- task_01/file.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 task_01/file.txt diff --git a/task_01/file.txt b/task_01/file.txt new file mode 100644 index 00000000..e69de29b From 53d98184873b841eadf5075e1d1413963f790462 Mon Sep 17 00:00:00 2001 From: sh1buya <92878896+sh1buya@users.noreply.github.com> Date: Sat, 10 Feb 2024 09:14:18 +0000 Subject: [PATCH 02/12] tst --- task_01/file.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/task_01/file.txt b/task_01/file.txt index e69de29b..d800886d 100644 --- a/task_01/file.txt +++ b/task_01/file.txt @@ -0,0 +1 @@ +123 \ No newline at end of file From 10c8c4339adccc04dea1d4e9d90b9f10d392bc15 Mon Sep 17 00:00:00 2001 From: sh1buya <92878896+sh1buya@users.noreply.github.com> Date: Sat, 10 Feb 2024 09:20:49 +0000 Subject: [PATCH 03/12] tst --- task_01/src/test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/task_01/src/test.cpp b/task_01/src/test.cpp index ef5a86ae..f96dab21 100644 --- a/task_01/src/test.cpp +++ b/task_01/src/test.cpp @@ -4,5 +4,5 @@ #include "topology_sort.hpp" TEST(TopologySort, Simple) { - ASSERT_EQ(1, 1); // Stack [] + ASSERT_EQ(1, 2); // Stack [] } From 773c6fdc61c16e077ab933dbb9d856367eede329 Mon Sep 17 00:00:00 2001 From: sh1buya <92878896+sh1buya@users.noreply.github.com> Date: Mon, 12 Feb 2024 14:48:18 +0000 Subject: [PATCH 04/12] tst --- task_01/README.md | 2 +- task_01/src/test.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/task_01/README.md b/task_01/README.md index 3e7840c5..2da22dae 100644 --- a/task_01/README.md +++ b/task_01/README.md @@ -1,3 +1,3 @@ # Задача 1 -Дано целое число и массив целых чисел, нужно найти 2 числа из массива которые в сумме дадут заданное число \ No newline at end of file +Дано целое число и отсортированный массив целых чисел, нужно найти 2 числа из массива которые в сумме дадут заданное число \ No newline at end of file diff --git a/task_01/src/test.cpp b/task_01/src/test.cpp index f96dab21..ef5a86ae 100644 --- a/task_01/src/test.cpp +++ b/task_01/src/test.cpp @@ -4,5 +4,5 @@ #include "topology_sort.hpp" TEST(TopologySort, Simple) { - ASSERT_EQ(1, 2); // Stack [] + ASSERT_EQ(1, 1); // Stack [] } From 0512b72eaa4563ea2af816aae99e02464148eaae Mon Sep 17 00:00:00 2001 From: sh1buya <92878896+sh1buya@users.noreply.github.com> Date: Mon, 12 Feb 2024 16:36:02 +0000 Subject: [PATCH 05/12] task without tests done --- task_01/src/topology_sort.cpp | 64 +++++++++++++++++++++++++++++++++++ task_01/src/topology_sort.hpp | 16 +++++++++ 2 files changed, 80 insertions(+) diff --git a/task_01/src/topology_sort.cpp b/task_01/src/topology_sort.cpp index e53f670c..3dfb7a0b 100644 --- a/task_01/src/topology_sort.cpp +++ b/task_01/src/topology_sort.cpp @@ -1 +1,65 @@ #include "topology_sort.hpp" + +// O(logn) +int BinSearch(std::vector mas, int num) { + int n = mas.size(); + int l = 0; + int r = n - 1; + int ind = n / 2; + while (r - l >= 0) { + if (mas[ind] == num) { + return ind; + } else if (mas[ind] > num) { + r = ind - 1; + } else if (mas[ind] < num) { + l = ind + 1; + } + ind = (r + l) / 2; + } + return -1; +} + +// O(n^2) +std::tuple NSquareSearch(std::vector mas, int num) { + int n = mas.size(); + for (int i = 0; i < n; i++) { + for (int j = i + 1; j < n; j++) { + if (mas[i] + mas[j] == num) { + return {i, j}; + } + } + } + return {-1, -1}; +} + +// O(nlogn) +std::tuple NLogNSearch(std::vector mas, int num) { + int n = mas.size(); + for (int i = 0; i < n; i++) { + int el1 = mas[i]; + int el2 = num - el1; + int j = BinSearch(mas, el2); + if (j != -1) { + return {i, j}; + } + } + return {-1, -1}; +} + +// O(n) +std::tuple NSearch(std::vector mas, int num) { + int len = mas.size(); + int l = 0; + int r = len - 1; + while (r != l) { + int sum = mas[l] + mas[r]; + if (sum == num) { + return {l, r}; + } else if (sum < num) { + l++; + } else if (sum > num) { + r--; + } + } + return {-1, -1}; +} \ No newline at end of file diff --git a/task_01/src/topology_sort.hpp b/task_01/src/topology_sort.hpp index 6f70f09b..7bd709f4 100644 --- a/task_01/src/topology_sort.hpp +++ b/task_01/src/topology_sort.hpp @@ -1 +1,17 @@ #pragma once + +#include +#include +#include + +// O(logn) +int BinSearch(std::vector mas, int num); + +// O(n^2) +std::tuple NSquareSearch(std::vector mas, int num); + +// O(nlogn) +std::tuple NLogNSearch(std::vector mas, int num); + +// O(n) +std::tuple NSearch(std::vector mas, int num); \ No newline at end of file From aafa1bec5ea94f02126ead31df4fd3426e28e863 Mon Sep 17 00:00:00 2001 From: sh1buya <92878896+sh1buya@users.noreply.github.com> Date: Mon, 12 Feb 2024 17:12:31 +0000 Subject: [PATCH 06/12] tests done --- task_01/src/test.cpp | 79 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 77 insertions(+), 2 deletions(-) diff --git a/task_01/src/test.cpp b/task_01/src/test.cpp index ef5a86ae..17e20c1e 100644 --- a/task_01/src/test.cpp +++ b/task_01/src/test.cpp @@ -3,6 +3,81 @@ #include "topology_sort.hpp" -TEST(TopologySort, Simple) { - ASSERT_EQ(1, 1); // Stack [] +// BinSearch tests +TEST(BinSearch, Simple) { + // even len + std::vector mas = {1, 2, 3, 4, 5, 6, 7, 8}; + ASSERT_EQ(BinSearch(mas, 1), 0); + ASSERT_EQ(BinSearch(mas, 2), 1); + ASSERT_EQ(BinSearch(mas, 3), 2); + ASSERT_EQ(BinSearch(mas, 4), 3); + ASSERT_EQ(BinSearch(mas, 5), 4); + ASSERT_EQ(BinSearch(mas, 6), 5); + ASSERT_EQ(BinSearch(mas, 7), 6); + ASSERT_EQ(BinSearch(mas, 8), 7); + + ASSERT_EQ(BinSearch(mas, 31), -1); + + // odd len + mas = {1, 2, 3, 4, 5, 6, 7, 8, 9}; + ASSERT_EQ(BinSearch(mas, 1), 0); + ASSERT_EQ(BinSearch(mas, 2), 1); + ASSERT_EQ(BinSearch(mas, 3), 2); + ASSERT_EQ(BinSearch(mas, 4), 3); + ASSERT_EQ(BinSearch(mas, 5), 4); + ASSERT_EQ(BinSearch(mas, 6), 5); + ASSERT_EQ(BinSearch(mas, 7), 6); + ASSERT_EQ(BinSearch(mas, 8), 7); + + ASSERT_EQ(BinSearch(mas, 31), -1); + + // empty mas + mas = {}; + ASSERT_EQ(BinSearch(mas, 8), -1); + + // mas of 1 elem + mas = {1}; + ASSERT_EQ(BinSearch(mas, 1), 0); + + ASSERT_EQ(BinSearch(mas, 31), -1); + + // mas of 2 elems + mas = {1, 2}; + ASSERT_EQ(BinSearch(mas, 1), 0); + ASSERT_EQ(BinSearch(mas, 2), 1); + + ASSERT_EQ(BinSearch(mas, 31), -1); } + +// NSquareSearch tests +TEST(NSquareSearch, Simple) { + std::vector mas = {1, 2, 3, 4, 5, 6, 7, 8}; + ASSERT_EQ(NSquareSearch(mas, 11), std::make_tuple(2, 7)); + ASSERT_EQ(NSquareSearch(mas, 9), std::make_tuple(0, 7)); + + ASSERT_EQ(NSquareSearch(mas, 1), std::make_tuple(-1, -1)); + ASSERT_EQ(NSquareSearch(mas, 16), + std::make_tuple(-1, -1)); // not (7, 7), as indexes cant be same +} + +// NLogNSearch tests +TEST(NLogNSearch, Simple) { + std::vector mas = {1, 2, 3, 4, 5, 6, 7, 8}; + ASSERT_EQ(NLogNSearch(mas, 11), std::make_tuple(2, 7)); + ASSERT_EQ(NLogNSearch(mas, 9), std::make_tuple(0, 7)); + + ASSERT_EQ(NLogNSearch(mas, 1), std::make_tuple(-1, -1)); + ASSERT_EQ(NLogNSearch(mas, 16), + std::make_tuple(-1, -1)); // not (7, 7), as indexes cant be same +} + +// NSearch tests +TEST(NSearch, Simple) { + std::vector mas = {1, 2, 3, 4, 5, 6, 7, 8}; + ASSERT_EQ(NSearch(mas, 11), std::make_tuple(2, 7)); + ASSERT_EQ(NSearch(mas, 9), std::make_tuple(0, 7)); + + ASSERT_EQ(NSearch(mas, 1), std::make_tuple(-1, -1)); + ASSERT_EQ(NSearch(mas, 16), + std::make_tuple(-1, -1)); // not (7, 7), as indexes cant be same +} \ No newline at end of file From 639824c5f4cf34b691bb595db9917bc49c51d82a Mon Sep 17 00:00:00 2001 From: sh1buya <92878896+sh1buya@users.noreply.github.com> Date: Sat, 2 Mar 2024 17:28:02 +0000 Subject: [PATCH 07/12] names fixed --- task_01/src/topology_sort.cpp | 66 +++++++++++++++++------------------ task_01/src/topology_sort.hpp | 6 ++-- 2 files changed, 36 insertions(+), 36 deletions(-) diff --git a/task_01/src/topology_sort.cpp b/task_01/src/topology_sort.cpp index 3dfb7a0b..86aaca13 100644 --- a/task_01/src/topology_sort.cpp +++ b/task_01/src/topology_sort.cpp @@ -1,30 +1,30 @@ #include "topology_sort.hpp" // O(logn) -int BinSearch(std::vector mas, int num) { - int n = mas.size(); - int l = 0; - int r = n - 1; - int ind = n / 2; - while (r - l >= 0) { - if (mas[ind] == num) { - return ind; - } else if (mas[ind] > num) { - r = ind - 1; - } else if (mas[ind] < num) { - l = ind + 1; +int BinSearch(std::vector massive, int num) { + int length = massive.size(); + int left = 0; + int right = length - 1; + int index = length / 2; + while (right - left >= 0) { + if (massive[index] == num) { + return index; + } else if (massive[index] > num) { + right = index - 1; + } else if (massive[index] < num) { + left = index + 1; } - ind = (r + l) / 2; + index = (right + left) / 2; } return -1; } // O(n^2) -std::tuple NSquareSearch(std::vector mas, int num) { - int n = mas.size(); - for (int i = 0; i < n; i++) { - for (int j = i + 1; j < n; j++) { - if (mas[i] + mas[j] == num) { +std::tuple NSquareSearch(std::vector massive, int num) { + int length = massive.size(); + for (int i = 0; i < length; i++) { + for (int j = i + 1; j < length; j++) { + if (massive[i] + massive[j] == num) { return {i, j}; } } @@ -33,13 +33,13 @@ std::tuple NSquareSearch(std::vector mas, int num) { } // O(nlogn) -std::tuple NLogNSearch(std::vector mas, int num) { - int n = mas.size(); - for (int i = 0; i < n; i++) { - int el1 = mas[i]; +std::tuple NLogNSearch(std::vector massive, int num) { + int length = massive.size(); + for (int i = 0; i < length; i++) { + int el1 = massive[i]; int el2 = num - el1; - int j = BinSearch(mas, el2); - if (j != -1) { + int j = BinSearch(massive, el2); + if (j != -1 && i != j) { return {i, j}; } } @@ -47,18 +47,18 @@ std::tuple NLogNSearch(std::vector mas, int num) { } // O(n) -std::tuple NSearch(std::vector mas, int num) { - int len = mas.size(); - int l = 0; - int r = len - 1; - while (r != l) { - int sum = mas[l] + mas[r]; +std::tuple NSearch(std::vector massive, int num) { + int length = massive.size(); + int left = 0; + int right = length - 1; + while (right != left) { + int sum = massive[left] + massive[right]; if (sum == num) { - return {l, r}; + return {left, right}; } else if (sum < num) { - l++; + left++; } else if (sum > num) { - r--; + right--; } } return {-1, -1}; diff --git a/task_01/src/topology_sort.hpp b/task_01/src/topology_sort.hpp index 7bd709f4..437493fd 100644 --- a/task_01/src/topology_sort.hpp +++ b/task_01/src/topology_sort.hpp @@ -8,10 +8,10 @@ int BinSearch(std::vector mas, int num); // O(n^2) -std::tuple NSquareSearch(std::vector mas, int num); +std::tuple NSquareSearch(std::vector massive, int num); // O(nlogn) -std::tuple NLogNSearch(std::vector mas, int num); +std::tuple NLogNSearch(std::vector massive, int num); // O(n) -std::tuple NSearch(std::vector mas, int num); \ No newline at end of file +std::tuple NSearch(std::vector massive, int num); \ No newline at end of file From d44edbe43f190df6d6912e717eafa66ece9a3949 Mon Sep 17 00:00:00 2001 From: sh1buya <92878896+sh1buya@users.noreply.github.com> Date: Tue, 28 May 2024 22:34:11 +0000 Subject: [PATCH 08/12] task_02 commited --- task_02/src/stack.cpp | 17 ++++++++++------- task_02/src/stack.hpp | 5 +++-- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/task_02/src/stack.cpp b/task_02/src/stack.cpp index 8ca89902..26a9e854 100644 --- a/task_02/src/stack.cpp +++ b/task_02/src/stack.cpp @@ -2,20 +2,23 @@ #include -void Stack::Push(int value) { data_.push(value); } +void Stack::Push(int value) { data_.push_back(value); } int Stack::Pop() { - auto result = data_.top(); - data_.pop(); + int result = data_[data_.size() - 1]; + data_.pop_back(); return result; } -void MinStack::Push(int value) { data_.push_back(value); } +void MinStack::Push(int value) { + data_.push_back(value); + if (value < minimum_) { + minimum_ = value; + } +} int MinStack::Pop() { - auto result = data_.back(); + int result = data_[data_.size() - 1]; data_.pop_back(); return result; } - -int MinStack::GetMin() { return *std::min_element(data_.begin(), data_.end()); } \ No newline at end of file diff --git a/task_02/src/stack.hpp b/task_02/src/stack.hpp index 138ec40f..0e646742 100644 --- a/task_02/src/stack.hpp +++ b/task_02/src/stack.hpp @@ -9,15 +9,16 @@ class Stack { int Pop(); private: - std::stack data_; + std::vector data_; }; class MinStack { public: void Push(int value); int Pop(); - int GetMin(); + int GetMin() { return minimum_; }; private: std::vector data_; + int minimum_; }; From 1376e8470ad1bd696cbc1ee6cbcc59b4dd0b88ca Mon Sep 17 00:00:00 2001 From: sh1buya <92878896+sh1buya@users.noreply.github.com> Date: Sun, 16 Jun 2024 21:45:21 +0000 Subject: [PATCH 09/12] stack fixed --- task_02/src/stack.cpp | 23 +++++++++++++++++------ task_02/src/stack.hpp | 9 +++++---- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/task_02/src/stack.cpp b/task_02/src/stack.cpp index 26a9e854..7c680fca 100644 --- a/task_02/src/stack.cpp +++ b/task_02/src/stack.cpp @@ -1,8 +1,8 @@ #include "stack.hpp" #include - -void Stack::Push(int value) { data_.push_back(value); } +#include +#include int Stack::Pop() { int result = data_[data_.size() - 1]; @@ -11,14 +11,25 @@ int Stack::Pop() { } void MinStack::Push(int value) { - data_.push_back(value); - if (value < minimum_) { - minimum_ = value; + std::pair new_element; + if (current_minimum_ == NULL) { + new_element = {value, value}; + } else if (value < current_minimum_) { + new_element = {value, value}; + } else { + new_element = {value, current_minimum_}; } + data_.push_back(new_element); + current_minimum_ = new_element.second; } int MinStack::Pop() { - int result = data_[data_.size() - 1]; + int result = data_[data_.size() - 1].first; data_.pop_back(); + if (data_.size() == 0) { + current_minimum_ = NULL; + } else { + current_minimum_ = data_[data_.size() - 1].second; + } return result; } diff --git a/task_02/src/stack.hpp b/task_02/src/stack.hpp index 0e646742..f0ae89c7 100644 --- a/task_02/src/stack.hpp +++ b/task_02/src/stack.hpp @@ -1,11 +1,12 @@ #pragma once #include +#include #include class Stack { public: - void Push(int value); + void Push(int value) { data_.push_back(value); } int Pop(); private: @@ -16,9 +17,9 @@ class MinStack { public: void Push(int value); int Pop(); - int GetMin() { return minimum_; }; + int GetMin() { return current_minimum_; }; private: - std::vector data_; - int minimum_; + std::vector> data_; + int current_minimum_; }; From 4e633c6b9324748a80eaf7550afd27faae153a4e Mon Sep 17 00:00:00 2001 From: sh1buya <92878896+sh1buya@users.noreply.github.com> Date: Sun, 16 Jun 2024 23:05:28 +0000 Subject: [PATCH 10/12] files renamed and tests in stack are done correctly --- task_01/src/{topology_sort.cpp => task.cpp} | 0 task_01/src/{topology_sort.hpp => task.hpp} | 0 task_02/src/stack.cpp | 4 ++-- task_02/src/stack.hpp | 2 +- 4 files changed, 3 insertions(+), 3 deletions(-) rename task_01/src/{topology_sort.cpp => task.cpp} (100%) rename task_01/src/{topology_sort.hpp => task.hpp} (100%) diff --git a/task_01/src/topology_sort.cpp b/task_01/src/task.cpp similarity index 100% rename from task_01/src/topology_sort.cpp rename to task_01/src/task.cpp diff --git a/task_01/src/topology_sort.hpp b/task_01/src/task.hpp similarity index 100% rename from task_01/src/topology_sort.hpp rename to task_01/src/task.hpp diff --git a/task_02/src/stack.cpp b/task_02/src/stack.cpp index 7c680fca..401b5723 100644 --- a/task_02/src/stack.cpp +++ b/task_02/src/stack.cpp @@ -12,7 +12,7 @@ int Stack::Pop() { void MinStack::Push(int value) { std::pair new_element; - if (current_minimum_ == NULL) { + if (current_minimum_ == 1e9) { new_element = {value, value}; } else if (value < current_minimum_) { new_element = {value, value}; @@ -27,7 +27,7 @@ int MinStack::Pop() { int result = data_[data_.size() - 1].first; data_.pop_back(); if (data_.size() == 0) { - current_minimum_ = NULL; + current_minimum_ = 1e9; } else { current_minimum_ = data_[data_.size() - 1].second; } diff --git a/task_02/src/stack.hpp b/task_02/src/stack.hpp index f0ae89c7..3a485672 100644 --- a/task_02/src/stack.hpp +++ b/task_02/src/stack.hpp @@ -21,5 +21,5 @@ class MinStack { private: std::vector> data_; - int current_minimum_; + int current_minimum_ = 1e9; }; From 2e701d6b950460806b49ecb95f116198885f7bc0 Mon Sep 17 00:00:00 2001 From: sh1buya <92878896+sh1buya@users.noreply.github.com> Date: Sun, 16 Jun 2024 23:08:58 +0000 Subject: [PATCH 11/12] one line corrected --- task_01/src/task.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/task_01/src/task.cpp b/task_01/src/task.cpp index 86aaca13..319120f2 100644 --- a/task_01/src/task.cpp +++ b/task_01/src/task.cpp @@ -1,4 +1,4 @@ -#include "topology_sort.hpp" +#include "task.hpp" // O(logn) int BinSearch(std::vector massive, int num) { From 55b421be2918d6d6f2802da44d5f20a68a3b00c6 Mon Sep 17 00:00:00 2001 From: sh1buya <92878896+sh1buya@users.noreply.github.com> Date: Sun, 16 Jun 2024 23:10:07 +0000 Subject: [PATCH 12/12] one line corrected --- task_01/src/test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/task_01/src/test.cpp b/task_01/src/test.cpp index 17e20c1e..96496788 100644 --- a/task_01/src/test.cpp +++ b/task_01/src/test.cpp @@ -1,7 +1,7 @@ #include -#include "topology_sort.hpp" +#include "task.hpp" // BinSearch tests TEST(BinSearch, Simple) {