Skip to content

Hw 2 #51

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 9 commits into
base: main
Choose a base branch
from
Open

Hw 2 #51

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 33 additions & 8 deletions task_02/src/stack.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,46 @@
#include "stack.hpp"

#include <algorithm>
#include <stdexcept>

void Stack::Push(int value) { data_.push(value); }
void Stack::Push(int value) {
Node* new_top = new Node();
new_top->value = value;
if (top == nullptr) {
top = new_top;
} else {
new_top->prev = top;
top = new_top;
}
}

int Stack::Pop() {
auto result = data_.top();
data_.pop();
if (top == nullptr) {
throw std::out_of_range("Stack is out of elements");
}
int result = top->value;
top = top->prev;
return result;
}

void MinStack::Push(int value) { data_.push_back(value); }
void MinStack::Push(int value) {
Stack::Push(value);

Node* new_min_top = new Node();
if (min_top == nullptr) {
new_min_top->value = value;
min_top = new_min_top;
} else {
new_min_top->value = std::min(value, min_top->value);
new_min_top->prev = min_top;
min_top = new_min_top;
}
}

int MinStack::Pop() {
auto result = data_.back();
data_.pop_back();
return result;
int a = Stack::Pop();
min_top = min_top->prev;
return a;
}

int MinStack::GetMin() { return *std::min_element(data_.begin(), data_.end()); }
int MinStack::GetMin() { return min_top->value; }
13 changes: 9 additions & 4 deletions task_02/src/stack.hpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
#pragma once

#include <stack>
#include <vector>

class Stack {
public:
struct Node {
int value;
Node* prev = nullptr;
};

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

private:
std::stack<int> data_;
Node* top = nullptr;
};

class MinStack {
class MinStack : public Stack {
public:
void Push(int value);
int Pop();
int GetMin();

private:
std::vector<int> data_;
Node* top = nullptr;
Node* min_top = nullptr;
};
18 changes: 17 additions & 1 deletion task_03/src/test.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@

#include <gtest/gtest.h>

#include <stdexcept>
#include <vector>

#include "topology_sort.hpp"

TEST(TopologySort, Simple) {
ASSERT_EQ(1, 1); // Stack []
ASSERT_EQ(find_higher_temp({0, 2, 6, 4, 8, 4, 6, 0, 3, -3, -2, -6, 1}),
std::vector<int>({1, 1, 2, 1, 0, 1, 0, 1, 0, 1, 2, 1, 0}));
ASSERT_EQ(find_higher_temp({0, 1, 2, 3, 4, 5, 4, 3, 2, 1, 0}),
std::vector<int>({1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0}));
ASSERT_EQ(find_higher_temp({0, 1, -1, 0, 1, -1, 0, 1}),
std::vector<int>({1, 0, 1, 1, 0, 1, 1, 0}));
ASSERT_EQ(find_higher_temp({1}), std::vector<int>({0}));
ASSERT_EQ(find_higher_temp({-1, -1, -1, -3, -3, -3, -2, -2, -2, 0, 0, 0}),
std::vector<int>({9, 8, 7, 3, 2, 1, 3, 2, 1, 0, 0, 0}));
ASSERT_EQ(find_higher_temp({0, 0, 0, 0, 0, 0, 0}),
std::vector<int>({0, 0, 0, 0, 0, 0, 0}));
ASSERT_EQ(find_higher_temp({}), std::vector<int>({}));
ASSERT_EQ(find_higher_temp({1, 5, 2, 3, 4, 0}),
std::vector<int>({1, 0, 1, 1, 0, 0}));
}
28 changes: 28 additions & 0 deletions task_03/src/topology_sort.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,29 @@
#include "topology_sort.hpp"

#include <stack>
#include <stdexcept>
#include <vector>

std::vector<int> find_higher_temp(std::vector<int> temp_list) {
if (temp_list.size() == 0) {
return {};
}

std::vector<int> ans_vect = std::vector<int>(temp_list.size(), 0);
std::stack<int> day_stack;

for (int i_day = temp_list.size() - 1; i_day >= 0; i_day--) {
while (!day_stack.empty()) {
if (temp_list[day_stack.top()] <= temp_list[i_day]) {
day_stack.pop();
} else {
break;
}
}
if (!day_stack.empty()) {
ans_vect[i_day] = day_stack.top() - i_day;
}
day_stack.push(i_day);
}
return ans_vect;
}
4 changes: 4 additions & 0 deletions task_03/src/topology_sort.hpp
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
#pragma once

#include <vector>

std::vector<int> find_higher_temp(std::vector<int> temp_vect);
101 changes: 101 additions & 0 deletions task_04/src/bin_heap.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#include "bin_heap.hpp"

BinHeap::BinHeap() {
heap = {};
size = 0;
}

BinHeap::BinHeap(std::vector<int> list) {
heap = list;
size = list.size();
heapify();
}

std::vector<int> BinHeap::get_heap() { return heap; }

void BinHeap::swap(int i, int j) {
if (i >= size or i < 0 or j >= size or j < 0) {
throw std::range_error("swap: indexes must be in range");
}

int i_val = heap[i];
heap[i] = heap[j];
heap[j] = i_val;
}

void BinHeap::swap(int i, char key) {
if (key != 'l' and key != 'r') {
throw std::invalid_argument("swap: Key must be 'l' or 'r'");
}

if (key == 'l') {
swap(i, i * 2 + 1);
}
if (key == 'r') {
swap(i, i * 2 + 2);
}
}

void BinHeap::heapify() {
for (int i = size / 2 - 1; i >= 0; i--) {
heapify(i);
}
}

void BinHeap::heapify(int i) {
if (i >= size or i < 0) {
return;
}

int l = 2 * i + 1;
int r = 2 * i + 2;
int largest = i;
if (l < size and heap[l] > heap[largest]) {
largest = l;
}
if (r < size and heap[r] > heap[largest]) {
largest = r;
}
if (largest != i) {
swap(i, largest);
heapify(largest);
}
}

void BinHeap::insert_key_value(int key, int value) {
if (key >= size or key < 0) {
throw std::range_error("insert_key_value: index must be in range");
}

if (value <= heap[key]) {
heap[key] = value;
heapify(key);

} else {
heap[key] = value;
for (int i = key; i > 0 and heap[i] > heap[(i - 1) / 2]; i = (i - 1) / 2) {
swap(i, (i - 1) / 2);
}
}
}

void BinHeap::push_value(int value) {
size++;
heap.push_back(std::numeric_limits<int>::lowest());
insert_key_value(size - 1, value);
}

int BinHeap::pop_max() {
if (size < 1) {
throw std::length_error("pop_max: heap is empty");
}

int max_value = heap[0];
heap[0] = heap[size - 1];
heap.pop_back();
size -= 1;

heapify(0);

return max_value;
}
29 changes: 29 additions & 0 deletions task_04/src/bin_heap.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

#include <numeric>
#include <stdexcept>
#include <vector>

class BinHeap {
public:
std::vector<int> heap;
int size;

BinHeap();
BinHeap(std::vector<int> list);

std::vector<int> get_heap();

void insert_key_value(int key, int value);
void push_value(int value);
int pop_max();

private:
void swap(int i, int j); // heap[i] <-> heap[j]
void swap(
int i,
char key); // swaps heap[i] with his left ('l') or right ('r') child

void heapify(); // make whole heap a binary heap
void heapify(int i); // fix heap to be a binart heap (at 'i')
};
35 changes: 32 additions & 3 deletions task_04/src/test.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,35 @@

#include <gtest/gtest.h>

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

#include "bin_heap.hpp"

TEST(BinHeap, Simple) {
BinHeap heap1({1, 2, 3, 4, 5, 6, 7});
ASSERT_EQ(heap1.get_heap(), std::vector<int>({7, 5, 6, 4, 2, 1, 3}));
heap1.push_value(8);
heap1.push_value(3);
heap1.push_value(3);
ASSERT_EQ(heap1.pop_max(), 8);
heap1.insert_key_value(0, 11);
ASSERT_EQ(heap1.pop_max(), 11);
heap1.insert_key_value(7, 20);
ASSERT_EQ(heap1.pop_max(), 20);
ASSERT_EQ(heap1.get_heap(), std::vector<int>({6, 5, 3, 4, 3, 1, 3}));
heap1.insert_key_value(4, -13);
heap1.insert_key_value(4, 7);
ASSERT_EQ(heap1.get_heap(), std::vector<int>({7, 6, 3, 4, 5, 1, 3}));

BinHeap heap2({0});
ASSERT_EQ(heap2.pop_max(), 0);
ASSERT_ANY_THROW(heap2.pop_max());
heap2.push_value(13);
heap2.push_value(13);
heap2.push_value(13);
heap2.push_value(14);
heap2.push_value(13);
heap2.push_value(13);
heap2.insert_key_value(0, 13);
ASSERT_EQ(heap2.pop_max(), 13);
ASSERT_EQ(heap2.get_heap(), std::vector<int>({13, 13, 13, 13, 13}));
}
Loading
Loading