-
Notifications
You must be signed in to change notification settings - Fork 16
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
dfsavffc
wants to merge
23
commits into
AlgorithmsDafeMipt2024:main
Choose a base branch
from
dfsavffc:task_2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Task 2 #24
Changes from 20 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
c6e9217
test
dfsavffc e18de5a
test
dfsavffc 51c3aa2
done
dfsavffc f21a713
fixed
dfsavffc 72fb431
fixed_again
dfsavffc 0240ad0
Merge branch 'task02'
dfsavffc f240dc8
task-02
dfsavffc 1a178c2
fixed
dfsavffc 5b9cd9c
need help
dfsavffc 7b9760a
need help
dfsavffc e23752f
Add more test
dfsavffc c3de1a1
Fix task_06_test
dfsavffc dc4cf29
Fix task_06 solution
dfsavffc 02cea03
Fix task_06 solution
dfsavffc 2ce8784
Fix task_07 solution
dfsavffc 71846b0
Complete task_08
dfsavffc 90dc70c
add avl tree
dfsavffc 8f072a2
Merge branch 'AlgorithmsDafeMipt2024:main' into task_2
dfsavffc 72fde84
add task_09
dfsavffc fd4c1d8
add task_9 again
dfsavffc 255e56d
Some fix in task_2 and task_8
dfsavffc 4642f89
some minor fix
dfsavffc e57492e
delete task1
dfsavffc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,94 @@ | ||
#pragma once | ||
|
||
#include <stack> | ||
#include <vector> | ||
#include <memory> | ||
|
||
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::runtime_error("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::runtime_error("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(); | ||
} |
LostPointer marked this conversation as resolved.
Show resolved
Hide resolved
LostPointer marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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::runtime_error); | ||
} | ||
|
||
TEST(StackTest, TopFromEmptyStack) { | ||
Stack<int> stack; | ||
ASSERT_THROW(stack.Top(), std::runtime_error); | ||
} | ||
|
||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. дубль, строка 31 |
||
MinStack<int> stack; | ||
ASSERT_THROW(stack.Pop(), std::runtime_error); | ||
} | ||
|
||
TEST(MinStackTest, TopFromEmptyStack) { | ||
MinStack<int> stack; | ||
ASSERT_THROW(stack.Top(), std::runtime_error); | ||
} | ||
|
||
TEST(MinStackTest, GetMinFromEmptyStack) { | ||
MinStack<int> stack; | ||
ASSERT_THROW(stack.GetMin(), std::runtime_error); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
давай кидать out_of_range, более подходит по смыслу
и в стандартных контейнерах это исключение кидается