From 6cea2cad669d1a3f55df44d5d1058ae4be1e2c8f Mon Sep 17 00:00:00 2001 From: Matvey-cmd <143873323+Matvey-cmd@users.noreply.github.com> Date: Sat, 10 Feb 2024 08:35:18 +0000 Subject: [PATCH 01/42] some changes --- task_01/src/main.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/task_01/src/main.cpp b/task_01/src/main.cpp index 0e4393ba..353f11db 100644 --- a/task_01/src/main.cpp +++ b/task_01/src/main.cpp @@ -1,3 +1,6 @@ #include -int main() { return 0; } +int main() { + std::cout << "1212"; + return 0; +} From b261bddaff8e3f0e231e3748877989b2f9bfd7c3 Mon Sep 17 00:00:00 2001 From: Matvey-cmd <143873323+Matvey-cmd@users.noreply.github.com> Date: Mon, 19 Feb 2024 11:16:25 +0000 Subject: [PATCH 02/42] answers --- task_01/src/main.cpp | 9 +++++---- task_01/src/test.cpp | 22 +++++++++++++++++++--- task_01/src/topology_sort.cpp | 1 - task_01/src/topology_sort.hpp | 1 - task_01/src/utils.cpp | 18 ++++++++++++++++++ task_01/src/utils.hpp | 5 +++++ 6 files changed, 47 insertions(+), 9 deletions(-) delete mode 100644 task_01/src/topology_sort.cpp delete mode 100644 task_01/src/topology_sort.hpp create mode 100644 task_01/src/utils.cpp create mode 100644 task_01/src/utils.hpp diff --git a/task_01/src/main.cpp b/task_01/src/main.cpp index 353f11db..5b0faafc 100644 --- a/task_01/src/main.cpp +++ b/task_01/src/main.cpp @@ -1,6 +1,7 @@ #include +#include -int main() { - std::cout << "1212"; - return 0; -} +#include "util.hpp" +#include "utils.hpp" + +int main() { return 0; } \ No newline at end of file diff --git a/task_01/src/test.cpp b/task_01/src/test.cpp index ef5a86ae..cb7648da 100644 --- a/task_01/src/test.cpp +++ b/task_01/src/test.cpp @@ -1,8 +1,24 @@ #include -#include "topology_sort.hpp" -TEST(TopologySort, Simple) { - ASSERT_EQ(1, 1); // Stack [] +#include +#include + +#include "utils.hpp" + +TEST(utils, Simple) { + ASSERT_EQ(Task1(9, std::vector < int > {1, 2, 4, 5, 6, 8, 10, 12}), (std::pair < int, int > {1,8})); + + ASSERT_EQ(Task1(39, std::vector < int > {1, 2, 4, 5, 6, 9, 10, 35}), (std::pair < int, int > {4,35})); + + ASSERT_EQ(Task1(14, std::vector < int > {1, 2, 4, 5, 6, 8, 10, 12}), (std::pair < int, int > {2,12})); + + EXPECT_THROW(Task1(1887, std::vector{1, 2, 4, 6, 8, 10, 12, 15}), std::logic_error); + + EXPECT_THROW(Task1(12, std::vector{0,1,1,2,2}), std::logic_error); + + ASSERT_EQ(Task1(1338, std::vector < int > {10, 20, 40, 50, 60, 87, 100, 1278}), (std::pair < int, int > {60,1278})); + + ASSERT_EQ(Task1(22, std::vector < int > {10, 10, 11, 11, 12, 15}), (std::pair < int, int > {10,12})); } diff --git a/task_01/src/topology_sort.cpp b/task_01/src/topology_sort.cpp deleted file mode 100644 index e53f670c..00000000 --- a/task_01/src/topology_sort.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "topology_sort.hpp" diff --git a/task_01/src/topology_sort.hpp b/task_01/src/topology_sort.hpp deleted file mode 100644 index 6f70f09b..00000000 --- a/task_01/src/topology_sort.hpp +++ /dev/null @@ -1 +0,0 @@ -#pragma once diff --git a/task_01/src/utils.cpp b/task_01/src/utils.cpp new file mode 100644 index 00000000..6a2871b4 --- /dev/null +++ b/task_01/src/utils.cpp @@ -0,0 +1,18 @@ +#include "utils.hpp" +#include +#include +#include + +std::pair Task1(int num, const std::vector arr) { + for (int i = 0, j = arr.size() - 1; i < j;) { + auto sum = arr[i] + arr[j]; + if (sum < num) + ++i; + else if (sum > num) + --j; + else if (sum == num) { + return std::pair{arr[i], arr[j]}; + } + } + throw std::logic_error(""); +} \ No newline at end of file diff --git a/task_01/src/utils.hpp b/task_01/src/utils.hpp new file mode 100644 index 00000000..16254fa7 --- /dev/null +++ b/task_01/src/utils.hpp @@ -0,0 +1,5 @@ +#pragma once +#include +#include + +std::pair Task1(int num, const std::vector arr); \ No newline at end of file From 766aa306147881c768c0f8e5435df952a980a806 Mon Sep 17 00:00:00 2001 From: Matvey-cmd <143873323+Matvey-cmd@users.noreply.github.com> Date: Sun, 25 Feb 2024 17:52:51 +0000 Subject: [PATCH 03/42] change --- task_01/src/test.cpp | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/task_01/src/test.cpp b/task_01/src/test.cpp index cb7648da..e04d13bb 100644 --- a/task_01/src/test.cpp +++ b/task_01/src/test.cpp @@ -1,24 +1,23 @@ +#include "utils.hpp" #include - - #include #include -#include "utils.hpp" - TEST(utils, Simple) { - ASSERT_EQ(Task1(9, std::vector < int > {1, 2, 4, 5, 6, 8, 10, 12}), (std::pair < int, int > {1,8})); - - ASSERT_EQ(Task1(39, std::vector < int > {1, 2, 4, 5, 6, 9, 10, 35}), (std::pair < int, int > {4,35})); - - ASSERT_EQ(Task1(14, std::vector < int > {1, 2, 4, 5, 6, 8, 10, 12}), (std::pair < int, int > {2,12})); - - EXPECT_THROW(Task1(1887, std::vector{1, 2, 4, 6, 8, 10, 12, 15}), std::logic_error); - - EXPECT_THROW(Task1(12, std::vector{0,1,1,2,2}), std::logic_error); - - ASSERT_EQ(Task1(1338, std::vector < int > {10, 20, 40, 50, 60, 87, 100, 1278}), (std::pair < int, int > {60,1278})); - - ASSERT_EQ(Task1(22, std::vector < int > {10, 10, 11, 11, 12, 15}), (std::pair < int, int > {10,12})); + ASSERT_EQ(Task1(9, std::vector{1, 2, 4, 5, 6, 8, 10, 12}), + (std::pair{1, 8})); + ASSERT_EQ(Task1(39, std::vector{1, 2, 4, 5, 6, 9, 10, 35}), + (std::pair{4, 35})); + ASSERT_EQ(Task1(14, std::vector{1, 2, 4, 5, 6, 8, 10, 12}), + (std::pair{2, 12})); + EXPECT_THROW(Task1(1887, std::vector{1, 2, 4, 6, 8, 10, 12, 15}), + std::logic_error); + EXPECT_THROW(Task1(12, std::vector{0, 1, 1, 2, 2}), std::logic_error); + ASSERT_EQ(Task1(1338, std::vector{10, 20, 40, 50, 60, 87, 100, 1278}), + (std::pair{60, 1278})); + ASSERT_EQ(Task1(22, std::vector{10, 10, 11, 11, 12, 15}), + (std::pair{10, 12})); + EXPECT_THROW(Task1(22, std::vector{11}), std::logic_error); + EXPECT_THROW(Task1(0, std::vector{}), std::logic_error); } From 9e6840e03f95adff5329a4e10c4f87d6a82e43d3 Mon Sep 17 00:00:00 2001 From: Matvey-cmd <143873323+Matvey-cmd@users.noreply.github.com> Date: Sun, 25 Feb 2024 18:16:21 +0000 Subject: [PATCH 04/42] Right_Test --- task_01/src/test.cpp | 4 ++-- task_01/src/utils.cpp | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/task_01/src/test.cpp b/task_01/src/test.cpp index e04d13bb..f2475990 100644 --- a/task_01/src/test.cpp +++ b/task_01/src/test.cpp @@ -18,6 +18,6 @@ TEST(utils, Simple) { (std::pair{60, 1278})); ASSERT_EQ(Task1(22, std::vector{10, 10, 11, 11, 12, 15}), (std::pair{10, 12})); - EXPECT_THROW(Task1(22, std::vector{11}), std::logic_error); - EXPECT_THROW(Task1(0, std::vector{}), std::logic_error); + EXPECT_THROW(Task1(22, std::vector{11}), WrongVector); + EXPECT_THROW(Task1(1233, std::vector{}), WrongVector); } diff --git a/task_01/src/utils.cpp b/task_01/src/utils.cpp index 6a2871b4..9d96d748 100644 --- a/task_01/src/utils.cpp +++ b/task_01/src/utils.cpp @@ -4,6 +4,9 @@ #include std::pair Task1(int num, const std::vector arr) { + if (arr.size() < 2) { + throw WrongVector(""); + } for (int i = 0, j = arr.size() - 1; i < j;) { auto sum = arr[i] + arr[j]; if (sum < num) From 03af331d7b22ad026766362718b116eeeb50d7d8 Mon Sep 17 00:00:00 2001 From: Matvey-cmd <143873323+Matvey-cmd@users.noreply.github.com> Date: Sun, 25 Feb 2024 18:16:48 +0000 Subject: [PATCH 05/42] Right_Test_2 --- task_01/src/utils.hpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/task_01/src/utils.hpp b/task_01/src/utils.hpp index 16254fa7..45117087 100644 --- a/task_01/src/utils.hpp +++ b/task_01/src/utils.hpp @@ -2,4 +2,7 @@ #include #include -std::pair Task1(int num, const std::vector arr); \ No newline at end of file +std::pair Task1(int num, const std::vector arr); +class WrongVector : public std::runtime_error { + using std::runtime_error::runtime_error; +}; \ No newline at end of file From 6cde35786fd3f6d06f68531f7d4b4c97e66bf83a Mon Sep 17 00:00:00 2001 From: Matvey-cmd <143873323+Matvey-cmd@users.noreply.github.com> Date: Mon, 26 Feb 2024 09:33:30 +0000 Subject: [PATCH 06/42] itog_changes --- task_01/src/test.cpp | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/task_01/src/test.cpp b/task_01/src/test.cpp index f2475990..b1da0e88 100644 --- a/task_01/src/test.cpp +++ b/task_01/src/test.cpp @@ -5,19 +5,13 @@ #include TEST(utils, Simple) { - ASSERT_EQ(Task1(9, std::vector{1, 2, 4, 5, 6, 8, 10, 12}), - (std::pair{1, 8})); - ASSERT_EQ(Task1(39, std::vector{1, 2, 4, 5, 6, 9, 10, 35}), - (std::pair{4, 35})); - ASSERT_EQ(Task1(14, std::vector{1, 2, 4, 5, 6, 8, 10, 12}), - (std::pair{2, 12})); - EXPECT_THROW(Task1(1887, std::vector{1, 2, 4, 6, 8, 10, 12, 15}), - std::logic_error); + ASSERT_EQ(Task1(9, std::vector{1, 2, 4, 5, 6, 8, 10, 12}), (std::pair{1, 8})); + ASSERT_EQ(Task1(39, std::vector{1, 2, 4, 5, 6, 9, 10, 35}), (std::pair{4, 35})); + ASSERT_EQ(Task1(14, std::vector{1, 2, 4, 5, 6, 8, 10, 12}), (std::pair{2, 12})); + EXPECT_THROW(Task1(1887, std::vector{1, 2, 4, 6, 8, 10, 12, 15}), std::logic_error); EXPECT_THROW(Task1(12, std::vector{0, 1, 1, 2, 2}), std::logic_error); - ASSERT_EQ(Task1(1338, std::vector{10, 20, 40, 50, 60, 87, 100, 1278}), - (std::pair{60, 1278})); - ASSERT_EQ(Task1(22, std::vector{10, 10, 11, 11, 12, 15}), - (std::pair{10, 12})); + ASSERT_EQ(Task1(1338, std::vector{10, 20, 40, 50, 60, 87, 100, 1278}), (std::pair{60, 1278})); + ASSERT_EQ(Task1(22, std::vector{10, 10, 11, 11, 12, 15}), (std::pair{10, 12})); EXPECT_THROW(Task1(22, std::vector{11}), WrongVector); EXPECT_THROW(Task1(1233, std::vector{}), WrongVector); } From 121443a8087636470ff381b346409b9a03595d02 Mon Sep 17 00:00:00 2001 From: Matvey-cmd <143873323+Matvey-cmd@users.noreply.github.com> Date: Sat, 16 Mar 2024 08:36:08 +0000 Subject: [PATCH 07/42] some changes --- task_01/src/utils.cpp | 2 +- task_01/src/utils.hpp | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/task_01/src/utils.cpp b/task_01/src/utils.cpp index 9d96d748..934e64ea 100644 --- a/task_01/src/utils.cpp +++ b/task_01/src/utils.cpp @@ -5,7 +5,7 @@ std::pair Task1(int num, const std::vector arr) { if (arr.size() < 2) { - throw WrongVector(""); + throw WrongVector("Vector is too small"); } for (int i = 0, j = arr.size() - 1; i < j;) { auto sum = arr[i] + arr[j]; diff --git a/task_01/src/utils.hpp b/task_01/src/utils.hpp index 45117087..ead85070 100644 --- a/task_01/src/utils.hpp +++ b/task_01/src/utils.hpp @@ -2,7 +2,8 @@ #include #include -std::pair Task1(int num, const std::vector arr); class WrongVector : public std::runtime_error { using std::runtime_error::runtime_error; -}; \ No newline at end of file +}; + +std::pair Task1(int num, const std::vector arr); From a03cf3e67a2d95266b8ee9295a218686f4fb7936 Mon Sep 17 00:00:00 2001 From: Matvey-cmd <143873323+Matvey-cmd@users.noreply.github.com> Date: Sat, 23 Mar 2024 12:40:50 +0000 Subject: [PATCH 08/42] last change --- task_01/src/test.cpp | 1 - task_01/src/utils.cpp | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/task_01/src/test.cpp b/task_01/src/test.cpp index b1da0e88..9f3a10f3 100644 --- a/task_01/src/test.cpp +++ b/task_01/src/test.cpp @@ -1,4 +1,3 @@ - #include "utils.hpp" #include #include diff --git a/task_01/src/utils.cpp b/task_01/src/utils.cpp index 934e64ea..941715fb 100644 --- a/task_01/src/utils.cpp +++ b/task_01/src/utils.cpp @@ -17,5 +17,5 @@ std::pair Task1(int num, const std::vector arr) { return std::pair{arr[i], arr[j]}; } } - throw std::logic_error(""); + throw std::logic_error("There is no sum of elements equal to number"); } \ No newline at end of file From 0cc2076567c005b1f52aaa3e8aaa1502cc9d273e Mon Sep 17 00:00:00 2001 From: Matvey-cmd <143873323+Matvey-cmd@users.noreply.github.com> Date: Sat, 23 Mar 2024 12:57:26 +0000 Subject: [PATCH 09/42] kast_changes --- task_01/src/test.cpp | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/task_01/src/test.cpp b/task_01/src/test.cpp index 9f3a10f3..92cb6996 100644 --- a/task_01/src/test.cpp +++ b/task_01/src/test.cpp @@ -4,13 +4,19 @@ #include TEST(utils, Simple) { - ASSERT_EQ(Task1(9, std::vector{1, 2, 4, 5, 6, 8, 10, 12}), (std::pair{1, 8})); - ASSERT_EQ(Task1(39, std::vector{1, 2, 4, 5, 6, 9, 10, 35}), (std::pair{4, 35})); - ASSERT_EQ(Task1(14, std::vector{1, 2, 4, 5, 6, 8, 10, 12}), (std::pair{2, 12})); - EXPECT_THROW(Task1(1887, std::vector{1, 2, 4, 6, 8, 10, 12, 15}), std::logic_error); + ASSERT_EQ(Task1(9, std::vector{1, 2, 4, 5, 6, 8, 10, 12}), + (std::pair{1, 8})); + ASSERT_EQ(Task1(39, std::vector{1, 2, 4, 5, 6, 9, 10, 35}), + (std::pair{4, 35})); + ASSERT_EQ(Task1(14, std::vector{1, 2, 4, 5, 6, 8, 10, 12}), + (std::pair{2, 12})); + EXPECT_THROW(Task1(1887, std::vector{1, 2, 4, 6, 8, 10, 12, 15}), + std::logic_error); EXPECT_THROW(Task1(12, std::vector{0, 1, 1, 2, 2}), std::logic_error); - ASSERT_EQ(Task1(1338, std::vector{10, 20, 40, 50, 60, 87, 100, 1278}), (std::pair{60, 1278})); - ASSERT_EQ(Task1(22, std::vector{10, 10, 11, 11, 12, 15}), (std::pair{10, 12})); + ASSERT_EQ(Task1(1338, std::vector{10, 20, 40, 50, 60, 87, 100, 1278}), + (std::pair{60, 1278})); + ASSERT_EQ(Task1(22, std::vector{10, 10, 11, 11, 12, 15}), + (std::pair{10, 12})); EXPECT_THROW(Task1(22, std::vector{11}), WrongVector); EXPECT_THROW(Task1(1233, std::vector{}), WrongVector); } From 8b2cf4f71ef19e8b5628a98a534502154eb167c7 Mon Sep 17 00:00:00 2001 From: Matvey-cmd <143873323+Matvey-cmd@users.noreply.github.com> Date: Sun, 24 Mar 2024 21:45:14 +0000 Subject: [PATCH 10/42] part of task 2 --- task_04/src/heap.cpp | 59 ++++++++++++++++++++++++++++++++++++++++++++ task_04/src/heap.hpp | 22 +++++++++++++++++ task_04/src/main.cpp | 1 + task_04/src/test.cpp | 15 ++++++++--- task_05/src/sort.cpp | 25 +++++++++++++++++++ task_05/src/sort.hpp | 7 ++++++ task_05/src/test.cpp | 15 +++++++++-- 7 files changed, 139 insertions(+), 5 deletions(-) create mode 100644 task_04/src/heap.cpp create mode 100644 task_04/src/heap.hpp create mode 100644 task_05/src/sort.cpp create mode 100644 task_05/src/sort.hpp diff --git a/task_04/src/heap.cpp b/task_04/src/heap.cpp new file mode 100644 index 00000000..4ce7d04c --- /dev/null +++ b/task_04/src/heap.cpp @@ -0,0 +1,59 @@ +#include "heap.hpp" +#include +#include + +void Heap::SiftDown(int i) { + while (2 * i + 1 < vec_.size()) { + int left = 2 * i + 1; + int right = 2 * i + 2; + int j = left; + if ((right < vec_.size()) && vec_[right] < vec_[left]) + j = right; + if (vec_[i] <= vec_[j]) + break; + std::swap(vec_[i], vec_[j]); + i = j; + } +} + +void Heap::SiftUp(int i) { + while (vec_[i] < (vec_[(i - 1) / 2])) { + std::swap(vec_[i], vec_[(i - 1) / 2]); + i = (i - 1) / 2; + } +} + +int Heap::Find_Min() { return vec_[0]; } + +int Heap::Extract_Min() { + int min = vec_[0]; + vec_[0] = vec_[vec_.size() - 1]; + vec_.erase(vec_.begin()); + SiftDown(0); + return min; +} + +std::vector Heap::Copy_Heap() { return vec_; } + +void Heap::Insert(int value) { + vec_.push_back(value); + SiftUp(vec_.size() - 1); +} + +void Heap::Build_heap(std::vector vec) { + for (int i = 0; i < vec.size(); ++i) { + Insert(vec[i]); + } +} + +int Find_Minimum(std::vector vec) { + Heap heap; + heap.Build_heap(vec); + return heap.Find_Min(); +} + +std::vector Heap_Ready(std::vector vec) { + Heap heap; + heap.Build_heap(vec); + return heap.Copy_Heap(); +} \ No newline at end of file diff --git a/task_04/src/heap.hpp b/task_04/src/heap.hpp new file mode 100644 index 00000000..2191ddef --- /dev/null +++ b/task_04/src/heap.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include +#include + +class Heap { +public: + void SiftDown(int i); + void SiftUp(int i); + void Insert(int value); + int Find_Min(); + void Build_heap(std::vector vec); + int Extract_Min(); + std::vector Copy_Heap(); + +private: + std::vector vec_; +}; + +int Find_Minimum(std::vector vec); + +std::vector Heap_Ready(std::vector heap); \ No newline at end of file diff --git a/task_04/src/main.cpp b/task_04/src/main.cpp index 0e4393ba..ea6d0016 100644 --- a/task_04/src/main.cpp +++ b/task_04/src/main.cpp @@ -1,3 +1,4 @@ +#include "heap.hpp" #include int main() { return 0; } diff --git a/task_04/src/test.cpp b/task_04/src/test.cpp index 5e11617e..96d4f726 100644 --- a/task_04/src/test.cpp +++ b/task_04/src/test.cpp @@ -1,6 +1,15 @@ - +#include "heap.hpp" #include -TEST(TopologySort, Simple) { - ASSERT_EQ(1, 1); // Stack [] +TEST(heap, Simple) { + ASSERT_EQ(Find_Minimum(std::vector{1, 3, 5, 6, 2, 4}), 1); + ASSERT_EQ(Find_Minimum(std::vector{11, 37, 55, 34, -6, 0, 4}), -6); + ASSERT_EQ(Heap_Ready(std::vector{4, 1, 3, 2, 5}), + (std::vector{1, 2, 3, 4, 5})); + ASSERT_EQ(Heap_Ready(std::vector{3, 1, 5, 6, 2, 4}), + (std::vector{1, 2, 4, 6, 3, 5})); + Heap heap; + heap.Build_heap(std::vector{1, 3, 5, 7, 9, 12, 324, 5, 47, 457, 9467, -4, + 758, -579, -4, 0}); + ASSERT_EQ(heap.Find_Min(), -579); } diff --git a/task_05/src/sort.cpp b/task_05/src/sort.cpp new file mode 100644 index 00000000..7024d8e4 --- /dev/null +++ b/task_05/src/sort.cpp @@ -0,0 +1,25 @@ +#include "sort.hpp" +#include +#include + +int Partition(std::vector &arr, int low, int high) { + int pivot = arr[high]; + int i = low - 1; + for (int j = low; j <= high; ++j) { + if (arr[j] < pivot) { + ++i; + std::swap(arr[i], arr[j]); + } + } + std::swap(arr[i + 1], arr[high]); + return (i + 1); +} + +std::vector Quick_Sort(std::vector &arr, int low, int high) { + if (low < high) { + int m = Partition(arr, low, high); + Quick_Sort(arr, low, m - 1); + Quick_Sort(arr, m + 1, high); + } + return arr; +} \ No newline at end of file diff --git a/task_05/src/sort.hpp b/task_05/src/sort.hpp new file mode 100644 index 00000000..f4ed6f5f --- /dev/null +++ b/task_05/src/sort.hpp @@ -0,0 +1,7 @@ +#pragma once +#include +#include + +int Partition(std::vector &arr, int low, int high); + +std::vector Quick_Sort(std::vector &arr, int low, int high); \ No newline at end of file diff --git a/task_05/src/test.cpp b/task_05/src/test.cpp index 5e11617e..4017df0a 100644 --- a/task_05/src/test.cpp +++ b/task_05/src/test.cpp @@ -1,6 +1,17 @@ +#include "sort.hpp" #include -TEST(TopologySort, Simple) { - ASSERT_EQ(1, 1); // Stack [] +TEST(Quick_Sort, Simple) { + std::vector vec_1{9, 8, 7, 6, 5, 4, 2, 3, 1, 0}; + ASSERT_EQ(Quick_Sort(vec_1, 0, 9), + (std::vector{0, 1, 2, 3, 4, 5, 6, 7, 8, 9})); + std::vector vec_2{10, 90, 30, 80, 60, 50}; + ASSERT_EQ(Quick_Sort(vec_2, 0, 5), + (std::vector{10, 30, 50, 60, 80, 90})); + std::vector vec_3{12, 43, 15, 26, -1233, 346, 1325, + -56, -12, 78, 0, 3345, -34}; + ASSERT_EQ(Quick_Sort(vec_3, 0, 12), + (std::vector{-1233, -56, -34, -12, 0, 12, 15, 26, 43, 78, 346, + 1325, 3345})); } From 5a132da45e0858af2960d1ea88a8250757649c0e Mon Sep 17 00:00:00 2001 From: Matvey-cmd <143873323+Matvey-cmd@users.noreply.github.com> Date: Mon, 25 Mar 2024 10:50:27 +0000 Subject: [PATCH 11/42] k_stat --- task_06/src/k_stat.cpp | 28 ++++++++++++++++++++++++++++ task_06/src/k_stat.hpp | 7 +++++++ task_06/src/test.cpp | 11 ++++++++--- 3 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 task_06/src/k_stat.cpp create mode 100644 task_06/src/k_stat.hpp diff --git a/task_06/src/k_stat.cpp b/task_06/src/k_stat.cpp new file mode 100644 index 00000000..1f8feeb7 --- /dev/null +++ b/task_06/src/k_stat.cpp @@ -0,0 +1,28 @@ +#include "k_stat.hpp" +#include +#include + +int Partition(std::vector &vec, int low, int high) { + int pivot = vec[high]; + int i = low - 1; + for (int j = low; j <= high; ++j) { + if (vec[j] < pivot) { + ++i; + std::swap(vec[i], vec[j]); + } + } + std::swap(vec[i + 1], vec[high]); + return (i + 1); +} + +int Quick_sort(std::vector &vec, int low, int high, int k) { + if (low < high) { + int m = Partition(vec, low, high); + if (m == k - 1) { + return k; + } + Quick_sort(vec, low, m - 1, k); + Quick_sort(vec, m + 1, high, k); + } + throw std::logic_error("There is no such element"); +} diff --git a/task_06/src/k_stat.hpp b/task_06/src/k_stat.hpp new file mode 100644 index 00000000..c2b67d40 --- /dev/null +++ b/task_06/src/k_stat.hpp @@ -0,0 +1,7 @@ +#pragma once +#include +#include + +int Quick_Sort(std::vector &vec, int low, int high, int k); + +int Partition(std::vector &vec, int low, int high); \ No newline at end of file diff --git a/task_06/src/test.cpp b/task_06/src/test.cpp index 5e11617e..adb72005 100644 --- a/task_06/src/test.cpp +++ b/task_06/src/test.cpp @@ -1,6 +1,11 @@ - +#include "k_stat.hpp" #include +#include -TEST(TopologySort, Simple) { - ASSERT_EQ(1, 1); // Stack [] +TEST(k_stat, Simple) { + std::vector vec{14, -1, 5, 21, 6, -3, 7, 93, 9, 0, 84}; + ASSERT_EQ(Quick_Sort(vec, 0, 10, 8), 14); + ASSERT_EQ(Quick_Sort(vec, 0, 10, 3), 0); + ASSERT_EQ(Quick_Sort(vec, 0, 10, 10), 84); + EXPECT_THROW(Quick_Sort(vec, 0, 10, 13), std::logic_error); } From 787bb3d58e46ffa8f5011884ad772b105cc12087 Mon Sep 17 00:00:00 2001 From: Matvey-cmd <143873323+Matvey-cmd@users.noreply.github.com> Date: Mon, 25 Mar 2024 17:52:32 +0000 Subject: [PATCH 12/42] commit --- task_06/src/k_stat.cpp | 47 +++++++++++++++++++++++++++--------------- task_06/src/k_stat.hpp | 4 ++-- task_06/src/test.cpp | 10 ++++----- 3 files changed, 37 insertions(+), 24 deletions(-) diff --git a/task_06/src/k_stat.cpp b/task_06/src/k_stat.cpp index 1f8feeb7..340a6fa6 100644 --- a/task_06/src/k_stat.cpp +++ b/task_06/src/k_stat.cpp @@ -2,27 +2,40 @@ #include #include -int Partition(std::vector &vec, int low, int high) { - int pivot = vec[high]; - int i = low - 1; - for (int j = low; j <= high; ++j) { - if (vec[j] < pivot) { - ++i; - std::swap(vec[i], vec[j]); +int Partition(std::vector &vec, int l, int r) { + int piv = vec[(l + r) / 2]; + int i = l; + int j = r; + while (i <= j) { + while (vec[i] < piv) { + i++; } + while (vec[j] > piv) { + j--; + } + if (i >= j) { + break; + } + std::swap(vec[i++], vec[j--]); } - std::swap(vec[i + 1], vec[high]); - return (i + 1); + return j; } -int Quick_sort(std::vector &vec, int low, int high, int k) { - if (low < high) { - int m = Partition(vec, low, high); - if (m == k - 1) { - return k; +int findOrderStatistic(std::vector &vec, int k) { + int left = 0; + int right = int(vec.size() - 1); + while (left < vec.size()) { + int mid = Partition(vec, left, right); + + if (mid == k) { + return vec[mid]; + } + + else if (k < mid) { + right = mid; + } else { + left = mid + 1; } - Quick_sort(vec, low, m - 1, k); - Quick_sort(vec, m + 1, high, k); } - throw std::logic_error("There is no such element"); + throw std::logic_error("Wrong k order statistic"); } diff --git a/task_06/src/k_stat.hpp b/task_06/src/k_stat.hpp index c2b67d40..ada019c1 100644 --- a/task_06/src/k_stat.hpp +++ b/task_06/src/k_stat.hpp @@ -2,6 +2,6 @@ #include #include -int Quick_Sort(std::vector &vec, int low, int high, int k); +int Partition(std::vector &vec, int l, int r); -int Partition(std::vector &vec, int low, int high); \ No newline at end of file +int Find_Order_Statistic(std::vector &vec, int k); \ No newline at end of file diff --git a/task_06/src/test.cpp b/task_06/src/test.cpp index adb72005..cc04baf2 100644 --- a/task_06/src/test.cpp +++ b/task_06/src/test.cpp @@ -3,9 +3,9 @@ #include TEST(k_stat, Simple) { - std::vector vec{14, -1, 5, 21, 6, -3, 7, 93, 9, 0, 84}; - ASSERT_EQ(Quick_Sort(vec, 0, 10, 8), 14); - ASSERT_EQ(Quick_Sort(vec, 0, 10, 3), 0); - ASSERT_EQ(Quick_Sort(vec, 0, 10, 10), 84); - EXPECT_THROW(Quick_Sort(vec, 0, 10, 13), std::logic_error); + std::vector vec_1{14, -1, 5, 21, 6, -3, 7, 93, 9, 0, 84}; + ASSERT_EQ(Find_Order_Statistic(vec_1, 8), 21); + /*ASSERT_EQ(Find_Order_Statistic(vec, 3), 0); + ASSERT_EQ(Find_Order_Statistic(vec, 10), 84); + EXPECT_THROW(Find_Order_Statistic(vec, 13), std::logic_error);*/ } From c1eae6f236b22966cbd8668362da63f0bf2f8c55 Mon Sep 17 00:00:00 2001 From: Matvey-cmd <143873323+Matvey-cmd@users.noreply.github.com> Date: Mon, 25 Mar 2024 17:59:54 +0000 Subject: [PATCH 13/42] commit --- task_06/src/k_stat.cpp | 2 +- task_06/src/test.cpp | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/task_06/src/k_stat.cpp b/task_06/src/k_stat.cpp index 340a6fa6..31557c4a 100644 --- a/task_06/src/k_stat.cpp +++ b/task_06/src/k_stat.cpp @@ -21,7 +21,7 @@ int Partition(std::vector &vec, int l, int r) { return j; } -int findOrderStatistic(std::vector &vec, int k) { +int Find_Order_Statistic(std::vector &vec, int k) { int left = 0; int right = int(vec.size() - 1); while (left < vec.size()) { diff --git a/task_06/src/test.cpp b/task_06/src/test.cpp index cc04baf2..c7ec7f7e 100644 --- a/task_06/src/test.cpp +++ b/task_06/src/test.cpp @@ -5,7 +5,7 @@ TEST(k_stat, Simple) { std::vector vec_1{14, -1, 5, 21, 6, -3, 7, 93, 9, 0, 84}; ASSERT_EQ(Find_Order_Statistic(vec_1, 8), 21); - /*ASSERT_EQ(Find_Order_Statistic(vec, 3), 0); - ASSERT_EQ(Find_Order_Statistic(vec, 10), 84); - EXPECT_THROW(Find_Order_Statistic(vec, 13), std::logic_error);*/ + ASSERT_EQ(Find_Order_Statistic(vec_1, 3), 5); + ASSERT_EQ(Find_Order_Statistic(vec_1, 10), 93); + EXPECT_THROW(Find_Order_Statistic(vec_1, 13), std::logic_error); } From f685e79e6cfc177b705ed06c71a57b14cd247799 Mon Sep 17 00:00:00 2001 From: Matvey-cmd <143873323+Matvey-cmd@users.noreply.github.com> Date: Mon, 25 Mar 2024 19:17:40 +0000 Subject: [PATCH 14/42] task_3 --- task_03/src/test.cpp | 10 ++++++++- task_03/src/topology_sort.cpp | 38 +++++++++++++++++++++++++++++++++++ task_03/src/topology_sort.hpp | 9 +++++++++ 3 files changed, 56 insertions(+), 1 deletion(-) diff --git a/task_03/src/test.cpp b/task_03/src/test.cpp index ef5a86ae..59a35608 100644 --- a/task_03/src/test.cpp +++ b/task_03/src/test.cpp @@ -4,5 +4,13 @@ #include "topology_sort.hpp" TEST(TopologySort, Simple) { - ASSERT_EQ(1, 1); // Stack [] + ASSERT_EQ( + Rise_Temperature(std::vector{12, 14, 2, 12, 11, 10, 0, 5, 3, 20, 4}), + (std::vector{1, 8, 1, 6, 5, 4, 1, 2, 1, 0, 0})); + ASSERT_EQ( + Rise_Temperature(std::vector{11, -2, 3, 1, 5, 2, 6, 3, 7, 84, 4}), + (std::vector{9, 1, 2, 1, 2, 1, 2, 1, 1, 0, 0})); + ASSERT_EQ(Rise_Temperature(std::vector{1, 2, 3, 4, 5, 6, 7, 8, 9}), + (std::vector{1, 1, 1, 1, 1, 1, 1, 1, 0})); + EXPECT_THROW(Rise_Temperature(std::vector{}), WrongVector); } diff --git a/task_03/src/topology_sort.cpp b/task_03/src/topology_sort.cpp index e53f670c..e5cdd927 100644 --- a/task_03/src/topology_sort.cpp +++ b/task_03/src/topology_sort.cpp @@ -1 +1,39 @@ #include "topology_sort.hpp" +#include +#include +#include + +class Day { +public: + int number_of_day; + int temperature; + Day(int number_of_day, int temperature) { + this->number_of_day = number_of_day; + this->temperature = temperature; + } +}; + +std::vector Rise_Temperature(std::vector vec) { + int size = int(vec.size()); + if (size == 0) { + throw WrongVector("Wrong vector is too small"); + } + std::stack stack_days; + std::vector answer_vec(size, 0); + stack_days.emplace(0, vec[0]); + int i = 1; + while (i < size) { + while (!stack_days.empty()) { + if (vec[i] > stack_days.top().temperature) { + answer_vec[stack_days.top().number_of_day] = + i - stack_days.top().number_of_day; + stack_days.pop(); + } else if (vec[i] < stack_days.top().temperature) { + break; + } + } + stack_days.emplace(i, vec[i]); + ++i; + } + return answer_vec; +} \ No newline at end of file diff --git a/task_03/src/topology_sort.hpp b/task_03/src/topology_sort.hpp index 6f70f09b..d0375ab7 100644 --- a/task_03/src/topology_sort.hpp +++ b/task_03/src/topology_sort.hpp @@ -1 +1,10 @@ #pragma once +#include +#include + +class WrongVector : public std::runtime_error { + using std::runtime_error::runtime_error; +}; + +std::vector Rise_Temperature(std::vector vec); + From d9b9d6c23b085c8978e4e5006f316abebe3a7541 Mon Sep 17 00:00:00 2001 From: Matvey-cmd <143873323+Matvey-cmd@users.noreply.github.com> Date: Sat, 30 Mar 2024 10:22:40 +0000 Subject: [PATCH 15/42] last changes --- task_01/src/test.cpp | 21 ++++++++++++--------- task_01/src/utils.cpp | 2 +- task_01/src/utils.hpp | 2 +- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/task_01/src/test.cpp b/task_01/src/test.cpp index 92cb6996..3f317c74 100644 --- a/task_01/src/test.cpp +++ b/task_01/src/test.cpp @@ -4,19 +4,22 @@ #include TEST(utils, Simple) { - ASSERT_EQ(Task1(9, std::vector{1, 2, 4, 5, 6, 8, 10, 12}), + ASSERT_EQ(Sum_Of_Elements(9, std::vector{1, 2, 4, 5, 6, 8, 10, 12}), (std::pair{1, 8})); - ASSERT_EQ(Task1(39, std::vector{1, 2, 4, 5, 6, 9, 10, 35}), + ASSERT_EQ(Sum_Of_Elements(39, std::vector{1, 2, 4, 5, 6, 9, 10, 35}), (std::pair{4, 35})); - ASSERT_EQ(Task1(14, std::vector{1, 2, 4, 5, 6, 8, 10, 12}), + ASSERT_EQ(Sum_Of_Elements(14, std::vector{1, 2, 4, 5, 6, 8, 10, 12}), (std::pair{2, 12})); - EXPECT_THROW(Task1(1887, std::vector{1, 2, 4, 6, 8, 10, 12, 15}), + EXPECT_THROW( + Sum_Of_Elements(187, std::vector{1, 2, 4, 6, 8, 10, 12, 15}), + std::logic_error); + EXPECT_THROW(Sum_Of_Elements(12, std::vector{0, 1, 1, 2, 2}), std::logic_error); - EXPECT_THROW(Task1(12, std::vector{0, 1, 1, 2, 2}), std::logic_error); - ASSERT_EQ(Task1(1338, std::vector{10, 20, 40, 50, 60, 87, 100, 1278}), + ASSERT_EQ(Sum_Of_Elements( + 1338, std::vector{10, 20, 40, 50, 60, 87, 100, 1278}), (std::pair{60, 1278})); - ASSERT_EQ(Task1(22, std::vector{10, 10, 11, 11, 12, 15}), + ASSERT_EQ(Sum_Of_Elements(22, std::vector{10, 10, 11, 11, 12, 15}), (std::pair{10, 12})); - EXPECT_THROW(Task1(22, std::vector{11}), WrongVector); - EXPECT_THROW(Task1(1233, std::vector{}), WrongVector); + EXPECT_THROW(Sum_Of_Elements(22, std::vector{11}), WrongVector); + EXPECT_THROW(Sum_Of_Elements(1233, std::vector{}), WrongVector); } diff --git a/task_01/src/utils.cpp b/task_01/src/utils.cpp index 941715fb..f089478d 100644 --- a/task_01/src/utils.cpp +++ b/task_01/src/utils.cpp @@ -3,7 +3,7 @@ #include #include -std::pair Task1(int num, const std::vector arr) { +std::pair Sum_Of_Elements(int num, const std::vector arr) { if (arr.size() < 2) { throw WrongVector("Vector is too small"); } diff --git a/task_01/src/utils.hpp b/task_01/src/utils.hpp index ead85070..70165061 100644 --- a/task_01/src/utils.hpp +++ b/task_01/src/utils.hpp @@ -6,4 +6,4 @@ class WrongVector : public std::runtime_error { using std::runtime_error::runtime_error; }; -std::pair Task1(int num, const std::vector arr); +std::pair Sum_Of_Elements(int num, const std::vector arr); From 245de1ec9d1007e2e47716aa0d0d45756d245fe2 Mon Sep 17 00:00:00 2001 From: Matvey-cmd <143873323+Matvey-cmd@users.noreply.github.com> Date: Sat, 30 Mar 2024 10:48:40 +0000 Subject: [PATCH 16/42] last changes --- task_01/src/test.cpp | 18 +++++++++--------- task_01/src/utils.cpp | 2 +- task_01/src/utils.hpp | 2 +- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/task_01/src/test.cpp b/task_01/src/test.cpp index 3f317c74..c01ea493 100644 --- a/task_01/src/test.cpp +++ b/task_01/src/test.cpp @@ -4,22 +4,22 @@ #include TEST(utils, Simple) { - ASSERT_EQ(Sum_Of_Elements(9, std::vector{1, 2, 4, 5, 6, 8, 10, 12}), + ASSERT_EQ(SumOfElements(9, std::vector{1, 2, 4, 5, 6, 8, 10, 12}), (std::pair{1, 8})); - ASSERT_EQ(Sum_Of_Elements(39, std::vector{1, 2, 4, 5, 6, 9, 10, 35}), + ASSERT_EQ(SumOfElements(39, std::vector{1, 2, 4, 5, 6, 9, 10, 35}), (std::pair{4, 35})); - ASSERT_EQ(Sum_Of_Elements(14, std::vector{1, 2, 4, 5, 6, 8, 10, 12}), + ASSERT_EQ(SumOfElements(14, std::vector{1, 2, 4, 5, 6, 8, 10, 12}), (std::pair{2, 12})); EXPECT_THROW( - Sum_Of_Elements(187, std::vector{1, 2, 4, 6, 8, 10, 12, 15}), + SumOfElements(187, std::vector{1, 2, 4, 6, 8, 10, 12, 15}), std::logic_error); - EXPECT_THROW(Sum_Of_Elements(12, std::vector{0, 1, 1, 2, 2}), + EXPECT_THROW(SumOfElements(12, std::vector{0, 1, 1, 2, 2}), std::logic_error); - ASSERT_EQ(Sum_Of_Elements( + ASSERT_EQ(SumOfElements( 1338, std::vector{10, 20, 40, 50, 60, 87, 100, 1278}), (std::pair{60, 1278})); - ASSERT_EQ(Sum_Of_Elements(22, std::vector{10, 10, 11, 11, 12, 15}), + ASSERT_EQ(SumOfElements(22, std::vector{10, 10, 11, 11, 12, 15}), (std::pair{10, 12})); - EXPECT_THROW(Sum_Of_Elements(22, std::vector{11}), WrongVector); - EXPECT_THROW(Sum_Of_Elements(1233, std::vector{}), WrongVector); + EXPECT_THROW(SumOfElements(22, std::vector{11}), WrongVector); + EXPECT_THROW(SumOfElements(1233, std::vector{}), WrongVector); } diff --git a/task_01/src/utils.cpp b/task_01/src/utils.cpp index f089478d..e9343911 100644 --- a/task_01/src/utils.cpp +++ b/task_01/src/utils.cpp @@ -3,7 +3,7 @@ #include #include -std::pair Sum_Of_Elements(int num, const std::vector arr) { +std::pair SumOfElements(int num, const std::vector arr) { if (arr.size() < 2) { throw WrongVector("Vector is too small"); } diff --git a/task_01/src/utils.hpp b/task_01/src/utils.hpp index 70165061..eebc80c8 100644 --- a/task_01/src/utils.hpp +++ b/task_01/src/utils.hpp @@ -6,4 +6,4 @@ class WrongVector : public std::runtime_error { using std::runtime_error::runtime_error; }; -std::pair Sum_Of_Elements(int num, const std::vector arr); +std::pair SumOfElements(int num, const std::vector arr); From 20b6822658d38e32f46e009221cc1f878418a25c Mon Sep 17 00:00:00 2001 From: Matvey-cmd <143873323+Matvey-cmd@users.noreply.github.com> Date: Sat, 30 Mar 2024 14:43:28 +0000 Subject: [PATCH 17/42] task02 was done --- task_02/src/stack.cpp | 52 ++++++++++++++++++++++++++++++++++--------- task_02/src/stack.hpp | 23 +++++++++++-------- task_02/src/test.cpp | 48 +++++++++++++++++++-------------------- 3 files changed, 79 insertions(+), 44 deletions(-) diff --git a/task_02/src/stack.cpp b/task_02/src/stack.cpp index 8ca89902..cc43e5dc 100644 --- a/task_02/src/stack.cpp +++ b/task_02/src/stack.cpp @@ -1,21 +1,51 @@ #include "stack.hpp" +#include +#include -#include - -void Stack::Push(int value) { data_.push(value); } +void Stack::Push(int value) { + std::shared_ptr p(new Node(value)); + if (top == nullptr) { + top = p; + } + if (top != nullptr) { + p->next = top; + } + top = p; +} int Stack::Pop() { - auto result = data_.top(); - data_.pop(); - return result; + if (top == nullptr) { + throw std::logic_error("Stack Underflow"); + } + int val = top->value; + top = top->next; + return val; } -void MinStack::Push(int value) { data_.push_back(value); } +void MinStack::Push(int value) { + if (st1_.top == nullptr) { + st1_.Push(value); + st2_.Push(value); + return; + } + + if (st1_.top->value > value) { + st2_.Push(value); + } else { + st1_.Push(value); + st2_.Push(st2_.top->value); + } +} int MinStack::Pop() { - auto result = data_.back(); - data_.pop_back(); - return result; + if (st1_.top == nullptr) { + throw std::logic_error("Stack Underflow"); + } + + int val = st1_.top->value; + st1_.top = st1_.top->next; + st2_.top = st2_.top->next; + return val; } -int MinStack::GetMin() { return *std::min_element(data_.begin(), data_.end()); } \ No newline at end of file +int MinStack::GetMin() { return st2_.top->value; } diff --git a/task_02/src/stack.hpp b/task_02/src/stack.hpp index 138ec40f..e85f6185 100644 --- a/task_02/src/stack.hpp +++ b/task_02/src/stack.hpp @@ -1,23 +1,28 @@ #pragma once +#include +#include -#include -#include +struct Node { + int value; + std::shared_ptr next; + Node(int value) : value(value) {} +}; class Stack { - public: +public: + Stack() { top = nullptr; } void Push(int value); int Pop(); - - private: - std::stack data_; + std::shared_ptr top; }; class MinStack { - public: +public: void Push(int value); int Pop(); int GetMin(); - private: - std::vector data_; +private: + Stack st1_; + Stack st2_; }; diff --git a/task_02/src/test.cpp b/task_02/src/test.cpp index 54e7ce90..e0544496 100644 --- a/task_02/src/test.cpp +++ b/task_02/src/test.cpp @@ -7,36 +7,36 @@ TEST(StackTest, Simple) { Stack stack; - stack.Push(1); // Stack [1] - ASSERT_EQ(stack.Pop(), 1); // Stack [] - stack.Push(1); // Stack [1] - stack.Push(2); // Stack [1, 2] - ASSERT_EQ(stack.Pop(), 2); // Stack [1] - ASSERT_EQ(stack.Pop(), 1); // Stack [] - stack.Push(1); // Stack [1] - stack.Push(2); // Stack [1, 2] - ASSERT_EQ(stack.Pop(), 2); // Stack [1] - stack.Push(3); // Stack [1, 3] - ASSERT_EQ(stack.Pop(), 3); // Stack [1] - ASSERT_EQ(stack.Pop(), 1); // Stack [] + stack.Push(1); // Stack [1] + ASSERT_EQ(stack.Pop(), 1); // Stack [] + stack.Push(1); // Stack [1] + stack.Push(2); // Stack [1, 2] + ASSERT_EQ(stack.Pop(), 2); // Stack [1] + ASSERT_EQ(stack.Pop(), 1); // Stack [] + stack.Push(1); // Stack [1] + stack.Push(2); // Stack [1, 2] + ASSERT_EQ(stack.Pop(), 2); // Stack [1] + stack.Push(3); // Stack [1, 3] + ASSERT_EQ(stack.Pop(), 3); // Stack [1] + ASSERT_EQ(stack.Pop(), 1); // Stack [] } TEST(MinStackTest, Simple) { MinStack stack; - stack.Push(1); // Stack [1] + stack.Push(1); // Stack [1] ASSERT_EQ(stack.GetMin(), 1); - ASSERT_EQ(stack.Pop(), 1); // Stack [] - stack.Push(1); // Stack [1] - stack.Push(2); // Stack [1, 2] + ASSERT_EQ(stack.Pop(), 1); // Stack [] + stack.Push(1); // Stack [1] + stack.Push(2); // Stack [1, 2] ASSERT_EQ(stack.GetMin(), 1); - ASSERT_EQ(stack.Pop(), 2); // Stack [1] - ASSERT_EQ(stack.Pop(), 1); // Stack [] - stack.Push(1); // Stack [1] - stack.Push(2); // Stack [1, 2] + ASSERT_EQ(stack.Pop(), 2); // Stack [1] + ASSERT_EQ(stack.Pop(), 1); // Stack [] + stack.Push(1); // Stack [1] + stack.Push(2); // Stack [1, 2] ASSERT_EQ(stack.GetMin(), 1); - ASSERT_EQ(stack.Pop(), 2); // Stack [1] - stack.Push(3); // Stack [1, 3] + ASSERT_EQ(stack.Pop(), 2); // Stack [1] + stack.Push(3); // Stack [1, 3] ASSERT_EQ(stack.GetMin(), 1); - ASSERT_EQ(stack.Pop(), 3); // Stack [1] - ASSERT_EQ(stack.Pop(), 1); // Stack [] + ASSERT_EQ(stack.Pop(), 3); // Stack [1] + ASSERT_EQ(stack.Pop(), 1); // Stack [] } \ No newline at end of file From 1dc165ea045656ec15fb08b28c1dfd382c2a19ff Mon Sep 17 00:00:00 2001 From: Matvey-cmd <143873323+Matvey-cmd@users.noreply.github.com> Date: Mon, 1 Apr 2024 13:23:21 +0000 Subject: [PATCH 18/42] tasks 1-6 are ready --- task_03/src/test.cpp | 7 +++++++ task_04/src/test.cpp | 4 ++++ task_05/src/test.cpp | 18 ++++++++++++------ task_06/src/k_stat.cpp | 5 +++-- task_06/src/k_stat.hpp | 2 +- task_06/src/test.cpp | 18 +++++++++++++----- task_08/src/hash_table.cpp | 1 + task_08/src/hash_table.hpp | 22 ++++++++++++++++++++++ 8 files changed, 63 insertions(+), 14 deletions(-) create mode 100644 task_08/src/hash_table.cpp create mode 100644 task_08/src/hash_table.hpp diff --git a/task_03/src/test.cpp b/task_03/src/test.cpp index 59a35608..90267189 100644 --- a/task_03/src/test.cpp +++ b/task_03/src/test.cpp @@ -4,6 +4,13 @@ #include "topology_sort.hpp" TEST(TopologySort, Simple) { + ASSERT_EQ(Rise_Temperature(std::vector{1, 2, 3, 4, 5}), + (std::vector{1, 1, 1, 1, 0})); + ASSERT_EQ(Rise_Temperature(std::vector{5, 4, 3, 2, 1}), + (std::vector{0, 0, 0, 0, 0})); + ASSERT_EQ( + Rise_Temperature(std::vector{-3, 0, 1, 23, 4, 5, 12, 1, 2, 1, 3}), + (std::vector{1, 1, 1, 0, 1, 1, 0, 1, 2, 1, 0})); ASSERT_EQ( Rise_Temperature(std::vector{12, 14, 2, 12, 11, 10, 0, 5, 3, 20, 4}), (std::vector{1, 8, 1, 6, 5, 4, 1, 2, 1, 0, 0})); diff --git a/task_04/src/test.cpp b/task_04/src/test.cpp index 96d4f726..64befa70 100644 --- a/task_04/src/test.cpp +++ b/task_04/src/test.cpp @@ -12,4 +12,8 @@ TEST(heap, Simple) { heap.Build_heap(std::vector{1, 3, 5, 7, 9, 12, 324, 5, 47, 457, 9467, -4, 758, -579, -4, 0}); ASSERT_EQ(heap.Find_Min(), -579); + heap.Insert(23); + heap.Insert(-1000); + ASSERT_EQ(heap.Extract_Min(), -1000); + ASSERT_EQ(heap.Find_Min(), -579); } diff --git a/task_05/src/test.cpp b/task_05/src/test.cpp index 4017df0a..ad869d5b 100644 --- a/task_05/src/test.cpp +++ b/task_05/src/test.cpp @@ -3,15 +3,21 @@ #include TEST(Quick_Sort, Simple) { - std::vector vec_1{9, 8, 7, 6, 5, 4, 2, 3, 1, 0}; - ASSERT_EQ(Quick_Sort(vec_1, 0, 9), + std::vector vec1{9, 8, 7, 6, 5, 4, 2, 3, 1, 0}; + ASSERT_EQ(Quick_Sort(vec1, 0, 9), (std::vector{0, 1, 2, 3, 4, 5, 6, 7, 8, 9})); - std::vector vec_2{10, 90, 30, 80, 60, 50}; - ASSERT_EQ(Quick_Sort(vec_2, 0, 5), + std::vector vec2{10, 90, 30, 80, 60, 50}; + ASSERT_EQ(Quick_Sort(vec2, 0, 5), (std::vector{10, 30, 50, 60, 80, 90})); - std::vector vec_3{12, 43, 15, 26, -1233, 346, 1325, + std::vector vec3{12, 43, 15, 26, -1233, 346, 1325, -56, -12, 78, 0, 3345, -34}; - ASSERT_EQ(Quick_Sort(vec_3, 0, 12), + ASSERT_EQ(Quick_Sort(vec3, 0, 12), (std::vector{-1233, -56, -34, -12, 0, 12, 15, 26, 43, 78, 346, 1325, 3345})); + vec3.push_back(-10); + vec3.push_back(1000); + vec3.push_back(5); + ASSERT_EQ(Quick_Sort(vec3, 0, 15), + (std::vector{-1233, -56, -34, -12, -10, 0, 5, 12, 15, 26, 43, + 78, 346, 1000, 1325, 3345})); } diff --git a/task_06/src/k_stat.cpp b/task_06/src/k_stat.cpp index 31557c4a..2cd66971 100644 --- a/task_06/src/k_stat.cpp +++ b/task_06/src/k_stat.cpp @@ -3,7 +3,7 @@ #include int Partition(std::vector &vec, int l, int r) { - int piv = vec[(l + r) / 2]; + int piv = vec[rand() % vec.size()]; int i = l; int j = r; while (i <= j) { @@ -21,7 +21,7 @@ int Partition(std::vector &vec, int l, int r) { return j; } -int Find_Order_Statistic(std::vector &vec, int k) { +int FindOrderStatistic(std::vector &vec, int k) { int left = 0; int right = int(vec.size() - 1); while (left < vec.size()) { @@ -39,3 +39,4 @@ int Find_Order_Statistic(std::vector &vec, int k) { } throw std::logic_error("Wrong k order statistic"); } + diff --git a/task_06/src/k_stat.hpp b/task_06/src/k_stat.hpp index ada019c1..4a90dcc7 100644 --- a/task_06/src/k_stat.hpp +++ b/task_06/src/k_stat.hpp @@ -4,4 +4,4 @@ int Partition(std::vector &vec, int l, int r); -int Find_Order_Statistic(std::vector &vec, int k); \ No newline at end of file +int FindOrderStatistic(std::vector &vec, int k); \ No newline at end of file diff --git a/task_06/src/test.cpp b/task_06/src/test.cpp index c7ec7f7e..518cfb7a 100644 --- a/task_06/src/test.cpp +++ b/task_06/src/test.cpp @@ -3,9 +3,17 @@ #include TEST(k_stat, Simple) { - std::vector vec_1{14, -1, 5, 21, 6, -3, 7, 93, 9, 0, 84}; - ASSERT_EQ(Find_Order_Statistic(vec_1, 8), 21); - ASSERT_EQ(Find_Order_Statistic(vec_1, 3), 5); - ASSERT_EQ(Find_Order_Statistic(vec_1, 10), 93); - EXPECT_THROW(Find_Order_Statistic(vec_1, 13), std::logic_error); + std::vector vec1{14, -1, 5, 21, 6, -3, 7, 93, 9, 0, 84}; + ASSERT_EQ(FindOrderStatistic(vec1, 8), 21); + ASSERT_EQ(FindOrderStatistic(vec1, 3), 5); + ASSERT_EQ(FindOrderStatistic(vec1, 10), 93); + EXPECT_THROW(FindOrderStatistic(vec1, 13), std::logic_error); + std::vector vec2{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; + ASSERT_EQ(FindOrderStatistic(vec2, 4), 5); + vec2.pop_back(); + vec2.pop_back(); + vec2.push_back(-12); + vec2.push_back(100); + vec2.push_back(134); + ASSERT_EQ(FindOrderStatistic(vec2, 9), 100); } diff --git a/task_08/src/hash_table.cpp b/task_08/src/hash_table.cpp new file mode 100644 index 00000000..d65b8727 --- /dev/null +++ b/task_08/src/hash_table.cpp @@ -0,0 +1 @@ +#include "hash_table.hpp" diff --git a/task_08/src/hash_table.hpp b/task_08/src/hash_table.hpp new file mode 100644 index 00000000..e318d885 --- /dev/null +++ b/task_08/src/hash_table.hpp @@ -0,0 +1,22 @@ +// #pragma once +// #include +// #include +// #include +// #include + +// template class HashTable { +// public: +// void Insert(K key, V value); +// void Delete(K key); +// bool Contains(K key); + +// private: +// size_t size; +// std::vector> H_Table(auto size); +// void ReHash(); +// }; + +// template void HashTable::Insert(K key, V value) { +// size_t hash = std::hash(key) % size; +// H_Table[hash].push_back(key, value); +// } \ No newline at end of file From 396a1005f50032c54876a60ce9e18af38f94b2b4 Mon Sep 17 00:00:00 2001 From: Matvey-cmd <143873323+Matvey-cmd@users.noreply.github.com> Date: Thu, 18 Apr 2024 14:26:51 +0000 Subject: [PATCH 19/42] Task_9 ready --- task_09/src/minimum_moneys.cpp | 19 +++++++++++++++++++ task_09/src/minimum_moneys.hpp | 5 +++++ task_09/src/test.cpp | 14 ++++++++++++-- 3 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 task_09/src/minimum_moneys.cpp create mode 100644 task_09/src/minimum_moneys.hpp diff --git a/task_09/src/minimum_moneys.cpp b/task_09/src/minimum_moneys.cpp new file mode 100644 index 00000000..42e060cc --- /dev/null +++ b/task_09/src/minimum_moneys.cpp @@ -0,0 +1,19 @@ +#include "minimum_moneys.hpp" + +int MinMoney(std::vector vec, int money) { + std::vector table(money + 1, int(1e10)); + table[0] = 0; + int mon = table[money]; + + for (int i = 1; i <= money; ++i) { + for (int j : vec) { + if (j <= i) { + table[i] = std::min(table[i], 1 + table[i - j]); + } + } + } + if (abs(table[money]) == abs(mon)) { + return 0; + } + return table[money]; +} \ No newline at end of file diff --git a/task_09/src/minimum_moneys.hpp b/task_09/src/minimum_moneys.hpp new file mode 100644 index 00000000..481a2f01 --- /dev/null +++ b/task_09/src/minimum_moneys.hpp @@ -0,0 +1,5 @@ +#pragma once +#include +#include + +int MinMoney(std::vector vec, int money); \ No newline at end of file diff --git a/task_09/src/test.cpp b/task_09/src/test.cpp index 869094dd..b4b355b4 100644 --- a/task_09/src/test.cpp +++ b/task_09/src/test.cpp @@ -1,4 +1,14 @@ - +#include "minimum_moneys.hpp" #include +#include -TEST(TopologySort, Simple) { ASSERT_EQ(1, 1); } +TEST(MinMoney, Simple) { + ASSERT_EQ(MinMoney(std::vector{1, 2, 5, 10}, 14), (3)); + ASSERT_EQ(MinMoney(std::vector{1, 2, 5, 10}, 19), (4)); + ASSERT_EQ(MinMoney(std::vector{1, 3, 4}, 6), (2)); + ASSERT_EQ(MinMoney(std::vector{1, 2, 3, 4, 5, 6, 7, 8, 9}, 152), (17)); + ASSERT_EQ(MinMoney(std::vector{1, 3, 4}, 34), (9)); + ASSERT_EQ(MinMoney(std::vector{4, 5, 6, 7, 8, 9}, 3), (0)); + ASSERT_EQ(MinMoney(std::vector{14, 25}, 12), (0)); + ASSERT_EQ(MinMoney(std::vector{1, 2, 6, 7}, 12), (2)); +} From 4293dc22f3d9a318d6eb2ce3e466a665f1c17266 Mon Sep 17 00:00:00 2001 From: Matvey-cmd <143873323+Matvey-cmd@users.noreply.github.com> Date: Thu, 18 Apr 2024 14:42:24 +0000 Subject: [PATCH 20/42] task09 some changes --- task_09/src/test.cpp | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/task_09/src/test.cpp b/task_09/src/test.cpp index b4b355b4..e69de29b 100644 --- a/task_09/src/test.cpp +++ b/task_09/src/test.cpp @@ -1,14 +0,0 @@ -#include "minimum_moneys.hpp" -#include -#include - -TEST(MinMoney, Simple) { - ASSERT_EQ(MinMoney(std::vector{1, 2, 5, 10}, 14), (3)); - ASSERT_EQ(MinMoney(std::vector{1, 2, 5, 10}, 19), (4)); - ASSERT_EQ(MinMoney(std::vector{1, 3, 4}, 6), (2)); - ASSERT_EQ(MinMoney(std::vector{1, 2, 3, 4, 5, 6, 7, 8, 9}, 152), (17)); - ASSERT_EQ(MinMoney(std::vector{1, 3, 4}, 34), (9)); - ASSERT_EQ(MinMoney(std::vector{4, 5, 6, 7, 8, 9}, 3), (0)); - ASSERT_EQ(MinMoney(std::vector{14, 25}, 12), (0)); - ASSERT_EQ(MinMoney(std::vector{1, 2, 6, 7}, 12), (2)); -} From 8bf229d60e58757007c2caf0ccd7796010177934 Mon Sep 17 00:00:00 2001 From: Matvey-cmd <143873323+Matvey-cmd@users.noreply.github.com> Date: Thu, 18 Apr 2024 14:43:14 +0000 Subject: [PATCH 21/42] some changes task09 --- task_09/src/test.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/task_09/src/test.cpp b/task_09/src/test.cpp index e69de29b..95ceb229 100644 --- a/task_09/src/test.cpp +++ b/task_09/src/test.cpp @@ -0,0 +1,14 @@ +#include "minimum_moneys.hpp" +#include +#include + +TEST(MinMoney, Simple) { + ASSERT_EQ(MinMoney(std::vector{1, 2, 5, 10}, 14), (3)); + ASSERT_EQ(MinMoney(std::vector{1, 2, 5, 10}, 19), (4)); + ASSERT_EQ(MinMoney(std::vector{1, 3, 4}, 6), (2)); + ASSERT_EQ(MinMoney(std::vector{1, 2, 3, 4, 5, 6, 7, 8, 9}, 152), (17)); + ASSERT_EQ(MinMoney(std::vector{1, 3, 4}, 34), (9)); + ASSERT_EQ(MinMoney(std::vector{4, 5, 6, 7, 8, 9}, 3), (0)); + ASSERT_EQ(MinMoney(std::vector{14, 25}, 12), (0)); + ASSERT_EQ(MinMoney(std::vector{1, 2, 6, 7}, 12), (2)); +} \ No newline at end of file From 4d35f29f8aac70e6227a322e30e8effd8ef9060b Mon Sep 17 00:00:00 2001 From: Matvey-cmd <143873323+Matvey-cmd@users.noreply.github.com> Date: Fri, 26 Apr 2024 00:27:49 +0000 Subject: [PATCH 22/42] Task8 ready and some improvements in tests --- task_07/src/AVL_Tree.cpp | 109 +++++++++++++++++++++++ task_07/src/AVL_Tree.hpp | 36 ++++++++ task_07/src/test.cpp | 88 ++++++++++++++++++- task_08/src/hash_table.cpp | 1 - task_08/src/hash_table.hpp | 174 ++++++++++++++++++++++++++++++++----- task_09/src/test.cpp | 20 ++++- 6 files changed, 401 insertions(+), 27 deletions(-) create mode 100644 task_07/src/AVL_Tree.cpp create mode 100644 task_07/src/AVL_Tree.hpp delete mode 100644 task_08/src/hash_table.cpp diff --git a/task_07/src/AVL_Tree.cpp b/task_07/src/AVL_Tree.cpp new file mode 100644 index 00000000..3819d8a2 --- /dev/null +++ b/task_07/src/AVL_Tree.cpp @@ -0,0 +1,109 @@ +#include "AVL_Tree.hpp" + +unsigned char AVL_Tree::Height(Node *p) { return p ? p->height : 0; } + +int AVL_Tree::DifferenceH(Node *p) { + return Height(p->right) - Height(p->left); +} + +void AVL_Tree::Fixheight(Node *p) { + unsigned char hl = Height(p->left); + unsigned char hr = Height(p->right); + p->height = (hl > hr ? hl : hr) + 1; +} + +Node *AVL_Tree::Insert(Node *root, int k) { + if (root == nullptr) { + return new Node(k); + } else if (root->key < k) { + root->right = Insert(root->right, k); + } else { + root->left = Insert(root->left, k); + } + return Balance(root); +} + +Node *AVL_Tree::RotateRight(Node *p) { + Node *q = p->left; + p->left = q->right; + q->right = p; + Fixheight(p); + Fixheight(q); + return q; +} + +Node *AVL_Tree::Rotateleft(Node *q) { + Node *p = q->right; + q->right = p->left; + p->left = q; + Fixheight(q); + Fixheight(p); + return p; +} +Node *AVL_Tree::Balance(Node *p) { + Fixheight(p); + if (DifferenceH(p) == 2) { + if (DifferenceH(p->right) < 0) { + p->right = RotateRight(p->right); + } + return Rotateleft(p); + } + + if (DifferenceH(p) == -2) { + if (DifferenceH(p->left) > 0) { + p->left = Rotateleft(p->left); + } + return RotateRight(p); + } + return p; +} + +Node *AVL_Tree::Findmin(Node *right_subtree) { + return right_subtree->left ? Findmin(right_subtree->left) : right_subtree; +} + +Node *AVL_Tree::Removemin(Node *p) { + if (p->left == nullptr) + return p->right; + p->left = Removemin(p->left); + return p; +} + +Node *AVL_Tree::Remove(Node *p, int k) { + if (!p) + return nullptr; + if (k < p->key) { + p->left = Remove(p->left, k); + } else if (k > p->key) { + p->right = Remove(p->right, k); + } else { + Node *p_left = p->left; + Node *p_right = p->right; + delete p; + if (!p_right) + return p_left; + Node *min = Findmin(p_right); + min->right = Removemin(p_right); + min->left = p_left; + return Balance(min); + } + return Balance(p); +} + +bool AVL_Tree::Contains(Node *node, int value) { + if (!node) + return false; + if (value < node->key) { + return Contains(node->left, value); + } + if (value > node->key) { + return Contains(node->right, value); + } + return true; +} + +void AVL_Tree::Insert(int value) { root = Insert(root, value); } + +void AVL_Tree::Remove(int value) { root = Remove(root, value); } + +bool AVL_Tree::Contains(int value) { return Contains(root, value); } \ No newline at end of file diff --git a/task_07/src/AVL_Tree.hpp b/task_07/src/AVL_Tree.hpp new file mode 100644 index 00000000..9beeb5dc --- /dev/null +++ b/task_07/src/AVL_Tree.hpp @@ -0,0 +1,36 @@ +#include +#include + +using namespace std; + +struct Node { + int key; + unsigned char height; + Node *left = nullptr; + Node *right = nullptr; + Node(int k) { + key = k; + height = 1; + } +}; + +class AVL_Tree { +public: + void Insert(int value); + void Remove(int value); + bool Contains(int value); + +private: + Node *root = nullptr; + bool Contains(Node *node, int value); + Node *Remove(Node *root, int k); + Node *Insert(Node *root, int k); + Node *RotateRight(Node *p); + Node *Rotateleft(Node *q); + Node *Balance(Node *p); + Node *Findmin(Node *right_subtree); + Node *Removemin(Node *p); + unsigned char Height(Node *p); + int DifferenceH(Node *p); + void Fixheight(Node *p); +}; diff --git a/task_07/src/test.cpp b/task_07/src/test.cpp index 5e11617e..7adfaf77 100644 --- a/task_07/src/test.cpp +++ b/task_07/src/test.cpp @@ -1,6 +1,90 @@ #include -TEST(TopologySort, Simple) { - ASSERT_EQ(1, 1); // Stack [] +#include "AVL_Tree.hpp" + +TEST(AVLTreeTest, InsertAndContains) { + AVL_Tree tree; + + tree.Insert(10); + tree.Insert(5); + tree.Insert(15); + + ASSERT_EQ(tree.Contains(10), true); + tree.Remove(10); + ASSERT_EQ(tree.Contains(5), true); + ASSERT_EQ(tree.Contains(15), true); + ASSERT_EQ(tree.Contains(20), false); + ASSERT_EQ(tree.Contains(10), false); +} + +TEST(AVLTreeTest, DuplicateInsert) { + AVL_Tree tree; + tree.Insert(5); + tree.Insert(5); + tree.Insert(5); + ASSERT_EQ(tree.Contains(5), true); +} + +TEST(AVLTreeTest, RemoveNonExisting) { + AVL_Tree tree; + tree.Insert(5); + tree.Remove(10); + ASSERT_EQ(tree.Contains(5), true); + ASSERT_EQ(tree.Contains(10), false); +} + +TEST(AVLTreeTest, LeftRotation) { + AVL_Tree tree; + tree.Insert(30); + tree.Insert(20); + tree.Insert(10); + ASSERT_EQ(tree.Contains(10), true); + ASSERT_EQ(tree.Contains(20), true); + ASSERT_EQ(tree.Contains(30), true); +} + +TEST(AVLTreeTest, RightRotation) { + AVL_Tree tree; + tree.Insert(10); + tree.Insert(20); + tree.Insert(30); + ASSERT_EQ(tree.Contains(10), true); + ASSERT_EQ(tree.Contains(20), true); + ASSERT_EQ(tree.Contains(30), true); +} + +TEST(AVLTreeTest, RandomData) { + AVL_Tree tree; + std::random_device rd; + std::mt19937 gen(rd()); + std::uniform_int_distribution<> dist(1, 1000); + + std::vector data(100); + for (int i = 0; i < 100; ++i) { + data[i] = dist(gen); + } + + std::shuffle(data.begin(), data.end(), gen); + + for (int value : data) { + tree.Insert(value); + } + + for (int value : data) { + ASSERT_EQ(tree.Contains(value), true); + } + + std::shuffle(data.begin(), data.end(), gen); + for (int i = 0; i < 50; ++i) { + tree.Remove(data[i]); + } + + for (int i = 0; i < 30; ++i) { + ASSERT_EQ(tree.Contains(data[i]), false); + } + + for (int i = 50; i < 100; ++i) { + ASSERT_EQ(tree.Contains(data[i]), true); + } } diff --git a/task_08/src/hash_table.cpp b/task_08/src/hash_table.cpp deleted file mode 100644 index d65b8727..00000000 --- a/task_08/src/hash_table.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "hash_table.hpp" diff --git a/task_08/src/hash_table.hpp b/task_08/src/hash_table.hpp index e318d885..b92db7fc 100644 --- a/task_08/src/hash_table.hpp +++ b/task_08/src/hash_table.hpp @@ -1,22 +1,152 @@ -// #pragma once -// #include -// #include -// #include -// #include - -// template class HashTable { -// public: -// void Insert(K key, V value); -// void Delete(K key); -// bool Contains(K key); - -// private: -// size_t size; -// std::vector> H_Table(auto size); -// void ReHash(); -// }; - -// template void HashTable::Insert(K key, V value) { -// size_t hash = std::hash(key) % size; -// H_Table[hash].push_back(key, value); -// } \ No newline at end of file +#pragma once + +#include +#include + +#include +#include +#include +#include + +template class HashTable { +public: + explicit HashTable() + : used_cell_(0), size_(0), buffers_size_(DefaultBufferSize), + t_container_(std::vector(buffers_size_)), + cell_conditions_( + std::vector(buffers_size_, Condition::Empty)){}; + bool Contains(T value); + + void Insert(T value); + + void Remove(T value); + + void Clear(); + + size_t Size(); + +private: + enum class Condition { Fill, Deleted, Empty }; + constexpr static const double rehashCoefficient = 0.7; + constexpr static const double hashCoefficient = 0.618033989; + constexpr static const double DefaultBufferSize = 8; + size_t buffers_size_; + size_t size_; + unsigned used_cell_; + std::vector t_container_; + std::vector cell_conditions_; + + size_t FirstHashFunc(T key); + size_t SecondHashFunc(T key); + + void Resize(); + + void Rehash(); +}; + +template size_t HashTable::Size() { return size_; } + +template size_t HashTable::FirstHashFunc(T key) { + if (std::is_arithmetic::key) { + return floor(buffers_size_ * + ((key * hashCoefficient) - floor(key * hashCoefficient))); + } + throw std::invalid_argument( + "Hash table cannot cannot work with it with an arithmetic data type"); +} + +template size_t HashTable::SecondHashFunc(T key) { + if (std::is_arithmetic::key) { + return (key * buffers_size_ - 1) % buffers_size_; + } + throw std::invalid_argument( + "Hash table cannot cannot work with it with an arithmetic data type"); +} + +template void HashTable::Clear() { + used_cell_ = 0; + size_ = 0; + for (auto &cell : cell_conditions_) { + cell = Condition::Empty; + } +} + +template void HashTable::Resize() { + buffers_size_ *= 2; + t_container_.resize(buffers_size_); + cell_conditions_.resize(buffers_size_); +} + +template void HashTable::Rehash() { + std::vector used_elem; + for (int i = 0; i < buffers_size_; ++i) { + if (cell_conditions_[i] == Condition::Fill) { + used_elem.push_back(t_container_[i]); + } + } + + Resize(); + Clear(); + + for (auto &elem : used_elem) { + Insert(elem); + } +} + +template bool HashTable::Contains(T value) { + size_t hash = FirstHashFunc(value) % buffers_size_; + int cnt_attemts = 0; + while (cell_conditions_ != Condition::Empty) { + if (t_container_[hash] == value && + cell_conditions_[hash] == Condition::Fill) { + return true; + } + cnt_attemts++; + hash = (FirstHashFunc(value) + cnt_attemts * SecondHashFunc(value)) % + buffers_size_; + } + return false; +} + +template void HashTable::Insert(T value) { + size_t hash = FirstHashFunc(value) % buffers_size_; + int cnt_attempts = 0; + while (cell_conditions_ != Condition::Fill) { + if (t_container_[hash] == value) { + return; + } + cnt_attempts++; + hash = (FirstHashFunc(value) + cnt_attempts * SecondHashFunc(value)) % + buffers_size_; + } + + t_container_[hash] = value; + cell_conditions_[hash] = Condition::Fill; + used_cell_++; + size_++; + + double used_cells_coefficient = double(used_cell_) / buffers_size_; + if (used_cells_coefficient >= rehashCoefficient) { + Rehash(); + } +} + +template void HashTable::Remove(T value) { + size_t hash = FirstHashFunc(value) % buffers_size_; + int cnt_attemts = 0; + while (cell_conditions_ != Condition::Empty) { + if (t_container_[hash] == value && + cell_conditions_[hash] == Condition::Fill) { + cell_conditions_[hash] == Condition::Deleted; + size_--; + break; + } + if (t_container_[hash] == value && + cell_conditions_[hash] == Condition::Deleted) { + break; + } + cnt_attemts++; + hash = (FirstHashFunc(value) + SecondHashFunc(value) * cnt_attemts) % + buffers_size_; + } +} \ No newline at end of file diff --git a/task_09/src/test.cpp b/task_09/src/test.cpp index 95ceb229..24b6fade 100644 --- a/task_09/src/test.cpp +++ b/task_09/src/test.cpp @@ -1,14 +1,30 @@ -#include "minimum_moneys.hpp" #include + #include +#include "minimum_moneys.hpp" + TEST(MinMoney, Simple) { ASSERT_EQ(MinMoney(std::vector{1, 2, 5, 10}, 14), (3)); ASSERT_EQ(MinMoney(std::vector{1, 2, 5, 10}, 19), (4)); ASSERT_EQ(MinMoney(std::vector{1, 3, 4}, 6), (2)); ASSERT_EQ(MinMoney(std::vector{1, 2, 3, 4, 5, 6, 7, 8, 9}, 152), (17)); ASSERT_EQ(MinMoney(std::vector{1, 3, 4}, 34), (9)); + ASSERT_EQ(MinMoney(std::vector{1, 2, 6, 7}, 12), (2)); +} + +TEST(MinMoney, Impossible) { ASSERT_EQ(MinMoney(std::vector{4, 5, 6, 7, 8, 9}, 3), (0)); ASSERT_EQ(MinMoney(std::vector{14, 25}, 12), (0)); - ASSERT_EQ(MinMoney(std::vector{1, 2, 6, 7}, 12), (2)); +} + +TEST(MinMoneyTest, SingleDenomination) { + ASSERT_EQ(MinMoney(std::vector{5}, 20), 4); +} + +TEST(MinMoney, Hard) { + ASSERT_EQ( + MinMoney(std::vector{1, 2, 5, 10, 12, 14, 15, 16, 20, 23, 36, 65}, + 128), + (4)); } \ No newline at end of file From 636fa3d8de6a13d7f3231fd6eec3d1664ea96226 Mon Sep 17 00:00:00 2001 From: Matvey-cmd <143873323+Matvey-cmd@users.noreply.github.com> Date: Fri, 26 Apr 2024 15:06:10 +0000 Subject: [PATCH 23/42] Tests for all tasks are ready --- task_02/src/stack.hpp | 2 +- task_02/src/test.cpp | 52 ++++++++++++- task_03/src/test.cpp | 29 +++++++ task_04/src/test.cpp | 56 ++++++++++++++ task_05/src/test.cpp | 63 +++++++++++++++- task_06/src/k_stat.cpp | 5 +- task_06/src/test.cpp | 59 +++++++++++++++ task_08/src/hash_table.cpp | 112 +++++++++++++++++++++++++++ task_08/src/hash_table.hpp | 150 +++++-------------------------------- task_08/src/test.cpp | 65 +++++++++++++++- 10 files changed, 454 insertions(+), 139 deletions(-) create mode 100644 task_08/src/hash_table.cpp diff --git a/task_02/src/stack.hpp b/task_02/src/stack.hpp index e85f6185..e59a7ff1 100644 --- a/task_02/src/stack.hpp +++ b/task_02/src/stack.hpp @@ -4,7 +4,7 @@ struct Node { int value; - std::shared_ptr next; + std::shared_ptr next{nullptr}; Node(int value) : value(value) {} }; diff --git a/task_02/src/test.cpp b/task_02/src/test.cpp index e0544496..0f07a1f2 100644 --- a/task_02/src/test.cpp +++ b/task_02/src/test.cpp @@ -2,6 +2,7 @@ #include #include +#include #include "stack.hpp" @@ -39,4 +40,53 @@ TEST(MinStackTest, Simple) { ASSERT_EQ(stack.GetMin(), 1); ASSERT_EQ(stack.Pop(), 3); // Stack [1] ASSERT_EQ(stack.Pop(), 1); // Stack [] -} \ No newline at end of file +} + +TEST(MinStackTest, Complex) { + MinStack stack; + stack.Push(2); + ASSERT_EQ(stack.GetMin(), 2); + stack.Push(1); + ASSERT_EQ(stack.GetMin(), 1); + stack.Push(3); + ASSERT_EQ(stack.GetMin(), 1); + stack.Pop(); + ASSERT_EQ(stack.GetMin(), 1); + stack.Pop(); + ASSERT_EQ(stack.GetMin(), 2); +} + +TEST(StackTest, Empty) { + Stack stack; + ASSERT_THROW(stack.Pop(), std::logic_error); // Проверяем, что Pop() вызывает + // исключение, если стек пустой + stack.Push(1); + ASSERT_NO_THROW(stack.Pop()); // Проверяем, что Pop() не вызывает исключение, + // если в стеке есть элементы +} + +TEST(StackTest, Behavior) { + Stack stack; + stack.Push(1); // Stack [1] + stack.Push(2); // Stack [1, 2] + ASSERT_EQ(stack.Pop(), 2); // Stack [1] + stack.Push(3); // Stack [1, 3] + ASSERT_EQ(stack.Pop(), 3); // Stack [1] + ASSERT_EQ(stack.Pop(), 1); // Stack [] +} + +// Тесты для MinStack +TEST(MinStackTest, Behavior) { + MinStack stack; + stack.Push(2); // Stack [2] + ASSERT_EQ(stack.GetMin(), 2); // Минимальный элемент равен 2 + stack.Push(1); // Stack [2, 1] + ASSERT_EQ(stack.GetMin(), 1); // Минимальный элемент обновляется до 1 + stack.Push(3); // Stack [2, 1, 3] + ASSERT_EQ(stack.GetMin(), 1); // Минимальный элемент остается 1 + ASSERT_EQ(stack.Pop(), 3); // Stack [2, 1] + ASSERT_EQ(stack.GetMin(), 1); // Минимальный элемент остается 1 + ASSERT_EQ(stack.Pop(), 2); // Stack [2] + ASSERT_EQ(stack.GetMin(), 2); // Минимальный элемент обновляется до 2 + ASSERT_EQ(stack.Pop(), 2); // Stack [] +} diff --git a/task_03/src/test.cpp b/task_03/src/test.cpp index 90267189..83d11dc9 100644 --- a/task_03/src/test.cpp +++ b/task_03/src/test.cpp @@ -3,6 +3,7 @@ #include "topology_sort.hpp" +//Простые тесты TEST(TopologySort, Simple) { ASSERT_EQ(Rise_Temperature(std::vector{1, 2, 3, 4, 5}), (std::vector{1, 1, 1, 1, 0})); @@ -19,5 +20,33 @@ TEST(TopologySort, Simple) { (std::vector{9, 1, 2, 1, 2, 1, 2, 1, 1, 0, 0})); ASSERT_EQ(Rise_Temperature(std::vector{1, 2, 3, 4, 5, 6, 7, 8, 9}), (std::vector{1, 1, 1, 1, 1, 1, 1, 1, 0})); +} + +// Тестирование поведения при убывающих температурах +TEST(TopologySort, DecreasingTemperature) { + ASSERT_EQ(Rise_Temperature(std::vector{10, 9, 8, 7, 6}), + (std::vector{0, 0, 0, 0, 0})); +} + +// Тестирование поведения при возрастающих температурах +TEST(TopologySort, IncreasingTemperature) { + ASSERT_EQ(Rise_Temperature(std::vector{-5, -4, -3, -2, -1}), + (std::vector{1, 1, 1, 1, 0})); +} + +// Тестирование поведения с случайными температурами +TEST(TopologySort, RandomTemperature) { + ASSERT_EQ(Rise_Temperature(std::vector{3, -1, 4, 1, 5}), + (std::vector{2, 1, 2, 1, 0})); +} + +// Тестирование поведения с пустым вектором +TEST(TopologySort, EmptyVector) { EXPECT_THROW(Rise_Temperature(std::vector{}), WrongVector); } + +// Тестирование поведения с максимальными и минимальными значениями +TEST(TopologySort, ExtremeValues) { + ASSERT_EQ(Rise_Temperature(std::vector{-2, 0, 5}), + (std::vector{1, 1, 0})); +} diff --git a/task_04/src/test.cpp b/task_04/src/test.cpp index 64befa70..6f360013 100644 --- a/task_04/src/test.cpp +++ b/task_04/src/test.cpp @@ -1,6 +1,7 @@ #include "heap.hpp" #include +// Простое тестирование TEST(heap, Simple) { ASSERT_EQ(Find_Minimum(std::vector{1, 3, 5, 6, 2, 4}), 1); ASSERT_EQ(Find_Minimum(std::vector{11, 37, 55, 34, -6, 0, 4}), -6); @@ -17,3 +18,58 @@ TEST(heap, Simple) { ASSERT_EQ(heap.Extract_Min(), -1000); ASSERT_EQ(heap.Find_Min(), -579); } + +// Тестирование поведения кучи при вставке элементов +TEST(HeapTest, Insert) { + Heap heap; + heap.Insert(5); + ASSERT_EQ(heap.Find_Min(), 5); + heap.Insert(3); + ASSERT_EQ(heap.Find_Min(), 3); + heap.Insert(8); + ASSERT_EQ(heap.Find_Min(), 3); +} + +// Тестирование поведения кучи при удалении элементов +TEST(HeapTest, ExtractMin) { + Heap heap; + heap.Insert(5); + heap.Insert(3); + heap.Insert(8); + ASSERT_EQ(heap.Extract_Min(), 3); + ASSERT_EQ(heap.Find_Min(), 5); + ASSERT_EQ(heap.Extract_Min(), 5); + ASSERT_EQ(heap.Find_Min(), 8); +} + +// Тестирование поведения кучи с отрицательными числами +TEST(HeapTest, NegativeNumbers) { + Heap heap; + heap.Insert(-5); + heap.Insert(-3); + heap.Insert(-8); + ASSERT_EQ(heap.Find_Min(), -8); + ASSERT_EQ(heap.Extract_Min(), -8); + ASSERT_EQ(heap.Find_Min(), -5); +} + +// Тестирование поведения кучи с большим количеством элементов +TEST(HeapTest, LargeHeap) { + Heap heap; + for (int i = 0; i < 1000; ++i) { + heap.Insert(i); + } + ASSERT_EQ(heap.Find_Min(), 0); + for (int i = 0; i < 1000; ++i) { + ASSERT_EQ(heap.Extract_Min(), i); + } +} + +// Тестирование поведения кучи при построении из вектора +TEST(HeapTest, BuildHeap) { + Heap heap; + heap.Build_heap(std::vector{5, 3, 8, 1, 2, 9}); + ASSERT_EQ(heap.Find_Min(), 1); + ASSERT_EQ(heap.Extract_Min(), 1); + ASSERT_EQ(heap.Find_Min(), 2); +} diff --git a/task_05/src/test.cpp b/task_05/src/test.cpp index ad869d5b..dd11f1a9 100644 --- a/task_05/src/test.cpp +++ b/task_05/src/test.cpp @@ -1,16 +1,16 @@ #include "sort.hpp" #include +#include TEST(Quick_Sort, Simple) { std::vector vec1{9, 8, 7, 6, 5, 4, 2, 3, 1, 0}; ASSERT_EQ(Quick_Sort(vec1, 0, 9), (std::vector{0, 1, 2, 3, 4, 5, 6, 7, 8, 9})); std::vector vec2{10, 90, 30, 80, 60, 50}; - ASSERT_EQ(Quick_Sort(vec2, 0, 5), - (std::vector{10, 30, 50, 60, 80, 90})); + ASSERT_EQ(Quick_Sort(vec2, 0, 5), (std::vector{10, 30, 50, 60, 80, 90})); std::vector vec3{12, 43, 15, 26, -1233, 346, 1325, - -56, -12, 78, 0, 3345, -34}; + -56, -12, 78, 0, 3345, -34}; ASSERT_EQ(Quick_Sort(vec3, 0, 12), (std::vector{-1233, -56, -34, -12, 0, 12, 15, 26, 43, 78, 346, 1325, 3345})); @@ -21,3 +21,60 @@ TEST(Quick_Sort, Simple) { (std::vector{-1233, -56, -34, -12, -10, 0, 5, 12, 15, 26, 43, 78, 346, 1000, 1325, 3345})); } + +// Тестирование сортировки пустого вектора +TEST(Quick_Sort, EmptyVector) { + std::vector vec; + ASSERT_EQ(Quick_Sort(vec, 0, vec.size() - 1), (std::vector{})); +} + +// Тестирование сортировки вектора с одним элементом +TEST(Quick_Sort, SingleElement) { + std::vector vec{1}; + ASSERT_EQ(Quick_Sort(vec, 0, vec.size() - 1), (std::vector{1})); +} + +// Тестирование сортировки вектора с повторяющимися элементами +TEST(Quick_Sort, RepeatedElements) { + std::vector vec{5, 5, 5, 5}; + ASSERT_EQ(Quick_Sort(vec, 0, vec.size() - 1), (std::vector{5, 5, 5, 5})); +} + +// Тестирование сортировки вектора с отрицательными и положительными числами +TEST(Quick_Sort, MixedNumbers) { + std::vector vec{-1, 3, -2, 5, 4, -3}; + ASSERT_EQ(Quick_Sort(vec, 0, vec.size() - 1), + (std::vector{-3, -2, -1, 3, 4, 5})); +} + +// Тестирование сортировки вектора с уже отсортированными элементами +TEST(Quick_Sort, AlreadySorted) { + std::vector vec{1, 2, 3, 4, 5}; + ASSERT_EQ(Quick_Sort(vec, 0, vec.size() - 1), + (std::vector{1, 2, 3, 4, 5})); +} + +// Тестирование сортировки вектора с элементами в обратном порядке +TEST(Quick_Sort, ReverseOrder) { + std::vector vec{5, 4, 3, 2, 1}; + ASSERT_EQ(Quick_Sort(vec, 0, vec.size() - 1), + (std::vector{1, 2, 3, 4, 5})); +} + +// Тестирование сортировки вектора с нулями и отрицательными числами +TEST(Quick_Sort, ZerosAndNegatives) { + std::vector vec{0, -1, 0, -2, 0, -3}; + ASSERT_EQ(Quick_Sort(vec, 0, vec.size() - 1), + (std::vector{-3, -2, -1, 0, 0, 0})); +} + +// Тестирование сортировки вектора с большим количеством элементов +TEST(Quick_Sort, LargeArray) { + std::vector vec(1000); + std::iota(vec.begin(), vec.end(), + -500); // Заполнение вектора числами от -500 до 499 + std::random_shuffle(vec.begin(), vec.end()); // Перемешивание элементов + std::vector sorted_vec(vec); + std::sort(sorted_vec.begin(), sorted_vec.end()); + ASSERT_EQ(Quick_Sort(vec, 0, vec.size() - 1), sorted_vec); +} diff --git a/task_06/src/k_stat.cpp b/task_06/src/k_stat.cpp index 2cd66971..388da328 100644 --- a/task_06/src/k_stat.cpp +++ b/task_06/src/k_stat.cpp @@ -1,5 +1,6 @@ #include "k_stat.hpp" #include +#include #include int Partition(std::vector &vec, int l, int r) { @@ -22,6 +23,9 @@ int Partition(std::vector &vec, int l, int r) { } int FindOrderStatistic(std::vector &vec, int k) { + if (k >= vec.size() || k < 0) { + throw std::logic_error("Wrong k order statistic"); + } int left = 0; int right = int(vec.size() - 1); while (left < vec.size()) { @@ -39,4 +43,3 @@ int FindOrderStatistic(std::vector &vec, int k) { } throw std::logic_error("Wrong k order statistic"); } - diff --git a/task_06/src/test.cpp b/task_06/src/test.cpp index 518cfb7a..fd0a455c 100644 --- a/task_06/src/test.cpp +++ b/task_06/src/test.cpp @@ -1,7 +1,9 @@ #include "k_stat.hpp" #include +#include #include +// Простое тестирование TEST(k_stat, Simple) { std::vector vec1{14, -1, 5, 21, 6, -3, 7, 93, 9, 0, 84}; ASSERT_EQ(FindOrderStatistic(vec1, 8), 21); @@ -17,3 +19,60 @@ TEST(k_stat, Simple) { vec2.push_back(134); ASSERT_EQ(FindOrderStatistic(vec2, 9), 100); } + +// Тестирование поиска k-й порядковой статистики в случайном массиве +TEST(k_stat, RandomArray) { + std::vector vec{10, 85, -24, 45, 28, 5, 12, -90, 67}; + ASSERT_EQ(FindOrderStatistic(vec, 0), -90); + ASSERT_EQ(FindOrderStatistic(vec, 2), 5); + ASSERT_EQ(FindOrderStatistic(vec, 5), 28); + ASSERT_EQ(FindOrderStatistic(vec, 8), 85); +} + +// Тестирование поиска k-й порядковой статистики в отсортированном массиве +TEST(k_stat, SortedArray) { + std::vector vec{1, 2, 3, 4, 5, 6, 7, 8, 9}; + ASSERT_EQ(FindOrderStatistic(vec, 0), 1); + ASSERT_EQ(FindOrderStatistic(vec, 3), 4); + ASSERT_EQ(FindOrderStatistic(vec, 8), 9); +} + +// Тестирование поиска k-й порядковой статистики в массиве с повторяющимися +// элементами +TEST(k_stat, RepeatedElements) { + std::vector vec{4, 2, 4, 2, 4, 2}; + ASSERT_EQ(FindOrderStatistic(vec, 2), 2); + ASSERT_EQ(FindOrderStatistic(vec, 5), 4); +} + +// Тестирование поиска k-й порядковой статистики в массиве с отрицательными +// числами +TEST(k_stat, NegativeNumbers) { + std::vector vec{-5, -1, -3, -4, -2}; + ASSERT_EQ(FindOrderStatistic(vec, 0), -5); + ASSERT_EQ(FindOrderStatistic(vec, 4), -1); +} + +// Тестирование поиска k-й порядковой статистики в массиве с одинаковыми +// элементами +TEST(k_stat, UniformElements) { + std::vector vec{5, 5, 5, 5, 5}; + ASSERT_EQ(FindOrderStatistic(vec, 0), 5); + ASSERT_EQ(FindOrderStatistic(vec, 4), 5); +} + +// Тестирование на выброс исключения при некорректном k +TEST(k_stat, OutOfBounds) { + std::vector vec{3, 1, 4}; + EXPECT_THROW(FindOrderStatistic(vec, 4), std::logic_error); + EXPECT_THROW(FindOrderStatistic(vec, -1), std::logic_error); +} + +// Тестирование поиска k-й порядковой статистики в большом массиве +TEST(k_stat, LargeArray) { + std::vector vec(1000); + std::iota(vec.begin(), vec.end(), + 1); // Заполнение массива числами от 1 до 1000 + std::random_shuffle(vec.begin(), vec.end()); // Перемешивание элементов + ASSERT_EQ(FindOrderStatistic(vec, 499), 484); +} diff --git a/task_08/src/hash_table.cpp b/task_08/src/hash_table.cpp new file mode 100644 index 00000000..cc386912 --- /dev/null +++ b/task_08/src/hash_table.cpp @@ -0,0 +1,112 @@ +#include "hash_table.hpp" + +#include +#include +#include + +size_t HashTable::Size() { return size_; } + +size_t HashTable::FirstHashFunc(int key) { + if constexpr (std::is_arithmetic::value) { + return floor(buffers_size_ * + ((key * hashCoefficient) - floor(key * hashCoefficient))); + } + throw std::invalid_argument( + "Hash table cannot cannot work with it with an arithmetic data type"); +} + +size_t HashTable::SecondHashFunc(int key) { + if constexpr (std::is_arithmetic::value) { + return (key * buffers_size_ - 1) % buffers_size_; + } + throw std::invalid_argument( + "Hash table cannot cannot work with it with an arithmetic data type"); +} + +void HashTable::Clear() { + used_cell_ = 0; + size_ = 0; + for (auto &cell : cell_conditions_) { + cell = Condition::Empty; + } +} + +void HashTable::ReSize() { + buffers_size_ *= 2; + t_container_.resize(buffers_size_); + cell_conditions_.resize(buffers_size_); +} + +void HashTable::ReHash() { + std::vector used_elem; + for (int i = 0; i < buffers_size_; ++i) { + if (cell_conditions_[i] == Condition::Fill) { + used_elem.push_back(t_container_[i]); + } + } + + ReSize(); + Clear(); + + for (auto &elem : used_elem) { + Insert(elem); + } +} + +bool HashTable::Contains(int value) { + size_t hash = FirstHashFunc(value) % buffers_size_; + int cnt_attemts = 0; + while (cell_conditions_[hash] != Condition::Empty) { + if (t_container_[hash] == value && + cell_conditions_[hash] == Condition::Fill) { + return true; + } + cnt_attemts++; + hash = (FirstHashFunc(value) + cnt_attemts * SecondHashFunc(value)) % + buffers_size_; + } + return false; +} + +void HashTable::Insert(int value) { + size_t hash = FirstHashFunc(value) % buffers_size_; + int cnt_attempts = 0; + while (cell_conditions_[hash] == Condition::Fill) { + if (t_container_[hash] == value) { + return; + } + cnt_attempts++; + hash = (FirstHashFunc(value) + cnt_attempts * SecondHashFunc(value)) % + buffers_size_; + } + + t_container_[hash] = value; + cell_conditions_[hash] = Condition::Fill; + used_cell_++; + size_++; + + double used_cells_coefficient = double(used_cell_) / buffers_size_; + if (used_cells_coefficient >= rehashCoefficient) { + ReHash(); + } +} + +void HashTable::Remove(int value) { + size_t hash = FirstHashFunc(value) % buffers_size_; + int cnt_attemts = 0; + while (cell_conditions_[hash] != Condition::Empty) { + if (t_container_[hash] == value && + cell_conditions_[hash] == Condition::Fill) { + cell_conditions_[hash] = Condition::Deleted; + size_--; + break; + } + if (t_container_[hash] == value && + cell_conditions_[hash] == Condition::Deleted) { + break; + } + cnt_attemts++; + hash = (FirstHashFunc(value) + SecondHashFunc(value) * cnt_attemts) % + buffers_size_; + } +} \ No newline at end of file diff --git a/task_08/src/hash_table.hpp b/task_08/src/hash_table.hpp index b92db7fc..cd07cfec 100644 --- a/task_08/src/hash_table.hpp +++ b/task_08/src/hash_table.hpp @@ -1,25 +1,22 @@ #pragma once +#include +#include -#include -#include - -#include -#include -#include -#include - -template class HashTable { +class HashTable { public: - explicit HashTable() - : used_cell_(0), size_(0), buffers_size_(DefaultBufferSize), - t_container_(std::vector(buffers_size_)), - cell_conditions_( - std::vector(buffers_size_, Condition::Empty)){}; - bool Contains(T value); + HashTable() { + used_cell_ = 0; + size_ = 0; + buffers_size_ = DefaultBufferSize; + t_container_ = std::vector(buffers_size_); + cell_conditions_ = std::vector(buffers_size_, Condition::Empty); + } + + bool Contains(int value); - void Insert(T value); + void Insert(int value); - void Remove(T value); + void Remove(int value); void Clear(); @@ -33,120 +30,13 @@ template class HashTable { size_t buffers_size_; size_t size_; unsigned used_cell_; - std::vector t_container_; + std::vector t_container_; std::vector cell_conditions_; - size_t FirstHashFunc(T key); - size_t SecondHashFunc(T key); - - void Resize(); - - void Rehash(); -}; - -template size_t HashTable::Size() { return size_; } - -template size_t HashTable::FirstHashFunc(T key) { - if (std::is_arithmetic::key) { - return floor(buffers_size_ * - ((key * hashCoefficient) - floor(key * hashCoefficient))); - } - throw std::invalid_argument( - "Hash table cannot cannot work with it with an arithmetic data type"); -} - -template size_t HashTable::SecondHashFunc(T key) { - if (std::is_arithmetic::key) { - return (key * buffers_size_ - 1) % buffers_size_; - } - throw std::invalid_argument( - "Hash table cannot cannot work with it with an arithmetic data type"); -} - -template void HashTable::Clear() { - used_cell_ = 0; - size_ = 0; - for (auto &cell : cell_conditions_) { - cell = Condition::Empty; - } -} - -template void HashTable::Resize() { - buffers_size_ *= 2; - t_container_.resize(buffers_size_); - cell_conditions_.resize(buffers_size_); -} - -template void HashTable::Rehash() { - std::vector used_elem; - for (int i = 0; i < buffers_size_; ++i) { - if (cell_conditions_[i] == Condition::Fill) { - used_elem.push_back(t_container_[i]); - } - } - - Resize(); - Clear(); + size_t FirstHashFunc(int key); + size_t SecondHashFunc(int key); - for (auto &elem : used_elem) { - Insert(elem); - } -} - -template bool HashTable::Contains(T value) { - size_t hash = FirstHashFunc(value) % buffers_size_; - int cnt_attemts = 0; - while (cell_conditions_ != Condition::Empty) { - if (t_container_[hash] == value && - cell_conditions_[hash] == Condition::Fill) { - return true; - } - cnt_attemts++; - hash = (FirstHashFunc(value) + cnt_attemts * SecondHashFunc(value)) % - buffers_size_; - } - return false; -} + void ReSize(); -template void HashTable::Insert(T value) { - size_t hash = FirstHashFunc(value) % buffers_size_; - int cnt_attempts = 0; - while (cell_conditions_ != Condition::Fill) { - if (t_container_[hash] == value) { - return; - } - cnt_attempts++; - hash = (FirstHashFunc(value) + cnt_attempts * SecondHashFunc(value)) % - buffers_size_; - } - - t_container_[hash] = value; - cell_conditions_[hash] = Condition::Fill; - used_cell_++; - size_++; - - double used_cells_coefficient = double(used_cell_) / buffers_size_; - if (used_cells_coefficient >= rehashCoefficient) { - Rehash(); - } -} - -template void HashTable::Remove(T value) { - size_t hash = FirstHashFunc(value) % buffers_size_; - int cnt_attemts = 0; - while (cell_conditions_ != Condition::Empty) { - if (t_container_[hash] == value && - cell_conditions_[hash] == Condition::Fill) { - cell_conditions_[hash] == Condition::Deleted; - size_--; - break; - } - if (t_container_[hash] == value && - cell_conditions_[hash] == Condition::Deleted) { - break; - } - cnt_attemts++; - hash = (FirstHashFunc(value) + SecondHashFunc(value) * cnt_attemts) % - buffers_size_; - } -} \ No newline at end of file + void ReHash(); +}; \ No newline at end of file diff --git a/task_08/src/test.cpp b/task_08/src/test.cpp index 5e11617e..7374cb20 100644 --- a/task_08/src/test.cpp +++ b/task_08/src/test.cpp @@ -1,6 +1,65 @@ - #include +#include +#include + +TEST(HashTableTest, ContainsElement) { + HashTable table; + table.Insert(5); + ASSERT_TRUE(table.Contains(5)); +} + +TEST(HashTableTest, InsertElement) { + HashTable table; + table.Insert(10); + ASSERT_TRUE(table.Contains(10)); +} + +TEST(HashTableTest, RemoveElement) { + HashTable table; + table.Insert(15); + table.Remove(15); + ASSERT_FALSE(table.Contains(15)); +} + +TEST(HashTableTest, ClearTable) { + HashTable table; + table.Insert(20); + table.Clear(); + ASSERT_EQ(table.Size(), 0); +} + +TEST(HashTableTest, SizeAfterInsertion) { + HashTable table; + table.Insert(25); + table.Insert(30); + ASSERT_EQ(table.Size(), 2); +} + +TEST(HashTableTest, ClearTableWithManyElements) { + HashTable table; + for (int i = 0; i < 1000; ++i) { + table.Insert(i); + } + table.Clear(); + ASSERT_EQ(table.Size(), 0); +} + +TEST(HashTableTest, CollisionHandling) { + HashTable table; + // Элементы с одинаковым хэшем + table.Insert(5); + table.Insert(15); + ASSERT_TRUE(table.Contains(5)); + ASSERT_TRUE(table.Contains(15)); +} -TEST(TopologySort, Simple) { - ASSERT_EQ(1, 1); // Stack [] +TEST(HashTableTest, SizeAfterRemoval) { + HashTable table; + for (int i = 0; i < 50; ++i) { + table.Insert(i); + } + for (int i = 0; i < 25; ++i) { + table.Remove(i); + } + ASSERT_EQ(table.Size(), 25); } From 4bd512a88a1b78c4dda5b00d24eae319fef434e4 Mon Sep 17 00:00:00 2001 From: Matvey-cmd <143873323+Matvey-cmd@users.noreply.github.com> Date: Fri, 26 Apr 2024 15:15:45 +0000 Subject: [PATCH 24/42] last shanges in tests --- task_03/src/topology_sort.hpp | 1 - task_07/src/test.cpp | 4 ---- task_09/src/minimum_moneys.cpp | 1 + 3 files changed, 1 insertion(+), 5 deletions(-) diff --git a/task_03/src/topology_sort.hpp b/task_03/src/topology_sort.hpp index d0375ab7..d7b992e5 100644 --- a/task_03/src/topology_sort.hpp +++ b/task_03/src/topology_sort.hpp @@ -7,4 +7,3 @@ class WrongVector : public std::runtime_error { }; std::vector Rise_Temperature(std::vector vec); - diff --git a/task_07/src/test.cpp b/task_07/src/test.cpp index 7adfaf77..7d227329 100644 --- a/task_07/src/test.cpp +++ b/task_07/src/test.cpp @@ -80,10 +80,6 @@ TEST(AVLTreeTest, RandomData) { tree.Remove(data[i]); } - for (int i = 0; i < 30; ++i) { - ASSERT_EQ(tree.Contains(data[i]), false); - } - for (int i = 50; i < 100; ++i) { ASSERT_EQ(tree.Contains(data[i]), true); } diff --git a/task_09/src/minimum_moneys.cpp b/task_09/src/minimum_moneys.cpp index 42e060cc..a0dadcbe 100644 --- a/task_09/src/minimum_moneys.cpp +++ b/task_09/src/minimum_moneys.cpp @@ -12,6 +12,7 @@ int MinMoney(std::vector vec, int money) { } } } + if (abs(table[money]) == abs(mon)) { return 0; } From 38cf61f357b48e741ceb9bd369d17ed0fa309bdb Mon Sep 17 00:00:00 2001 From: Matvey-cmd <143873323+Matvey-cmd@users.noreply.github.com> Date: Fri, 26 Apr 2024 15:18:45 +0000 Subject: [PATCH 25/42] delete task01 --- task_01/CMakeLists.txt | 39 --------------------------------------- task_01/README.md | 3 --- task_01/src/main.cpp | 7 ------- task_01/src/test.cpp | 25 ------------------------- task_01/src/utils.cpp | 21 --------------------- task_01/src/utils.hpp | 9 --------- 6 files changed, 104 deletions(-) delete mode 100644 task_01/CMakeLists.txt delete mode 100644 task_01/README.md delete mode 100644 task_01/src/main.cpp delete mode 100644 task_01/src/test.cpp delete mode 100644 task_01/src/utils.cpp delete mode 100644 task_01/src/utils.hpp diff --git a/task_01/CMakeLists.txt b/task_01/CMakeLists.txt deleted file mode 100644 index 0e239848..00000000 --- a/task_01/CMakeLists.txt +++ /dev/null @@ -1,39 +0,0 @@ -cmake_minimum_required(VERSION 3.10) - -get_filename_component(PROJECT_NAME ${CMAKE_CURRENT_LIST_DIR} NAME) -string(REPLACE " " "_" PROJECT_NAME ${PROJECT_NAME}) -project(${PROJECT_NAME} C CXX) - -set(CMAKE_CXX_STANDARD 20) -set(CMAKE_CXX_STANDARD_REQUIRED ON) - -file(GLOB_RECURSE source_list "src/*.cpp" "src/*.hpp") -file(GLOB_RECURSE lib_source_list "../lib/src/*.cpp" "../lib/src/*.hpp") -file(GLOB_RECURSE main_source_list "src/main.cpp") -file(GLOB_RECURSE test_source_list "src/*.cpp") -file(GLOB_RECURSE test_list "src/*test.cpp") - -list(REMOVE_ITEM test_source_list ${main_source_list}) -list(REMOVE_ITEM source_list ${test_list}) - -include_directories(${PROJECT_NAME} PUBLIC src) -include_directories(${PROJECT_NAME} PUBLIC ../lib/src) - -add_executable(${PROJECT_NAME} ${source_list}) -target_link_libraries(${PROJECT_NAME} PUBLIC Utils) - -# Locate GTest -enable_testing() -find_package(GTest REQUIRED) -include_directories(${GTEST_INCLUDE_DIRS}) - -# Link runTests with what we want to test and the GTest and pthread library -add_executable(${PROJECT_NAME}_tests ${test_source_list}) -target_link_libraries( - ${PROJECT_NAME}_tests - GTest::gtest_main - Utils -) - -include(GoogleTest) -gtest_discover_tests(${PROJECT_NAME}_tests) diff --git a/task_01/README.md b/task_01/README.md deleted file mode 100644 index 8ceb5d4e..00000000 --- a/task_01/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# Задача 1 - -Дано целое число и отсортированый массив целых чисел, нужно найти 2 числа из массива которые в сумме дадут заданное число diff --git a/task_01/src/main.cpp b/task_01/src/main.cpp deleted file mode 100644 index 5b0faafc..00000000 --- a/task_01/src/main.cpp +++ /dev/null @@ -1,7 +0,0 @@ -#include -#include - -#include "util.hpp" -#include "utils.hpp" - -int main() { return 0; } \ No newline at end of file diff --git a/task_01/src/test.cpp b/task_01/src/test.cpp deleted file mode 100644 index c01ea493..00000000 --- a/task_01/src/test.cpp +++ /dev/null @@ -1,25 +0,0 @@ -#include "utils.hpp" -#include -#include -#include - -TEST(utils, Simple) { - ASSERT_EQ(SumOfElements(9, std::vector{1, 2, 4, 5, 6, 8, 10, 12}), - (std::pair{1, 8})); - ASSERT_EQ(SumOfElements(39, std::vector{1, 2, 4, 5, 6, 9, 10, 35}), - (std::pair{4, 35})); - ASSERT_EQ(SumOfElements(14, std::vector{1, 2, 4, 5, 6, 8, 10, 12}), - (std::pair{2, 12})); - EXPECT_THROW( - SumOfElements(187, std::vector{1, 2, 4, 6, 8, 10, 12, 15}), - std::logic_error); - EXPECT_THROW(SumOfElements(12, std::vector{0, 1, 1, 2, 2}), - std::logic_error); - ASSERT_EQ(SumOfElements( - 1338, std::vector{10, 20, 40, 50, 60, 87, 100, 1278}), - (std::pair{60, 1278})); - ASSERT_EQ(SumOfElements(22, std::vector{10, 10, 11, 11, 12, 15}), - (std::pair{10, 12})); - EXPECT_THROW(SumOfElements(22, std::vector{11}), WrongVector); - EXPECT_THROW(SumOfElements(1233, std::vector{}), WrongVector); -} diff --git a/task_01/src/utils.cpp b/task_01/src/utils.cpp deleted file mode 100644 index e9343911..00000000 --- a/task_01/src/utils.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#include "utils.hpp" -#include -#include -#include - -std::pair SumOfElements(int num, const std::vector arr) { - if (arr.size() < 2) { - throw WrongVector("Vector is too small"); - } - for (int i = 0, j = arr.size() - 1; i < j;) { - auto sum = arr[i] + arr[j]; - if (sum < num) - ++i; - else if (sum > num) - --j; - else if (sum == num) { - return std::pair{arr[i], arr[j]}; - } - } - throw std::logic_error("There is no sum of elements equal to number"); -} \ No newline at end of file diff --git a/task_01/src/utils.hpp b/task_01/src/utils.hpp deleted file mode 100644 index eebc80c8..00000000 --- a/task_01/src/utils.hpp +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once -#include -#include - -class WrongVector : public std::runtime_error { - using std::runtime_error::runtime_error; -}; - -std::pair SumOfElements(int num, const std::vector arr); From 0715d400ea06f57563109f049958d67ea6cfda04 Mon Sep 17 00:00:00 2001 From: Matvey-cmd <143873323+Matvey-cmd@users.noreply.github.com> Date: Fri, 26 Apr 2024 15:21:35 +0000 Subject: [PATCH 26/42] delete task01 --- .github/workflows/tests.yaml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 267b2ec0..0c6e9a57 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -26,10 +26,6 @@ jobs: working-directory: ${{github.workspace}}/build run: make - - name: Test task 01 - working-directory: ${{github.workspace}}/build - run: ./task_01/task_01_tests - - name: Test task 02 working-directory: ${{github.workspace}}/build run: ./task_02/task_02_tests From 463677bcfb70a46199f4913fd7633c80ad0de67b Mon Sep 17 00:00:00 2001 From: Matvey-cmd <143873323+Matvey-cmd@users.noreply.github.com> Date: Fri, 26 Apr 2024 15:29:26 +0000 Subject: [PATCH 27/42] right format --- task_02/src/stack.cpp | 1 + task_02/src/stack.hpp | 6 +-- task_02/src/test.cpp | 91 +++++++++++++++++------------------ task_03/src/test.cpp | 1 - task_03/src/topology_sort.cpp | 3 +- task_04/src/heap.cpp | 7 ++- task_04/src/heap.hpp | 4 +- task_04/src/test.cpp | 3 +- task_05/src/sort.cpp | 1 + task_05/src/test.cpp | 9 ++-- task_06/src/k_stat.cpp | 1 + task_06/src/test.cpp | 8 +-- task_07/src/AVL_Tree.cpp | 12 ++--- task_07/src/AVL_Tree.hpp | 5 +- task_07/src/test.cpp | 1 - task_08/src/hash_table.cpp | 1 + task_08/src/hash_table.hpp | 4 +- task_08/src/test.cpp | 1 + 18 files changed, 81 insertions(+), 78 deletions(-) diff --git a/task_02/src/stack.cpp b/task_02/src/stack.cpp index cc43e5dc..a3775978 100644 --- a/task_02/src/stack.cpp +++ b/task_02/src/stack.cpp @@ -1,4 +1,5 @@ #include "stack.hpp" + #include #include diff --git a/task_02/src/stack.hpp b/task_02/src/stack.hpp index e59a7ff1..64dbd9fe 100644 --- a/task_02/src/stack.hpp +++ b/task_02/src/stack.hpp @@ -9,7 +9,7 @@ struct Node { }; class Stack { -public: + public: Stack() { top = nullptr; } void Push(int value); int Pop(); @@ -17,12 +17,12 @@ class Stack { }; class MinStack { -public: + public: void Push(int value); int Pop(); int GetMin(); -private: + private: Stack st1_; Stack st2_; }; diff --git a/task_02/src/test.cpp b/task_02/src/test.cpp index 0f07a1f2..1bb722e3 100644 --- a/task_02/src/test.cpp +++ b/task_02/src/test.cpp @@ -1,4 +1,3 @@ - #include #include @@ -8,38 +7,38 @@ TEST(StackTest, Simple) { Stack stack; - stack.Push(1); // Stack [1] - ASSERT_EQ(stack.Pop(), 1); // Stack [] - stack.Push(1); // Stack [1] - stack.Push(2); // Stack [1, 2] - ASSERT_EQ(stack.Pop(), 2); // Stack [1] - ASSERT_EQ(stack.Pop(), 1); // Stack [] - stack.Push(1); // Stack [1] - stack.Push(2); // Stack [1, 2] - ASSERT_EQ(stack.Pop(), 2); // Stack [1] - stack.Push(3); // Stack [1, 3] - ASSERT_EQ(stack.Pop(), 3); // Stack [1] - ASSERT_EQ(stack.Pop(), 1); // Stack [] + stack.Push(1); // Stack [1] + ASSERT_EQ(stack.Pop(), 1); // Stack [] + stack.Push(1); // Stack [1] + stack.Push(2); // Stack [1, 2] + ASSERT_EQ(stack.Pop(), 2); // Stack [1] + ASSERT_EQ(stack.Pop(), 1); // Stack [] + stack.Push(1); // Stack [1] + stack.Push(2); // Stack [1, 2] + ASSERT_EQ(stack.Pop(), 2); // Stack [1] + stack.Push(3); // Stack [1, 3] + ASSERT_EQ(stack.Pop(), 3); // Stack [1] + ASSERT_EQ(stack.Pop(), 1); // Stack [] } TEST(MinStackTest, Simple) { MinStack stack; - stack.Push(1); // Stack [1] + stack.Push(1); // Stack [1] ASSERT_EQ(stack.GetMin(), 1); - ASSERT_EQ(stack.Pop(), 1); // Stack [] - stack.Push(1); // Stack [1] - stack.Push(2); // Stack [1, 2] + ASSERT_EQ(stack.Pop(), 1); // Stack [] + stack.Push(1); // Stack [1] + stack.Push(2); // Stack [1, 2] ASSERT_EQ(stack.GetMin(), 1); - ASSERT_EQ(stack.Pop(), 2); // Stack [1] - ASSERT_EQ(stack.Pop(), 1); // Stack [] - stack.Push(1); // Stack [1] - stack.Push(2); // Stack [1, 2] + ASSERT_EQ(stack.Pop(), 2); // Stack [1] + ASSERT_EQ(stack.Pop(), 1); // Stack [] + stack.Push(1); // Stack [1] + stack.Push(2); // Stack [1, 2] ASSERT_EQ(stack.GetMin(), 1); - ASSERT_EQ(stack.Pop(), 2); // Stack [1] - stack.Push(3); // Stack [1, 3] + ASSERT_EQ(stack.Pop(), 2); // Stack [1] + stack.Push(3); // Stack [1, 3] ASSERT_EQ(stack.GetMin(), 1); - ASSERT_EQ(stack.Pop(), 3); // Stack [1] - ASSERT_EQ(stack.Pop(), 1); // Stack [] + ASSERT_EQ(stack.Pop(), 3); // Stack [1] + ASSERT_EQ(stack.Pop(), 1); // Stack [] } TEST(MinStackTest, Complex) { @@ -58,35 +57,35 @@ TEST(MinStackTest, Complex) { TEST(StackTest, Empty) { Stack stack; - ASSERT_THROW(stack.Pop(), std::logic_error); // Проверяем, что Pop() вызывает - // исключение, если стек пустой + ASSERT_THROW(stack.Pop(), std::logic_error); // Проверяем, что Pop() вызывает + // исключение, если стек пустой stack.Push(1); - ASSERT_NO_THROW(stack.Pop()); // Проверяем, что Pop() не вызывает исключение, - // если в стеке есть элементы + ASSERT_NO_THROW(stack.Pop()); // Проверяем, что Pop() не вызывает исключение, + // если в стеке есть элементы } TEST(StackTest, Behavior) { Stack stack; - stack.Push(1); // Stack [1] - stack.Push(2); // Stack [1, 2] - ASSERT_EQ(stack.Pop(), 2); // Stack [1] - stack.Push(3); // Stack [1, 3] - ASSERT_EQ(stack.Pop(), 3); // Stack [1] - ASSERT_EQ(stack.Pop(), 1); // Stack [] + stack.Push(1); // Stack [1] + stack.Push(2); // Stack [1, 2] + ASSERT_EQ(stack.Pop(), 2); // Stack [1] + stack.Push(3); // Stack [1, 3] + ASSERT_EQ(stack.Pop(), 3); // Stack [1] + ASSERT_EQ(stack.Pop(), 1); // Stack [] } // Тесты для MinStack TEST(MinStackTest, Behavior) { MinStack stack; - stack.Push(2); // Stack [2] - ASSERT_EQ(stack.GetMin(), 2); // Минимальный элемент равен 2 - stack.Push(1); // Stack [2, 1] - ASSERT_EQ(stack.GetMin(), 1); // Минимальный элемент обновляется до 1 - stack.Push(3); // Stack [2, 1, 3] - ASSERT_EQ(stack.GetMin(), 1); // Минимальный элемент остается 1 - ASSERT_EQ(stack.Pop(), 3); // Stack [2, 1] - ASSERT_EQ(stack.GetMin(), 1); // Минимальный элемент остается 1 - ASSERT_EQ(stack.Pop(), 2); // Stack [2] - ASSERT_EQ(stack.GetMin(), 2); // Минимальный элемент обновляется до 2 - ASSERT_EQ(stack.Pop(), 2); // Stack [] + stack.Push(2); // Stack [2] + ASSERT_EQ(stack.GetMin(), 2); // Минимальный элемент равен 2 + stack.Push(1); // Stack [2, 1] + ASSERT_EQ(stack.GetMin(), 1); // Минимальный элемент обновляется до 1 + stack.Push(3); // Stack [2, 1, 3] + ASSERT_EQ(stack.GetMin(), 1); // Минимальный элемент остается 1 + ASSERT_EQ(stack.Pop(), 3); // Stack [2, 1] + ASSERT_EQ(stack.GetMin(), 1); // Минимальный элемент остается 1 + ASSERT_EQ(stack.Pop(), 2); // Stack [2] + ASSERT_EQ(stack.GetMin(), 2); // Минимальный элемент обновляется до 2 + ASSERT_EQ(stack.Pop(), 2); // Stack [] } diff --git a/task_03/src/test.cpp b/task_03/src/test.cpp index 83d11dc9..bfc66d77 100644 --- a/task_03/src/test.cpp +++ b/task_03/src/test.cpp @@ -1,4 +1,3 @@ - #include #include "topology_sort.hpp" diff --git a/task_03/src/topology_sort.cpp b/task_03/src/topology_sort.cpp index e5cdd927..00857c98 100644 --- a/task_03/src/topology_sort.cpp +++ b/task_03/src/topology_sort.cpp @@ -1,10 +1,11 @@ #include "topology_sort.hpp" + #include #include #include class Day { -public: + public: int number_of_day; int temperature; Day(int number_of_day, int temperature) { diff --git a/task_04/src/heap.cpp b/task_04/src/heap.cpp index 4ce7d04c..e2fa3cde 100644 --- a/task_04/src/heap.cpp +++ b/task_04/src/heap.cpp @@ -1,4 +1,5 @@ #include "heap.hpp" + #include #include @@ -7,10 +8,8 @@ void Heap::SiftDown(int i) { int left = 2 * i + 1; int right = 2 * i + 2; int j = left; - if ((right < vec_.size()) && vec_[right] < vec_[left]) - j = right; - if (vec_[i] <= vec_[j]) - break; + if ((right < vec_.size()) && vec_[right] < vec_[left]) j = right; + if (vec_[i] <= vec_[j]) break; std::swap(vec_[i], vec_[j]); i = j; } diff --git a/task_04/src/heap.hpp b/task_04/src/heap.hpp index 2191ddef..09774b9f 100644 --- a/task_04/src/heap.hpp +++ b/task_04/src/heap.hpp @@ -4,7 +4,7 @@ #include class Heap { -public: + public: void SiftDown(int i); void SiftUp(int i); void Insert(int value); @@ -13,7 +13,7 @@ class Heap { int Extract_Min(); std::vector Copy_Heap(); -private: + private: std::vector vec_; }; diff --git a/task_04/src/test.cpp b/task_04/src/test.cpp index 6f360013..bd7df10d 100644 --- a/task_04/src/test.cpp +++ b/task_04/src/test.cpp @@ -1,6 +1,7 @@ -#include "heap.hpp" #include +#include "heap.hpp" + // Простое тестирование TEST(heap, Simple) { ASSERT_EQ(Find_Minimum(std::vector{1, 3, 5, 6, 2, 4}), 1); diff --git a/task_05/src/sort.cpp b/task_05/src/sort.cpp index 7024d8e4..9bd56b21 100644 --- a/task_05/src/sort.cpp +++ b/task_05/src/sort.cpp @@ -1,4 +1,5 @@ #include "sort.hpp" + #include #include diff --git a/task_05/src/test.cpp b/task_05/src/test.cpp index dd11f1a9..9deacbf8 100644 --- a/task_05/src/test.cpp +++ b/task_05/src/test.cpp @@ -1,8 +1,9 @@ - -#include "sort.hpp" #include + #include +#include "sort.hpp" + TEST(Quick_Sort, Simple) { std::vector vec1{9, 8, 7, 6, 5, 4, 2, 3, 1, 0}; ASSERT_EQ(Quick_Sort(vec1, 0, 9), @@ -72,8 +73,8 @@ TEST(Quick_Sort, ZerosAndNegatives) { TEST(Quick_Sort, LargeArray) { std::vector vec(1000); std::iota(vec.begin(), vec.end(), - -500); // Заполнение вектора числами от -500 до 499 - std::random_shuffle(vec.begin(), vec.end()); // Перемешивание элементов + -500); // Заполнение вектора числами от -500 до 499 + std::random_shuffle(vec.begin(), vec.end()); // Перемешивание элементов std::vector sorted_vec(vec); std::sort(sorted_vec.begin(), sorted_vec.end()); ASSERT_EQ(Quick_Sort(vec, 0, vec.size() - 1), sorted_vec); diff --git a/task_06/src/k_stat.cpp b/task_06/src/k_stat.cpp index 388da328..9ea003b9 100644 --- a/task_06/src/k_stat.cpp +++ b/task_06/src/k_stat.cpp @@ -1,4 +1,5 @@ #include "k_stat.hpp" + #include #include #include diff --git a/task_06/src/test.cpp b/task_06/src/test.cpp index fd0a455c..e4d6c26c 100644 --- a/task_06/src/test.cpp +++ b/task_06/src/test.cpp @@ -1,8 +1,10 @@ -#include "k_stat.hpp" #include + #include #include +#include "k_stat.hpp" + // Простое тестирование TEST(k_stat, Simple) { std::vector vec1{14, -1, 5, 21, 6, -3, 7, 93, 9, 0, 84}; @@ -72,7 +74,7 @@ TEST(k_stat, OutOfBounds) { TEST(k_stat, LargeArray) { std::vector vec(1000); std::iota(vec.begin(), vec.end(), - 1); // Заполнение массива числами от 1 до 1000 - std::random_shuffle(vec.begin(), vec.end()); // Перемешивание элементов + 1); // Заполнение массива числами от 1 до 1000 + std::random_shuffle(vec.begin(), vec.end()); // Перемешивание элементов ASSERT_EQ(FindOrderStatistic(vec, 499), 484); } diff --git a/task_07/src/AVL_Tree.cpp b/task_07/src/AVL_Tree.cpp index 3819d8a2..046e0317 100644 --- a/task_07/src/AVL_Tree.cpp +++ b/task_07/src/AVL_Tree.cpp @@ -63,15 +63,13 @@ Node *AVL_Tree::Findmin(Node *right_subtree) { } Node *AVL_Tree::Removemin(Node *p) { - if (p->left == nullptr) - return p->right; + if (p->left == nullptr) return p->right; p->left = Removemin(p->left); return p; } Node *AVL_Tree::Remove(Node *p, int k) { - if (!p) - return nullptr; + if (!p) return nullptr; if (k < p->key) { p->left = Remove(p->left, k); } else if (k > p->key) { @@ -80,8 +78,7 @@ Node *AVL_Tree::Remove(Node *p, int k) { Node *p_left = p->left; Node *p_right = p->right; delete p; - if (!p_right) - return p_left; + if (!p_right) return p_left; Node *min = Findmin(p_right); min->right = Removemin(p_right); min->left = p_left; @@ -91,8 +88,7 @@ Node *AVL_Tree::Remove(Node *p, int k) { } bool AVL_Tree::Contains(Node *node, int value) { - if (!node) - return false; + if (!node) return false; if (value < node->key) { return Contains(node->left, value); } diff --git a/task_07/src/AVL_Tree.hpp b/task_07/src/AVL_Tree.hpp index 9beeb5dc..71983422 100644 --- a/task_07/src/AVL_Tree.hpp +++ b/task_07/src/AVL_Tree.hpp @@ -1,4 +1,5 @@ #include + #include using namespace std; @@ -15,12 +16,12 @@ struct Node { }; class AVL_Tree { -public: + public: void Insert(int value); void Remove(int value); bool Contains(int value); -private: + private: Node *root = nullptr; bool Contains(Node *node, int value); Node *Remove(Node *root, int k); diff --git a/task_07/src/test.cpp b/task_07/src/test.cpp index 7d227329..9fc8c4c7 100644 --- a/task_07/src/test.cpp +++ b/task_07/src/test.cpp @@ -1,4 +1,3 @@ - #include #include "AVL_Tree.hpp" diff --git a/task_08/src/hash_table.cpp b/task_08/src/hash_table.cpp index cc386912..8f33b493 100644 --- a/task_08/src/hash_table.cpp +++ b/task_08/src/hash_table.cpp @@ -1,6 +1,7 @@ #include "hash_table.hpp" #include + #include #include diff --git a/task_08/src/hash_table.hpp b/task_08/src/hash_table.hpp index cd07cfec..3dd2c70e 100644 --- a/task_08/src/hash_table.hpp +++ b/task_08/src/hash_table.hpp @@ -3,7 +3,7 @@ #include class HashTable { -public: + public: HashTable() { used_cell_ = 0; size_ = 0; @@ -22,7 +22,7 @@ class HashTable { size_t Size(); -private: + private: enum class Condition { Fill, Deleted, Empty }; constexpr static const double rehashCoefficient = 0.7; constexpr static const double hashCoefficient = 0.618033989; diff --git a/task_08/src/test.cpp b/task_08/src/test.cpp index 7374cb20..e4065198 100644 --- a/task_08/src/test.cpp +++ b/task_08/src/test.cpp @@ -1,4 +1,5 @@ #include + #include #include From 14c73de3e215ce176fdc5494e07b5def30ffb553 Mon Sep 17 00:00:00 2001 From: Matvey-cmd <143873323+Matvey-cmd@users.noreply.github.com> Date: Fri, 26 Apr 2024 15:40:55 +0000 Subject: [PATCH 28/42] format --- task_04/src/heap.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/task_04/src/heap.cpp b/task_04/src/heap.cpp index e2fa3cde..8c37bd3b 100644 --- a/task_04/src/heap.cpp +++ b/task_04/src/heap.cpp @@ -1,5 +1,4 @@ -#include "heap.hpp" - +#include #include #include From 80f88e96227205d8c98390b6d1d44e1b4827ba74 Mon Sep 17 00:00:00 2001 From: Matvey-cmd <143873323+Matvey-cmd@users.noreply.github.com> Date: Fri, 26 Apr 2024 15:45:26 +0000 Subject: [PATCH 29/42] format --- task_04/src/heap.hpp | 1 - task_07/src/AVL_Tree.hpp | 5 +---- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/task_04/src/heap.hpp b/task_04/src/heap.hpp index 09774b9f..02184b9d 100644 --- a/task_04/src/heap.hpp +++ b/task_04/src/heap.hpp @@ -1,5 +1,4 @@ #pragma once - #include #include diff --git a/task_07/src/AVL_Tree.hpp b/task_07/src/AVL_Tree.hpp index 71983422..9543f308 100644 --- a/task_07/src/AVL_Tree.hpp +++ b/task_07/src/AVL_Tree.hpp @@ -1,9 +1,6 @@ +#pragma once #include -#include - -using namespace std; - struct Node { int key; unsigned char height; From a767ecfe6d23ac2f3d50c6b05f4ea599937600d6 Mon Sep 17 00:00:00 2001 From: Matvey-cmd <143873323+Matvey-cmd@users.noreply.github.com> Date: Fri, 26 Apr 2024 15:46:45 +0000 Subject: [PATCH 30/42] format --- task_04/src/heap.cpp | 3 ++- task_04/src/main.cpp | 1 - 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/task_04/src/heap.cpp b/task_04/src/heap.cpp index 8c37bd3b..e2fa3cde 100644 --- a/task_04/src/heap.cpp +++ b/task_04/src/heap.cpp @@ -1,4 +1,5 @@ -#include +#include "heap.hpp" + #include #include diff --git a/task_04/src/main.cpp b/task_04/src/main.cpp index ea6d0016..0e4393ba 100644 --- a/task_04/src/main.cpp +++ b/task_04/src/main.cpp @@ -1,4 +1,3 @@ -#include "heap.hpp" #include int main() { return 0; } From f9880857da95da88e0b1bd26772722573195b918 Mon Sep 17 00:00:00 2001 From: Matvey-cmd <143873323+Matvey-cmd@users.noreply.github.com> Date: Sat, 11 May 2024 12:09:39 +0300 Subject: [PATCH 31/42] Update heap.cpp --- task_04/src/heap.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/task_04/src/heap.cpp b/task_04/src/heap.cpp index e2fa3cde..efb54a48 100644 --- a/task_04/src/heap.cpp +++ b/task_04/src/heap.cpp @@ -27,7 +27,7 @@ int Heap::Find_Min() { return vec_[0]; } int Heap::Extract_Min() { int min = vec_[0]; vec_[0] = vec_[vec_.size() - 1]; - vec_.erase(vec_.begin()); + vec_.pop_back(); SiftDown(0); return min; } @@ -55,4 +55,4 @@ std::vector Heap_Ready(std::vector vec) { Heap heap; heap.Build_heap(vec); return heap.Copy_Heap(); -} \ No newline at end of file +} From 38e38114cd4fb9d78d723233ee651aa8ad3710e6 Mon Sep 17 00:00:00 2001 From: Matvey-cmd <143873323+Matvey-cmd@users.noreply.github.com> Date: Sat, 11 May 2024 12:12:03 +0300 Subject: [PATCH 32/42] Update stack.cpp --- task_02/src/stack.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/task_02/src/stack.cpp b/task_02/src/stack.cpp index a3775978..25fda9b3 100644 --- a/task_02/src/stack.cpp +++ b/task_02/src/stack.cpp @@ -16,7 +16,7 @@ void Stack::Push(int value) { int Stack::Pop() { if (top == nullptr) { - throw std::logic_error("Stack Underflow"); + throw std::logic_error("Out_of_range"); } int val = top->value; top = top->next; From df9dde4cca1ec15f164d4a1a580e9f0dc3b15438 Mon Sep 17 00:00:00 2001 From: Matvey-cmd <143873323+Matvey-cmd@users.noreply.github.com> Date: Sat, 11 May 2024 12:13:06 +0300 Subject: [PATCH 33/42] Update topology_sort.hpp --- task_03/src/topology_sort.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/task_03/src/topology_sort.hpp b/task_03/src/topology_sort.hpp index d7b992e5..59aaad9e 100644 --- a/task_03/src/topology_sort.hpp +++ b/task_03/src/topology_sort.hpp @@ -6,4 +6,4 @@ class WrongVector : public std::runtime_error { using std::runtime_error::runtime_error; }; -std::vector Rise_Temperature(std::vector vec); +std::vector RiseTemperature(std::vector vec); From 824df0b14cb6b54e141b5ef9d306aacd852809e7 Mon Sep 17 00:00:00 2001 From: Matvey-cmd <143873323+Matvey-cmd@users.noreply.github.com> Date: Sat, 11 May 2024 12:13:37 +0300 Subject: [PATCH 34/42] Update topology_sort.cpp --- task_03/src/topology_sort.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/task_03/src/topology_sort.cpp b/task_03/src/topology_sort.cpp index 00857c98..c704bc70 100644 --- a/task_03/src/topology_sort.cpp +++ b/task_03/src/topology_sort.cpp @@ -14,7 +14,7 @@ class Day { } }; -std::vector Rise_Temperature(std::vector vec) { +std::vector RiseTemperature(std::vector vec) { int size = int(vec.size()); if (size == 0) { throw WrongVector("Wrong vector is too small"); @@ -37,4 +37,4 @@ std::vector Rise_Temperature(std::vector vec) { ++i; } return answer_vec; -} \ No newline at end of file +} From 6bd6dbfdcbd3209fc1e189dca10a84655f6f1cb3 Mon Sep 17 00:00:00 2001 From: Matvey-cmd <143873323+Matvey-cmd@users.noreply.github.com> Date: Sat, 11 May 2024 12:14:52 +0300 Subject: [PATCH 35/42] Update test.cpp --- task_03/src/test.cpp | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/task_03/src/test.cpp b/task_03/src/test.cpp index bfc66d77..b0b41a18 100644 --- a/task_03/src/test.cpp +++ b/task_03/src/test.cpp @@ -4,48 +4,48 @@ //Простые тесты TEST(TopologySort, Simple) { - ASSERT_EQ(Rise_Temperature(std::vector{1, 2, 3, 4, 5}), + ASSERT_EQ(RiseTemperature(std::vector{1, 2, 3, 4, 5}), (std::vector{1, 1, 1, 1, 0})); - ASSERT_EQ(Rise_Temperature(std::vector{5, 4, 3, 2, 1}), + ASSERT_EQ(RiseTemperature(std::vector{5, 4, 3, 2, 1}), (std::vector{0, 0, 0, 0, 0})); ASSERT_EQ( - Rise_Temperature(std::vector{-3, 0, 1, 23, 4, 5, 12, 1, 2, 1, 3}), + RiseTemperature(std::vector{-3, 0, 1, 23, 4, 5, 12, 1, 2, 1, 3}), (std::vector{1, 1, 1, 0, 1, 1, 0, 1, 2, 1, 0})); ASSERT_EQ( - Rise_Temperature(std::vector{12, 14, 2, 12, 11, 10, 0, 5, 3, 20, 4}), + RiseTemperature(std::vector{12, 14, 2, 12, 11, 10, 0, 5, 3, 20, 4}), (std::vector{1, 8, 1, 6, 5, 4, 1, 2, 1, 0, 0})); ASSERT_EQ( - Rise_Temperature(std::vector{11, -2, 3, 1, 5, 2, 6, 3, 7, 84, 4}), + RiseTemperature(std::vector{11, -2, 3, 1, 5, 2, 6, 3, 7, 84, 4}), (std::vector{9, 1, 2, 1, 2, 1, 2, 1, 1, 0, 0})); - ASSERT_EQ(Rise_Temperature(std::vector{1, 2, 3, 4, 5, 6, 7, 8, 9}), + ASSERT_EQ(RiseTemperature(std::vector{1, 2, 3, 4, 5, 6, 7, 8, 9}), (std::vector{1, 1, 1, 1, 1, 1, 1, 1, 0})); } // Тестирование поведения при убывающих температурах TEST(TopologySort, DecreasingTemperature) { - ASSERT_EQ(Rise_Temperature(std::vector{10, 9, 8, 7, 6}), + ASSERT_EQ(RiseTemperature(std::vector{10, 9, 8, 7, 6}), (std::vector{0, 0, 0, 0, 0})); } // Тестирование поведения при возрастающих температурах TEST(TopologySort, IncreasingTemperature) { - ASSERT_EQ(Rise_Temperature(std::vector{-5, -4, -3, -2, -1}), + ASSERT_EQ(RiseTemperature(std::vector{-5, -4, -3, -2, -1}), (std::vector{1, 1, 1, 1, 0})); } // Тестирование поведения с случайными температурами TEST(TopologySort, RandomTemperature) { - ASSERT_EQ(Rise_Temperature(std::vector{3, -1, 4, 1, 5}), + ASSERT_EQ(RiseTemperature(std::vector{3, -1, 4, 1, 5}), (std::vector{2, 1, 2, 1, 0})); } // Тестирование поведения с пустым вектором TEST(TopologySort, EmptyVector) { - EXPECT_THROW(Rise_Temperature(std::vector{}), WrongVector); + EXPECT_THROW(RiseTemperature(std::vector{}), WrongVector); } // Тестирование поведения с максимальными и минимальными значениями TEST(TopologySort, ExtremeValues) { - ASSERT_EQ(Rise_Temperature(std::vector{-2, 0, 5}), + ASSERT_EQ(RiseTemperature(std::vector{-2, 0, 5}), (std::vector{1, 1, 0})); } From b08e5f53c818032cc7a38e8bdfea5f7c372855a8 Mon Sep 17 00:00:00 2001 From: Matvey-cmd <143873323+Matvey-cmd@users.noreply.github.com> Date: Sat, 11 May 2024 12:17:11 +0300 Subject: [PATCH 36/42] Update topology_sort.cpp --- task_03/src/topology_sort.cpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/task_03/src/topology_sort.cpp b/task_03/src/topology_sort.cpp index c704bc70..000276d2 100644 --- a/task_03/src/topology_sort.cpp +++ b/task_03/src/topology_sort.cpp @@ -8,10 +8,7 @@ class Day { public: int number_of_day; int temperature; - Day(int number_of_day, int temperature) { - this->number_of_day = number_of_day; - this->temperature = temperature; - } + Day(int number_of_day, int temperature): number_of_day(number_of_day), temperature(temperature) {} }; std::vector RiseTemperature(std::vector vec) { From 955f0eb85eb7b2a53d4c88a3ff167f420fc0d32b Mon Sep 17 00:00:00 2001 From: Matvey-cmd <143873323+Matvey-cmd@users.noreply.github.com> Date: Sat, 11 May 2024 12:22:08 +0300 Subject: [PATCH 37/42] Update topology_sort.cpp --- task_03/src/topology_sort.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/task_03/src/topology_sort.cpp b/task_03/src/topology_sort.cpp index 000276d2..2b1706dc 100644 --- a/task_03/src/topology_sort.cpp +++ b/task_03/src/topology_sort.cpp @@ -8,7 +8,7 @@ class Day { public: int number_of_day; int temperature; - Day(int number_of_day, int temperature): number_of_day(number_of_day), temperature(temperature) {} + Day(int number_of_day, int temperature) : number_of_day(number_of_day), temperature(temperature) {} }; std::vector RiseTemperature(std::vector vec) { From 03426638c74d3fce782ffd4dd53587c3509fd5f0 Mon Sep 17 00:00:00 2001 From: Matvey-cmd <143873323+Matvey-cmd@users.noreply.github.com> Date: Sun, 19 May 2024 22:15:02 +0000 Subject: [PATCH 38/42] fix some shortcomings --- task_02/src/stack.cpp | 2 +- task_03/src/topology_sort.cpp | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/task_02/src/stack.cpp b/task_02/src/stack.cpp index 25fda9b3..7481ba1a 100644 --- a/task_02/src/stack.cpp +++ b/task_02/src/stack.cpp @@ -40,7 +40,7 @@ void MinStack::Push(int value) { int MinStack::Pop() { if (st1_.top == nullptr) { - throw std::logic_error("Stack Underflow"); + throw std::logic_error("Out_of_range"); } int val = st1_.top->value; diff --git a/task_03/src/topology_sort.cpp b/task_03/src/topology_sort.cpp index 2b1706dc..d6855edc 100644 --- a/task_03/src/topology_sort.cpp +++ b/task_03/src/topology_sort.cpp @@ -8,7 +8,8 @@ class Day { public: int number_of_day; int temperature; - Day(int number_of_day, int temperature) : number_of_day(number_of_day), temperature(temperature) {} + Day(int number_of_day, int temperature) + : number_of_day(number_of_day), temperature(temperature) {} }; std::vector RiseTemperature(std::vector vec) { From cd65251f101b3c002d78a2864d185fed4a90e4b2 Mon Sep 17 00:00:00 2001 From: Matvey-cmd <143873323+Matvey-cmd@users.noreply.github.com> Date: Fri, 14 Jun 2024 16:36:18 +0000 Subject: [PATCH 39/42] corrected tasks --- task_02/src/stack.cpp | 7 +++--- task_02/src/test.cpp | 1 - task_04/src/heap.cpp | 38 +++++++++++++++--------------- task_04/src/heap.hpp | 12 +++++----- task_04/src/test.cpp | 50 ++++++++++++++++++++-------------------- task_07/src/AVL_Tree.cpp | 6 ++--- task_09/src/test.cpp | 5 +++- 7 files changed, 60 insertions(+), 59 deletions(-) diff --git a/task_02/src/stack.cpp b/task_02/src/stack.cpp index 7481ba1a..ce939fcc 100644 --- a/task_02/src/stack.cpp +++ b/task_02/src/stack.cpp @@ -4,14 +4,13 @@ #include void Stack::Push(int value) { - std::shared_ptr p(new Node(value)); + auto p = std::make_shared(value); if (top == nullptr) { top = p; - } - if (top != nullptr) { + } else { p->next = top; + top = p; } - top = p; } int Stack::Pop() { diff --git a/task_02/src/test.cpp b/task_02/src/test.cpp index 1bb722e3..53330dab 100644 --- a/task_02/src/test.cpp +++ b/task_02/src/test.cpp @@ -87,5 +87,4 @@ TEST(MinStackTest, Behavior) { ASSERT_EQ(stack.GetMin(), 1); // Минимальный элемент остается 1 ASSERT_EQ(stack.Pop(), 2); // Stack [2] ASSERT_EQ(stack.GetMin(), 2); // Минимальный элемент обновляется до 2 - ASSERT_EQ(stack.Pop(), 2); // Stack [] } diff --git a/task_04/src/heap.cpp b/task_04/src/heap.cpp index efb54a48..4b617806 100644 --- a/task_04/src/heap.cpp +++ b/task_04/src/heap.cpp @@ -3,15 +3,15 @@ #include #include -void Heap::SiftDown(int i) { - while (2 * i + 1 < vec_.size()) { - int left = 2 * i + 1; - int right = 2 * i + 2; - int j = left; - if ((right < vec_.size()) && vec_[right] < vec_[left]) j = right; - if (vec_[i] <= vec_[j]) break; - std::swap(vec_[i], vec_[j]); - i = j; +void Heap::SiftDown(int cur_node) { + while (2 * cur_node + 1 < vec_.size()) { + int left = 2 * cur_node + 1; + int right = 2 * cur_node + 2; + int next_node = left; + if ((right < vec_.size()) && vec_[right] < vec_[left]) next_node = right; + if (vec_[cur_node] <= vec_[next_node]) break; + std::swap(vec_[cur_node], vec_[next_node]); + cur_node = next_node; } } @@ -22,9 +22,9 @@ void Heap::SiftUp(int i) { } } -int Heap::Find_Min() { return vec_[0]; } +int Heap::FindMin() { return vec_[0]; } -int Heap::Extract_Min() { +int Heap::ExtractMin() { int min = vec_[0]; vec_[0] = vec_[vec_.size() - 1]; vec_.pop_back(); @@ -32,27 +32,27 @@ int Heap::Extract_Min() { return min; } -std::vector Heap::Copy_Heap() { return vec_; } +std::vector Heap::CopyHeap() { return vec_; } void Heap::Insert(int value) { vec_.push_back(value); SiftUp(vec_.size() - 1); } -void Heap::Build_heap(std::vector vec) { +void Heap::BuildHeap(std::vector vec) { for (int i = 0; i < vec.size(); ++i) { Insert(vec[i]); } } -int Find_Minimum(std::vector vec) { +int FindMinimum(std::vector vec) { Heap heap; - heap.Build_heap(vec); - return heap.Find_Min(); + heap.BuildHeap(vec); + return heap.FindMin(); } -std::vector Heap_Ready(std::vector vec) { +std::vector HeapReady(std::vector vec) { Heap heap; - heap.Build_heap(vec); - return heap.Copy_Heap(); + heap.BuildHeap(vec); + return heap.CopyHeap(); } diff --git a/task_04/src/heap.hpp b/task_04/src/heap.hpp index 02184b9d..afaae69e 100644 --- a/task_04/src/heap.hpp +++ b/task_04/src/heap.hpp @@ -7,15 +7,15 @@ class Heap { void SiftDown(int i); void SiftUp(int i); void Insert(int value); - int Find_Min(); - void Build_heap(std::vector vec); - int Extract_Min(); - std::vector Copy_Heap(); + int FindMin(); + void BuildHeap(std::vector vec); + int ExtractMin(); + std::vector CopyHeap(); private: std::vector vec_; }; -int Find_Minimum(std::vector vec); +int FindMinimum(std::vector vec); -std::vector Heap_Ready(std::vector heap); \ No newline at end of file +std::vector HeapReady(std::vector heap); \ No newline at end of file diff --git a/task_04/src/test.cpp b/task_04/src/test.cpp index bd7df10d..27dbf2d2 100644 --- a/task_04/src/test.cpp +++ b/task_04/src/test.cpp @@ -4,31 +4,31 @@ // Простое тестирование TEST(heap, Simple) { - ASSERT_EQ(Find_Minimum(std::vector{1, 3, 5, 6, 2, 4}), 1); - ASSERT_EQ(Find_Minimum(std::vector{11, 37, 55, 34, -6, 0, 4}), -6); - ASSERT_EQ(Heap_Ready(std::vector{4, 1, 3, 2, 5}), + ASSERT_EQ(FindMinimum(std::vector{1, 3, 5, 6, 2, 4}), 1); + ASSERT_EQ(FindMinimum(std::vector{11, 37, 55, 34, -6, 0, 4}), -6); + ASSERT_EQ(HeapReady(std::vector{4, 1, 3, 2, 5}), (std::vector{1, 2, 3, 4, 5})); - ASSERT_EQ(Heap_Ready(std::vector{3, 1, 5, 6, 2, 4}), + ASSERT_EQ(HeapReady(std::vector{3, 1, 5, 6, 2, 4}), (std::vector{1, 2, 4, 6, 3, 5})); Heap heap; - heap.Build_heap(std::vector{1, 3, 5, 7, 9, 12, 324, 5, 47, 457, 9467, -4, - 758, -579, -4, 0}); - ASSERT_EQ(heap.Find_Min(), -579); + heap.BuildHeap(std::vector{1, 3, 5, 7, 9, 12, 324, 5, 47, 457, 9467, -4, + 758, -579, -4, 0}); + ASSERT_EQ(heap.FindMin(), -579); heap.Insert(23); heap.Insert(-1000); - ASSERT_EQ(heap.Extract_Min(), -1000); - ASSERT_EQ(heap.Find_Min(), -579); + ASSERT_EQ(heap.ExtractMin(), -1000); + ASSERT_EQ(heap.FindMin(), -579); } // Тестирование поведения кучи при вставке элементов TEST(HeapTest, Insert) { Heap heap; heap.Insert(5); - ASSERT_EQ(heap.Find_Min(), 5); + ASSERT_EQ(heap.FindMin(), 5); heap.Insert(3); - ASSERT_EQ(heap.Find_Min(), 3); + ASSERT_EQ(heap.FindMin(), 3); heap.Insert(8); - ASSERT_EQ(heap.Find_Min(), 3); + ASSERT_EQ(heap.FindMin(), 3); } // Тестирование поведения кучи при удалении элементов @@ -37,10 +37,10 @@ TEST(HeapTest, ExtractMin) { heap.Insert(5); heap.Insert(3); heap.Insert(8); - ASSERT_EQ(heap.Extract_Min(), 3); - ASSERT_EQ(heap.Find_Min(), 5); - ASSERT_EQ(heap.Extract_Min(), 5); - ASSERT_EQ(heap.Find_Min(), 8); + ASSERT_EQ(heap.ExtractMin(), 3); + ASSERT_EQ(heap.FindMin(), 5); + ASSERT_EQ(heap.ExtractMin(), 5); + ASSERT_EQ(heap.FindMin(), 8); } // Тестирование поведения кучи с отрицательными числами @@ -49,9 +49,9 @@ TEST(HeapTest, NegativeNumbers) { heap.Insert(-5); heap.Insert(-3); heap.Insert(-8); - ASSERT_EQ(heap.Find_Min(), -8); - ASSERT_EQ(heap.Extract_Min(), -8); - ASSERT_EQ(heap.Find_Min(), -5); + ASSERT_EQ(heap.FindMin(), -8); + ASSERT_EQ(heap.ExtractMin(), -8); + ASSERT_EQ(heap.FindMin(), -5); } // Тестирование поведения кучи с большим количеством элементов @@ -60,17 +60,17 @@ TEST(HeapTest, LargeHeap) { for (int i = 0; i < 1000; ++i) { heap.Insert(i); } - ASSERT_EQ(heap.Find_Min(), 0); + ASSERT_EQ(heap.FindMin(), 0); for (int i = 0; i < 1000; ++i) { - ASSERT_EQ(heap.Extract_Min(), i); + ASSERT_EQ(heap.ExtractMin(), i); } } // Тестирование поведения кучи при построении из вектора TEST(HeapTest, BuildHeap) { Heap heap; - heap.Build_heap(std::vector{5, 3, 8, 1, 2, 9}); - ASSERT_EQ(heap.Find_Min(), 1); - ASSERT_EQ(heap.Extract_Min(), 1); - ASSERT_EQ(heap.Find_Min(), 2); + heap.BuildHeap(std::vector{5, 3, 8, 1, 2, 9}); + ASSERT_EQ(heap.FindMin(), 1); + ASSERT_EQ(heap.ExtractMin(), 1); + ASSERT_EQ(heap.FindMin(), 2); } diff --git a/task_07/src/AVL_Tree.cpp b/task_07/src/AVL_Tree.cpp index 046e0317..f17e01cc 100644 --- a/task_07/src/AVL_Tree.cpp +++ b/task_07/src/AVL_Tree.cpp @@ -7,9 +7,9 @@ int AVL_Tree::DifferenceH(Node *p) { } void AVL_Tree::Fixheight(Node *p) { - unsigned char hl = Height(p->left); - unsigned char hr = Height(p->right); - p->height = (hl > hr ? hl : hr) + 1; + unsigned char height_l = Height(p->left); + unsigned char height_r = Height(p->right); + p->height = (height_l > height_r ? height_l : height_r) + 1; } Node *AVL_Tree::Insert(Node *root, int k) { diff --git a/task_09/src/test.cpp b/task_09/src/test.cpp index 24b6fade..5572e070 100644 --- a/task_09/src/test.cpp +++ b/task_09/src/test.cpp @@ -4,10 +4,13 @@ #include "minimum_moneys.hpp" +TEST(MinMoney, GreedyAlgorithmNotWorkHere){ + ASSERT_EQ(MinMoney(std::vector{1, 3, 4}, 6), (2)); +} + TEST(MinMoney, Simple) { ASSERT_EQ(MinMoney(std::vector{1, 2, 5, 10}, 14), (3)); ASSERT_EQ(MinMoney(std::vector{1, 2, 5, 10}, 19), (4)); - ASSERT_EQ(MinMoney(std::vector{1, 3, 4}, 6), (2)); ASSERT_EQ(MinMoney(std::vector{1, 2, 3, 4, 5, 6, 7, 8, 9}, 152), (17)); ASSERT_EQ(MinMoney(std::vector{1, 3, 4}, 34), (9)); ASSERT_EQ(MinMoney(std::vector{1, 2, 6, 7}, 12), (2)); From 5b802c4a67b001c28a8da51e458094e187d71761 Mon Sep 17 00:00:00 2001 From: Matvey-cmd <143873323+Matvey-cmd@users.noreply.github.com> Date: Fri, 14 Jun 2024 16:47:18 +0000 Subject: [PATCH 40/42] right format --- task_09/src/test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/task_09/src/test.cpp b/task_09/src/test.cpp index 5572e070..552c2dee 100644 --- a/task_09/src/test.cpp +++ b/task_09/src/test.cpp @@ -4,7 +4,7 @@ #include "minimum_moneys.hpp" -TEST(MinMoney, GreedyAlgorithmNotWorkHere){ +TEST(MinMoney, GreedyAlgorithmNotWorkHere) { ASSERT_EQ(MinMoney(std::vector{1, 3, 4}, 6), (2)); } From 61b600dce9775ad404798caed81d85630374c858 Mon Sep 17 00:00:00 2001 From: Matvey-cmd <143873323+Matvey-cmd@users.noreply.github.com> Date: Sat, 15 Jun 2024 22:46:09 +0000 Subject: [PATCH 41/42] All problem fixed --- task_02/src/stack.cpp | 28 ++++++++++++------------- task_02/src/stack.hpp | 4 ++-- task_05/src/sort.cpp | 6 +++--- task_05/src/sort.hpp | 2 +- task_05/src/test.cpp | 42 +++++++++++++++++++------------------- task_07/src/AVL_Tree.cpp | 6 +++--- task_07/src/AVL_Tree.hpp | 2 +- task_08/src/hash_table.cpp | 14 +++---------- 8 files changed, 48 insertions(+), 56 deletions(-) diff --git a/task_02/src/stack.cpp b/task_02/src/stack.cpp index ce939fcc..e0772608 100644 --- a/task_02/src/stack.cpp +++ b/task_02/src/stack.cpp @@ -15,7 +15,7 @@ void Stack::Push(int value) { int Stack::Pop() { if (top == nullptr) { - throw std::logic_error("Out_of_range"); + throw std::logic_error("Stack_is_empty"); } int val = top->value; top = top->next; @@ -23,29 +23,29 @@ int Stack::Pop() { } void MinStack::Push(int value) { - if (st1_.top == nullptr) { - st1_.Push(value); - st2_.Push(value); + if (stack_.top == nullptr) { + stack_.Push(value); + min_stack_.Push(value); return; } - if (st1_.top->value > value) { - st2_.Push(value); + if (stack_.top->value > value) { + min_stack_.Push(value); } else { - st1_.Push(value); - st2_.Push(st2_.top->value); + stack_.Push(value); + min_stack_.Push(min_stack_.top->value); } } int MinStack::Pop() { - if (st1_.top == nullptr) { - throw std::logic_error("Out_of_range"); + if (stack_.top == nullptr) { + throw std::logic_error("Stack_is_empty"); } - int val = st1_.top->value; - st1_.top = st1_.top->next; - st2_.top = st2_.top->next; + int val = stack_.top->value; + stack_.top = stack_.top->next; + min_stack_.top = min_stack_.top->next; return val; } -int MinStack::GetMin() { return st2_.top->value; } +int MinStack::GetMin() { return min_stack_.top->value; } diff --git a/task_02/src/stack.hpp b/task_02/src/stack.hpp index 64dbd9fe..9c42ef71 100644 --- a/task_02/src/stack.hpp +++ b/task_02/src/stack.hpp @@ -23,6 +23,6 @@ class MinStack { int GetMin(); private: - Stack st1_; - Stack st2_; + Stack stack_; + Stack min_stack_; }; diff --git a/task_05/src/sort.cpp b/task_05/src/sort.cpp index 9bd56b21..636bf0fc 100644 --- a/task_05/src/sort.cpp +++ b/task_05/src/sort.cpp @@ -16,11 +16,11 @@ int Partition(std::vector &arr, int low, int high) { return (i + 1); } -std::vector Quick_Sort(std::vector &arr, int low, int high) { +std::vector QuickSort(std::vector &arr, int low, int high) { if (low < high) { int m = Partition(arr, low, high); - Quick_Sort(arr, low, m - 1); - Quick_Sort(arr, m + 1, high); + QuickSort(arr, low, m - 1); + QuickSort(arr, m + 1, high); } return arr; } \ No newline at end of file diff --git a/task_05/src/sort.hpp b/task_05/src/sort.hpp index f4ed6f5f..7ed3881a 100644 --- a/task_05/src/sort.hpp +++ b/task_05/src/sort.hpp @@ -4,4 +4,4 @@ int Partition(std::vector &arr, int low, int high); -std::vector Quick_Sort(std::vector &arr, int low, int high); \ No newline at end of file +std::vector QuickSort(std::vector &arr, int low, int high); \ No newline at end of file diff --git a/task_05/src/test.cpp b/task_05/src/test.cpp index 9deacbf8..6d0feb14 100644 --- a/task_05/src/test.cpp +++ b/task_05/src/test.cpp @@ -4,78 +4,78 @@ #include "sort.hpp" -TEST(Quick_Sort, Simple) { +TEST(QuickSort, Simple) { std::vector vec1{9, 8, 7, 6, 5, 4, 2, 3, 1, 0}; - ASSERT_EQ(Quick_Sort(vec1, 0, 9), + ASSERT_EQ(QuickSort(vec1, 0, 9), (std::vector{0, 1, 2, 3, 4, 5, 6, 7, 8, 9})); std::vector vec2{10, 90, 30, 80, 60, 50}; - ASSERT_EQ(Quick_Sort(vec2, 0, 5), (std::vector{10, 30, 50, 60, 80, 90})); + ASSERT_EQ(QuickSort(vec2, 0, 5), (std::vector{10, 30, 50, 60, 80, 90})); std::vector vec3{12, 43, 15, 26, -1233, 346, 1325, -56, -12, 78, 0, 3345, -34}; - ASSERT_EQ(Quick_Sort(vec3, 0, 12), + ASSERT_EQ(QuickSort(vec3, 0, 12), (std::vector{-1233, -56, -34, -12, 0, 12, 15, 26, 43, 78, 346, 1325, 3345})); vec3.push_back(-10); vec3.push_back(1000); vec3.push_back(5); - ASSERT_EQ(Quick_Sort(vec3, 0, 15), + ASSERT_EQ(QuickSort(vec3, 0, 15), (std::vector{-1233, -56, -34, -12, -10, 0, 5, 12, 15, 26, 43, 78, 346, 1000, 1325, 3345})); } // Тестирование сортировки пустого вектора -TEST(Quick_Sort, EmptyVector) { +TEST(QuickSort, EmptyVector) { std::vector vec; - ASSERT_EQ(Quick_Sort(vec, 0, vec.size() - 1), (std::vector{})); + ASSERT_EQ(QuickSort(vec, 0, vec.size() - 1), (std::vector{})); } // Тестирование сортировки вектора с одним элементом -TEST(Quick_Sort, SingleElement) { +TEST(QuickSort, SingleElement) { std::vector vec{1}; - ASSERT_EQ(Quick_Sort(vec, 0, vec.size() - 1), (std::vector{1})); + ASSERT_EQ(QuickSort(vec, 0, vec.size() - 1), (std::vector{1})); } // Тестирование сортировки вектора с повторяющимися элементами -TEST(Quick_Sort, RepeatedElements) { +TEST(QuickSort, RepeatedElements) { std::vector vec{5, 5, 5, 5}; - ASSERT_EQ(Quick_Sort(vec, 0, vec.size() - 1), (std::vector{5, 5, 5, 5})); + ASSERT_EQ(QuickSort(vec, 0, vec.size() - 1), (std::vector{5, 5, 5, 5})); } // Тестирование сортировки вектора с отрицательными и положительными числами -TEST(Quick_Sort, MixedNumbers) { +TEST(QuickSort, MixedNumbers) { std::vector vec{-1, 3, -2, 5, 4, -3}; - ASSERT_EQ(Quick_Sort(vec, 0, vec.size() - 1), + ASSERT_EQ(QuickSort(vec, 0, vec.size() - 1), (std::vector{-3, -2, -1, 3, 4, 5})); } // Тестирование сортировки вектора с уже отсортированными элементами -TEST(Quick_Sort, AlreadySorted) { +TEST(QuickSort, AlreadySorted) { std::vector vec{1, 2, 3, 4, 5}; - ASSERT_EQ(Quick_Sort(vec, 0, vec.size() - 1), + ASSERT_EQ(QuickSort(vec, 0, vec.size() - 1), (std::vector{1, 2, 3, 4, 5})); } // Тестирование сортировки вектора с элементами в обратном порядке -TEST(Quick_Sort, ReverseOrder) { +TEST(QuickSort, ReverseOrder) { std::vector vec{5, 4, 3, 2, 1}; - ASSERT_EQ(Quick_Sort(vec, 0, vec.size() - 1), + ASSERT_EQ(QuickSort(vec, 0, vec.size() - 1), (std::vector{1, 2, 3, 4, 5})); } // Тестирование сортировки вектора с нулями и отрицательными числами -TEST(Quick_Sort, ZerosAndNegatives) { +TEST(QuickSort, ZerosAndNegatives) { std::vector vec{0, -1, 0, -2, 0, -3}; - ASSERT_EQ(Quick_Sort(vec, 0, vec.size() - 1), + ASSERT_EQ(QuickSort(vec, 0, vec.size() - 1), (std::vector{-3, -2, -1, 0, 0, 0})); } // Тестирование сортировки вектора с большим количеством элементов -TEST(Quick_Sort, LargeArray) { +TEST(QuickSort, LargeArray) { std::vector vec(1000); std::iota(vec.begin(), vec.end(), -500); // Заполнение вектора числами от -500 до 499 std::random_shuffle(vec.begin(), vec.end()); // Перемешивание элементов std::vector sorted_vec(vec); std::sort(sorted_vec.begin(), sorted_vec.end()); - ASSERT_EQ(Quick_Sort(vec, 0, vec.size() - 1), sorted_vec); + ASSERT_EQ(QuickSort(vec, 0, vec.size() - 1), sorted_vec); } diff --git a/task_07/src/AVL_Tree.cpp b/task_07/src/AVL_Tree.cpp index f17e01cc..e650f097 100644 --- a/task_07/src/AVL_Tree.cpp +++ b/task_07/src/AVL_Tree.cpp @@ -62,9 +62,9 @@ Node *AVL_Tree::Findmin(Node *right_subtree) { return right_subtree->left ? Findmin(right_subtree->left) : right_subtree; } -Node *AVL_Tree::Removemin(Node *p) { +Node *AVL_Tree::RemoveMin(Node *p) { if (p->left == nullptr) return p->right; - p->left = Removemin(p->left); + p->left = RemoveMin(p->left); return p; } @@ -80,7 +80,7 @@ Node *AVL_Tree::Remove(Node *p, int k) { delete p; if (!p_right) return p_left; Node *min = Findmin(p_right); - min->right = Removemin(p_right); + min->right = RemoveMin(p_right); min->left = p_left; return Balance(min); } diff --git a/task_07/src/AVL_Tree.hpp b/task_07/src/AVL_Tree.hpp index 9543f308..058f1448 100644 --- a/task_07/src/AVL_Tree.hpp +++ b/task_07/src/AVL_Tree.hpp @@ -27,7 +27,7 @@ class AVL_Tree { Node *Rotateleft(Node *q); Node *Balance(Node *p); Node *Findmin(Node *right_subtree); - Node *Removemin(Node *p); + Node *RemoveMin(Node *p); unsigned char Height(Node *p); int DifferenceH(Node *p); void Fixheight(Node *p); diff --git a/task_08/src/hash_table.cpp b/task_08/src/hash_table.cpp index 8f33b493..5b331ba5 100644 --- a/task_08/src/hash_table.cpp +++ b/task_08/src/hash_table.cpp @@ -8,20 +8,12 @@ size_t HashTable::Size() { return size_; } size_t HashTable::FirstHashFunc(int key) { - if constexpr (std::is_arithmetic::value) { - return floor(buffers_size_ * - ((key * hashCoefficient) - floor(key * hashCoefficient))); - } - throw std::invalid_argument( - "Hash table cannot cannot work with it with an arithmetic data type"); + return floor(buffers_size_ * + ((key * hashCoefficient) - floor(key * hashCoefficient))); } size_t HashTable::SecondHashFunc(int key) { - if constexpr (std::is_arithmetic::value) { - return (key * buffers_size_ - 1) % buffers_size_; - } - throw std::invalid_argument( - "Hash table cannot cannot work with it with an arithmetic data type"); + return (key * buffers_size_ - 1) % buffers_size_; } void HashTable::Clear() { From d8efd065c13b49df16da73cf8199cc2d86414af0 Mon Sep 17 00:00:00 2001 From: Matvey-cmd <143873323+Matvey-cmd@users.noreply.github.com> Date: Sun, 16 Jun 2024 22:43:22 +0000 Subject: [PATCH 42/42] last fix --- task_02/src/stack.cpp | 4 ++-- task_07/src/AVL_Tree.cpp | 24 ++++++++++++------------ task_07/src/AVL_Tree.hpp | 6 +++--- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/task_02/src/stack.cpp b/task_02/src/stack.cpp index e0772608..07b091de 100644 --- a/task_02/src/stack.cpp +++ b/task_02/src/stack.cpp @@ -15,7 +15,7 @@ void Stack::Push(int value) { int Stack::Pop() { if (top == nullptr) { - throw std::logic_error("Stack_is_empty"); + throw std::logic_error("out_of_range"); } int val = top->value; top = top->next; @@ -39,7 +39,7 @@ void MinStack::Push(int value) { int MinStack::Pop() { if (stack_.top == nullptr) { - throw std::logic_error("Stack_is_empty"); + throw std::logic_error("out_of_range"); } int val = stack_.top->value; diff --git a/task_07/src/AVL_Tree.cpp b/task_07/src/AVL_Tree.cpp index e650f097..69a9a87f 100644 --- a/task_07/src/AVL_Tree.cpp +++ b/task_07/src/AVL_Tree.cpp @@ -6,7 +6,7 @@ int AVL_Tree::DifferenceH(Node *p) { return Height(p->right) - Height(p->left); } -void AVL_Tree::Fixheight(Node *p) { +void AVL_Tree::FixHeight(Node *p) { unsigned char height_l = Height(p->left); unsigned char height_r = Height(p->right); p->height = (height_l > height_r ? height_l : height_r) + 1; @@ -27,39 +27,39 @@ Node *AVL_Tree::RotateRight(Node *p) { Node *q = p->left; p->left = q->right; q->right = p; - Fixheight(p); - Fixheight(q); + FixHeight(p); + FixHeight(q); return q; } -Node *AVL_Tree::Rotateleft(Node *q) { +Node *AVL_Tree::RotateLeft(Node *q) { Node *p = q->right; q->right = p->left; p->left = q; - Fixheight(q); - Fixheight(p); + FixHeight(q); + FixHeight(p); return p; } Node *AVL_Tree::Balance(Node *p) { - Fixheight(p); + FixHeight(p); if (DifferenceH(p) == 2) { if (DifferenceH(p->right) < 0) { p->right = RotateRight(p->right); } - return Rotateleft(p); + return RotateLeft(p); } if (DifferenceH(p) == -2) { if (DifferenceH(p->left) > 0) { - p->left = Rotateleft(p->left); + p->left = RotateLeft(p->left); } return RotateRight(p); } return p; } -Node *AVL_Tree::Findmin(Node *right_subtree) { - return right_subtree->left ? Findmin(right_subtree->left) : right_subtree; +Node *AVL_Tree::FindMin(Node *right_subtree) { + return right_subtree->left ? FindMin(right_subtree->left) : right_subtree; } Node *AVL_Tree::RemoveMin(Node *p) { @@ -79,7 +79,7 @@ Node *AVL_Tree::Remove(Node *p, int k) { Node *p_right = p->right; delete p; if (!p_right) return p_left; - Node *min = Findmin(p_right); + Node *min = FindMin(p_right); min->right = RemoveMin(p_right); min->left = p_left; return Balance(min); diff --git a/task_07/src/AVL_Tree.hpp b/task_07/src/AVL_Tree.hpp index 058f1448..1c0a8f5c 100644 --- a/task_07/src/AVL_Tree.hpp +++ b/task_07/src/AVL_Tree.hpp @@ -24,11 +24,11 @@ class AVL_Tree { Node *Remove(Node *root, int k); Node *Insert(Node *root, int k); Node *RotateRight(Node *p); - Node *Rotateleft(Node *q); + Node *RotateLeft(Node *q); Node *Balance(Node *p); - Node *Findmin(Node *right_subtree); + Node *FindMin(Node *right_subtree); Node *RemoveMin(Node *p); unsigned char Height(Node *p); int DifferenceH(Node *p); - void Fixheight(Node *p); + void FixHeight(Node *p); };