From 8bea4972c7041b1c5b78eb5c2d8b2d806b707565 Mon Sep 17 00:00:00 2001 From: dalnoboy75 <39648068+dalnoboy75@users.noreply.github.com> Date: Sat, 30 Mar 2024 12:19:00 +0000 Subject: [PATCH 01/14] task 2-4 --- task_01/src/test.cpp | 2 +- task_01/src/topology_sort.cpp | 1 - task_01/src/utils.cpp | 1 + task_01/src/{topology_sort.hpp => utils.hpp} | 0 task_02/src/main.cpp | 1 + task_02/src/stack.cpp | 34 +++++++++++---- task_02/src/stack.hpp | 20 ++++----- task_03/src/temp_up_days.cpp | 23 ++++++++++ task_03/src/temp_up_days.hpp | 9 ++++ task_03/src/test.cpp | 11 +++-- task_03/src/topology_sort.cpp | 1 - task_03/src/topology_sort.hpp | 1 - task_04/src/heap.cpp | 44 ++++++++++++++++++++ task_04/src/heap.hpp | 14 +++++++ task_04/src/test.cpp | 9 ++-- 15 files changed, 142 insertions(+), 29 deletions(-) delete mode 100644 task_01/src/topology_sort.cpp create mode 100644 task_01/src/utils.cpp rename task_01/src/{topology_sort.hpp => utils.hpp} (100%) create mode 100644 task_03/src/temp_up_days.cpp create mode 100644 task_03/src/temp_up_days.hpp delete mode 100644 task_03/src/topology_sort.cpp delete mode 100644 task_03/src/topology_sort.hpp create mode 100644 task_04/src/heap.cpp create mode 100644 task_04/src/heap.hpp diff --git a/task_01/src/test.cpp b/task_01/src/test.cpp index ef5a86ae..babf52ce 100644 --- a/task_01/src/test.cpp +++ b/task_01/src/test.cpp @@ -1,7 +1,7 @@ #include -#include "topology_sort.hpp" +#include "utils.hpp" TEST(TopologySort, Simple) { ASSERT_EQ(1, 1); // Stack [] 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/utils.cpp b/task_01/src/utils.cpp new file mode 100644 index 00000000..0ee624c5 --- /dev/null +++ b/task_01/src/utils.cpp @@ -0,0 +1 @@ +#include "utils.hpp" diff --git a/task_01/src/topology_sort.hpp b/task_01/src/utils.hpp similarity index 100% rename from task_01/src/topology_sort.hpp rename to task_01/src/utils.hpp diff --git a/task_02/src/main.cpp b/task_02/src/main.cpp index 0e4393ba..87ff7786 100644 --- a/task_02/src/main.cpp +++ b/task_02/src/main.cpp @@ -1,3 +1,4 @@ #include +#include "stack.hpp" int main() { return 0; } diff --git a/task_02/src/stack.cpp b/task_02/src/stack.cpp index 8ca89902..a73b31a0 100644 --- a/task_02/src/stack.cpp +++ b/task_02/src/stack.cpp @@ -2,20 +2,36 @@ #include -void Stack::Push(int value) { data_.push(value); } +void Stack::Push(int value) { + Node* t = new Node(); + t->value_ = value; + if (this->top_ == nullptr) { + this->top_ = t; + } else { + t->prev_ = this->top_; + this->top_ = t; + } +} int Stack::Pop() { - auto result = data_.top(); - data_.pop(); - return result; + Node* t = this->top_; + this->top_ = this->top_->prev_; + return t->value_; } -void MinStack::Push(int value) { data_.push_back(value); } +Node* Stack::Top() { return this->top_; } +void MinStack::Push(int value) { + if (s_.Top() == nullptr) { + m_.Push(value); + } else { + m_.Push(std::min(value, s_.Top()->value_)); + } + s_.Push(value); +} int MinStack::Pop() { - auto result = data_.back(); - data_.pop_back(); - return result; + int t = m_.Pop(); + return s_.Pop(); } -int MinStack::GetMin() { return *std::min_element(data_.begin(), data_.end()); } \ No newline at end of file +int MinStack::GetMin() { return m_.Top()->value_; } \ No newline at end of file diff --git a/task_02/src/stack.hpp b/task_02/src/stack.hpp index 138ec40f..e76fcb5a 100644 --- a/task_02/src/stack.hpp +++ b/task_02/src/stack.hpp @@ -1,23 +1,23 @@ #pragma once -#include -#include +struct Node { + int value_ = 0; + Node* prev_ = nullptr; +}; -class Stack { - public: +struct Stack { void Push(int value); int Pop(); + Node* Top(); - private: - std::stack data_; + Node* top_ = nullptr; }; -class MinStack { - public: +struct MinStack { void Push(int value); int Pop(); int GetMin(); - private: - std::vector data_; + Stack s_; + Stack m_; }; diff --git a/task_03/src/temp_up_days.cpp b/task_03/src/temp_up_days.cpp new file mode 100644 index 00000000..57a2c760 --- /dev/null +++ b/task_03/src/temp_up_days.cpp @@ -0,0 +1,23 @@ +#include "temp_up_days.hpp" + +#include + +std::vector TempUpDayCounter(std::vector temps) { + std::stack days; + std::vector result(temps.size(), 0); + for (int i = 0; i < temps.size(); i++) { + Day d; + d.index_ = i; + d.temp_ = temps[i]; + while (!days.empty()) { + if (days.top().temp_ < d.temp_) { + result[days.top().index_] = d.index_ - days.top().index_; + days.pop(); + } else { + break; + } + } + days.push(d); + } + return result; +} \ No newline at end of file diff --git a/task_03/src/temp_up_days.hpp b/task_03/src/temp_up_days.hpp new file mode 100644 index 00000000..ab255eb3 --- /dev/null +++ b/task_03/src/temp_up_days.hpp @@ -0,0 +1,9 @@ +#pragma once +#include + +struct Day { + int index_; + int temp_; +}; + +std::vector TempUpDayCounter(std::vector temps); diff --git a/task_03/src/test.cpp b/task_03/src/test.cpp index ef5a86ae..05bacf66 100644 --- a/task_03/src/test.cpp +++ b/task_03/src/test.cpp @@ -1,8 +1,13 @@ #include -#include "topology_sort.hpp" +#include "temp_up_days.hpp" -TEST(TopologySort, Simple) { - ASSERT_EQ(1, 1); // Stack [] +TEST(TempUpDays, Simple) { + ASSERT_EQ(TempUpDayCounter(std::vector{5, 7, 4, 5}), + (std::vector{1, 0, 1, 0})); + ASSERT_EQ(TempUpDayCounter(std::vector{5, 12, 4, 9, 5, 4, 2}), + (std::vector{1, 0, 1, 0, 0, 0, 0})); + ASSERT_EQ(TempUpDayCounter(std::vector{2, 6, 17, 7, 3, 4}), + (std::vector{1, 1, 0, 0, 1, 0})); } diff --git a/task_03/src/topology_sort.cpp b/task_03/src/topology_sort.cpp deleted file mode 100644 index e53f670c..00000000 --- a/task_03/src/topology_sort.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "topology_sort.hpp" diff --git a/task_03/src/topology_sort.hpp b/task_03/src/topology_sort.hpp deleted file mode 100644 index 6f70f09b..00000000 --- a/task_03/src/topology_sort.hpp +++ /dev/null @@ -1 +0,0 @@ -#pragma once diff --git a/task_04/src/heap.cpp b/task_04/src/heap.cpp new file mode 100644 index 00000000..97dff160 --- /dev/null +++ b/task_04/src/heap.cpp @@ -0,0 +1,44 @@ +#include "heap.hpp" + +#include + +void Heap::SiftUp(int index) { + while (heap_[index] < heap_[(index - 1) / 2]) { + std::swap(heap_[index], heap_[(index - 1) / 2]); + index = (index - 1) / 2; + } +} + +void Heap::SiftDown(int index) { + while (2 * index + 1 < heap_.size()) { + int l = 2 * index + 1, r = 2 * index + 2; + int j = l; + if (r < heap_.size() && heap_[r] < heap_[l]) { + j = r; + } + if (heap_[index] < heap_[j]) { + break; + } + std::swap(heap_[index], heap_[j]); + index = j; + } +} + +int Heap::Min() { return heap_[0]; } + +void Heap::Insert(int value) { + heap_.push_back(value); + this->SiftUp(heap_.size() - 1); +} + +void Heap::Build(std::vector data) { + for (auto x : data) { + this->Insert(x); + } +} + +int FindMin(std::vector data) { + Heap heap; + heap.Build(data); + return heap.Min(); +} diff --git a/task_04/src/heap.hpp b/task_04/src/heap.hpp new file mode 100644 index 00000000..c9bfb493 --- /dev/null +++ b/task_04/src/heap.hpp @@ -0,0 +1,14 @@ +#pragma once + +#include +struct Heap { + void SiftUp(int index); + void SiftDown(int index); + int Min(); + void Insert(int value); + void Build(std::vector data); + + std::vector heap_; +}; + +int FindMin(std::vector data); \ No newline at end of file diff --git a/task_04/src/test.cpp b/task_04/src/test.cpp index 5e11617e..b561f707 100644 --- a/task_04/src/test.cpp +++ b/task_04/src/test.cpp @@ -1,6 +1,9 @@ - #include -TEST(TopologySort, Simple) { - ASSERT_EQ(1, 1); // Stack [] +#include "heap.hpp" + +TEST(HeapMin, Simple) { + ASSERT_EQ(FindMin(std::vector{4, 5, 16, 3, 6}), 3); + ASSERT_EQ(FindMin(std::vector{29, 25, 10, 13, 14, 23, 4, 6}), 4); + ASSERT_EQ(FindMin(std::vector{29, 17, 16, 27, 6, 11}), 6); } From 23a900167646c59c3e316cb6a191a61368b678d2 Mon Sep 17 00:00:00 2001 From: dalnoboy75 <39648068+dalnoboy75@users.noreply.github.com> Date: Sat, 13 Apr 2024 08:41:28 +0000 Subject: [PATCH 02/14] task 5 --- task_05/src/main.cpp | 3 +++ task_05/src/qsort.cpp | 0 task_05/src/qsort.hpp | 38 ++++++++++++++++++++++++++++++++++++++ task_05/src/test.cpp | 30 +++++++++++++++++++++++++++--- 4 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 task_05/src/qsort.cpp create mode 100644 task_05/src/qsort.hpp diff --git a/task_05/src/main.cpp b/task_05/src/main.cpp index 0e4393ba..c2134b68 100644 --- a/task_05/src/main.cpp +++ b/task_05/src/main.cpp @@ -1,3 +1,6 @@ #include +#include + +#include "qsort.hpp" int main() { return 0; } diff --git a/task_05/src/qsort.cpp b/task_05/src/qsort.cpp new file mode 100644 index 00000000..e69de29b diff --git a/task_05/src/qsort.hpp b/task_05/src/qsort.hpp new file mode 100644 index 00000000..ed781cda --- /dev/null +++ b/task_05/src/qsort.hpp @@ -0,0 +1,38 @@ +#pragma once + +#include +#include +#include +#include +#include + +template +int Partition(std::vector& arr, int l, int r) { + std::srand(std::time(nullptr)); + int pv_index = l + std::rand() % (r - l + 1); + T pivot = arr[pv_index]; + std::swap(arr[pv_index], arr[(l + r) / 2]); + int i = l, j = r; + while (i <= j) { + while (arr[i] < pivot) { + i++; + } + while (arr[j] > pivot) { + j--; + } + if (i >= j) { + break; + } + std::swap(arr[i++], arr[j--]); + } + return j; +} + +template +void QuickSort(std::vector& arr, int l, int r) { + if (l < r) { + int q = Partition(arr, l, r); + QuickSort(arr, l, q); + QuickSort(arr, q + 1, r); + } +} diff --git a/task_05/src/test.cpp b/task_05/src/test.cpp index 5e11617e..767238a5 100644 --- a/task_05/src/test.cpp +++ b/task_05/src/test.cpp @@ -1,6 +1,30 @@ - #include -TEST(TopologySort, Simple) { - ASSERT_EQ(1, 1); // Stack [] +#include + +#include "qsort.hpp" + +TEST(Qsort, Simple1) { + std::vector vec = {77, 42, 19, 53, 18, 20}; + QuickSort(vec, 0, vec.size() - 1); + ASSERT_EQ((vec), (std::vector{18, 19, 20, 42, 53, 77})); +} + +TEST(Qsort, Simple2) { + std::vector vec2{12.75, 5.3, 1.1, 23.223, -13.1, 37.37}; + QuickSort(vec2, 0, vec2.size() - 1); + ASSERT_EQ((vec2), + (std::vector{-13.1, 1.1, 5.3, 12.75, 23.223, 37.37})); +} + +TEST(Qsort, Simple3) { + std::vector vec3 = {-1, 2, -1, 3, 4, 5, 2}; + QuickSort(vec3, 0, vec3.size() - 1); + ASSERT_EQ((vec3), (std::vector{-1, -1, 2, 2, 3, 4, 5})); +} + +TEST(Qsort, Simple4) { + std::vector vec4{2, 4, 2, 1, 3, 4, 1}; + QuickSort(vec4, 0, vec4.size() - 1); + ASSERT_EQ((vec4), (std::vector{1, 1, 2, 2, 3, 4, 4})); } From 685222cffa567242e6e3d6cedfe9fdac4b1b9219 Mon Sep 17 00:00:00 2001 From: dalnoboy75 <39648068+dalnoboy75@users.noreply.github.com> Date: Sat, 20 Apr 2024 20:44:40 +0000 Subject: [PATCH 03/14] task 6 --- task_06/src/order_stats.hpp | 39 +++++++++++++++++++++++++++++++++++++ task_06/src/test.cpp | 29 ++++++++++++++++++++++++--- 2 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 task_06/src/order_stats.hpp diff --git a/task_06/src/order_stats.hpp b/task_06/src/order_stats.hpp new file mode 100644 index 00000000..f2eb4c26 --- /dev/null +++ b/task_06/src/order_stats.hpp @@ -0,0 +1,39 @@ +#pragma once +#include +#include +template +int Partition(std::vector& data, int l, int r) { + std::srand(std::time(nullptr)); + int pivotPos = l + std::rand() % (r - l); + if (pivotPos != r - 1) { + std::swap(data[r - 1], data[pivotPos]); + } + + int i = l, j = l; + T pivot = data[r - 1]; + while (j < r - 1) { + if (data[j] <= pivot) { + std::swap(data[i++], data[j]); + } + j++; + } + if (i != r - 1) { + std::swap(data[i], data[r - 1]); + } + return i; +} + +template +T FindKStatistics(std::vector& data, int l, int r, int k) { + int lastPivotPos = 0, left = l, right = r; + while (left < right) { + if ((lastPivotPos = Partition(data, left, right)) == k) { + return data[lastPivotPos]; + } else if (lastPivotPos > k) { + right = lastPivotPos; + } else { + left = lastPivotPos + 1; + } + } + return data[lastPivotPos]; +} diff --git a/task_06/src/test.cpp b/task_06/src/test.cpp index 5e11617e..dffc0874 100644 --- a/task_06/src/test.cpp +++ b/task_06/src/test.cpp @@ -1,6 +1,29 @@ - #include -TEST(TopologySort, Simple) { - ASSERT_EQ(1, 1); // Stack [] +#include + +#include "order_stats.hpp" + +TEST(OrderStats, Simple1) { + std::vector v{50, 82, 11, 64, 66, 57, 35}; + ASSERT_EQ(FindKStatistics(v, 0, v.size(), 3), 57); +} +TEST(OrderStats, Simple2) { + std::vector v{ + 40, 99, 63, 22, 37, 60, 61, 8, 84, 97, + }; + ASSERT_EQ(FindKStatistics(v, 0, v.size(), 5), 61); +} +TEST(OrderStats, Simple3) { + std::vector v{69, 91, 69, 64, 18, 76, 28, 9, 59, 40, 22, 48}; + ASSERT_EQ(FindKStatistics(v, 0, v.size(), 0), 9); +} +TEST(OrderStats, Simple4) { + std::vector v{84.68, 39.62, 36.57, 17.44, 34.22, 66.11, 57.68, 1.58}; + ASSERT_EQ(FindKStatistics(v, 0, v.size(), 2), 34.22); +} + +TEST(OrderStats, Simple5) { + std::vector v{'d', 's', 'a', 'b'}; + ASSERT_EQ(FindKStatistics(v, 0, v.size(), 1), 'b'); } From 16ec7e18708502de9b83fa571e78035f294c15a8 Mon Sep 17 00:00:00 2001 From: dalnoboy75 <39648068+dalnoboy75@users.noreply.github.com> Date: Fri, 3 May 2024 19:59:33 +0000 Subject: [PATCH 04/14] full tasks --- task_04/src/heap.cpp | 8 +- task_07/src/test.cpp | 48 ++++++++++- task_07/src/tree.hpp | 154 +++++++++++++++++++++++++++++++++++ task_08/src/hashtable.cpp | 88 ++++++++++++++++++++ task_08/src/hashtable.hpp | 35 ++++++++ task_08/src/test.cpp | 33 +++++++- task_09/src/change_money.cpp | 15 ++++ task_09/src/change_money.hpp | 4 + task_09/src/test.cpp | 12 ++- 9 files changed, 388 insertions(+), 9 deletions(-) create mode 100644 task_07/src/tree.hpp create mode 100644 task_08/src/hashtable.cpp create mode 100644 task_08/src/hashtable.hpp create mode 100644 task_09/src/change_money.cpp create mode 100644 task_09/src/change_money.hpp diff --git a/task_04/src/heap.cpp b/task_04/src/heap.cpp index 97dff160..575d2ca1 100644 --- a/task_04/src/heap.cpp +++ b/task_04/src/heap.cpp @@ -24,7 +24,13 @@ void Heap::SiftDown(int index) { } } -int Heap::Min() { return heap_[0]; } +int Heap::Min() { + int m = heap_[0]; + std::swap(heap_[0], heap_[heap_.size() - 1]); + heap_.pop_back(); + SiftDown(0); + return m; +} void Heap::Insert(int value) { heap_.push_back(value); diff --git a/task_07/src/test.cpp b/task_07/src/test.cpp index 5e11617e..4f5100a5 100644 --- a/task_07/src/test.cpp +++ b/task_07/src/test.cpp @@ -1,6 +1,48 @@ - #include -TEST(TopologySort, Simple) { - ASSERT_EQ(1, 1); // Stack [] +#include + +#include "tree.hpp" + +TEST(AVLTree, EmptyTree) { + Tree avl; + ASSERT_EQ(avl.Contains(10), false); + ASSERT_EQ(avl.Contains(5), false); + ASSERT_EQ(avl.Contains(0), false); +} + +TEST(AVLTree, AddElements) { + Tree avl; + avl.Push(10); + avl.Push(5); + avl.Push(15); + ASSERT_EQ(avl.Contains(10), true); + ASSERT_EQ(avl.Contains(5), true); + ASSERT_EQ(avl.Contains(15), true); } + +TEST(AVLTree, RemoveElements) { + Tree avl; + avl.Push(10); + avl.Push(5); + avl.Push(15); + avl.Pop(5); + ASSERT_EQ(avl.Contains(5), false); +} + +TEST(AVLTree, ContainsMethod) { + Tree avl; + avl.Push(10); + avl.Push(5); + ASSERT_EQ(avl.Contains(10), true); + ASSERT_EQ(avl.Contains(5), true); + ASSERT_EQ(avl.Contains(15), false); +} + +TEST(AVLTree, RemoveNonExistentElement) { + Tree avl; + avl.Push(10); + avl.Push(5); + avl.Pop(15); + ASSERT_EQ(avl.Contains(15), false); +} \ No newline at end of file diff --git a/task_07/src/tree.hpp b/task_07/src/tree.hpp new file mode 100644 index 00000000..1a988b97 --- /dev/null +++ b/task_07/src/tree.hpp @@ -0,0 +1,154 @@ +#pragma once + +#include +#include + +template +struct Node { + T key; + unsigned int height = 1 ; + Node* left = nullptr; + Node* right = nullptr; + Node(T k):key{k}{}; +}; + +template +class Tree { // AVl-tree + public: + void Push(int k); + void Pop(int k); + bool Contains(int k); + + private: + unsigned int Height(Node* n); + int BFactor(Node* n); + void FixHeight(Node* n); + Node* RotateRight(Node* n); + Node* RotateLeft(Node* n); + Node* Balance(Node* n); + Node* Insert(Node* n, T k); + Node* FindMin(Node* n); + Node* RemoveMin(Node* n); + Node* Remove(Node* n, int k); + Node* Find(Node* n, int k); + Node* root_ = nullptr; +}; + +template +void Tree::Push(int k) { + root_ = Insert(root_, k); +} +template +void Tree::Pop(int k) { + root_ = Remove(root_, k); +} +template +bool Tree::Contains(int k) { + return Find(root_, k) ? true : false; +} + +template +unsigned int Tree::Height(Node* n) { return n ? n->height : 0; } + +template +int Tree::BFactor(Node* n) { return Height(n->right) - Height(n->left); } + +template +void Tree::FixHeight(Node* n) { + unsigned int hl = Height(n->left); + unsigned int hr = Height(n->right); + n->height = std::max(hl, hr) + 1; +} + +template +Node* Tree::RotateRight(Node* n) { + Node* q = n->left; + n->left = q->right; + q->right = n; + FixHeight(n); + FixHeight(q); + return q; +} + +template +Node* Tree::RotateLeft(Node* n) { + Node* q = n->right; + n->right = q->left; + q->left = n; + FixHeight(q); + FixHeight(n); + return q; +} + +template +Node* Tree::Balance(Node* n) { + FixHeight(n); + if (BFactor(n) == 2) { + if (BFactor(n->right) < 0) n->right = RotateRight(n->right); + return RotateLeft(n); + } + if (BFactor(n) == -2) { + if (BFactor(n->left) > 0) n->left = RotateLeft(n->left); + return RotateRight(n); + } + return n; +} + +template +Node* Tree::Insert(Node* n, T k) { + if (!n) return new Node(k); + if (k < n->key) + n->left = Insert(n->left, k); + else + n->right = Insert(n->right, k); + return Balance(n); +} + +template +Node* Tree::FindMin(Node* n) { return n->left ? FindMin(n->left) : n; } + +template +Node* Tree::RemoveMin(Node* n) { + if (n->left == nullptr) { + return n->right; + } + n->left = RemoveMin(n->left); + return Balance(n); +} + +template +Node* Tree::Remove(Node* n, int k) { + if (!n) { + return nullptr; + } + if (k < n->key) { + n->left = Remove(n->left, k); + } else if (k > n->key) { + n->right = Remove(n->right, k); + } else { + Node* l = n->left; + Node* r = n->right; + delete n; + if (!r) { + return l; + } + Node* m = FindMin(r); + m->right = RemoveMin(r); + m->left = l; + return Balance(m); + } + return Balance(n); +} + +template +Node* Tree::Find(Node* n, int k) { + if (n == nullptr || n->key == k) { + return n; + } + if (k < n->key) { + return Find(n->left, k); + } + if (k > n->key) { + return Find(n->right, k); + } +} \ No newline at end of file diff --git a/task_08/src/hashtable.cpp b/task_08/src/hashtable.cpp new file mode 100644 index 00000000..86294ecf --- /dev/null +++ b/task_08/src/hashtable.cpp @@ -0,0 +1,88 @@ +#include "hashtable.hpp" + +#include + +unsigned HashTable::HashFunction1(int key) { + return floor(buffer_size_ * ((key * HashCoeff) - floor(key * HashCoeff))); +} + +unsigned HashTable::HashFunction2(int key) { + return (key * buffer_size_ - 1) % buffer_size_; +} + +void HashTable::Resize() { + buffer_size_ *= 2; + table_.resize(buffer_size_); + cell_status_.resize(buffer_size_); +} + +void HashTable::Clear() { + size_ = 0; + used_cell_size_ = 0; + for (int i = 0; i < buffer_size_; i++) { + cell_status_[i] = Status::Empty; + } +} +void HashTable::Rehash() { + std::vector temp; + for (int i = 0; i < buffer_size_; i++) { + if (cell_status_[i] == Status::Fill) { + temp.push_back(table_[i]); + } + } + Resize(); + Clear(); + for (int i = 0; i < temp.size(); i++) { + Insert(temp[i]); + } +} + +void HashTable::Insert(int key) { + unsigned hash = HashFunction1(key) % buffer_size_; + int counter = 0; + while (cell_status_[hash] == Status::Fill) { + if (table_[hash] == key) { + return; + } + counter++; + hash = (HashFunction1(key) + counter * HashFunction2(key)) % buffer_size_; + } + table_[hash] = key; + cell_status_[hash] = Status::Fill; + used_cell_size_++; + size_++; +} + +void HashTable::Remove(int key) { + unsigned hash = HashFunction1(key) % buffer_size_; + int counter = 0; + while (cell_status_[hash] != Status::Empty) { + if (table_[hash] == key && cell_status_[hash] == Status::Deleted) { + return; + } + if (table_[hash] == key && cell_status_[hash] == Status::Fill) { + size_--; + cell_status_[hash] = Status::Deleted; + return; + } + counter++; + hash = (HashFunction1(key) + counter * HashFunction2(key)) % buffer_size_; + } +} + +bool HashTable::Contains(int key) { + unsigned hash = HashFunction1(key) % buffer_size_; + int counter = 0; + while (cell_status_[hash] != Status::Empty) { + if (table_[hash] == key && cell_status_[hash] == Status::Fill) { + return true; + } + counter++; + hash = (HashFunction1(key) + counter * HashFunction2(key)) % buffer_size_; + } + return false; +} + +unsigned HashTable::Size(){ + return size_; +} \ No newline at end of file diff --git a/task_08/src/hashtable.hpp b/task_08/src/hashtable.hpp new file mode 100644 index 00000000..a8f20d44 --- /dev/null +++ b/task_08/src/hashtable.hpp @@ -0,0 +1,35 @@ +#pragma once + +#include + +class HashTable { + public: + HashTable() + : size_{0}, + buffer_size_{DefaultSize}, + used_cell_size_{0}, + table_{std::vector(buffer_size_)}, + cell_status_{std::vector(buffer_size_, Status::Empty)} {}; + void Insert(int key); + void Remove(int key); + bool Contains(int key); + unsigned Size(); + + private: + constexpr static const int DefaultSize = 8; + constexpr static const double RehashSize = 0.75; + constexpr static const double HashCoeff = 0.61803398; + unsigned size_; // занятые ячейки без учёта Deleted + unsigned buffer_size_; + unsigned used_cell_size_;// занятые ячейки с учётом Deleted + std::vector table_; + enum class Status { Empty, Deleted, Fill }; + std::vector cell_status_; + + unsigned HashFunction1(int key); + unsigned HashFunction2(int key); + void Rehash(); + void Resize(); + void Clear(); +}; + diff --git a/task_08/src/test.cpp b/task_08/src/test.cpp index 5e11617e..86b57cff 100644 --- a/task_08/src/test.cpp +++ b/task_08/src/test.cpp @@ -1,6 +1,33 @@ - +#include "hashtable.hpp" #include -TEST(TopologySort, Simple) { - ASSERT_EQ(1, 1); // Stack [] +TEST(Hashtable, Simple) { + HashTable table; + table.Insert(5); + table.Insert(17); + table.Insert(33); + ASSERT_EQ(table.Contains(17), true); + ASSERT_EQ(table.Size(), 3); + table.Remove(5); + ASSERT_EQ(table.Contains(5),false); +} + +TEST(Hashtable, EmptyTable) { + HashTable table; + ASSERT_EQ(table.Size(), 0); + ASSERT_EQ(table.Contains(10), false); } + +TEST(Hashtable, RemoveNonExistentElement) { + HashTable table; + table.Insert(7); + table.Remove(10); + ASSERT_EQ(table.Size(), 1); +} + +TEST(Hashtable, InsertDuplicateElement) { + HashTable table; + table.Insert(5); + table.Insert(5); + ASSERT_EQ(table.Size(), 1); +} \ No newline at end of file diff --git a/task_09/src/change_money.cpp b/task_09/src/change_money.cpp new file mode 100644 index 00000000..960d1a40 --- /dev/null +++ b/task_09/src/change_money.cpp @@ -0,0 +1,15 @@ +#include "change_money.hpp" +#include + +int CoinExchange(int sum, std::vector coins){ + std::vector money(sum+1, 1e9); + money[0] = 0; + for(int m = 1; m <= sum; m++){ + for (auto coin : coins){ + if (coin <= m){ + money[m] = std::min(money[m], money[m-coin]+1); + } + } + } + return money[sum]; +} \ No newline at end of file diff --git a/task_09/src/change_money.hpp b/task_09/src/change_money.hpp new file mode 100644 index 00000000..ec7b644d --- /dev/null +++ b/task_09/src/change_money.hpp @@ -0,0 +1,4 @@ +#pragma once + +#include +int CoinExchange(int sum, std::vector coins); \ No newline at end of file diff --git a/task_09/src/test.cpp b/task_09/src/test.cpp index 869094dd..584cafcb 100644 --- a/task_09/src/test.cpp +++ b/task_09/src/test.cpp @@ -1,4 +1,12 @@ - #include -TEST(TopologySort, Simple) { ASSERT_EQ(1, 1); } +#include + +#include "change_money.hpp" + +TEST(ChangeMoney, Simple) { + ASSERT_EQ(CoinExchange(14, std::vector{1, 2, 5, 10}), 3); + ASSERT_EQ(CoinExchange(21, std::vector{1, 3, 6}), 4); + ASSERT_EQ(CoinExchange(25, std::vector{1, 2, 3, 5}), 5); + ASSERT_EQ(CoinExchange(137, std::vector{1, 3, 7, 19}), 9); +} From d547e35b883d016f909d4970345d3facf5193585 Mon Sep 17 00:00:00 2001 From: dalnoboy75 <39648068+dalnoboy75@users.noreply.github.com> Date: Fri, 3 May 2024 20:02:38 +0000 Subject: [PATCH 05/14] fix --- task_08/src/hashtable.cpp | 4 +--- task_08/src/hashtable.hpp | 5 ++--- task_08/src/test.cpp | 5 +++-- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/task_08/src/hashtable.cpp b/task_08/src/hashtable.cpp index 86294ecf..2923edc6 100644 --- a/task_08/src/hashtable.cpp +++ b/task_08/src/hashtable.cpp @@ -83,6 +83,4 @@ bool HashTable::Contains(int key) { return false; } -unsigned HashTable::Size(){ - return size_; -} \ No newline at end of file +unsigned HashTable::Size() { return size_; } \ No newline at end of file diff --git a/task_08/src/hashtable.hpp b/task_08/src/hashtable.hpp index a8f20d44..4d4027b3 100644 --- a/task_08/src/hashtable.hpp +++ b/task_08/src/hashtable.hpp @@ -19,9 +19,9 @@ class HashTable { constexpr static const int DefaultSize = 8; constexpr static const double RehashSize = 0.75; constexpr static const double HashCoeff = 0.61803398; - unsigned size_; // занятые ячейки без учёта Deleted + unsigned size_; // занятые ячейки без учёта Deleted unsigned buffer_size_; - unsigned used_cell_size_;// занятые ячейки с учётом Deleted + unsigned used_cell_size_; // занятые ячейки с учётом Deleted std::vector table_; enum class Status { Empty, Deleted, Fill }; std::vector cell_status_; @@ -32,4 +32,3 @@ class HashTable { void Resize(); void Clear(); }; - diff --git a/task_08/src/test.cpp b/task_08/src/test.cpp index 86b57cff..3e12290c 100644 --- a/task_08/src/test.cpp +++ b/task_08/src/test.cpp @@ -1,6 +1,7 @@ -#include "hashtable.hpp" #include +#include "hashtable.hpp" + TEST(Hashtable, Simple) { HashTable table; table.Insert(5); @@ -9,7 +10,7 @@ TEST(Hashtable, Simple) { ASSERT_EQ(table.Contains(17), true); ASSERT_EQ(table.Size(), 3); table.Remove(5); - ASSERT_EQ(table.Contains(5),false); + ASSERT_EQ(table.Contains(5), false); } TEST(Hashtable, EmptyTable) { From 66bb2df8135345219e5c6676ea566f14c686c3b0 Mon Sep 17 00:00:00 2001 From: dalnoboy75 <39648068+dalnoboy75@users.noreply.github.com> Date: Fri, 3 May 2024 20:08:56 +0000 Subject: [PATCH 06/14] fix --- task_09/src/change_money.cpp | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/task_09/src/change_money.cpp b/task_09/src/change_money.cpp index 960d1a40..399b30da 100644 --- a/task_09/src/change_money.cpp +++ b/task_09/src/change_money.cpp @@ -1,15 +1,16 @@ #include "change_money.hpp" + #include -int CoinExchange(int sum, std::vector coins){ - std::vector money(sum+1, 1e9); - money[0] = 0; - for(int m = 1; m <= sum; m++){ - for (auto coin : coins){ - if (coin <= m){ - money[m] = std::min(money[m], money[m-coin]+1); - } - } +int CoinExchange(int sum, std::vector coins) { + std::vector money(sum + 1, 1e9); + money[0] = 0; + for (int m = 1; m <= sum; m++) { + for (auto coin : coins) { + if (coin <= m) { + money[m] = std::min(money[m], money[m - coin] + 1); + } } - return money[sum]; + } + return money[sum]; } \ No newline at end of file From 3b66e4f46852004667f4ae5ee4477b626d7ad716 Mon Sep 17 00:00:00 2001 From: dalnoboy75 <39648068+dalnoboy75@users.noreply.github.com> Date: Fri, 3 May 2024 20:10:15 +0000 Subject: [PATCH 07/14] fix --- task_07/src/tree.hpp | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/task_07/src/tree.hpp b/task_07/src/tree.hpp index 1a988b97..7c516902 100644 --- a/task_07/src/tree.hpp +++ b/task_07/src/tree.hpp @@ -6,10 +6,10 @@ template struct Node { T key; - unsigned int height = 1 ; + unsigned int height = 1; Node* left = nullptr; Node* right = nullptr; - Node(T k):key{k}{}; + Node(T k) : key{k} {}; }; template @@ -48,10 +48,14 @@ bool Tree::Contains(int k) { } template -unsigned int Tree::Height(Node* n) { return n ? n->height : 0; } +unsigned int Tree::Height(Node* n) { + return n ? n->height : 0; +} template -int Tree::BFactor(Node* n) { return Height(n->right) - Height(n->left); } +int Tree::BFactor(Node* n) { + return Height(n->right) - Height(n->left); +} template void Tree::FixHeight(Node* n) { @@ -105,7 +109,9 @@ Node* Tree::Insert(Node* n, T k) { } template -Node* Tree::FindMin(Node* n) { return n->left ? FindMin(n->left) : n; } +Node* Tree::FindMin(Node* n) { + return n->left ? FindMin(n->left) : n; +} template Node* Tree::RemoveMin(Node* n) { From 60bc985e494e2d24f8875bed9f2f9f728f20650a Mon Sep 17 00:00:00 2001 From: dalnoboy75 <39648068+dalnoboy75@users.noreply.github.com> Date: Sat, 4 May 2024 08:30:11 +0000 Subject: [PATCH 08/14] fix --- task_08/src/hashtable.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/task_08/src/hashtable.cpp b/task_08/src/hashtable.cpp index 2923edc6..64c60604 100644 --- a/task_08/src/hashtable.cpp +++ b/task_08/src/hashtable.cpp @@ -51,6 +51,9 @@ void HashTable::Insert(int key) { cell_status_[hash] = Status::Fill; used_cell_size_++; size_++; + + double fill_coeff = double(used_cell_size_) / buffer_size_; + if (fill_coeff >= RehashSize) Rehash(); } void HashTable::Remove(int key) { From 9504f9ddb4a38ece5807029f5fa5cfe0e98c2e85 Mon Sep 17 00:00:00 2001 From: dalnoboy75 <39648068+dalnoboy75@users.noreply.github.com> Date: Sun, 19 May 2024 13:37:47 +0000 Subject: [PATCH 09/14] fix --- lib/src/util.hpp | 27 +++++++++++++++++++++++++++ task_02/src/stack.cpp | 2 +- task_02/src/stack.hpp | 15 ++++++++------- task_03/src/test.cpp | 1 + task_04/src/heap.cpp | 3 ++- task_05/src/qsort.hpp | 26 +------------------------- task_05/src/test.cpp | 6 ++++++ task_06/src/order_stats.hpp | 2 ++ task_07/src/test.cpp | 8 ++++++++ task_09/src/test.cpp | 2 ++ 10 files changed, 58 insertions(+), 34 deletions(-) diff --git a/lib/src/util.hpp b/lib/src/util.hpp index e69de29b..8a16d405 100644 --- a/lib/src/util.hpp +++ b/lib/src/util.hpp @@ -0,0 +1,27 @@ +#pragma once +#include +#include +#include +#include + +template +int Partition(std::vector& arr, int l, int r) { + std::srand(std::time(nullptr)); + int pv_index = l + std::rand() % (r - l + 1); + T pivot = arr[pv_index]; + std::swap(arr[pv_index], arr[(l + r) / 2]); + int i = l, j = r; + while (i <= j) { + while (arr[i] < pivot) { + i++; + } + while (arr[j] > pivot) { + j--; + } + if (i >= j) { + break; + } + std::swap(arr[i++], arr[j--]); + } + return j; +} \ No newline at end of file diff --git a/task_02/src/stack.cpp b/task_02/src/stack.cpp index a73b31a0..e2b7a680 100644 --- a/task_02/src/stack.cpp +++ b/task_02/src/stack.cpp @@ -19,7 +19,7 @@ int Stack::Pop() { return t->value_; } -Node* Stack::Top() { return this->top_; } +Stack::Node* Stack::Top() { return this->top_; } void MinStack::Push(int value) { if (s_.Top() == nullptr) { m_.Push(value); diff --git a/task_02/src/stack.hpp b/task_02/src/stack.hpp index e76fcb5a..4ab01296 100644 --- a/task_02/src/stack.hpp +++ b/task_02/src/stack.hpp @@ -1,16 +1,17 @@ #pragma once -struct Node { - int value_ = 0; - Node* prev_ = nullptr; -}; +class Stack { + private: + struct Node { + int value_ = 0; + Node* prev_ = nullptr; + }; + Node* top_ = nullptr; -struct Stack { + public: void Push(int value); int Pop(); Node* Top(); - - Node* top_ = nullptr; }; struct MinStack { diff --git a/task_03/src/test.cpp b/task_03/src/test.cpp index 05bacf66..d7795c70 100644 --- a/task_03/src/test.cpp +++ b/task_03/src/test.cpp @@ -10,4 +10,5 @@ TEST(TempUpDays, Simple) { (std::vector{1, 0, 1, 0, 0, 0, 0})); ASSERT_EQ(TempUpDayCounter(std::vector{2, 6, 17, 7, 3, 4}), (std::vector{1, 1, 0, 0, 1, 0})); + ASSERT_EQ(TempUpDayCounter(std::vector{}), std::vector{}); } diff --git a/task_04/src/heap.cpp b/task_04/src/heap.cpp index 575d2ca1..3c64e0b2 100644 --- a/task_04/src/heap.cpp +++ b/task_04/src/heap.cpp @@ -11,7 +11,8 @@ void Heap::SiftUp(int index) { void Heap::SiftDown(int index) { while (2 * index + 1 < heap_.size()) { - int l = 2 * index + 1, r = 2 * index + 2; + int l = 2 * index + 1; + int r = 2 * index + 2; int j = l; if (r < heap_.size() && heap_[r] < heap_[l]) { j = r; diff --git a/task_05/src/qsort.hpp b/task_05/src/qsort.hpp index ed781cda..95dfda8e 100644 --- a/task_05/src/qsort.hpp +++ b/task_05/src/qsort.hpp @@ -1,33 +1,9 @@ #pragma once -#include -#include -#include +#include "../../lib/src/util.hpp" #include #include -template -int Partition(std::vector& arr, int l, int r) { - std::srand(std::time(nullptr)); - int pv_index = l + std::rand() % (r - l + 1); - T pivot = arr[pv_index]; - std::swap(arr[pv_index], arr[(l + r) / 2]); - int i = l, j = r; - while (i <= j) { - while (arr[i] < pivot) { - i++; - } - while (arr[j] > pivot) { - j--; - } - if (i >= j) { - break; - } - std::swap(arr[i++], arr[j--]); - } - return j; -} - template void QuickSort(std::vector& arr, int l, int r) { if (l < r) { diff --git a/task_05/src/test.cpp b/task_05/src/test.cpp index 767238a5..d1758e75 100644 --- a/task_05/src/test.cpp +++ b/task_05/src/test.cpp @@ -28,3 +28,9 @@ TEST(Qsort, Simple4) { QuickSort(vec4, 0, vec4.size() - 1); ASSERT_EQ((vec4), (std::vector{1, 1, 2, 2, 3, 4, 4})); } + +TEST(Qsort, Simple5){ + std::vector vec5{}; + QuickSort(vec5, 0, vec5.size()-1); + ASSERT_EQ((vec5), std::vector{}); +} diff --git a/task_06/src/order_stats.hpp b/task_06/src/order_stats.hpp index f2eb4c26..6abacb61 100644 --- a/task_06/src/order_stats.hpp +++ b/task_06/src/order_stats.hpp @@ -1,6 +1,8 @@ #pragma once + #include #include + template int Partition(std::vector& data, int l, int r) { std::srand(std::time(nullptr)); diff --git a/task_07/src/test.cpp b/task_07/src/test.cpp index 4f5100a5..dbda586a 100644 --- a/task_07/src/test.cpp +++ b/task_07/src/test.cpp @@ -45,4 +45,12 @@ TEST(AVLTree, RemoveNonExistentElement) { avl.Push(5); avl.Pop(15); ASSERT_EQ(avl.Contains(15), false); +} + +TEST(AVLTree, RemoveInEmptyTree) { + Tree avl; + ASSERT_EQ(avl.Contains(13), false); + avl.Pop(13); + ASSERT_EQ(avl.Contains(13), false); + ASSERT_EQ(avl.Contains(37), false); } \ No newline at end of file diff --git a/task_09/src/test.cpp b/task_09/src/test.cpp index 584cafcb..b702efd2 100644 --- a/task_09/src/test.cpp +++ b/task_09/src/test.cpp @@ -9,4 +9,6 @@ TEST(ChangeMoney, Simple) { ASSERT_EQ(CoinExchange(21, std::vector{1, 3, 6}), 4); ASSERT_EQ(CoinExchange(25, std::vector{1, 2, 3, 5}), 5); ASSERT_EQ(CoinExchange(137, std::vector{1, 3, 7, 19}), 9); + + ASSERT_EQ(CoinExchange(14, std::vector{1, 2, 5, 7, 10}), 2); } From 200b7cb35ca17af39decf53e047ab8af7ff7cdd8 Mon Sep 17 00:00:00 2001 From: dalnoboy75 <39648068+dalnoboy75@users.noreply.github.com> Date: Sun, 19 May 2024 13:40:41 +0000 Subject: [PATCH 10/14] fix --- lib/src/util.hpp | 2 +- task_05/src/qsort.hpp | 3 ++- task_05/src/test.cpp | 4 ++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/src/util.hpp b/lib/src/util.hpp index 8a16d405..675809a0 100644 --- a/lib/src/util.hpp +++ b/lib/src/util.hpp @@ -2,7 +2,7 @@ #include #include #include -#include +#include template int Partition(std::vector& arr, int l, int r) { diff --git a/task_05/src/qsort.hpp b/task_05/src/qsort.hpp index 95dfda8e..e46b1764 100644 --- a/task_05/src/qsort.hpp +++ b/task_05/src/qsort.hpp @@ -1,9 +1,10 @@ #pragma once -#include "../../lib/src/util.hpp" #include #include +#include "../../lib/src/util.hpp" + template void QuickSort(std::vector& arr, int l, int r) { if (l < r) { diff --git a/task_05/src/test.cpp b/task_05/src/test.cpp index d1758e75..4c48f0d8 100644 --- a/task_05/src/test.cpp +++ b/task_05/src/test.cpp @@ -29,8 +29,8 @@ TEST(Qsort, Simple4) { ASSERT_EQ((vec4), (std::vector{1, 1, 2, 2, 3, 4, 4})); } -TEST(Qsort, Simple5){ +TEST(Qsort, Simple5) { std::vector vec5{}; - QuickSort(vec5, 0, vec5.size()-1); + QuickSort(vec5, 0, vec5.size() - 1); ASSERT_EQ((vec5), std::vector{}); } From 383f94e4a7602f34a29e1c309bbe092d6911feba Mon Sep 17 00:00:00 2001 From: dalnoboy75 <39648068+dalnoboy75@users.noreply.github.com> Date: Sat, 15 Jun 2024 12:21:04 +0000 Subject: [PATCH 11/14] fix --- task_02/src/stack.cpp | 30 +++++--- task_02/src/stack.hpp | 8 ++- task_04/src/heap.cpp | 26 ++++--- task_04/src/heap.hpp | 10 ++- task_04/src/test.cpp | 24 +++++++ task_05/src/qsort.hpp | 2 +- task_06/src/order_stats.hpp | 6 +- task_07/src/tree.hpp | 138 ++++++++++++++++++------------------ 8 files changed, 149 insertions(+), 95 deletions(-) diff --git a/task_02/src/stack.cpp b/task_02/src/stack.cpp index e2b7a680..ad31f452 100644 --- a/task_02/src/stack.cpp +++ b/task_02/src/stack.cpp @@ -5,26 +5,36 @@ void Stack::Push(int value) { Node* t = new Node(); t->value_ = value; - if (this->top_ == nullptr) { - this->top_ = t; + if (top_ == nullptr) { + top_ = t; } else { - t->prev_ = this->top_; - this->top_ = t; + t->prev_ = top_; + top_ = t; } } +bool Stack::CheckTop() { + if (Top() == nullptr) { + return true; + } else { + return false; + } +} + +int Stack::GetTopValue() { return Top()->value_; } + int Stack::Pop() { - Node* t = this->top_; - this->top_ = this->top_->prev_; + Node* t = top_; + top_ = top_->prev_; return t->value_; } -Stack::Node* Stack::Top() { return this->top_; } +Stack::Node* Stack::Top() { return top_; } void MinStack::Push(int value) { - if (s_.Top() == nullptr) { + if (s_.CheckTop()) { m_.Push(value); } else { - m_.Push(std::min(value, s_.Top()->value_)); + m_.Push(std::min(value, s_.GetTopValue())); } s_.Push(value); } @@ -34,4 +44,4 @@ int MinStack::Pop() { return s_.Pop(); } -int MinStack::GetMin() { return m_.Top()->value_; } \ No newline at end of file +int MinStack::GetMin() { return m_.GetTopValue(); } \ No newline at end of file diff --git a/task_02/src/stack.hpp b/task_02/src/stack.hpp index 4ab01296..084095bb 100644 --- a/task_02/src/stack.hpp +++ b/task_02/src/stack.hpp @@ -7,18 +7,22 @@ class Stack { Node* prev_ = nullptr; }; Node* top_ = nullptr; + Node* Top(); public: void Push(int value); int Pop(); - Node* Top(); + bool CheckTop(); + int GetTopValue(); }; -struct MinStack { +class MinStack { + public: void Push(int value); int Pop(); int GetMin(); + private: Stack s_; Stack m_; }; diff --git a/task_04/src/heap.cpp b/task_04/src/heap.cpp index 3c64e0b2..831b0d40 100644 --- a/task_04/src/heap.cpp +++ b/task_04/src/heap.cpp @@ -1,5 +1,7 @@ #include "heap.hpp" +#include +#include #include void Heap::SiftUp(int index) { @@ -11,21 +13,22 @@ void Heap::SiftUp(int index) { void Heap::SiftDown(int index) { while (2 * index + 1 < heap_.size()) { - int l = 2 * index + 1; - int r = 2 * index + 2; - int j = l; - if (r < heap_.size() && heap_[r] < heap_[l]) { - j = r; + int left = 2 * index + 1; + int right = 2 * index + 2; + int small_child_index = left; + if (right < heap_.size() && heap_[right] < heap_[left]) { + small_child_index = right; } - if (heap_[index] < heap_[j]) { + if (heap_[index] < heap_[small_child_index]) { break; } - std::swap(heap_[index], heap_[j]); - index = j; + std::swap(heap_[index], heap_[small_child_index]); + index = small_child_index; } } int Heap::Min() { + if (Size() == 0) throw std::runtime_error("Empty Heap"); int m = heap_[0]; std::swap(heap_[0], heap_[heap_.size() - 1]); heap_.pop_back(); @@ -44,6 +47,13 @@ void Heap::Build(std::vector data) { } } +int Heap::Size() { return heap_.size(); } + +int Heap::Top() { + if (Size() == 0) throw std::runtime_error("Empty Heap"); + return heap_[0]; +} + int FindMin(std::vector data) { Heap heap; heap.Build(data); diff --git a/task_04/src/heap.hpp b/task_04/src/heap.hpp index c9bfb493..29255f59 100644 --- a/task_04/src/heap.hpp +++ b/task_04/src/heap.hpp @@ -1,14 +1,18 @@ #pragma once #include -struct Heap { - void SiftUp(int index); - void SiftDown(int index); +class Heap { + public: + int Top(); + int Size(); int Min(); void Insert(int value); void Build(std::vector data); + private: std::vector heap_; + void SiftUp(int index); + void SiftDown(int index); }; int FindMin(std::vector data); \ No newline at end of file diff --git a/task_04/src/test.cpp b/task_04/src/test.cpp index b561f707..49da1095 100644 --- a/task_04/src/test.cpp +++ b/task_04/src/test.cpp @@ -1,5 +1,7 @@ #include +#include + #include "heap.hpp" TEST(HeapMin, Simple) { @@ -7,3 +9,25 @@ TEST(HeapMin, Simple) { ASSERT_EQ(FindMin(std::vector{29, 25, 10, 13, 14, 23, 4, 6}), 4); ASSERT_EQ(FindMin(std::vector{29, 17, 16, 27, 6, 11}), 6); } + +TEST(EmptyHeap, Simple) { + Heap empty_heap; + + ASSERT_EQ(empty_heap.Size(), 0); + EXPECT_THROW(empty_heap.Top(), std::runtime_error); + EXPECT_THROW(empty_heap.Min(), std::runtime_error); +} + +TEST(Heap, Simple) { + Heap heap; + heap.Insert(5); + heap.Insert(3); + + ASSERT_EQ(heap.Size(), 2); + ASSERT_EQ(heap.Top(), 3); + + ASSERT_EQ(heap.Min(), 3); + + ASSERT_EQ(heap.Size(), 1); + ASSERT_EQ(heap.Top(), 5); +} \ No newline at end of file diff --git a/task_05/src/qsort.hpp b/task_05/src/qsort.hpp index e46b1764..58e09fbb 100644 --- a/task_05/src/qsort.hpp +++ b/task_05/src/qsort.hpp @@ -3,7 +3,7 @@ #include #include -#include "../../lib/src/util.hpp" +#include template void QuickSort(std::vector& arr, int l, int r) { diff --git a/task_06/src/order_stats.hpp b/task_06/src/order_stats.hpp index 6abacb61..9df85de1 100644 --- a/task_06/src/order_stats.hpp +++ b/task_06/src/order_stats.hpp @@ -6,9 +6,9 @@ template int Partition(std::vector& data, int l, int r) { std::srand(std::time(nullptr)); - int pivotPos = l + std::rand() % (r - l); - if (pivotPos != r - 1) { - std::swap(data[r - 1], data[pivotPos]); + int pivot_pos = l + std::rand() % (r - l); + if (pivot_pos != r - 1) { + std::swap(data[r - 1], data[pivot_pos]); } int i = l, j = l; diff --git a/task_07/src/tree.hpp b/task_07/src/tree.hpp index 7c516902..f4a89e6e 100644 --- a/task_07/src/tree.hpp +++ b/task_07/src/tree.hpp @@ -20,17 +20,17 @@ class Tree { // AVl-tree bool Contains(int k); private: - unsigned int Height(Node* n); - int BFactor(Node* n); - void FixHeight(Node* n); - Node* RotateRight(Node* n); - Node* RotateLeft(Node* n); - Node* Balance(Node* n); - Node* Insert(Node* n, T k); - Node* FindMin(Node* n); - Node* RemoveMin(Node* n); - Node* Remove(Node* n, int k); - Node* Find(Node* n, int k); + unsigned int Height(Node* node_ptr); + int BFactor(Node* node_ptr); + void FixHeight(Node* node_ptr); + Node* RotateRight(Node* node_ptr); + Node* RotateLeft(Node* node_ptr); + Node* Balance(Node* node_ptr); + Node* Insert(Node* node_ptr, T k); + Node* FindMin(Node* node_ptr); + Node* RemoveMin(Node* node_ptr); + Node* Remove(Node* node_ptr, int k); + Node* Find(Node* node_ptr, int k); Node* root_ = nullptr; }; @@ -48,93 +48,95 @@ bool Tree::Contains(int k) { } template -unsigned int Tree::Height(Node* n) { - return n ? n->height : 0; +unsigned int Tree::Height(Node* node_ptr) { + return node_ptr ? node_ptr->height : 0; } template -int Tree::BFactor(Node* n) { - return Height(n->right) - Height(n->left); +int Tree::BFactor(Node* node_ptr) { + return Height(node_ptr->right) - Height(node_ptr->left); } template -void Tree::FixHeight(Node* n) { - unsigned int hl = Height(n->left); - unsigned int hr = Height(n->right); - n->height = std::max(hl, hr) + 1; +void Tree::FixHeight(Node* node_ptr) { + unsigned int hl = Height(node_ptr->left); + unsigned int hr = Height(node_ptr->right); + node_ptr->height = std::max(hl, hr) + 1; } template -Node* Tree::RotateRight(Node* n) { - Node* q = n->left; - n->left = q->right; - q->right = n; - FixHeight(n); +Node* Tree::RotateRight(Node* node_ptr) { + Node* q = node_ptr->left; + node_ptr->left = q->right; + q->right = node_ptr; + FixHeight(node_ptr); FixHeight(q); return q; } template -Node* Tree::RotateLeft(Node* n) { - Node* q = n->right; - n->right = q->left; - q->left = n; +Node* Tree::RotateLeft(Node* node_ptr) { + Node* q = node_ptr->right; + node_ptr->right = q->left; + q->left = node_ptr; FixHeight(q); - FixHeight(n); + FixHeight(node_ptr); return q; } template -Node* Tree::Balance(Node* n) { - FixHeight(n); - if (BFactor(n) == 2) { - if (BFactor(n->right) < 0) n->right = RotateRight(n->right); - return RotateLeft(n); +Node* Tree::Balance(Node* node_ptr) { + FixHeight(node_ptr); + if (BFactor(node_ptr) == 2) { + if (BFactor(node_ptr->right) < 0) + node_ptr->right = RotateRight(node_ptr->right); + return RotateLeft(node_ptr); } - if (BFactor(n) == -2) { - if (BFactor(n->left) > 0) n->left = RotateLeft(n->left); - return RotateRight(n); + if (BFactor(node_ptr) == -2) { + if (BFactor(node_ptr->left) > 0) + node_ptr->left = RotateLeft(node_ptr->left); + return RotateRight(node_ptr); } - return n; + return node_ptr; } template -Node* Tree::Insert(Node* n, T k) { - if (!n) return new Node(k); - if (k < n->key) - n->left = Insert(n->left, k); +Node* Tree::Insert(Node* node_ptr, T k) { + if (!node_ptr) return new Node(k); + if (k < node_ptr->key) + node_ptr->left = Insert(node_ptr->left, k); else - n->right = Insert(n->right, k); - return Balance(n); + node_ptr->right = Insert(node_ptr->right, k); + return Balance(node_ptr); } template -Node* Tree::FindMin(Node* n) { - return n->left ? FindMin(n->left) : n; +Node* Tree::FindMin(Node* node_ptr) { + return node_ptr->left ? FindMin(node_ptr->left) : node_ptr; } template -Node* Tree::RemoveMin(Node* n) { - if (n->left == nullptr) { - return n->right; +Node* Tree::RemoveMin(Node* node_ptr) { + if (node_ptr->left == nullptr) { + return node_ptr->right; } - n->left = RemoveMin(n->left); - return Balance(n); + node_ptr->left = RemoveMin(node_ptr->left); + return Balance(node_ptr); } template -Node* Tree::Remove(Node* n, int k) { - if (!n) { +Node* Tree::Remove(Node* node_ptr, int k) { + if (!node_ptr) { return nullptr; } - if (k < n->key) { - n->left = Remove(n->left, k); - } else if (k > n->key) { - n->right = Remove(n->right, k); + if (k < node_ptr->key) { + node_ptr->left = Remove(node_ptr->left, k); + } else if (k > node_ptr->key) { + node_ptr->right = Remove(node_ptr->right, k); } else { - Node* l = n->left; - Node* r = n->right; - delete n; + Node* l = node_ptr->left; + Node* r = node_ptr->right; + delete node_ptr; if (!r) { return l; } @@ -143,18 +145,18 @@ Node* Tree::Remove(Node* n, int k) { m->left = l; return Balance(m); } - return Balance(n); + return Balance(node_ptr); } template -Node* Tree::Find(Node* n, int k) { - if (n == nullptr || n->key == k) { - return n; +Node* Tree::Find(Node* node_ptr, int k) { + if (node_ptr == nullptr || node_ptr->key == k) { + return node_ptr; } - if (k < n->key) { - return Find(n->left, k); + if (k < node_ptr->key) { + return Find(node_ptr->left, k); } - if (k > n->key) { - return Find(n->right, k); + if (k > node_ptr->key) { + return Find(node_ptr->right, k); } } \ No newline at end of file From 51c7c6ef4f80451bdf0b15e318590d6f09a9bfb7 Mon Sep 17 00:00:00 2001 From: dalnoboy75 <39648068+dalnoboy75@users.noreply.github.com> Date: Sat, 15 Jun 2024 12:24:03 +0000 Subject: [PATCH 12/14] fix --- task_05/src/qsort.hpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/task_05/src/qsort.hpp b/task_05/src/qsort.hpp index 58e09fbb..63752ae9 100644 --- a/task_05/src/qsort.hpp +++ b/task_05/src/qsort.hpp @@ -1,9 +1,8 @@ #pragma once #include -#include - #include +#include template void QuickSort(std::vector& arr, int l, int r) { From a64e53e7f7bcf3aa30b412fefe92ab2a12a566ba Mon Sep 17 00:00:00 2001 From: dalnoboy75 <39648068+dalnoboy75@users.noreply.github.com> Date: Sun, 16 Jun 2024 10:31:03 +0000 Subject: [PATCH 13/14] fix --- task_02/src/stack.cpp | 18 ++++++++++-------- task_02/src/stack.hpp | 4 ++-- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/task_02/src/stack.cpp b/task_02/src/stack.cpp index ad31f452..06af66bf 100644 --- a/task_02/src/stack.cpp +++ b/task_02/src/stack.cpp @@ -26,22 +26,24 @@ int Stack::GetTopValue() { return Top()->value_; } int Stack::Pop() { Node* t = top_; top_ = top_->prev_; - return t->value_; + int value = t->value_; + delete t; + return value; } Stack::Node* Stack::Top() { return top_; } void MinStack::Push(int value) { - if (s_.CheckTop()) { - m_.Push(value); + if (data_.CheckTop()) { + min_values_.Push(value); } else { - m_.Push(std::min(value, s_.GetTopValue())); + min_values_.Push(std::min(value, data_.GetTopValue())); } - s_.Push(value); + data_.Push(value); } int MinStack::Pop() { - int t = m_.Pop(); - return s_.Pop(); + int t = min_values_.Pop(); + return data_.Pop(); } -int MinStack::GetMin() { return m_.GetTopValue(); } \ No newline at end of file +int MinStack::GetMin() { return min_values_.GetTopValue(); } \ No newline at end of file diff --git a/task_02/src/stack.hpp b/task_02/src/stack.hpp index 084095bb..4445fcd1 100644 --- a/task_02/src/stack.hpp +++ b/task_02/src/stack.hpp @@ -23,6 +23,6 @@ class MinStack { int GetMin(); private: - Stack s_; - Stack m_; + Stack data_; + Stack min_values_; }; From de322b2c9cca4aad6a1df795e90461af4bd3b50c Mon Sep 17 00:00:00 2001 From: dalnoboy75 <39648068+dalnoboy75@users.noreply.github.com> Date: Sun, 16 Jun 2024 22:39:16 +0000 Subject: [PATCH 14/14] fix --- task_03/src/test.cpp | 7 +++++++ task_04/src/heap.cpp | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/task_03/src/test.cpp b/task_03/src/test.cpp index d7795c70..539ca88c 100644 --- a/task_03/src/test.cpp +++ b/task_03/src/test.cpp @@ -11,4 +11,11 @@ TEST(TempUpDays, Simple) { ASSERT_EQ(TempUpDayCounter(std::vector{2, 6, 17, 7, 3, 4}), (std::vector{1, 1, 0, 0, 1, 0})); ASSERT_EQ(TempUpDayCounter(std::vector{}), std::vector{}); + + ASSERT_EQ(TempUpDayCounter( + std::vector{70, 41, 86, 49, 31, 71, 39, 79, 24, 46}), + (std::vector{2, 1, 0, 2, 1, 2, 1, 0, 1, 0})); + ASSERT_EQ(TempUpDayCounter(std::vector{84, 44, 32, 65, 33, 11, 70, 57, + 73, 98, 52, 93}), + (std::vector{9, 2, 1, 3, 2, 1, 2, 1, 1, 0, 1, 0})); } diff --git a/task_04/src/heap.cpp b/task_04/src/heap.cpp index 831b0d40..fa06672e 100644 --- a/task_04/src/heap.cpp +++ b/task_04/src/heap.cpp @@ -5,7 +5,7 @@ #include void Heap::SiftUp(int index) { - while (heap_[index] < heap_[(index - 1) / 2]) { + while (index >= 0 && heap_[index] < heap_[(index - 1) / 2]) { std::swap(heap_[index], heap_[(index - 1) / 2]); index = (index - 1) / 2; }