Skip to content

Task 1 #20

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

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

int main() {
int size;
std::cin >> size;
std::vector<int> v(size);
for (int i = 0; i < v.size(); i++) {
std::cin >> v[i];
}
int targetSum;
std::cin >> targetSum;
std::pair<int, int> ans = FindTargetSumInArray(v, targetSum);
if (ans.first != -1)
std::cout << ans.first << " " << ans.second;
else
std::cout << "Not found";

return 0;
}
31 changes: 28 additions & 3 deletions task_01/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 <utility>
#include <vector>

#include "utils.hpp"

TEST(TopologySort, Simple) {
ASSERT_EQ(1, 1); // Stack []
}
std::vector<int> v1{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
ASSERT_EQ(FindTargetSumInArray(v1, 0), std::make_pair(-1, -1));
ASSERT_EQ(FindTargetSumInArray(v1, 1), std::make_pair(0, 1));
ASSERT_EQ(FindTargetSumInArray(v1, 9), std::make_pair(0, 9));
ASSERT_EQ(FindTargetSumInArray(v1, 11), std::make_pair(1, 10));
ASSERT_EQ(FindTargetSumInArray(v1, -5), std::make_pair(-1, -1));

std::vector<int> v2{0};
ASSERT_EQ(FindTargetSumInArray(v2, 0), std::make_pair(-1, -1));
ASSERT_EQ(FindTargetSumInArray(v2, 47), std::make_pair(-1, -1));
ASSERT_EQ(FindTargetSumInArray(v2, -47), std::make_pair(-1, -1));

std::vector<int> v3{-11, -6, 0, 1, 2, 4, 10, 16};
ASSERT_EQ(FindTargetSumInArray(v3, -6), std::make_pair(1, 2));
ASSERT_EQ(FindTargetSumInArray(v3, -7), std::make_pair(0, 5));
ASSERT_EQ(FindTargetSumInArray(v3, -8), std::make_pair(-1, -1));
ASSERT_EQ(FindTargetSumInArray(v3, 5), std::make_pair(0, 7));
ASSERT_EQ(FindTargetSumInArray(v3, 4), std::make_pair(1, 6));
ASSERT_EQ(FindTargetSumInArray(v3, 10), std::make_pair(1, 7));
ASSERT_EQ(FindTargetSumInArray(v3, -60), std::make_pair(-1, -1));
ASSERT_EQ(FindTargetSumInArray(v3, -12), std::make_pair(-1, -1));
ASSERT_EQ(FindTargetSumInArray(v3, 47), std::make_pair(-1, -1));
ASSERT_EQ(FindTargetSumInArray(v3, 11), std::make_pair(3, 6));
}
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.

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

std::pair<int, int> FindTargetSumInArray(std::vector<int> v, int targetSum) {
int i = 0, j = v.size() - 1;
while (v[i] < v[j]) {
if (v[i] + v[j] > targetSum)
j--;
else if (v[i] + v[j] < targetSum)
i++;
else
return std::pair<int, int>(i, j);
}
return std::pair<int, int>(-1, -1);
}
8 changes: 8 additions & 0 deletions task_01/src/utils.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once
#include <iostream>
#include <vector>
// Returns pair of indices, which gives in sum target sum
// If target sun is not presented returns pair -1; -1
// If we target sum presented in several ways returns i, j
// So that i is minimumal index and j is maximum index
std::pair<int, int> FindTargetSumInArray(std::vector<int> v, int targetSum);
4 changes: 4 additions & 0 deletions task_02/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#include <iostream>

#include "stack.hpp"

using namespace std;

int main() { return 0; }
21 changes: 0 additions & 21 deletions task_02/src/stack.cpp

This file was deleted.

94 changes: 87 additions & 7 deletions task_02/src/stack.hpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,103 @@
#pragma once

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

template <class T>
class StackNode {
public:
explicit StackNode(T _data, StackNode<T> *_prev = nullptr)
: data(_data), prevptr(_prev) {}
StackNode() : data(), prevptr(nullptr) {}
T data;
StackNode<T> *prevptr;
Copy link
Contributor

Choose a reason for hiding this comment

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

prev_ptr

};

template <class T>
class Stack {
public:
void Push(int value);
int Pop();
Stack() : top(nullptr), size(0) {}
void push(T value);
T pop();
Copy link
Contributor

Choose a reason for hiding this comment

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

Pop

T getTop();
Copy link
Contributor

Choose a reason for hiding this comment

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

GetTop

int getSize();
Copy link
Contributor

Choose a reason for hiding this comment

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

GetSize


private:
std::stack<int> data_;
StackNode<T> *top;
Copy link
Contributor

Choose a reason for hiding this comment

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

top_

int size;
Copy link
Contributor

Choose a reason for hiding this comment

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

size_

};

template <class T>
void Stack<T>::push(T value) {
StackNode<T> *newElem = new StackNode<T>(value, top);
size++;
top = newElem;
}

template <class T>
T Stack<T>::pop() {
if (size == 0) {
throw std::underflow_error("Empty stack");
}
T returningValue = top->data;
StackNode<T> *oldTop = top;
Copy link
Contributor

Choose a reason for hiding this comment

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

old_top

top = top->prevptr;
delete oldTop;
size--;
return returningValue;
}

template <class T>
int Stack<T>::getSize() {
return size;
}

template <class T>
T Stack<T>::getTop() {
if (size == 0) {
throw std::underflow_error("Empty stack");
}
return top->data;
}
template <class T>
class MinStack {
public:
void Push(int value);
int Pop();
int GetMin();
MinStack() : mainStack(), minStack() {}
void push(T value);
T pop();
T getMin();
int getSize();

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

template <class T>
void MinStack<T>::push(T value) {
mainStack.push(value);
if (minStack.getSize() == 0)
minStack.push(value);
else
minStack.push(std::min(value, minStack.getTop()));
}

template <class T>
T MinStack<T>::pop() {
minStack.pop();
return mainStack.pop();
}

template <class T>
T MinStack<T>::getMin() {
return minStack.getTop();
}

template <class T>
int MinStack<T>::getSize() {
return mainStack.getSize();
}
60 changes: 30 additions & 30 deletions task_02/src/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,37 @@
#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 []
Stack<int> 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]
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]
ASSERT_EQ(stack.GetMin(), 1);
ASSERT_EQ(stack.Pop(), 3); // Stack [1]
ASSERT_EQ(stack.Pop(), 1); // Stack []
MinStack<int> 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]
ASSERT_EQ(stack.getMin(), 1);
ASSERT_EQ(stack.pop(), 3); // Stack [1]
ASSERT_EQ(stack.pop(), 1); // Stack []
}
3 changes: 2 additions & 1 deletion task_03/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Задача на количество дней перед потеплением

Написать функцию в которую передается темепература за каждый день в определенный момент день, нужно вернуть количество дней до повышения температуры для каждого дня, если температура не повысится, то для этого дня поставить в результирующий массив 0.
Написать функцию в которую передается темепература за каждый день в определенный момент дня. Вернуть количество дней до повышения температуры для каждого дня. Если температура не повысится, для этого дня поставить в результирующий массив 0.

22 changes: 22 additions & 0 deletions task_03/src/WeatherReport.cpp
Copy link
Contributor

Choose a reason for hiding this comment

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

weather_report.cpp
ну и hpp тоже переименуй

Copy link
Contributor

Choose a reason for hiding this comment

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

и в других задачах тоже

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "WeatherReport.h"

#include <stack>
#include <vector>

std::vector<int> findNextGreater(std::vector<int> v) {
Copy link
Contributor

Choose a reason for hiding this comment

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

FindNextGreater

std::vector<int> res(v.size(), -1);
std::stack<int> stack;

for (int i = v.size() - 1; i >= 0; i--) {
while (stack.size() != 0 && v[i] >= v[stack.top()]) {
stack.pop();
}
if (stack.size() != 0) {
res[i] = stack.top() - i;
} else {
res[i] = -1;
}
stack.push(i);
}
return res;
}
4 changes: 4 additions & 0 deletions task_03/src/WeatherReport.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#include <stack>
#include <vector>

std::vector<int> findNextGreater(std::vector<int> v);
1 change: 1 addition & 0 deletions task_03/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <iostream>

#include "WeatherReport.h"
int main() { return 0; }
17 changes: 13 additions & 4 deletions task_03/src/test.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@

#include <gtest/gtest.h>

#include "topology_sort.hpp"
#include "WeatherReport.h"

TEST(TopologySort, Simple) {
ASSERT_EQ(1, 1); // Stack []
}
TEST(findNextGreater, Simple) {
ASSERT_EQ(findNextGreater(std::vector<int>{1}), (std::vector<int>{-1}));
ASSERT_EQ(findNextGreater(std::vector<int>{}), (std::vector<int>{}));
ASSERT_EQ(findNextGreater(std::vector<int>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}),
(std::vector<int>{1, 1, 1, 1, 1, 1, 1, 1, 1, -1}));
ASSERT_EQ(
findNextGreater(std::vector<int>{16, 19, 10, 14, 22, 27, 12, 10, 16, 24}),
(std::vector<int>{{1, 3, 1, 1, 1, -1, 2, 1, 1, -1}}));
ASSERT_EQ(findNextGreater(
std::vector<int>{-29, 0, -7, -14, 0, -11, -13, -3, -27, -25}),
(std::vector<int>{{1, -1, 2, 1, -1, 2, 1, -1, 1, -1}}));
}
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.

Loading
Loading