-
Notifications
You must be signed in to change notification settings - Fork 15
Homework 2 #39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Homework 2 #39
Changes from 11 commits
74ad1a1
8bea497
23a9001
119edad
a588723
685222c
16ec7e1
d547e35
66bb2df
3b66e4f
60bc985
9504f9d
200b7cb
383f94e
51c7c6e
a64e53e
de322b2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| #include "utils.hpp" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| #include <iostream> | ||
|
|
||
| #include "stack.hpp" | ||
| int main() { return 0; } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,23 +1,23 @@ | ||
| #pragma once | ||
|
|
||
| #include <stack> | ||
| #include <vector> | ||
| struct Node { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. лучше перенести эту структуру в сам класс стека |
||
| int value_ = 0; | ||
| Node* prev_ = nullptr; | ||
| }; | ||
|
|
||
| class Stack { | ||
| public: | ||
| struct Stack { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. и тут лучше все-таки класс применить и наружу не светить Node, если исправишь будет чуть больше баллов за д.з. |
||
| void Push(int value); | ||
| int Pop(); | ||
| Node* Top(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Node это приватный тип, а Top публичный метод, довольно странно |
||
|
|
||
| private: | ||
| std::stack<int> data_; | ||
| Node* top_ = nullptr; | ||
| }; | ||
|
|
||
| class MinStack { | ||
| public: | ||
| struct MinStack { | ||
| void Push(int value); | ||
| int Pop(); | ||
| int GetMin(); | ||
|
|
||
| private: | ||
| std::vector<int> data_; | ||
| Stack s_; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. давай сделаем их приватными полями
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. давай еще нормальные имена дадим, не однобуквенные |
||
| Stack m_; | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| #include "temp_up_days.hpp" | ||
|
|
||
| #include <stack> | ||
|
|
||
| std::vector<int> TempUpDayCounter(std::vector<int> temps) { | ||
| std::stack<Day> days; | ||
| std::vector<int> 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; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| #pragma once | ||
| #include <vector> | ||
|
|
||
| struct Day { | ||
| int index_; | ||
| int temp_; | ||
| }; | ||
|
|
||
| std::vector<int> TempUpDayCounter(std::vector<int> temps); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,13 @@ | ||
|
|
||
| #include <gtest/gtest.h> | ||
|
|
||
| #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<int>{5, 7, 4, 5}), | ||
| (std::vector<int>{1, 0, 1, 0})); | ||
| ASSERT_EQ(TempUpDayCounter(std::vector<int>{5, 12, 4, 9, 5, 4, 2}), | ||
| (std::vector<int>{1, 0, 1, 0, 0, 0, 0})); | ||
| ASSERT_EQ(TempUpDayCounter(std::vector<int>{2, 6, 17, 7, 3, 4}), | ||
| (std::vector<int>{1, 1, 0, 0, 1, 0})); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. везде в ответах 0 или 1 давай еще добавим еще каких-нибудь чисел) |
||
| } | ||
|
LostPointer marked this conversation as resolved.
|
||
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| #include "heap.hpp" | ||
|
|
||
| #include <vector> | ||
|
|
||
| void Heap::SiftUp(int index) { | ||
| while (heap_[index] < heap_[(index - 1) / 2]) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. я бы добавил еще index != 0 |
||
| 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; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. давай 1 объявление 1 строчка? |
||
| int j = l; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. и тут j не понятно что за переменная |
||
| 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() { | ||
| 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); | ||
| this->SiftUp(heap_.size() - 1); | ||
| } | ||
|
|
||
| void Heap::Build(std::vector<int> data) { | ||
| for (auto x : data) { | ||
| this->Insert(x); | ||
| } | ||
| } | ||
|
|
||
| int FindMin(std::vector<int> data) { | ||
| Heap heap; | ||
| heap.Build(data); | ||
|
LostPointer marked this conversation as resolved.
|
||
| return heap.Min(); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| #pragma once | ||
|
|
||
| #include <vector> | ||
| struct Heap { | ||
| void SiftUp(int index); | ||
| void SiftDown(int index); | ||
| int Min(); | ||
| void Insert(int value); | ||
| void Build(std::vector<int> data); | ||
|
|
||
| std::vector<int> heap_; | ||
| }; | ||
|
|
||
| int FindMin(std::vector<int> data); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,9 @@ | ||
|
|
||
| #include <gtest/gtest.h> | ||
|
|
||
| TEST(TopologySort, Simple) { | ||
| ASSERT_EQ(1, 1); // Stack [] | ||
| #include "heap.hpp" | ||
|
|
||
| TEST(HeapMin, Simple) { | ||
| ASSERT_EQ(FindMin(std::vector<int>{4, 5, 16, 3, 6}), 3); | ||
| ASSERT_EQ(FindMin(std::vector<int>{29, 25, 10, 13, 14, 23, 4, 6}), 4); | ||
| ASSERT_EQ(FindMin(std::vector<int>{29, 17, 16, 27, 6, 11}), 6); | ||
| } | ||
|
LostPointer marked this conversation as resolved.
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,6 @@ | ||
| #include <iostream> | ||
| #include <vector> | ||
|
|
||
| #include "qsort.hpp" | ||
|
|
||
| int main() { return 0; } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| #pragma once | ||
|
|
||
| #include <algorithm> | ||
| #include <cstdlib> | ||
| #include <ctime> | ||
| #include <iostream> | ||
| #include <vector> | ||
|
|
||
| template <typename T> | ||
| int Partition(std::vector<T>& arr, int l, int r) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. можно унести в lib/src/util.hpp уберем дублирование кода) |
||
| 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 <typename T> | ||
| void QuickSort(std::vector<T>& arr, int l, int r) { | ||
| if (l < r) { | ||
| int q = Partition(arr, l, r); | ||
| QuickSort(arr, l, q); | ||
| QuickSort(arr, q + 1, r); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,30 @@ | ||
|
|
||
| #include <gtest/gtest.h> | ||
|
|
||
| TEST(TopologySort, Simple) { | ||
| ASSERT_EQ(1, 1); // Stack [] | ||
| #include <vector> | ||
|
|
||
| #include "qsort.hpp" | ||
|
|
||
| TEST(Qsort, Simple1) { | ||
| std::vector<int> vec = {77, 42, 19, 53, 18, 20}; | ||
| QuickSort<int>(vec, 0, vec.size() - 1); | ||
| ASSERT_EQ((vec), (std::vector<int>{18, 19, 20, 42, 53, 77})); | ||
| } | ||
|
|
||
| TEST(Qsort, Simple2) { | ||
| std::vector<double> vec2{12.75, 5.3, 1.1, 23.223, -13.1, 37.37}; | ||
| QuickSort<double>(vec2, 0, vec2.size() - 1); | ||
| ASSERT_EQ((vec2), | ||
| (std::vector<double>{-13.1, 1.1, 5.3, 12.75, 23.223, 37.37})); | ||
| } | ||
|
|
||
| TEST(Qsort, Simple3) { | ||
| std::vector<int> vec3 = {-1, 2, -1, 3, 4, 5, 2}; | ||
| QuickSort<int>(vec3, 0, vec3.size() - 1); | ||
| ASSERT_EQ((vec3), (std::vector<int>{-1, -1, 2, 2, 3, 4, 5})); | ||
| } | ||
|
|
||
| TEST(Qsort, Simple4) { | ||
| std::vector<int> vec4{2, 4, 2, 1, 3, 4, 1}; | ||
| QuickSort<int>(vec4, 0, vec4.size() - 1); | ||
| ASSERT_EQ((vec4), (std::vector<int>{1, 1, 2, 2, 3, 4, 4})); | ||
| } | ||
|
LostPointer marked this conversation as resolved.
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| #pragma once | ||
| #include <ctime> | ||
|
LostPointer marked this conversation as resolved.
|
||
| #include <vector> | ||
| template <class T> | ||
|
LostPointer marked this conversation as resolved.
|
||
| int Partition(std::vector<T>& data, int l, int r) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. а из util.hpp не получится использовать? |
||
| std::srand(std::time(nullptr)); | ||
| int pivotPos = l + std::rand() % (r - l); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pivot_pos |
||
| 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 <class T> | ||
| T FindKStatistics(std::vector<T>& 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]; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,29 @@ | ||
|
|
||
| #include <gtest/gtest.h> | ||
|
|
||
| TEST(TopologySort, Simple) { | ||
| ASSERT_EQ(1, 1); // Stack [] | ||
| #include <vector> | ||
|
|
||
| #include "order_stats.hpp" | ||
|
|
||
| TEST(OrderStats, Simple1) { | ||
| std::vector<int> v{50, 82, 11, 64, 66, 57, 35}; | ||
| ASSERT_EQ(FindKStatistics(v, 0, v.size(), 3), 57); | ||
| } | ||
| TEST(OrderStats, Simple2) { | ||
| std::vector<int> 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<int> 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<double> 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<char> v{'d', 's', 'a', 'b'}; | ||
| ASSERT_EQ(FindKStatistics(v, 0, v.size(), 1), 'b'); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
давай не будем использовать тут this, только усложняет чтение