Skip to content

Commit

Permalink
revert other
Browse files Browse the repository at this point in the history
  • Loading branch information
Matvey-cmd committed Apr 13, 2024
1 parent ccf0e42 commit 20840e3
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 131 deletions.
52 changes: 11 additions & 41 deletions task_02/src/stack.cpp
Original file line number Diff line number Diff line change
@@ -1,51 +1,21 @@
#include "stack.hpp"
#include <memory>
#include <stdexcept>

void Stack::Push(int value) {
std::shared_ptr<Node> p(new Node(value));
if (top == nullptr) {
top = p;
}
if (top != nullptr) {
p->next = top;
}
top = p;
}
#include <algorithm>

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

int Stack::Pop() {
if (top == nullptr) {
throw std::logic_error("Stack Underflow");
}
int val = top->value;
top = top->next;
return val;
auto result = data_.top();
data_.pop();
return result;
}

void MinStack::Push(int value) {
if (st1_.top == nullptr) {
st1_.Push(value);
st2_.Push(value);
return;
}

if (st1_.top->value > value) {
st2_.Push(value);
} else {
st1_.Push(value);
st2_.Push(st2_.top->value);
}
}
void MinStack::Push(int value) { data_.push_back(value); }

int MinStack::Pop() {
if (st1_.top == nullptr) {
throw std::logic_error("Stack Underflow");
}

int val = st1_.top->value;
st1_.top = st1_.top->next;
st2_.top = st2_.top->next;
return val;
auto result = data_.back();
data_.pop_back();
return result;
}

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

struct Node {
int value;
std::shared_ptr<Node> next;
Node(int value) : value(value) {}
};
#include <stack>
#include <vector>

class Stack {
public:
Stack() { top = nullptr; }
public:
void Push(int value);
int Pop();
std::shared_ptr<Node> top;

private:
std::stack<int> data_;
};

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

private:
Stack st1_;
Stack st2_;
private:
std::vector<int> data_;
};
48 changes: 24 additions & 24 deletions task_02/src/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,36 @@

TEST(StackTest, Simple) {
Stack stack;
stack.Push(1); // Stack [1]
ASSERT_EQ(stack.Pop(), 1); // Stack []
stack.Push(1); // Stack [1]
stack.Push(2); // Stack [1, 2]
ASSERT_EQ(stack.Pop(), 2); // Stack [1]
ASSERT_EQ(stack.Pop(), 1); // Stack []
stack.Push(1); // Stack [1]
stack.Push(2); // Stack [1, 2]
ASSERT_EQ(stack.Pop(), 2); // Stack [1]
stack.Push(3); // Stack [1, 3]
ASSERT_EQ(stack.Pop(), 3); // Stack [1]
ASSERT_EQ(stack.Pop(), 1); // Stack []
stack.Push(1); // Stack [1]
ASSERT_EQ(stack.Pop(), 1); // Stack []
stack.Push(1); // Stack [1]
stack.Push(2); // Stack [1, 2]
ASSERT_EQ(stack.Pop(), 2); // Stack [1]
ASSERT_EQ(stack.Pop(), 1); // Stack []
stack.Push(1); // Stack [1]
stack.Push(2); // Stack [1, 2]
ASSERT_EQ(stack.Pop(), 2); // Stack [1]
stack.Push(3); // Stack [1, 3]
ASSERT_EQ(stack.Pop(), 3); // Stack [1]
ASSERT_EQ(stack.Pop(), 1); // Stack []
}

TEST(MinStackTest, Simple) {
MinStack stack;
stack.Push(1); // Stack [1]
stack.Push(1); // Stack [1]
ASSERT_EQ(stack.GetMin(), 1);
ASSERT_EQ(stack.Pop(), 1); // Stack []
stack.Push(1); // Stack [1]
stack.Push(2); // Stack [1, 2]
ASSERT_EQ(stack.Pop(), 1); // Stack []
stack.Push(1); // Stack [1]
stack.Push(2); // Stack [1, 2]
ASSERT_EQ(stack.GetMin(), 1);
ASSERT_EQ(stack.Pop(), 2); // Stack [1]
ASSERT_EQ(stack.Pop(), 1); // Stack []
stack.Push(1); // Stack [1]
stack.Push(2); // Stack [1, 2]
ASSERT_EQ(stack.Pop(), 2); // Stack [1]
ASSERT_EQ(stack.Pop(), 1); // Stack []
stack.Push(1); // Stack [1]
stack.Push(2); // Stack [1, 2]
ASSERT_EQ(stack.GetMin(), 1);
ASSERT_EQ(stack.Pop(), 2); // Stack [1]
stack.Push(3); // Stack [1, 3]
ASSERT_EQ(stack.Pop(), 2); // Stack [1]
stack.Push(3); // Stack [1, 3]
ASSERT_EQ(stack.GetMin(), 1);
ASSERT_EQ(stack.Pop(), 3); // Stack [1]
ASSERT_EQ(stack.Pop(), 1); // Stack []
ASSERT_EQ(stack.Pop(), 3); // Stack [1]
ASSERT_EQ(stack.Pop(), 1); // Stack []
}
1 change: 0 additions & 1 deletion task_04/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include "heap.hpp"
#include <iostream>

int main() { return 0; }
19 changes: 3 additions & 16 deletions task_04/src/test.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,6 @@
#include "heap.hpp"

#include <gtest/gtest.h>

TEST(heap, Simple) {
ASSERT_EQ(Find_Minimum(std::vector<int>{1, 3, 5, 6, 2, 4}), 1);
ASSERT_EQ(Find_Minimum(std::vector<int>{11, 37, 55, 34, -6, 0, 4}), -6);
ASSERT_EQ(Heap_Ready(std::vector<int>{4, 1, 3, 2, 5}),
(std::vector<int>{1, 2, 3, 4, 5}));
ASSERT_EQ(Heap_Ready(std::vector<int>{3, 1, 5, 6, 2, 4}),
(std::vector<int>{1, 2, 4, 6, 3, 5}));
Heap heap;
heap.Build_heap(std::vector<int>{1, 3, 5, 7, 9, 12, 324, 5, 47, 457, 9467, -4,
758, -579, -4, 0});
ASSERT_EQ(heap.Find_Min(), -579);
heap.Insert(23);
heap.Insert(-1000);
ASSERT_EQ(heap.Extract_Min(), -1000);
ASSERT_EQ(heap.Find_Min(), -579);
TEST(TopologySort, Simple) {
ASSERT_EQ(1, 1); // Stack []
}
21 changes: 2 additions & 19 deletions task_05/src/test.cpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,6 @@

#include "sort.hpp"
#include <gtest/gtest.h>

TEST(Quick_Sort, Simple) {
std::vector<int> vec1{9, 8, 7, 6, 5, 4, 2, 3, 1, 0};
ASSERT_EQ(Quick_Sort(vec1, 0, 9),
(std::vector<int>{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}));
std::vector<int> vec2{10, 90, 30, 80, 60, 50};
ASSERT_EQ(Quick_Sort(vec2, 0, 5),
(std::vector<int>{10, 30, 50, 60, 80, 90}));
std::vector<int> vec3{12, 43, 15, 26, -1233, 346, 1325,
-56, -12, 78, 0, 3345, -34};
ASSERT_EQ(Quick_Sort(vec3, 0, 12),
(std::vector<int>{-1233, -56, -34, -12, 0, 12, 15, 26, 43, 78, 346,
1325, 3345}));
vec3.push_back(-10);
vec3.push_back(1000);
vec3.push_back(5);
ASSERT_EQ(Quick_Sort(vec3, 0, 15),
(std::vector<int>{-1233, -56, -34, -12, -10, 0, 5, 12, 15, 26, 43,
78, 346, 1000, 1325, 3345}));
TEST(TopologySort, Simple) {
ASSERT_EQ(1, 1); // Stack []
}
19 changes: 3 additions & 16 deletions task_06/src/test.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,6 @@
#include "k_stat.hpp"

#include <gtest/gtest.h>
#include <stdexcept>

TEST(k_stat, Simple) {
std::vector<int> vec1{14, -1, 5, 21, 6, -3, 7, 93, 9, 0, 84};
ASSERT_EQ(FindOrderStatistic(vec1, 8), 21);
ASSERT_EQ(FindOrderStatistic(vec1, 3), 5);
ASSERT_EQ(FindOrderStatistic(vec1, 10), 93);
EXPECT_THROW(FindOrderStatistic(vec1, 13), std::logic_error);
std::vector<int> vec2{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
ASSERT_EQ(FindOrderStatistic(vec2, 4), 5);
vec2.pop_back();
vec2.pop_back();
vec2.push_back(-12);
vec2.push_back(100);
vec2.push_back(134);
ASSERT_EQ(FindOrderStatistic(vec2, 9), 100);
TEST(TopologySort, Simple) {
ASSERT_EQ(1, 1); // Stack []
}

0 comments on commit 20840e3

Please sign in to comment.