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

Task 2 #24

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 21 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
6 changes: 5 additions & 1 deletion task_01/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#include <iostream>
#include <vector>

int main() { return 0; }
#include "util.hpp"
#include "utils.hpp"

int main() { return 0; }
19 changes: 16 additions & 3 deletions task_01/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 <stdexcept>
#include <vector>

TEST(TopologySort, Simple) {
ASSERT_EQ(1, 1); // Stack []
#include "utils.hpp"
TEST(utils, Simple) {
ASSERT_EQ(FindSummands(std::vector<int>{1, 2, 3, 4, 5, 7, 8, 12}, 7),
(std::pair<int, int>{2, 5}));
ASSERT_EQ(FindSummands(std::vector<int>{1, 2, 3, 4, 5, 7, 8, 12}, 13),
(std::pair<int, int>{1, 12}));
EXPECT_THROW(FindSummands(std::vector<int>{1, 2, 3, 4, 5, 7, 8, 12}, 21),
NoAnswer);
EXPECT_THROW(FindSummands(std::vector<int>{1, 2, 3, 4, 5, 7, 8, 12}, -1),
NoAnswer);
EXPECT_THROW(FindSummands(std::vector<int>{1, 1, 1, 1}, 3), NoAnswer);
ASSERT_EQ(FindSummands(std::vector<int>{1, 1, 1, 1}, 2),
(std::pair<int, int>{1, 1}));
EXPECT_THROW(FindSummands(std::vector<int>{}, 0), NoAnswer);
}
1 change: 0 additions & 1 deletion task_01/src/topology_sort.cpp

This file was deleted.

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

This file was deleted.

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

#include <cstddef>
#include <iostream>
#include <stdexcept>
#include <utility>
#include <vector>

std::pair<int, int> FindSummands(const std::vector<int> array, int number) {
int first_index = 0;
int second_index = array.size() - 1;

while (first_index < second_index) {
int first_summand = array[first_index];
int second_summand = array[second_index];
int sum = first_summand + second_summand;
if (sum == number)
return std::pair<int, int>{first_summand, second_summand};
if (sum < number) first_index++;
if (sum > number) second_index--;
}
throw NoAnswer("Unable to find the right elements");
}
11 changes: 11 additions & 0 deletions task_01/src/utils.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once
#include <stdexcept>
#include <utility>
#include <vector>

class NoAnswer : public std::runtime_error {
public:
using std::runtime_error::runtime_error;
};

std::pair<int, int> FindSummands(const std::vector<int> array, int number);
21 changes: 0 additions & 21 deletions task_02/src/stack.cpp

This file was deleted.

90 changes: 81 additions & 9 deletions task_02/src/stack.hpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,95 @@
#pragma once

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

template <typename T>
class Stack {
public:
void Push(int value);
int Pop();
explicit Stack() : top_(nullptr) {}
void Push(T data);
T Pop();
T Top() const;
size_t Size() const;

private:
std::stack<int> data_;
struct StackNode {
T data_;
std::shared_ptr<StackNode> next_;
explicit StackNode(T data, const std::shared_ptr<StackNode>& next)
: data_(data), next_(next) {}
};
size_t size_ = 0;
std::shared_ptr<StackNode> top_;
};

template <typename T>
class MinStack {
public:
void Push(int value);
int Pop();
int GetMin();
explicit MinStack() : data_stack_(), minimums_stack_() {}

void Push(T data);
T Pop();
T Top() const;
T GetMin() const;
size_t Size() const;

private:
std::vector<int> data_;
Stack<T> data_stack_;
Stack<T> minimums_stack_;
};
template <typename T>
void Stack<T>::Push(T data) {
top_ = std::make_shared<StackNode>(data, top_);
size_++;
}

template <typename T>
T Stack<T>::Pop() {
if (size_ == 0) throw std::out_of_range("Stack is empty");
T removed_value = top_->data_;
top_ = std::move(top_->next_);
size_--;
return removed_value;
}

template <typename T>
T Stack<T>::Top() const {
if (size_ == 0) throw std::out_of_range("Stack is empty");
return top_->data_;
}

template <typename T>
size_t Stack<T>::Size() const {
return size_;
}

template <typename T>
void MinStack<T>::Push(T data) {
data_stack_.Push(data);
if (minimums_stack_.Size() > 0 && data > minimums_stack_.Top())
minimums_stack_.Push(minimums_stack_.Top());
else
minimums_stack_.Push(data);
}

template <typename T>
T MinStack<T>::Pop() {
minimums_stack_.Pop();
return data_stack_.Pop();
}

template <typename T>
T MinStack<T>::GetMin() const {
return minimums_stack_.Top();
}

template <typename T>
size_t MinStack<T>::Size() const {
return data_stack_.Size();
}

template <typename T>
T MinStack<T>::Top() const {
return data_stack_.Top();
}
99 changes: 66 additions & 33 deletions task_02/src/test.cpp
LostPointer marked this conversation as resolved.
Show resolved Hide resolved
LostPointer marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,42 +1,75 @@

#include <gtest/gtest.h>

#include <stack>
#include <stdexcept>

#include "stack.hpp"

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 []
TEST(StackTest, PushAndPop) {
Stack<int> stack;
stack.Push(1);
stack.Push(2);
stack.Push(3);

ASSERT_EQ(stack.Size(), 3);
ASSERT_EQ(stack.Top(), 3);
ASSERT_EQ(stack.Pop(), 3);
ASSERT_EQ(stack.Size(), 2);
ASSERT_EQ(stack.Top(), 2);
ASSERT_EQ(stack.Pop(), 2);
ASSERT_EQ(stack.Size(), 1);
ASSERT_EQ(stack.Top(), 1);
ASSERT_EQ(stack.Pop(), 1);
ASSERT_EQ(stack.Size(), 0);
}

TEST(MinStackTest, Simple) {
MinStack stack;
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.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.GetMin(), 1);
ASSERT_EQ(stack.Pop(), 2); // Stack [1]
stack.Push(3); // Stack [1, 3]
TEST(StackTest, PopFromEmptyStack) {
Stack<int> stack;
ASSERT_THROW(stack.Pop(), std::out_of_range);
}

TEST(StackTest, TopFromEmptyStack) {
Stack<int> stack;
ASSERT_THROW(stack.Top(), std::out_of_range);
}

TEST(MinStackTest, PushAndPop) {
MinStack<int> stack;
stack.Push(3);
stack.Push(2);
stack.Push(1);
stack.Push(0);

ASSERT_EQ(stack.Size(), 4);
ASSERT_EQ(stack.Top(), 0);
ASSERT_EQ(stack.GetMin(), 0);
ASSERT_EQ(stack.Pop(), 0);
ASSERT_EQ(stack.Size(), 3);
ASSERT_EQ(stack.Top(), 1);
ASSERT_EQ(stack.GetMin(), 1);
ASSERT_EQ(stack.Pop(), 3); // Stack [1]
ASSERT_EQ(stack.Pop(), 1); // Stack []
}
ASSERT_EQ(stack.Pop(), 1);
ASSERT_EQ(stack.Size(), 2);
ASSERT_EQ(stack.Top(), 2);
ASSERT_EQ(stack.GetMin(), 2);
ASSERT_EQ(stack.Pop(), 2);
ASSERT_EQ(stack.Size(), 1);
ASSERT_EQ(stack.Top(), 3);
ASSERT_EQ(stack.GetMin(), 3);
ASSERT_EQ(stack.Pop(), 3);
ASSERT_EQ(stack.Size(), 0);
}

TEST(MinStackTest, PopFromEmptyStack) {
Copy link
Contributor

Choose a reason for hiding this comment

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

дубль, строка 31

MinStack<int> stack;
ASSERT_THROW(stack.Pop(), std::out_of_range);
}

TEST(MinStackTest, TopFromEmptyStack) {
MinStack<int> stack;
ASSERT_THROW(stack.Top(), std::out_of_range);
}

TEST(MinStackTest, GetMinFromEmptyStack) {
MinStack<int> stack;
ASSERT_THROW(stack.GetMin(), std::out_of_range);
}
31 changes: 28 additions & 3 deletions task_03/src/test.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,33 @@

#include <gtest/gtest.h>

#include "topology_sort.hpp"
#include <stdexcept>

TEST(TopologySort, Simple) {
ASSERT_EQ(1, 1); // Stack []
#include "warming_prediction.hpp"
TEST(warming_prediction, Simple) {
ASSERT_EQ(DoWarmingPrediction(std::vector<int>{23, 29, 12, 16, 45, 10, -8,
-39, 35, 6, 4, -11}),
(std::vector<unsigned int>{1, 3, 1, 1, 0, 3, 2, 1, 0, 0, 0, 0}));

ASSERT_EQ(DoWarmingPrediction(std::vector<int>{-32, -5, 34, -37, -48, -17, 33,
27, 18, -36, 22, 32}),
(std::vector<unsigned int>{1, 1, 0, 2, 1, 1, 0, 4, 2, 1, 1, 0}));

ASSERT_EQ(DoWarmingPrediction(std::vector<int>{34, -49, 7, -44, 25, -45, -29,
-14, 2, -30, 34, -36}),
(std::vector<unsigned int>{0, 1, 2, 1, 6, 1, 1, 1, 2, 1, 0, 0}));

ASSERT_EQ(DoWarmingPrediction(std::vector<int>{}),
(std::vector<unsigned int>{}));

ASSERT_EQ(
DoWarmingPrediction(std::vector<int>{32, 11, 11, 10, 9, 7, 5, 3, 1}),
(std::vector<unsigned int>{0, 0, 0, 0, 0, 0, 0, 0, 0}));

ASSERT_EQ(DoWarmingPrediction(
std::vector<float>{4, 12, 8, 7, 9, 1, 11, 9, 9, 10, 11, 12}),
(std::vector<unsigned int>{1, 0, 2, 1, 2, 1, 5, 2, 1, 1, 1, 0}));

EXPECT_THROW(DoWarmingPrediction(std::vector<std::string>{"1"}),
std::logic_error);
}
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.

26 changes: 26 additions & 0 deletions task_03/src/warming_prediction.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once
#include <stack>
#include <stdexcept>
#include <vector>

template <typename T>
std::vector<unsigned int> DoWarmingPrediction(
const std::vector<T>& temperature_data) {
if (!std::is_arithmetic<T>::value)
throw std::logic_error("Invalid data type");
std::stack<unsigned int> unpredictable_days;
std::vector<unsigned int> days_of_waiting(temperature_data.size());
for (unsigned int current_day = 0; current_day != temperature_data.size();
current_day++) {
while (!unpredictable_days.empty() &&
temperature_data[current_day] >
temperature_data[unpredictable_days.top()]) {
unsigned int last_unpredictable_day = unpredictable_days.top();
days_of_waiting[last_unpredictable_day] =
current_day - last_unpredictable_day;
unpredictable_days.pop();
}
unpredictable_days.push(current_day);
}
return days_of_waiting;
}
Loading
Loading