Skip to content

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

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
27 changes: 27 additions & 0 deletions lib/src/util.hpp
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;
}
2 changes: 1 addition & 1 deletion task_01/src/test.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

#include <gtest/gtest.h>

#include "topology_sort.hpp"
#include "utils.hpp"

TEST(TopologySort, Simple) {
ASSERT_EQ(1, 1); // Stack []
Expand Down
1 change: 0 additions & 1 deletion task_01/src/topology_sort.cpp

This file was deleted.

1 change: 1 addition & 0 deletions task_01/src/utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "utils.hpp"
File renamed without changes.
1 change: 1 addition & 0 deletions task_02/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <iostream>

#include "stack.hpp"
int main() { return 0; }
34 changes: 25 additions & 9 deletions task_02/src/stack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,36 @@

#include <algorithm>

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_;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

давай не будем использовать тут this, только усложняет чтение

return t->value_;
}

void MinStack::Push(int value) { data_.push_back(value); }
Stack::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()); }
int MinStack::GetMin() { return m_.Top()->value_; }
21 changes: 11 additions & 10 deletions task_02/src/stack.hpp
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();
Copy link
Contributor

Choose a reason for hiding this comment

The 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_;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

давай сделаем их приватными полями

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

давай еще нормальные имена дадим, не однобуквенные

Stack m_;
};
23 changes: 23 additions & 0 deletions task_03/src/temp_up_days.cpp
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;
}
9 changes: 9 additions & 0 deletions task_03/src/temp_up_days.hpp
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);
12 changes: 9 additions & 3 deletions task_03/src/test.cpp
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}));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

везде в ответах 0 или 1 давай еще добавим еще каких-нибудь чисел)

ASSERT_EQ(TempUpDayCounter(std::vector<int>{}), std::vector<int>{});
}
1 change: 0 additions & 1 deletion task_03/src/topology_sort.cpp

This file was deleted.

1 change: 0 additions & 1 deletion task_03/src/topology_sort.hpp

This file was deleted.

51 changes: 51 additions & 0 deletions task_04/src/heap.cpp
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]) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

я бы добавил еще index != 0
что бы было явно понятно что в отрицательные числа не пойдем (можно просто 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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

давай по длинее имена переменных сделаем: left

int r = 2 * index + 2;
int j = l;
Copy link
Contributor

Choose a reason for hiding this comment

The 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);
return heap.Min();
}
14 changes: 14 additions & 0 deletions task_04/src/heap.hpp
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);
9 changes: 6 additions & 3 deletions task_04/src/test.cpp
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);
}
3 changes: 3 additions & 0 deletions task_05/src/main.cpp
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; }
Empty file added task_05/src/qsort.cpp
Empty file.
15 changes: 15 additions & 0 deletions task_05/src/qsort.hpp
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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

а просто #include <util.hpp> не работает?


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);
}
}
36 changes: 33 additions & 3 deletions task_05/src/test.cpp
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}));
}

TEST(Qsort, Simple5) {
std::vector<int> vec5{};
QuickSort<int>(vec5, 0, vec5.size() - 1);
ASSERT_EQ((vec5), std::vector<int>{});
}
41 changes: 41 additions & 0 deletions task_06/src/order_stats.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#pragma once

#include <ctime>
#include <vector>

template <class T>
int Partition(std::vector<T>& data, int l, int r) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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);
Copy link
Contributor

Choose a reason for hiding this comment

The 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];
}
Loading
Loading