-
Notifications
You must be signed in to change notification settings - Fork 15
Homework 2 #37
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
Matvey-cmd
wants to merge
46
commits into
AlgorithmsDafeMipt2024:main
Choose a base branch
from
Matvey-cmd:homework_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
Homework 2 #37
Changes from all commits
Commits
Show all changes
46 commits
Select commit
Hold shift + click to select a range
6cea2ca
some changes
Matvey-cmd b261bdd
answers
Matvey-cmd 766aa30
change
Matvey-cmd 9e6840e
Right_Test
Matvey-cmd 03af331
Right_Test_2
Matvey-cmd 6cde357
itog_changes
Matvey-cmd 121443a
some changes
Matvey-cmd 7b8c3c0
Merge branch 'AlgorithmsDafeMipt2024:main' into homework_1
Matvey-cmd a03cf3e
last change
Matvey-cmd 0cc2076
kast_changes
Matvey-cmd 8b2cf4f
part of task 2
Matvey-cmd 5a132da
k_stat
Matvey-cmd 787bb3d
commit
Matvey-cmd c1eae6f
commit
Matvey-cmd f685e79
task_3
Matvey-cmd d9b9d6c
last changes
Matvey-cmd 245de1e
last changes
Matvey-cmd 20b6822
task02 was done
Matvey-cmd 1dc165e
tasks 1-6 are ready
Matvey-cmd 226b712
Merge branch 'AlgorithmsDafeMipt2024:main' into homework_2
Matvey-cmd 3980a61
Merge branch 'AlgorithmsDafeMipt2024:main' into homework_2
Matvey-cmd 396a100
Task_9 ready
Matvey-cmd 4293dc2
task09 some changes
Matvey-cmd 8bf229d
some changes task09
Matvey-cmd 4d35f29
Task8 ready and some improvements in tests
Matvey-cmd 8449440
Merge branches 'homework_2' and 'homework_2' of https://github.com/Ma…
Matvey-cmd 636fa3d
Tests for all tasks are ready
Matvey-cmd 4bd512a
last shanges in tests
Matvey-cmd 38cf61f
delete task01
Matvey-cmd 0715d40
delete task01
Matvey-cmd 463677b
right format
Matvey-cmd 14c73de
format
Matvey-cmd 80f88e9
format
Matvey-cmd a767ecf
format
Matvey-cmd f988085
Update heap.cpp
Matvey-cmd 38e3811
Update stack.cpp
Matvey-cmd df9dde4
Update topology_sort.hpp
Matvey-cmd 824df0b
Update topology_sort.cpp
Matvey-cmd 6bd6dbf
Update test.cpp
Matvey-cmd b08e5f5
Update topology_sort.cpp
Matvey-cmd 955f0eb
Update topology_sort.cpp
Matvey-cmd 0342663
fix some shortcomings
Matvey-cmd cd65251
corrected tasks
Matvey-cmd 5b802c4
right format
Matvey-cmd 61b600d
All problem fixed
Matvey-cmd d8efd06
last fix
Matvey-cmd 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 hidden or 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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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,21 +1,51 @@ | ||
#include "stack.hpp" | ||
|
||
#include <algorithm> | ||
#include <memory> | ||
#include <stdexcept> | ||
|
||
void Stack::Push(int value) { data_.push(value); } | ||
void Stack::Push(int value) { | ||
auto p = std::make_shared<Node>(value); | ||
if (top == nullptr) { | ||
top = p; | ||
} else { | ||
p->next = top; | ||
top = p; | ||
} | ||
} | ||
|
||
int Stack::Pop() { | ||
auto result = data_.top(); | ||
data_.pop(); | ||
return result; | ||
if (top == nullptr) { | ||
throw std::logic_error("out_of_range"); | ||
} | ||
int val = top->value; | ||
top = top->next; | ||
return val; | ||
} | ||
|
||
void MinStack::Push(int value) { data_.push_back(value); } | ||
void MinStack::Push(int value) { | ||
if (stack_.top == nullptr) { | ||
stack_.Push(value); | ||
min_stack_.Push(value); | ||
return; | ||
} | ||
|
||
if (stack_.top->value > value) { | ||
min_stack_.Push(value); | ||
} else { | ||
stack_.Push(value); | ||
min_stack_.Push(min_stack_.top->value); | ||
} | ||
} | ||
|
||
int MinStack::Pop() { | ||
auto result = data_.back(); | ||
data_.pop_back(); | ||
return result; | ||
if (stack_.top == nullptr) { | ||
throw std::logic_error("out_of_range"); | ||
} | ||
|
||
int val = stack_.top->value; | ||
stack_.top = stack_.top->next; | ||
min_stack_.top = min_stack_.top->next; | ||
return val; | ||
} | ||
|
||
int MinStack::GetMin() { return *std::min_element(data_.begin(), data_.end()); } | ||
int MinStack::GetMin() { return min_stack_.top->value; } |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,51 @@ | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include "topology_sort.hpp" | ||
|
||
//Простые тесты | ||
TEST(TopologySort, Simple) { | ||
ASSERT_EQ(1, 1); // Stack [] | ||
ASSERT_EQ(RiseTemperature(std::vector<int>{1, 2, 3, 4, 5}), | ||
(std::vector<int>{1, 1, 1, 1, 0})); | ||
ASSERT_EQ(RiseTemperature(std::vector<int>{5, 4, 3, 2, 1}), | ||
(std::vector<int>{0, 0, 0, 0, 0})); | ||
ASSERT_EQ( | ||
RiseTemperature(std::vector<int>{-3, 0, 1, 23, 4, 5, 12, 1, 2, 1, 3}), | ||
(std::vector<int>{1, 1, 1, 0, 1, 1, 0, 1, 2, 1, 0})); | ||
ASSERT_EQ( | ||
RiseTemperature(std::vector<int>{12, 14, 2, 12, 11, 10, 0, 5, 3, 20, 4}), | ||
(std::vector<int>{1, 8, 1, 6, 5, 4, 1, 2, 1, 0, 0})); | ||
ASSERT_EQ( | ||
RiseTemperature(std::vector<int>{11, -2, 3, 1, 5, 2, 6, 3, 7, 84, 4}), | ||
(std::vector<int>{9, 1, 2, 1, 2, 1, 2, 1, 1, 0, 0})); | ||
ASSERT_EQ(RiseTemperature(std::vector<int>{1, 2, 3, 4, 5, 6, 7, 8, 9}), | ||
(std::vector<int>{1, 1, 1, 1, 1, 1, 1, 1, 0})); | ||
} | ||
|
||
// Тестирование поведения при убывающих температурах | ||
TEST(TopologySort, DecreasingTemperature) { | ||
ASSERT_EQ(RiseTemperature(std::vector<int>{10, 9, 8, 7, 6}), | ||
(std::vector<int>{0, 0, 0, 0, 0})); | ||
} | ||
|
||
// Тестирование поведения при возрастающих температурах | ||
TEST(TopologySort, IncreasingTemperature) { | ||
ASSERT_EQ(RiseTemperature(std::vector<int>{-5, -4, -3, -2, -1}), | ||
(std::vector<int>{1, 1, 1, 1, 0})); | ||
} | ||
|
||
// Тестирование поведения с случайными температурами | ||
TEST(TopologySort, RandomTemperature) { | ||
ASSERT_EQ(RiseTemperature(std::vector<int>{3, -1, 4, 1, 5}), | ||
(std::vector<int>{2, 1, 2, 1, 0})); | ||
} | ||
|
||
// Тестирование поведения с пустым вектором | ||
TEST(TopologySort, EmptyVector) { | ||
EXPECT_THROW(RiseTemperature(std::vector<int>{}), WrongVector); | ||
} | ||
|
||
// Тестирование поведения с максимальными и минимальными значениями | ||
TEST(TopologySort, ExtremeValues) { | ||
ASSERT_EQ(RiseTemperature(std::vector<int>{-2, 0, 5}), | ||
(std::vector<int>{1, 1, 0})); | ||
} |
This file contains hidden or 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 +1,38 @@ | ||
#include "topology_sort.hpp" | ||
|
||
#include <iostream> | ||
#include <stack> | ||
#include <vector> | ||
|
||
class Day { | ||
public: | ||
int number_of_day; | ||
int temperature; | ||
Day(int number_of_day, int temperature) | ||
: number_of_day(number_of_day), temperature(temperature) {} | ||
}; | ||
|
||
std::vector<int> RiseTemperature(std::vector<int> vec) { | ||
int size = int(vec.size()); | ||
if (size == 0) { | ||
throw WrongVector("Wrong vector is too small"); | ||
} | ||
std::stack<Day> stack_days; | ||
std::vector<int> answer_vec(size, 0); | ||
stack_days.emplace(0, vec[0]); | ||
int i = 1; | ||
while (i < size) { | ||
while (!stack_days.empty()) { | ||
if (vec[i] > stack_days.top().temperature) { | ||
answer_vec[stack_days.top().number_of_day] = | ||
i - stack_days.top().number_of_day; | ||
stack_days.pop(); | ||
} else if (vec[i] < stack_days.top().temperature) { | ||
break; | ||
} | ||
} | ||
stack_days.emplace(i, vec[i]); | ||
++i; | ||
} | ||
return answer_vec; | ||
} |
This file contains hidden or 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 +1,9 @@ | ||
#pragma once | ||
#include <iostream> | ||
#include <vector> | ||
|
||
class WrongVector : public std::runtime_error { | ||
using std::runtime_error::runtime_error; | ||
}; | ||
|
||
std::vector<int> RiseTemperature(std::vector<int> vec); |
This file contains hidden or 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,58 @@ | ||
#include "heap.hpp" | ||
|
||
#include <iostream> | ||
#include <vector> | ||
|
||
void Heap::SiftDown(int cur_node) { | ||
while (2 * cur_node + 1 < vec_.size()) { | ||
int left = 2 * cur_node + 1; | ||
int right = 2 * cur_node + 2; | ||
int next_node = left; | ||
if ((right < vec_.size()) && vec_[right] < vec_[left]) next_node = right; | ||
if (vec_[cur_node] <= vec_[next_node]) break; | ||
std::swap(vec_[cur_node], vec_[next_node]); | ||
cur_node = next_node; | ||
} | ||
} | ||
|
||
void Heap::SiftUp(int i) { | ||
while (vec_[i] < (vec_[(i - 1) / 2])) { | ||
std::swap(vec_[i], vec_[(i - 1) / 2]); | ||
i = (i - 1) / 2; | ||
} | ||
} | ||
|
||
int Heap::FindMin() { return vec_[0]; } | ||
|
||
int Heap::ExtractMin() { | ||
int min = vec_[0]; | ||
vec_[0] = vec_[vec_.size() - 1]; | ||
vec_.pop_back(); | ||
SiftDown(0); | ||
return min; | ||
} | ||
|
||
std::vector<int> Heap::CopyHeap() { return vec_; } | ||
|
||
void Heap::Insert(int value) { | ||
vec_.push_back(value); | ||
SiftUp(vec_.size() - 1); | ||
} | ||
|
||
void Heap::BuildHeap(std::vector<int> vec) { | ||
for (int i = 0; i < vec.size(); ++i) { | ||
Insert(vec[i]); | ||
} | ||
} | ||
|
||
int FindMinimum(std::vector<int> vec) { | ||
Heap heap; | ||
heap.BuildHeap(vec); | ||
return heap.FindMin(); | ||
} | ||
|
||
std::vector<int> HeapReady(std::vector<int> vec) { | ||
Heap heap; | ||
heap.BuildHeap(vec); | ||
return heap.CopyHeap(); | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.