-
Notifications
You must be signed in to change notification settings - Fork 15
Done homework_1 #18
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
MrWh1teF0x
wants to merge
15
commits into
AlgorithmsDafeMipt2024:main
Choose a base branch
from
MrWh1teF0x: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
Done homework_1 #18
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
e03f276
Merge pull request #1 from AlgorithmsDafeMipt2024/main
MrWh1teF0x b8c822e
done (without tests)
MrWh1teF0x b5f7957
homework_1 done (with tests)
MrWh1teF0x dab51dc
some changes
MrWh1teF0x 8fa71be
rename files and add tests
MrWh1teF0x e8a0858
Merge commit 'e7f966369390607365f401c4ae91c0ba79a58239'
MrWh1teF0x e0c84c6
complete task_05 with tests
MrWh1teF0x 2ec3133
complete task_03 with tests
MrWh1teF0x 4792881
complete task_02 with tests
MrWh1teF0x 4c7c18c
complete task_06 with tests
MrWh1teF0x 83ccac3
Merge commit '6f9ae9e014666642349a7ffc5cae15ec8b14a8e9' into homework_2
MrWh1teF0x 9e4b8e3
complete task_04 with tests
MrWh1teF0x 21590be
complete task_09 with tests
MrWh1teF0x 75be983
complete task_08 with tests
MrWh1teF0x b544a6b
complete task_07 with tests
MrWh1teF0x 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 |
---|---|---|
@@ -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."); | ||
} |
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,7 @@ | ||
#pragma once | ||
|
||
#include <iostream> | ||
#include <vector> | ||
|
||
std::pair<long long, long long> GetTwoNums(long long number, | ||
std::vector<long long> nums); |
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,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; | ||
} |
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,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); | ||
} |
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,8 @@ | ||
#include <iostream> | ||
|
||
int main() { return 0; } | ||
#include "stack.hpp" | ||
|
||
int main() { | ||
MinStack<int> stack; | ||
stack.Push(1); | ||
} |
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,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(); | ||
} |
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 |
---|---|---|
@@ -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; | ||
} |
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,5 @@ | ||
#pragma once | ||
#include <vector> | ||
using namespace std; | ||
|
||
vector<size_t> DaysBeforeWarming(vector<short> temperatures); |
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.