Skip to content
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

Homework 2 #39

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; }
46 changes: 37 additions & 9 deletions task_02/src/stack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,48 @@

#include <algorithm>

void Stack::Push(int value) { data_.push(value); }
void Stack::Push(int value) {
Node* t = new Node();
t->value_ = value;
if (top_ == nullptr) {
top_ = t;
} else {
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() {
auto result = data_.top();
data_.pop();
return result;
Node* t = top_;
LostPointer marked this conversation as resolved.
Show resolved Hide resolved
top_ = top_->prev_;
int value = t->value_;
delete t;
return value;
}

void MinStack::Push(int value) { data_.push_back(value); }
Stack::Node* Stack::Top() { return top_; }
void MinStack::Push(int value) {
if (data_.CheckTop()) {
min_values_.Push(value);
} else {
min_values_.Push(std::min(value, data_.GetTopValue()));
}
data_.Push(value);
}

int MinStack::Pop() {
auto result = data_.back();
data_.pop_back();
return result;
int t = min_values_.Pop();
return data_.Pop();
}

int MinStack::GetMin() { return *std::min_element(data_.begin(), data_.end()); }
int MinStack::GetMin() { return min_values_.GetTopValue(); }
19 changes: 12 additions & 7 deletions task_02/src/stack.hpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
#pragma once

#include <stack>
#include <vector>

class Stack {
private:
struct Node {
int value_ = 0;
Node* prev_ = nullptr;
};
Node* top_ = nullptr;
Node* Top();

public:
void Push(int value);
int Pop();

private:
std::stack<int> data_;
bool CheckTop();
int GetTopValue();
};

class MinStack {
Expand All @@ -19,5 +23,6 @@ class MinStack {
int GetMin();

private:
std::vector<int> data_;
Stack data_;
Stack min_values_;
};
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);
19 changes: 16 additions & 3 deletions task_03/src/test.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@

#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>{});

ASSERT_EQ(TempUpDayCounter(
std::vector<int>{70, 41, 86, 49, 31, 71, 39, 79, 24, 46}),
(std::vector<int>{2, 1, 0, 2, 1, 2, 1, 0, 1, 0}));
ASSERT_EQ(TempUpDayCounter(std::vector<int>{84, 44, 32, 65, 33, 11, 70, 57,
73, 98, 52, 93}),
(std::vector<int>{9, 2, 1, 3, 2, 1, 2, 1, 1, 0, 1, 0}));
}
LostPointer marked this conversation as resolved.
Show resolved Hide resolved
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.

61 changes: 61 additions & 0 deletions task_04/src/heap.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include "heap.hpp"

#include <cstddef>
#include <stdexcept>
#include <vector>

void Heap::SiftUp(int index) {
while (index >= 0 && 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 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_[small_child_index]) {
break;
}
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();
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 Heap::Size() { return heap_.size(); }

int Heap::Top() {
if (Size() == 0) throw std::runtime_error("Empty Heap");
return heap_[0];
}

int FindMin(std::vector<int> data) {
Heap heap;
heap.Build(data);
LostPointer marked this conversation as resolved.
Show resolved Hide resolved
return heap.Min();
}
18 changes: 18 additions & 0 deletions task_04/src/heap.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include <vector>
class Heap {
public:
int Top();
int Size();
int Min();
void Insert(int value);
void Build(std::vector<int> data);

private:
std::vector<int> heap_;
void SiftUp(int index);
void SiftDown(int index);
};

int FindMin(std::vector<int> data);
33 changes: 30 additions & 3 deletions task_04/src/test.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,33 @@

#include <gtest/gtest.h>

TEST(TopologySort, Simple) {
ASSERT_EQ(1, 1); // Stack []
#include <stdexcept>

#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.
Show resolved Hide resolved
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);
}
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.
14 changes: 14 additions & 0 deletions task_05/src/qsort.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

#include <iostream>
#include <util.hpp>
#include <vector>

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}));
}

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>{});
}
Loading
Loading