-
Notifications
You must be signed in to change notification settings - Fork 26
Homework #34
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
melanikot-mipt
wants to merge
12
commits into
DafeCpp:main
Choose a base branch
from
melanikot-mipt: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
Homework #34
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
fdf029e
task01
melanikot-mipt 33dbbe6
tasks 1, 4, 7, 9
melanikot-mipt 373f627
task 2
melanikot-mipt 84175e7
task_02
melanikot-mipt 93284fa
task_05
melanikot-mipt 3a3c7f8
new tasks added
melanikot-mipt 19ffb61
fixed
melanikot-mipt 4d451b1
Merge pull request #2 from DafeCpp/main
melanikot-mipt b384502
task13
melanikot-mipt 15184b2
.
melanikot-mipt e7c777e
task14
melanikot-mipt 58e6850
task_06 added
melanikot-mipt 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,27 @@ | ||
| #include <iostream> | ||
| #include <sstream> | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| std::pair<int, int> GetTerms(const std::vector<int>& array, int sum) { | ||
| int n = array.size(); | ||
|
|
||
| if (n == 0 || n == 1) return std::pair(-1, -1); | ||
|
|
||
| int l{0}, r{n - 1}; | ||
|
|
||
| int current_sum; | ||
| while (l != r) { | ||
| current_sum = array[l] + array[r]; | ||
| if (current_sum == sum) { | ||
| return std::pair(array[l], array[r]); | ||
| } else { | ||
| if (current_sum > sum) | ||
| --r; | ||
| else | ||
| ++l; | ||
| } | ||
| } | ||
|
|
||
| return std::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 |
|---|---|---|
| @@ -1,3 +1,31 @@ | ||
| #include <get_terms.hpp> | ||
| #include <iostream> | ||
| #include <sstream> | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| int main() { return 0; } | ||
| // python3 ./scripts/run_cases.py --tasks task_01 | ||
|
|
||
| int main() { | ||
| int S, N; | ||
| std::vector<int> v; | ||
| std::string line3, line1, line2; | ||
|
|
||
| getline(std::cin, line1); | ||
| getline(std::cin, line2); | ||
| getline(std::cin, line3); | ||
|
|
||
| S = stoi(line1); | ||
| N = stoi(line2); | ||
|
|
||
| std::istringstream is(line3); | ||
|
|
||
| int x; | ||
| while (is >> x) v.push_back(x); | ||
| std::pair<int, int> result = GetTerms(v, S); | ||
| if (result == std::pair(-1, -1)) | ||
| std::cout << -1 << std::endl; | ||
| else | ||
| std::cout << result.first << " " << result.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 |
|---|---|---|
| @@ -1,5 +1,35 @@ | ||
| #include <gtest/gtest.h> | ||
|
|
||
| #include <get_terms.hpp> | ||
| #include <stdexcept> | ||
| #include <vector> | ||
|
|
||
| TEST(Test, Simple) { | ||
| ASSERT_EQ(1, 1); // Stack [] | ||
| std::vector<int> v = {1, 2, 4}; | ||
| std::vector<int> v1 = {2, 7, 11, 15}; | ||
| std::vector<int> v2 = {3, 7, 12}; | ||
| std::vector<int> v3 = {-67, -42, 0, 52, 108, 252}; | ||
| std::vector<int> v4 = {2, 4}; | ||
| ASSERT_EQ(GetTerms(v, 52), std::make_pair(-1, -1)); | ||
| ASSERT_EQ(GetTerms(v, -52), std::make_pair(-1, -1)); | ||
| ASSERT_EQ(GetTerms(v, 0), std::make_pair(-1, -1)); | ||
| ASSERT_EQ(GetTerms(v1, 9), std::make_pair(2, 7)); | ||
| ASSERT_EQ(GetTerms(v2, 19), std::make_pair(7, 12)); | ||
| ASSERT_EQ(GetTerms(v3, 10), std::make_pair(-42, 52)); | ||
| ASSERT_EQ(GetTerms(v3, 1), std::make_pair(-1, -1)); | ||
| ASSERT_EQ(GetTerms(v4, 6), std::make_pair(2, 4)); | ||
| } | ||
|
|
||
| TEST(Test, Empty) { | ||
| std::vector<int> v_empty = {}; | ||
| ASSERT_EQ(GetTerms(v_empty, 52), std::make_pair(-1, -1)); | ||
| ASSERT_EQ(GetTerms(v_empty, -52), std::make_pair(-1, -1)); | ||
| ASSERT_EQ(GetTerms(v_empty, 0), std::make_pair(-1, -1)); | ||
| } | ||
|
|
||
| TEST(Test, Single) { | ||
| std::vector<int> v_single = {0}; | ||
| ASSERT_EQ(GetTerms(v_single, 52), std::make_pair(-1, -1)); | ||
| ASSERT_EQ(GetTerms(v_single, -52), std::make_pair(-1, -1)); | ||
| ASSERT_EQ(GetTerms(v_single, 0), 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,15 @@ | ||
| #include <vector> | ||
|
|
||
| int GetBorderIndex(const std::vector<int> &v) { | ||
| int left = 0, right = v.size() - 1; | ||
|
|
||
| while (left + 1 < right) { | ||
| int mid = (left + right) / 2; | ||
|
|
||
| if (v[mid] == 1) | ||
| right = mid; | ||
| else | ||
| left = mid; | ||
| } | ||
| return left; | ||
| } |
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,23 @@ | ||
| #include <get_border_index.hpp> | ||
| #include <iostream> | ||
| #include <sstream> | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| int main() { return 0; } | ||
| int main() { | ||
| int N; | ||
| std::vector<int> v; | ||
| std::string line1, line2; | ||
|
|
||
| getline(std::cin, line1); | ||
| getline(std::cin, line2); | ||
|
|
||
| N = stoi(line1); | ||
|
|
||
| std::istringstream is(line2); | ||
| int x; | ||
| while (is >> x) v.push_back(x); | ||
|
|
||
| std::cout << GetBorderIndex(v) << 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 |
|---|---|---|
| @@ -1,5 +1,56 @@ | ||
| #include <gtest/gtest.h> | ||
|
|
||
| #include <get_border_index.hpp> | ||
| #include <vector> | ||
|
|
||
| TEST(Test, Simple) { | ||
| ASSERT_EQ(1, 1); // placeholder | ||
| std::vector<int> v1{0, 1}; | ||
| std::vector<int> v2{0, 0, 0, 1, 1}; | ||
| std::vector<int> v3{0, 0, 0, 0, 0, 1}; | ||
| std::vector<int> v4{0, 1, 1, 1, 1}; | ||
| std::vector<int> v5{0, 0, 1, 1, 1, 1}; | ||
| std::vector<int> v6{0, 0, 0, 1}; | ||
| std::vector<int> v7{0, 0, 1}; | ||
| std::vector<int> v8{0, 1, 1}; | ||
| std::vector<int> v9{0, 0, 0, 0, 1, 1, 1}; | ||
| std::vector<int> v10{0, 0, 0, 0, 0, 0, 0, 0, 1}; | ||
| std::vector<int> v11{0, 0, 0, 0, 1}; | ||
| std::vector<int> v12{0, 0, 1, 1}; | ||
| std::vector<int> v13{0, 1, 1, 1}; | ||
| std::vector<int> v14{0, 1, 0, 1, 0, 1, 0, 1}; | ||
|
|
||
| ASSERT_EQ(GetBorderIndex(v1), 0); | ||
| ASSERT_EQ(GetBorderIndex(v2), 2); | ||
| ASSERT_EQ(GetBorderIndex(v3), 4); | ||
| ASSERT_EQ(GetBorderIndex(v4), 0); | ||
| ASSERT_EQ(GetBorderIndex(v5), 1); | ||
| ASSERT_EQ(GetBorderIndex(v6), 2); | ||
| ASSERT_EQ(GetBorderIndex(v7), 1); | ||
| ASSERT_EQ(GetBorderIndex(v8), 0); | ||
| ASSERT_EQ(GetBorderIndex(v9), 3); | ||
| ASSERT_EQ(GetBorderIndex(v10), 7); | ||
| ASSERT_EQ(GetBorderIndex(v11), 3); | ||
| ASSERT_EQ(GetBorderIndex(v12), 1); | ||
| ASSERT_EQ(GetBorderIndex(v13), 0); | ||
| } | ||
|
|
||
| void CheckAnswer(int i, std::vector<int> v) { | ||
| ASSERT_EQ(v[i], 0); | ||
| ASSERT_EQ(v[i] + 1, 1); | ||
| } | ||
|
|
||
| TEST(Test, OutOfSync) { | ||
| std::vector<int> v1{0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1}; | ||
| std::vector<int> v2{0, 1, 0, 1, 0, 1, 0, 1}; | ||
| std::vector<int> v3{0, 0, 0, 1, 0, 0, 0, 1}; | ||
| std::vector<int> v4{0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1}; | ||
| std::vector<int> v5{0, 1, 0, 1}; | ||
| std::vector<int> v6{0, 1, 1, 1, 1, 0, 1}; | ||
|
|
||
| CheckAnswer(GetBorderIndex(v1), v1); | ||
| CheckAnswer(GetBorderIndex(v2), v2); | ||
| CheckAnswer(GetBorderIndex(v3), v3); | ||
| CheckAnswer(GetBorderIndex(v4), v4); | ||
| CheckAnswer(GetBorderIndex(v5), v5); | ||
| CheckAnswer(GetBorderIndex(v6), v6); | ||
| } |
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,38 @@ | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| const std::vector<std::string> num_to_let{"abc", "def", "ghi", "jkl", | ||
| "mno", "pqrs", "tuv", "wxyz"}; | ||
| constexpr int shift = 2; | ||
|
|
||
| void FillingArray(std::vector<int> nums, int index, std::string combination, | ||
| std::vector<std::string> &result) { | ||
| if (index == nums.size()) { | ||
| result.push_back(combination); | ||
| return; | ||
| } | ||
|
|
||
| std::string new_combination; | ||
| for (char c : num_to_let[nums[index] - shift]) { | ||
| new_combination = combination + c; | ||
| FillingArray(nums, index + 1, new_combination, result); | ||
| } | ||
| } | ||
|
|
||
| std::vector<std::string> GetCombinations(std::string digits) { | ||
| std::vector<int> nums; | ||
| for (char c : digits) { | ||
| if (c >= '2' && c <= '9') { | ||
| nums.push_back(c - '0'); | ||
| } | ||
| } | ||
| std::vector<std::string> result; | ||
|
|
||
| if (nums.empty()) return result; | ||
|
|
||
| std::string combination; | ||
| int index = 0; | ||
|
|
||
| FillingArray(nums, index, combination, result); | ||
| 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 |
|---|---|---|
| @@ -1,3 +1,13 @@ | ||
| #include <get_combinations.hpp> | ||
| #include <iostream> | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| int main() { return 0; } | ||
| int main() { | ||
| std::string digits; | ||
| getline(std::cin, digits); | ||
|
|
||
| std::vector<std::string> result = GetCombinations(digits); | ||
|
|
||
| for (std::string str : result) std::cout << str << " "; | ||
| } |
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,4 +1,55 @@ | ||
|
|
||
| #include <gtest/gtest.h> | ||
|
|
||
| TEST(TopologySort, Simple) { ASSERT_EQ(1, 1); } | ||
| #include <vector> | ||
|
|
||
| #include "get_combinations.hpp" | ||
|
|
||
| TEST(Test, Single) { | ||
| // Одна цифра | ||
| std::vector<std::string> empty{}; | ||
| std::vector<std::string> expected2{"a", "b", "c"}; | ||
| std::vector<std::string> expected3{"d", "e", "f"}; | ||
| std::vector<std::string> expected4{"g", "h", "i"}; | ||
| std::vector<std::string> expected5{"j", "k", "l"}; | ||
| std::vector<std::string> expected6{"m", "n", "o"}; | ||
| std::vector<std::string> expected7{"p", "q", "r", "s"}; | ||
| std::vector<std::string> expected8{"t", "u", "v"}; | ||
| std::vector<std::string> expected9{"w", "x", "y", "z"}; | ||
|
|
||
| ASSERT_EQ(GetCombinations("0"), empty); | ||
| ASSERT_EQ(GetCombinations("1"), empty); | ||
| ASSERT_EQ(GetCombinations("2"), expected2); | ||
| ASSERT_EQ(GetCombinations("3"), expected3); | ||
| ASSERT_EQ(GetCombinations("4"), expected4); | ||
| ASSERT_EQ(GetCombinations("5"), expected5); | ||
| ASSERT_EQ(GetCombinations("6"), expected6); | ||
| ASSERT_EQ(GetCombinations("7"), expected7); | ||
| ASSERT_EQ(GetCombinations("8"), expected8); | ||
| ASSERT_EQ(GetCombinations("9"), expected9); | ||
| } | ||
|
|
||
| TEST(Test, TwoDigits) { | ||
| // Две цифры | ||
| std::vector<std::string> expected1{"ad", "ae", "af", "bd", "be", | ||
| "bf", "cd", "ce", "cf"}; | ||
| std::vector<std::string> expected2{"pw", "px", "py", "pz", "qw", "qx", | ||
| "qy", "qz", "rw", "rx", "ry", "rz", | ||
| "sw", "sx", "sy", "sz"}; | ||
| ASSERT_EQ(GetCombinations("23"), expected1); | ||
| ASSERT_EQ(GetCombinations("79"), expected2); | ||
| } | ||
|
|
||
| TEST(Test, ThreeDigits) { | ||
| // Три цифры | ||
| std::vector<std::string> expected{ | ||
| "adg", "adh", "adi", "aeg", "aeh", "aei", "afg", "afh", "afi", | ||
| "bdg", "bdh", "bdi", "beg", "beh", "bei", "bfg", "bfh", "bfi", | ||
| "cdg", "cdh", "cdi", "ceg", "ceh", "cei", "cfg", "cfh", "cfi"}; | ||
| ASSERT_EQ(GetCombinations("234"), expected); | ||
| } | ||
|
|
||
| TEST(Test, Empty) { | ||
| // Пустая строка | ||
| std::vector<std::string> empty{}; | ||
| ASSERT_EQ(GetCombinations(""), empty); | ||
| } |
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,26 @@ | ||
| #include "stack.hpp" | ||
|
|
||
| #include <algorithm> | ||
|
|
||
| void Stack::Push(int value) { data_.push(value); } | ||
| void Stack::Push(int value) { stack_.push_back(value); } | ||
|
|
||
| int Stack::Pop() { | ||
| auto result = data_.top(); | ||
| data_.pop(); | ||
| auto result = stack_.back(); | ||
| stack_.pop_back(); | ||
| return result; | ||
| } | ||
|
|
||
| void MinStack::Push(int value) { data_.push_back(value); } | ||
| void MinStack::Push(int value) { | ||
| stack_.push_back(value); | ||
| if (min_stack_.empty() || value < min_stack_.back()) | ||
| min_stack_.push_back(value); | ||
| else | ||
| min_stack_.push_back(min_stack_.back()); | ||
| } | ||
|
|
||
| int MinStack::Pop() { | ||
| auto result = data_.back(); | ||
| data_.pop_back(); | ||
| auto result = stack_.back(); | ||
| stack_.pop_back(); | ||
| min_stack_.pop_back(); | ||
| return result; | ||
| } | ||
|
|
||
| int MinStack::GetMin() { return *std::min_element(data_.begin(), data_.end()); } | ||
| int MinStack::GetMin() { return min_stack_.back(); } |
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,18 @@ | ||
| #include <iostream> | ||
| #include <vector> | ||
|
|
||
| int main() { return 0; } | ||
| #include "temperature_rise.hpp" | ||
|
|
||
| int main() { | ||
| int N; | ||
| std::cin >> N; | ||
| std::vector<int> v; | ||
|
|
||
| for (int i = 0; i < N; ++i) { | ||
| int n; | ||
| std::cin >> n; | ||
| v.push_back(n); | ||
| } | ||
| std::vector<int> v2 = TemperatureRise(v); | ||
| for (int t : v2) std::cout << t << " "; | ||
| } |
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.
cin,cout,endlбезstd::— код не скомпилируется. Нужно добавитьstd::везде по аналогии с другими задачами.