-
Notifications
You must be signed in to change notification settings - Fork 15
Mathew_tasks #45
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
Mathew131
wants to merge
46
commits into
AlgorithmsDafeMipt2024:main
Choose a base branch
from
Mathew131:task1
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
Mathew_tasks #45
Changes from all commits
Commits
Show all changes
46 commits
Select commit
Hold shift + click to select a range
df0c810
asd
Mathew131 27fa911
a2
Mathew131 df82b7f
WIP
Mathew131 8e1f461
proverka
Mathew131 71b9130
Merge branch 'AlgorithmsDafeMipt2024:main' into main
Mathew131 acd10e5
Merge branch 'AlgorithmsDafeMipt2024:main' into main
Mathew131 fde1a86
Merge branch 'AlgorithmsDafeMipt2024:main' into main
Mathew131 ad66904
proverka 3
Mathew131 443fe7b
new
Mathew131 7059a82
update task_01
Mathew131 ec0b265
updates test task_01
Mathew131 6fecfd8
format
Mathew131 98ce20a
task_03
Mathew131 a8121d8
task_04
Mathew131 aee6bc4
update task_03
Mathew131 004deef
update task_03
Mathew131 70cc035
update test in task_03
Mathew131 8c354cc
update task_04
Mathew131 92029b3
update task_04
Mathew131 253a40c
task_05
Mathew131 f3b691e
task_06
Mathew131 230696f
update task_05
Mathew131 fb915bb
update task_06
Mathew131 c79105f
task_07
Mathew131 668a692
task_09
Mathew131 63415fb
remove using std
Mathew131 5e930f0
remove excess include
Mathew131 fc2aaeb
task_08
Mathew131 abc65fa
update task_03
Mathew131 a763ac3
format
Mathew131 0619d9d
final update task_03
Mathew131 6888602
update task_04
Mathew131 69f7a5f
update task_05
Mathew131 4fc4760
update task_05
Mathew131 c7063ba
task_02
Mathew131 858f42f
format
Mathew131 ee315c5
delete main in task_01
Mathew131 65a5f13
update task_06
Mathew131 47f2029
update task_07:
Mathew131 2b81cba
update task_08
Mathew131 d592237
add new test in task_09
Mathew131 e3f2736
rename task_03
Mathew131 02e5150
rename and add new variable in task_04
Mathew131 2cd7fd8
reformat
Mathew131 a0dfea2
rename in task_06
Mathew131 c8c9cea
rename in task_07 and task_08
Mathew131 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,8 +1,22 @@ | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include "topology_sort.hpp" | ||
#include <vector> | ||
|
||
#include "utils.h" | ||
|
||
TEST(main, Simple) { | ||
ASSERT_EQ(Task1(9, std::vector<int>{1, 2, 4, 5, 6, 8, 10, 12}), | ||
(std::pair<int, int>{1, 8})); | ||
|
||
ASSERT_EQ(Task1(39, std::vector<int>{1, 2, 4, 5, 6, 9, 10, 35}), | ||
(std::pair<int, int>{4, 35})); | ||
|
||
ASSERT_EQ(Task1(14, std::vector<int>{1, 2, 4, 5, 6, 8, 10, 12}), | ||
(std::pair<int, int>{2, 12})); | ||
|
||
ASSERT_EQ(Task1(1338, std::vector<int>{10, 20, 40, 50, 60, 87, 100, 1278}), | ||
(std::pair<int, int>{60, 1278})); | ||
|
||
TEST(TopologySort, Simple) { | ||
ASSERT_EQ(1, 1); // Stack [] | ||
} | ||
ASSERT_EQ(Task1(22, std::vector<int>{10, 10, 11, 11, 12, 15}), | ||
(std::pair<int, int>{10, 12})); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#include "utils.h" | ||
|
||
std::pair<int, int> Task1(int number, std::vector<int> array_of_numbers) { | ||
int i = 0; | ||
int j = array_of_numbers.size() - 1; | ||
while (array_of_numbers[i] + array_of_numbers[j] != number) { | ||
if (array_of_numbers[i] + array_of_numbers[j] < number) i++; | ||
if (array_of_numbers[i] + array_of_numbers[j] > number) j--; | ||
} | ||
return {array_of_numbers[i], array_of_numbers[j]}; | ||
} |
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::pair<int, int> Task1(int, std::vector<int>); |
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
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,21 @@ | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include <vector> | ||
|
||
#include "topology_sort.hpp" | ||
|
||
TEST(TopologySort, Simple) { | ||
ASSERT_EQ(1, 1); // Stack [] | ||
TEST(DaysUntilWarmer, MultipleCases) { | ||
ASSERT_EQ(DaysUntilWarmer(std::vector<int>{73, 74, 75, 71, 69, 72, 76, 73}), | ||
(std::vector<int>{1, 1, 4, 2, 1, 1, 0, 0})); | ||
ASSERT_EQ(DaysUntilWarmer(std::vector<int>{30, 40, 50, 60}), | ||
(std::vector<int>{1, 1, 1, 0})); | ||
ASSERT_EQ(DaysUntilWarmer(std::vector<int>{30, 60, 90, 70, 80, 40, 30}), | ||
(std::vector<int>{1, 1, 0, 1, 0, 0, 0})); | ||
ASSERT_EQ(DaysUntilWarmer(std::vector<int>{55, 55, 55, 55, 55}), | ||
(std::vector<int>{0, 0, 0, 0, 0})); | ||
ASSERT_EQ(DaysUntilWarmer(std::vector<int>{76, 75, 74, 73, 72, 71, 70}), | ||
(std::vector<int>{0, 0, 0, 0, 0, 0, 0})); | ||
ASSERT_EQ(DaysUntilWarmer(std::vector<int>{75, 72, 71, 70, 74, 76}), | ||
(std::vector<int>{5, 3, 2, 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,17 @@ | ||
#include "topology_sort.hpp" | ||
|
||
std::vector<int> DaysUntilWarmer(std::vector<int> temperature) { | ||
std::vector<int> result(temperature.size(), 0); | ||
std::stack<int> index; | ||
|
||
for (int i = 0; i < temperature.size(); ++i) { | ||
while (!index.empty() && temperature[index.top()] < temperature[i]) { | ||
int current_index = index.top(); | ||
index.pop(); | ||
result[current_index] = i - current_index; | ||
} | ||
index.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 |
---|---|---|
@@ -1 +1,5 @@ | ||
#pragma once | ||
#include <stack> | ||
#include <vector> | ||
|
||
std::vector<int> DaysUntilWarmer(std::vector<int>); |
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,57 @@ | ||
#include "heap.hpp" | ||
|
||
void Heap::SiftUp(int i) { | ||
int half_i = i / 2; | ||
while (elements[i] < elements[half_i]) { | ||
std::swap(elements[i], elements[half_i]); | ||
i /= 2; | ||
half_i /= 2; | ||
} | ||
} | ||
|
||
void Heap::SiftDown(int i) { | ||
int leftChildIndex = 2 * i; | ||
int rightChildIndex = 2 * i + 1; | ||
while (leftChildIndex < n) { | ||
int j = -1; | ||
if (elements[leftChildIndex] < elements[i]) { | ||
j = leftChildIndex; | ||
} | ||
if (rightChildIndex < n && elements[rightChildIndex] < elements[i] && | ||
(j == -1 || elements[leftChildIndex] > elements[rightChildIndex])) { | ||
j = rightChildIndex; | ||
} | ||
if (j == -1) | ||
break; | ||
else { | ||
std::swap(elements[i], elements[j]); | ||
leftChildIndex = 2 * j; | ||
rightChildIndex = 2 * j + 1; | ||
} | ||
} | ||
} | ||
|
||
int Heap::Size() { return elements.size(); } | ||
|
||
int Heap::GetMin() { | ||
if (elements.size() == 0) { | ||
throw std::out_of_range("Heap is empty"); | ||
} | ||
return elements[0]; | ||
} | ||
|
||
void Heap::Insert(int x) { | ||
elements.push_back(x); | ||
n++; | ||
SiftUp(n - 1); | ||
} | ||
|
||
void Heap::ExtractMin() { | ||
if (elements.size() == 0) { | ||
throw std::out_of_range("Heap is empty"); | ||
} | ||
elements[0] = elements[n - 1]; | ||
elements.erase(elements.begin() + n - 1); | ||
n--; | ||
SiftDown(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,16 @@ | ||
#include <iostream> | ||
#include <vector> | ||
|
||
class Heap { | ||
public: | ||
void SiftUp(int i); | ||
void SiftDown(int i); | ||
void Insert(int x); | ||
int GetMin(); | ||
void ExtractMin(); | ||
int Size(); | ||
|
||
private: | ||
std::vector<int> elements; | ||
int n = 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,6 +1,27 @@ | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include "heap.hpp" | ||
|
||
TEST(TopologySort, Simple) { | ||
ASSERT_EQ(1, 1); // Stack [] | ||
} | ||
Heap heap; | ||
|
||
EXPECT_THROW(heap.ExtractMin(), std::out_of_range); | ||
EXPECT_THROW(heap.GetMin(), std::out_of_range); | ||
|
||
heap.Insert(-1); | ||
|
||
heap.ExtractMin(); | ||
EXPECT_THROW(heap.ExtractMin(), std::out_of_range); | ||
|
||
ASSERT_EQ(heap.Size(), 0); | ||
heap.Insert(5); | ||
ASSERT_EQ(heap.GetMin(), 5); | ||
heap.Insert(3); | ||
heap.Insert(1); | ||
heap.Insert(7); | ||
heap.Insert(-2); | ||
heap.ExtractMin(); | ||
ASSERT_EQ(heap.Size(), 4); | ||
ASSERT_EQ(heap.GetMin(), 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,43 @@ | ||
#pragma once | ||
|
||
#include <vector> | ||
|
||
std::vector<int> Merge(const std::vector<int>& left, | ||
const std::vector<int>& right) { | ||
std::vector<int> result; | ||
int i = 0, j = 0; | ||
|
||
while (i < left.size() && j < right.size()) { | ||
if (left[i] <= right[j]) { | ||
result.push_back(left[i]); | ||
++i; | ||
} else { | ||
result.push_back(right[j]); | ||
++j; | ||
} | ||
} | ||
|
||
while (i < left.size()) { | ||
result.push_back(left[i]); | ||
++i; | ||
} | ||
|
||
while (j < right.size()) { | ||
result.push_back(right[j]); | ||
++j; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
std::vector<int> MergeSort(std::vector<int> arr) { | ||
if (arr.size() <= 1) { | ||
return arr; | ||
} | ||
|
||
int mid = arr.size() / 2; | ||
std::vector<int> left(arr.begin(), arr.begin() + mid); | ||
std::vector<int> right(arr.begin() + mid, arr.end()); | ||
|
||
return Merge(MergeSort(left), MergeSort(right)); | ||
} |
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.