-
Notifications
You must be signed in to change notification settings - Fork 26
Homework all done #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
salexkon
wants to merge
23
commits into
DafeCpp:main
Choose a base branch
from
salexkon:homework
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
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
d3d7d03
task_1_done_no_tests
salexkon 7db5c52
task_01 clang-format
salexkon 5c4d4d8
task_03 done without test, clang-formatted
salexkon 9ddc619
task_04 done no test, clang-formatted
salexkon 94dfe8d
task_05 done no test, clang-formatted
salexkon 221fe94
task_06 done no test, clang-formatted
salexkon 5a5182f
task_07 done no tests, clang-formatted
salexkon 8345d8a
task_08 done no test, clang-formatted
salexkon 2211c73
task_09 done no tests, clang-formatted
salexkon e76926e
task_01 tests
salexkon 928a363
task_03 tests
salexkon 51ed9cd
task_04 tests
salexkon 363407e
task_05 tests
salexkon fa9d1a4
task_06 tests
salexkon ca4da35
task_07 tests
salexkon d587a5f
task_08 tests
salexkon 248aa2c
task_09 tests
salexkon 90a2574
task_08 template
salexkon 8b50a80
all clang-formatted
salexkon 772e03e
task_02 modified
salexkon d1d812c
all tasks editted
salexkon f1ebb09
clang-formatted
salexkon c92c389
task_02 tests fixed
salexkon 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,17 @@ | ||
| #include "task_01.hpp" | ||
|
|
||
| std::vector<int> FindTwoNums(int target, const std::vector<int>& arr) { | ||
| int left = 0; | ||
| int right = arr.size() - 1; | ||
| while (left < right) { | ||
| int sum = arr[left] + arr[right]; | ||
| if (sum == target) { | ||
| return {arr[left], arr[right]}; | ||
| } else if (sum < target) { | ||
| ++left; | ||
| } else { | ||
| --right; | ||
| } | ||
| } | ||
| return {}; | ||
| }; |
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,4 @@ | ||
| #pragma once | ||
| #include <vector> | ||
|
|
||
| std::vector<int> FindTwoNums(int target, const std::vector<int>& arr); |
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,5 +1,39 @@ | ||
| #include <gtest/gtest.h> | ||
|
|
||
| TEST(Test, Simple) { | ||
| ASSERT_EQ(1, 1); // Stack [] | ||
| #include <algorithm> | ||
| #include <stdexcept> | ||
| #include <vector> | ||
|
|
||
| #include "task_01.hpp" | ||
|
|
||
| TEST(FindTwoNumsTest, BasicMatch) { | ||
| std::vector<int> arr = {1, 2, 3, 4, 6}; | ||
| auto result = FindTwoNums(7, arr); | ||
| std::sort(result.begin(), result.end()); | ||
| ASSERT_EQ(result, std::vector<int>({1, 6})); | ||
| } | ||
|
|
||
| TEST(FindTwoNumsTest, MultiplePairsPossible) { | ||
| std::vector<int> arr = {1, 2, 3, 4, 4, 5}; | ||
| auto result = FindTwoNums(8, arr); | ||
| ASSERT_EQ(result[0] + result[1], 8); | ||
| } | ||
|
|
||
| TEST(FindTwoNumsTest, NoSolution) { | ||
| std::vector<int> arr = {1, 2, 3}; | ||
| ASSERT_EQ(FindTwoNums(10, arr), std::vector<int>{}); | ||
| } | ||
|
|
||
| TEST(FindTwoNumsTest, NegativeNumbers) { | ||
| std::vector<int> arr = {-5, -2, 0, 3, 7, 9}; | ||
| auto result = FindTwoNums(4, arr); | ||
| std::sort(result.begin(), result.end()); | ||
| ASSERT_EQ(result, std::vector<int>({-5, 9})); | ||
| } | ||
|
|
||
| TEST(FindTwoNumsTest, SameElementTwice) { | ||
| std::vector<int> arr = {1, 2, 4, 4, 5, 6}; | ||
| auto result = FindTwoNums(8, arr); | ||
| std::sort(result.begin(), result.end()); | ||
| ASSERT_EQ(result, std::vector<int>({2, 6})); | ||
| } |
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,117 @@ | ||
| #pragma once | ||
|
|
||
| #include <stack> | ||
| #include <stdexcept> | ||
| #include <vector> | ||
|
|
||
| template <typename T> | ||
| class Stack { | ||
| public: | ||
| void Push(int value); | ||
| int Pop(); | ||
| Stack(){}; | ||
| Stack(std::vector<T> vec) { | ||
| for (auto val : vec) { | ||
| push(val); | ||
| } | ||
| } | ||
| void Push(T value) { data_.push_back(value); } | ||
| T Pop() { | ||
| if (data_.empty()) { | ||
| throw std::runtime_error("Stack is empty."); | ||
| } | ||
| T result = data_.back(); | ||
| data_.pop_back(); | ||
| return result; | ||
| } | ||
|
|
||
| int Size() { return data_.size(); } | ||
|
|
||
| bool IsEmpty() { return data_.empty(); } | ||
|
|
||
| private: | ||
| std::stack<int> data_; | ||
| std::vector<T> data_; | ||
| }; | ||
|
|
||
| template <typename T> | ||
| class MinStack { | ||
| public: | ||
| void Push(int value); | ||
| int Pop(); | ||
| int GetMin(); | ||
| MinStack(){}; | ||
| MinStack(std::vector<T> vec) { | ||
| for (auto val : vec) { | ||
| push(val); | ||
| } | ||
| } | ||
| void Push(T value) { | ||
| data_.push_back(value); | ||
| if (data_mins_.size() == 0 || value <= data_mins_.back()) { | ||
| data_mins_.push_back(value); | ||
| } else { | ||
| data_mins_.push_back(data_mins_.back()); | ||
| } | ||
| } | ||
| T Pop() { | ||
| if (data_.empty()) { | ||
| throw std::runtime_error("Stack is empty."); | ||
| } | ||
| T result = data_.back(); | ||
| data_.pop_back(); | ||
| data_mins_.pop_back(); | ||
| return result; | ||
| } | ||
|
|
||
| T GetMin() { | ||
| if (data_mins_.size() == 0) { | ||
| throw std::runtime_error("Пустой стек - нет минимума"); | ||
| } | ||
| return data_mins_.back(); | ||
| } | ||
|
|
||
| int Size() { return data_.size(); } | ||
|
|
||
| bool IsEmpty() { return data_.empty(); } | ||
|
|
||
| private: | ||
| std::vector<int> data_; | ||
| std::vector<T> data_; | ||
| std::vector<T> data_mins_; | ||
| }; | ||
|
|
||
| template <typename T> | ||
| class MaxStack { | ||
| public: | ||
| MaxStack(){}; | ||
| MaxStack(std::vector<T> vec) { | ||
| for (auto val : vec) { | ||
| push(val); | ||
| } | ||
| } | ||
| void Push(T value) { | ||
| data_.push_back(value); | ||
| if (data_maxs.size() == 0 || value >= data_maxs.back()) { | ||
| data_maxs.push_back(value); | ||
| } else { | ||
| data_maxs.push_back(data_maxs.back()); | ||
| } | ||
| } | ||
| T Pop() { | ||
| if (data_.empty()) { | ||
| throw std::runtime_error("Stack is empty."); | ||
| } | ||
| T result = data_.back(); | ||
| data_.pop_back(); | ||
| data_maxs.pop_back(); | ||
| return result; | ||
| } | ||
|
|
||
| T GetMax() { | ||
| if (data_maxs.size() == 0) { | ||
| throw std::runtime_error("Пустой стек - нет минимума"); | ||
| } | ||
| return data_maxs.back(); | ||
| } | ||
|
|
||
| int Size() { return data_.size(); } | ||
|
|
||
| bool IsEmpty() { return data_.empty(); } | ||
|
|
||
| private: | ||
| std::vector<T> data_; | ||
| std::vector<T> data_maxs; | ||
| }; | ||
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,23 @@ | ||
| #include "days_untill_warmer.hpp" | ||
|
|
||
| #include <stdexcept> | ||
|
|
||
| std::vector<int> DaysUntilWarmer(const std::vector<int>& temperatures) { | ||
| int n = temperatures.size(); | ||
| if (n == 0) { | ||
| throw std::runtime_error("Empty array"); | ||
| } | ||
| std::vector<int> result(n, 0); | ||
| std::stack<int> st; | ||
|
|
||
| for (int i = 0; i < n; ++i) { | ||
| while (!st.empty() && temperatures[i] > temperatures[st.top()]) { | ||
| int prev_index = st.top(); | ||
| st.pop(); | ||
| result[prev_index] = i - prev_index; | ||
| } | ||
| st.push(i); | ||
| } | ||
|
|
||
| return result; | ||
| } |
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 <stack> | ||
| #include <vector> | ||
|
|
||
| std::vector<int> DaysUntilWarmer(const std::vector<int>& temperatures); |
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.
давай хотя бы еще метод Size добавим, а лучше и ещё IsEpty (можно и просто Empty)