-
Notifications
You must be signed in to change notification settings - Fork 15
Homework 1 #17
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
Romanov-Fedor
wants to merge
16
commits into
AlgorithmsDafeMipt2024:main
Choose a base branch
from
Romanov-Fedor:main
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 1 #17
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
bde3da7
Homework 1
Romanov-Fedor 0e576ab
Delete empty files, rename some fies, some smoll code correction and …
Romanov-Fedor 6817d9e
Merge branch 'AlgorithmsDafeMipt2024:main' into main
Romanov-Fedor 7bd6195
task_02 complete
Romanov-Fedor ea43adb
Change type of stack elements from int to any
Romanov-Fedor c573471
fix bug
Romanov-Fedor a5bb915
Merge pull request #1 from Romanov-Fedor/main
Romanov-Fedor b8e4331
add main
Romanov-Fedor 3ccce2d
delete usless files
Romanov-Fedor a4b9f5a
Solve task
Romanov-Fedor 68f6b0a
add tests
Romanov-Fedor 01ed17c
try to fix git hub problem
Romanov-Fedor 1b9a87c
add main to task2
Romanov-Fedor 4372938
Merge pull request #2 from Romanov-Fedor/homework_2
Romanov-Fedor 41cc3d6
Merge branch 'main' into homework_3
Romanov-Fedor cc38d65
Merge pull request #3 from Romanov-Fedor/homework_3
Romanov-Fedor 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,17 @@ | ||
#include <iostream> | ||
#include <utility> | ||
#include <vector> | ||
|
||
int main() { return 0; } | ||
#include "searching_sum.hpp" | ||
|
||
int main() { | ||
int n, k; | ||
std::cin >> n >> k; | ||
std::vector<int> vec(n); | ||
for (int i = 0; i < n; i++) { | ||
std::cin >> vec[i]; | ||
} | ||
std::pair<int, int> ans = SearchingSum(vec, k); | ||
std::cout << ans.first << " " << ans.second << std::endl; | ||
return 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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#include "searching_sum.hpp" | ||
|
||
#include <utility> | ||
|
||
std::pair<int, int> SearchingSum(std::vector<int> vec, int num) { | ||
int start = 0; | ||
int end = vec.size() - 1; | ||
while (start < end) { | ||
if (vec[start] + vec[end] > num) | ||
--end; | ||
else if (vec[start] + vec[end] < num) | ||
++start; | ||
else | ||
return std::make_pair(start, end); | ||
} | ||
return std::make_pair(-1, -1); | ||
} |
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,9 @@ | ||
#ifndef SEARCHING_SUM_HPP | ||
#define SEARCHING_SUM_HPP | ||
|
||
#include <utility> | ||
#include <vector> | ||
|
||
std::pair<int, int> SearchingSum(std::vector<int> vec, int num); | ||
|
||
#endif // define Searching_sum.hpp |
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,48 @@ | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include "topology_sort.hpp" | ||
#include <utility> | ||
#include <vector> | ||
|
||
#include "searching_sum.hpp" | ||
|
||
TEST(TopologySort, Simple) { | ||
ASSERT_EQ(1, 1); // Stack [] | ||
LostPointer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
int sum1 = 6; | ||
std::vector<int> vec1{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; | ||
std::pair<int, int> ans1{0, 6}; | ||
ASSERT_EQ(SearchingSum(vec1, sum1), ans1); | ||
|
||
int sum2 = 6; | ||
std::vector<int> vec2{0}; | ||
std::pair<int, int> ans2{-1, -1}; | ||
ASSERT_EQ(SearchingSum(vec2, sum2), ans2); | ||
|
||
int sum3 = 4; | ||
std::vector<int> vec3{-11, -6, 0, 1, 2, 4, 10, 16}; | ||
std::pair<int, int> ans3{1, 6}; | ||
ASSERT_EQ(SearchingSum(vec3, sum3), ans3); | ||
|
||
int sum4 = 0; | ||
std::vector<int> vec4{-17, -8, -1, 1, 2, 4, 10}; | ||
std::pair<int, int> ans4{2, 3}; | ||
ASSERT_EQ(SearchingSum(vec4, sum4), ans4); | ||
|
||
int sum5 = -9; | ||
std::vector<int> vec5{-17, -8, -1, 1, 2, 4, 10}; | ||
std::pair<int, int> ans5{1, 2}; | ||
ASSERT_EQ(SearchingSum(vec5, sum5), ans5); | ||
|
||
int sum6 = 17; | ||
std::vector<int> vec6{-17, -8, -1, 1, 2, 4, 10}; | ||
std::pair<int, int> ans6{-1, -1}; | ||
ASSERT_EQ(SearchingSum(vec6, sum6), ans6); | ||
|
||
int sum7 = -1; | ||
std::vector<int> vec7{-18, -10, -1, -1, -1, 0, 3, 5, 10}; | ||
std::pair<int, int> ans7{2, 5}; | ||
ASSERT_EQ(SearchingSum(vec7, sum7), ans7); | ||
|
||
int sum8 = 5; | ||
std::vector<int> vec8(0); | ||
std::pair<int, int> ans8{-1, -1}; | ||
ASSERT_EQ(SearchingSum(vec8, sum8), ans8); | ||
} |
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,3 +1,3 @@ | ||
#include <iostream> | ||
#include "stack.hpp" | ||
|
||
int main() { return 0; } | ||
int main() { Stack<int> stack; } |
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,23 +1,107 @@ | ||
#pragma once | ||
|
||
#include <stack> | ||
#include <vector> | ||
#include <memory> | ||
|
||
template <typename T> | ||
struct Element { | ||
explicit Element(T data, std::shared_ptr<Element<T>> prev = nullptr) | ||
: element{data}, prev{prev} {} | ||
|
||
explicit Element() : element{}, prev{nullptr} {} | ||
|
||
T element; | ||
|
||
std::shared_ptr<Element<T>> prev; | ||
}; | ||
|
||
template <typename T> | ||
class Stack { | ||
public: | ||
void Push(int value); | ||
int Pop(); | ||
explicit Stack() : data_{} {} | ||
|
||
void Push(T value); | ||
|
||
T Pop(); | ||
|
||
bool Empty() const; | ||
|
||
const T& Top() const; | ||
|
||
size_t Size() const; | ||
|
||
private: | ||
std::stack<int> data_; | ||
Element<T> data_; | ||
|
||
size_t size_ = 0; | ||
}; | ||
|
||
template <typename T> | ||
void Stack<T>::Push(T value) { | ||
Element new_data{value, std::make_shared<Element<T>>(data_)}; | ||
size_ += 1; | ||
data_ = new_data; | ||
} | ||
|
||
template <typename T> | ||
T Stack<T>::Pop() { | ||
T deleted_element = data_.element; | ||
Element new_data = *data_.prev; | ||
data_.prev = nullptr; | ||
data_ = new_data; | ||
size_ -= 1; | ||
return deleted_element; | ||
} | ||
|
||
template <typename T> | ||
bool Stack<T>::Empty() const { | ||
return data_.prev == nullptr; | ||
} | ||
|
||
template <typename T> | ||
const T& Stack<T>::Top() const { | ||
return data_.element; | ||
} | ||
|
||
template <typename T> | ||
size_t Stack<T>::Size() const { | ||
return size_; | ||
} | ||
|
||
template <typename T> | ||
class MinStack { | ||
public: | ||
void Push(int value); | ||
int Pop(); | ||
int GetMin(); | ||
void Push(T value); | ||
T Pop(); | ||
T GetMin() const; | ||
|
||
private: | ||
std::vector<int> data_; | ||
Stack<T> main_stack_; | ||
Stack<T> min_stack_; | ||
}; | ||
|
||
template <typename T> | ||
void MinStack<T>::Push(T value) { | ||
if (main_stack_.Size() == 0) { | ||
min_stack_.Push(value); | ||
main_stack_.Push(value); | ||
} else { | ||
main_stack_.Push(value); | ||
if (value < min_stack_.Top()) | ||
min_stack_.Push(value); | ||
else | ||
min_stack_.Push(min_stack_.Top()); | ||
} | ||
} | ||
|
||
template <typename T> | ||
T MinStack<T>::Pop() { | ||
T result = main_stack_.Top(); | ||
main_stack_.Pop(); | ||
min_stack_.Pop(); | ||
return result; | ||
} | ||
|
||
template <typename T> | ||
T MinStack<T>::GetMin() const { | ||
return min_stack_.Top(); | ||
} |
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,3 +1,3 @@ | ||
#include <iostream> | ||
#include "temperature.hpp" | ||
|
||
int main() { return 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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#include "temperature.hpp" | ||
|
||
#include <stack> | ||
#include <vector> | ||
|
||
std::vector<int> days_before_warming(std::vector<int> daily_temp) { | ||
std::vector<int> days_amount(daily_temp.size()); | ||
std::stack<int> previous_days; | ||
for (int day_index = 0; day_index < daily_temp.size(); ++day_index) { | ||
days_amount[day_index] = 0; | ||
while (!previous_days.empty() && | ||
daily_temp[previous_days.top()] <= daily_temp[day_index]) { | ||
days_amount[previous_days.top()] = day_index - previous_days.top(); | ||
previous_days.pop(); | ||
} | ||
previous_days.push(day_index); | ||
} | ||
return days_amount; | ||
} |
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,3 @@ | ||
#include <vector> | ||
|
||
std::vector<int> days_before_warming(std::vector<int> daily_temp); |
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,33 @@ | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include "topology_sort.hpp" | ||
#include <cstddef> | ||
|
||
#include "temperature.hpp" | ||
|
||
TEST(temperature, Simple) { | ||
std::vector<int> daily_temp_1 = days_before_warming({1, 2, 3, 4, 5}); | ||
std::vector<int> result_1 = {1, 1, 1, 1, 0}; | ||
ASSERT_EQ(daily_temp_1, result_1); | ||
|
||
std::vector<int> daily_temp_2 = days_before_warming({0}); | ||
std::vector<int> result_2 = {0}; | ||
ASSERT_EQ(daily_temp_2, result_2); | ||
|
||
std::vector<int> daily_temp_3 = days_before_warming({}); | ||
std::vector<int> result_3 = {}; | ||
ASSERT_EQ(daily_temp_3, result_3); | ||
|
||
std::vector<int> daily_temp_4 = | ||
days_before_warming({1, 2, -4, -5, -6, -8, 9}); | ||
std::vector<int> result_4 = {1, 5, 4, 3, 2, 1, 0}; | ||
ASSERT_EQ(daily_temp_4, result_4); | ||
|
||
std::vector<int> daily_temp_5 = days_before_warming({1, 2, 1, 2, 1, 2, 1, 2}); | ||
std::vector<int> result_5 = {1, 2, 1, 2, 1, 2, 1, 0}; | ||
ASSERT_EQ(daily_temp_5, result_5); | ||
|
||
TEST(TopologySort, Simple) { | ||
ASSERT_EQ(1, 1); // Stack [] | ||
} | ||
std::vector<int> daily_temp_6 = | ||
days_before_warming({2, 1, 3, 1, 4, 1, 3, 1, 2, 1, 5}); | ||
std::vector<int> result_6 = {2, 1, 2, 1, 6, 1, 4, 1, 2, 1, 0}; | ||
ASSERT_EQ(daily_temp_6, result_6); | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
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.