-
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 13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#pragma once | ||
#include <algorithm> | ||
#include <cstdlib> | ||
#include <ctime> | ||
#include <vector> | ||
|
||
template <typename T> | ||
int Partition(std::vector<T>& 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; | ||
} |
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,24 @@ | ||
#pragma once | ||
|
||
#include <stack> | ||
#include <vector> | ||
|
||
class Stack { | ||
private: | ||
struct Node { | ||
int value_ = 0; | ||
Node* prev_ = nullptr; | ||
}; | ||
Node* top_ = nullptr; | ||
|
||
public: | ||
void Push(int value); | ||
int Pop(); | ||
|
||
private: | ||
std::stack<int> data_; | ||
Node* Top(); | ||
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 публичный метод, довольно странно |
||
}; | ||
|
||
class MinStack { | ||
public: | ||
struct MinStack { | ||
void Push(int value); | ||
int Pop(); | ||
int GetMin(); | ||
|
||
private: | ||
std::vector<int> data_; | ||
Stack s_; | ||
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. давай сделаем их приватными полями 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,14 @@ | ||
|
||
#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})); | ||
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 давай еще добавим еще каких-нибудь чисел) |
||
ASSERT_EQ(TempUpDayCounter(std::vector<int>{}), std::vector<int>{}); | ||
} | ||
LostPointer marked this conversation as resolved.
Show resolved
Hide resolved
|
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#include "heap.hpp" | ||
|
||
#include <vector> | ||
|
||
void Heap::SiftUp(int index) { | ||
while (heap_[index] < heap_[(index - 1) / 2]) { | ||
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; | ||
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. давай по длинее имена переменных сделаем: left |
||
int r = 2 * index + 2; | ||
int j = l; | ||
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.
Show resolved
Hide 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); | ||
} |
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,15 @@ | ||
#pragma once | ||
|
||
#include <iostream> | ||
#include <vector> | ||
|
||
#include "../../lib/src/util.hpp" | ||
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. а просто |
||
|
||
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,36 @@ | ||
|
||
#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.
Show resolved
Hide resolved
|
||
TEST(Qsort, Simple5) { | ||
std::vector<int> vec5{}; | ||
QuickSort<int>(vec5, 0, vec5.size() - 1); | ||
ASSERT_EQ((vec5), std::vector<int>{}); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#pragma once | ||
|
||
#include <ctime> | ||
LostPointer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#include <vector> | ||
|
||
template <class T> | ||
LostPointer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
int Partition(std::vector<T>& data, int l, int r) { | ||
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); | ||
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]; | ||
} |
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, только усложняет чтение