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

Done homework_1 #18

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
20 changes: 20 additions & 0 deletions task_01/src/get_two_nums.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "get_two_nums.hpp"

std::pair<long long, long long> GetTwoNums(long long number,
std::vector<long long> nums) {
if (nums.size() <= 1) {
throw std::length_error("The array size must be greater than 1.");
}
size_t left = 0;
size_t right = nums.size() - 1;
while (left != right) {
if (nums[left] + nums[right] == number)
return {nums[left], nums[right]};
else if (nums[left] + nums[right] < number)
left++;
else if (nums[left] + nums[right] > number)
right--;
}
throw std::logic_error(
"There are no two elements, which add up to a given number.");
}
7 changes: 7 additions & 0 deletions task_01/src/get_two_nums.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

#include <iostream>
#include <vector>

std::pair<long long, long long> GetTwoNums(long long number,
std::vector<long long> nums);
43 changes: 42 additions & 1 deletion task_01/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,44 @@
#include <iostream>
#include <stdexcept>
#include <vector>

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

size_t GetSize() {
size_t length;
std::cout << "Enter a length of array: ";
std::cin >> length;
if (length <= 1) {
std::cout << "You need to enter a number greater than 1\n";
GetSize();
}
return length;
};

long long GetNumber() {
long long number;
std::cout << "Enter a number: ";
std::cin >> number;
return number;
};

std::vector<long long> GetArray(size_t length) {
std::vector<long long> nums;
std::cout << "Enter the array: ";
for (int i = 0; i < length; i++) {
int number;
std::cin >> number;
nums.push_back(number);
}
return nums;
}

int main() {
long long number = GetNumber();
size_t length = GetSize();
std::vector<long long> nums = GetArray(length);
std::pair<long long, long long> res = GetTwoNums(number, nums);
std::cout << "Two numbers, which add up to a given number: " << res.first
<< ", " << res.second;
return 0;
}
82 changes: 78 additions & 4 deletions task_01/src/test.cpp
LostPointer marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,8 +1,82 @@

#include <gtest/gtest.h>

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

#include "get_two_nums.hpp"

TEST(GetTwoNums, Test_1) {
ASSERT_EQ(GetTwoNums(15, std::vector<long long>{5, 10, 15, 20, 25}),
(std::pair<long long, long long>{5, 10}));
}

TEST(GetTwoNums, Test_2) {
ASSERT_EQ(
GetTwoNums(18, std::vector<long long>{1, 2, 3, 5, 7, 8, 10, 14, 19, 25}),
(std::pair<long long, long long>{8, 10}));
}

TEST(GetTwoNums, Test_3) {
ASSERT_EQ(GetTwoNums(-20, std::vector<long long>{-100, -98, -72, -54, -11, 0,
10, 20, 34, 60}),
(std::pair<long long, long long>{-54, 34}));
}

TEST(GetTwoNums, Test_4) {
ASSERT_EQ(
GetTwoNums(-6, std::vector<long long>{-120, -100, -84, -20, -11, -5, -1}),
(std::pair<long long, long long>{-5, -1}));
}

TEST(GetTwoNums, Test_5) {
ASSERT_EQ(
GetTwoNums(21000000000,
std::vector<long long>{1000000000, 2000000000, 4000000000,
8000000000, 11000000000, 13000000000}),
(std::pair<long long, long long>{8000000000, 13000000000}));
}

TEST(GetTwoNums, Test_6) {
ASSERT_EQ(
GetTwoNums(
11, std::vector<long long>{0, 0, 0, 0, 0, 1, 1, 2, 2, 2, 2, 3,
6, 7, 8, 9, 9, 9, 12, 13, 15, 15, 16, 17}),
(std::pair<long long, long long>{2, 9}));
}

TEST(GetTwoNums, Test_7) {
ASSERT_EQ(GetTwoNums(100000000000000,
std::vector<long long>{
10000000000000, 20000000000000, 30000000000000,
30000000000000, 50000000000000, 50000000000000,
60000000000000, 110000000000000, 110000000000000,
110000000000000}),
(std::pair<long long, long long>{50000000000000, 50000000000000}));
}

TEST(GetTwoNums, Test_8) {
ASSERT_EQ(GetTwoNums(84, std::vector<long long>{-12, -6, -4, 1, 23, 25, 26,
33, 38, 46, 52, 58, 64, 69,
76, 82, 97, 101}),
(std::pair<long long, long long>{26, 58}));
}

TEST(GetTwoNums, Test_9) {
EXPECT_THROW(GetTwoNums(2, std::vector<long long>{2}), std::length_error);
}

TEST(GetTwoNums, Test_10) {
EXPECT_THROW(GetTwoNums(100, std::vector<long long>{}), std::length_error);
}

TEST(GetTwoNums, Test_11) {
EXPECT_THROW(GetTwoNums(5, std::vector<long long>{1, 2, 5, 7, 9, 11, 23, 55}),
std::logic_error);
}

TEST(TopologySort, Simple) {
ASSERT_EQ(1, 1); // Stack []
TEST(GetTwoNums, Test_12) {
EXPECT_THROW(GetTwoNums(-10, std::vector<long long>{1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14}),
std::logic_error);
}
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.

7 changes: 6 additions & 1 deletion task_02/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
#include <iostream>

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

int main() {
MinStack<int> stack;
stack.Push(1);
}
21 changes: 0 additions & 21 deletions task_02/src/stack.cpp

This file was deleted.

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

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

template <typename T>
struct StackElem {
explicit StackElem(T value, const std::shared_ptr<StackElem>& prev)
: value_{value}, prev_{prev} {}

T value_;
std::shared_ptr<StackElem> prev_;
};

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

void Push(T value);
T Pop();

T Top() const {
if (IsEmpty()) throw std::out_of_range("Stack is empty!");
return top_->value_;
}
size_t Size() const { return size_; }
bool IsEmpty() const { return size_ == 0; }

private:
std::stack<int> data_;
std::shared_ptr<StackElem<T>> top_;
size_t size_ = 0;
};

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

void Push(T value);
T Pop();

T Top() const { return data_.Top(); }
T GetMin() const { return mins_data_.Top(); }
size_t Size() const { return data_.Size(); }
bool IsEmpty() const { return data_.IsEmpty(); }

private:
std::vector<int> data_;
Stack<T> data_;
Stack<T> mins_data_;
};

template <typename T>
void Stack<T>::Push(T value) {
top_ = std::make_shared<StackElem<T>>(value, top_);
size_++;
}

template <typename T>
T Stack<T>::Pop() {
if (IsEmpty()) throw std::out_of_range("Stack is empty!");
T res = top_->value_;
top_ = std::move(top_->prev_);
size_--;
return res;
}

template <typename T>
void MinStack<T>::Push(T value) {
data_.Push(value);
if (mins_data_.Size() > 0 && mins_data_.Top() < value)
mins_data_.Push(mins_data_.Top());
else
mins_data_.Push(value);
}

template <typename T>
T MinStack<T>::Pop() {
mins_data_.Pop();
return data_.Pop();
}
21 changes: 18 additions & 3 deletions task_02/src/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "stack.hpp"

TEST(StackTest, Simple) {
Stack stack;
Stack<int> stack;
stack.Push(1); // Stack [1]
ASSERT_EQ(stack.Pop(), 1); // Stack []
stack.Push(1); // Stack [1]
Expand All @@ -22,7 +22,7 @@ TEST(StackTest, Simple) {
}

TEST(MinStackTest, Simple) {
MinStack stack;
MinStack<int> stack;
stack.Push(1); // Stack [1]
ASSERT_EQ(stack.GetMin(), 1);
ASSERT_EQ(stack.Pop(), 1); // Stack []
Expand All @@ -39,4 +39,19 @@ TEST(MinStackTest, Simple) {
ASSERT_EQ(stack.GetMin(), 1);
ASSERT_EQ(stack.Pop(), 3); // Stack [1]
ASSERT_EQ(stack.Pop(), 1); // Stack []
}
}

TEST(StackTest, Empty) {
Stack<double> stack;
EXPECT_THROW(stack.Pop(), std::out_of_range);
}

TEST(MinStackTest, Empty) {
MinStack<int> stack;
EXPECT_THROW(stack.Pop(), std::out_of_range);
}

TEST(MinStackTest, Empty_2) {
MinStack<int> stack;
EXPECT_THROW(stack.GetMin(), std::out_of_range);
}
25 changes: 25 additions & 0 deletions task_03/src/temperature.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "temperature.hpp"

vector<size_t> DaysBeforeWarming(vector<short> temperatures) {
vector<size_t> days(temperatures.size());

if (temperatures.empty()) return days;

for (size_t i = 0; i < temperatures.size() - 1; i++) {
// Если у предыдущего дня температура больше и кол-во дней до потепления
// больше 1, то для текущего дня можно не считать кол-во дней
if (i > 0 && temperatures[i - 1] > temperatures[i] && days[i - 1] > 1)
days[i] = temperatures[i - 1] - 1;
// Иначе считаем кол-во дней по-честному
else {
for (size_t j = i; j < temperatures.size(); j++) {
if (temperatures[j] > temperatures[i]) break;
days[i]++;
}
// Если мы вышли из цикла без break, значит потепления не было на
// протяжении всех дней
if (days[i] == temperatures.size() - i) days[i] = 0;
}
}
return days;
}
5 changes: 5 additions & 0 deletions task_03/src/temperature.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once
#include <vector>
using namespace std;

vector<size_t> DaysBeforeWarming(vector<short> temperatures);
Loading
Loading