-
Notifications
You must be signed in to change notification settings - Fork 15
Homework 2 #39
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
dalnoboy75
wants to merge
17
commits into
AlgorithmsDafeMipt2024:main
Choose a base branch
from
dalnoboy75:homework-2
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 2 #39
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
74ad1a1
Merge pull request #1 from AlgorithmsDafeMipt2024/main
dalnoboy75 8bea497
task 2-4
dalnoboy75 23a9001
task 5
dalnoboy75 119edad
Merge branch 'AlgorithmsDafeMipt2024:main' into main
dalnoboy75 a588723
Merge remote-tracking branch 'origin/main' into homework-2
dalnoboy75 685222c
task 6
dalnoboy75 16ec7e1
full tasks
dalnoboy75 d547e35
fix
dalnoboy75 66bb2df
fix
dalnoboy75 3b66e4f
fix
dalnoboy75 60bc985
fix
dalnoboy75 9504f9d
fix
dalnoboy75 200b7cb
fix
dalnoboy75 383f94e
fix
dalnoboy75 51c7c6e
fix
dalnoboy75 a64e53e
fix
dalnoboy75 de322b2
fix
dalnoboy75 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 @@ | ||
#pragma once | ||
#include <algorithm> | ||
#include <cstdlib> | ||
#include <ctime> | ||
#include <vector> | ||
|
||
template <typename T> | ||
int Partition(std::vector<T>& arr, int l, int r) { | ||
std::srand(std::time(nullptr)); | ||
int pv_index = l + std::rand() % (r - l + 1); | ||
T pivot = arr[pv_index]; | ||
std::swap(arr[pv_index], arr[(l + r) / 2]); | ||
int i = l, j = r; | ||
while (i <= j) { | ||
while (arr[i] < pivot) { | ||
i++; | ||
} | ||
while (arr[j] > pivot) { | ||
j--; | ||
} | ||
if (i >= j) { | ||
break; | ||
} | ||
std::swap(arr[i++], arr[j--]); | ||
} | ||
return 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
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 @@ | ||
#include "utils.hpp" |
File renamed without changes.
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,4 @@ | ||
#include <iostream> | ||
|
||
#include "stack.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
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 "temp_up_days.hpp" | ||
|
||
#include <stack> | ||
|
||
std::vector<int> TempUpDayCounter(std::vector<int> temps) { | ||
std::stack<Day> days; | ||
std::vector<int> result(temps.size(), 0); | ||
for (int i = 0; i < temps.size(); i++) { | ||
Day d; | ||
d.index_ = i; | ||
d.temp_ = temps[i]; | ||
while (!days.empty()) { | ||
if (days.top().temp_ < d.temp_) { | ||
result[days.top().index_] = d.index_ - days.top().index_; | ||
days.pop(); | ||
} else { | ||
break; | ||
} | ||
} | ||
days.push(d); | ||
} | ||
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,9 @@ | ||
#pragma once | ||
#include <vector> | ||
|
||
struct Day { | ||
int index_; | ||
int temp_; | ||
}; | ||
|
||
std::vector<int> TempUpDayCounter(std::vector<int> temps); |
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 "topology_sort.hpp" | ||
#include "temp_up_days.hpp" | ||
|
||
TEST(TopologySort, Simple) { | ||
ASSERT_EQ(1, 1); // Stack [] | ||
TEST(TempUpDays, Simple) { | ||
ASSERT_EQ(TempUpDayCounter(std::vector<int>{5, 7, 4, 5}), | ||
(std::vector<int>{1, 0, 1, 0})); | ||
ASSERT_EQ(TempUpDayCounter(std::vector<int>{5, 12, 4, 9, 5, 4, 2}), | ||
(std::vector<int>{1, 0, 1, 0, 0, 0, 0})); | ||
ASSERT_EQ(TempUpDayCounter(std::vector<int>{2, 6, 17, 7, 3, 4}), | ||
(std::vector<int>{1, 1, 0, 0, 1, 0})); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. везде в ответах 0 или 1 давай еще добавим еще каких-нибудь чисел) |
||
ASSERT_EQ(TempUpDayCounter(std::vector<int>{}), std::vector<int>{}); | ||
|
||
ASSERT_EQ(TempUpDayCounter( | ||
std::vector<int>{70, 41, 86, 49, 31, 71, 39, 79, 24, 46}), | ||
(std::vector<int>{2, 1, 0, 2, 1, 2, 1, 0, 1, 0})); | ||
ASSERT_EQ(TempUpDayCounter(std::vector<int>{84, 44, 32, 65, 33, 11, 70, 57, | ||
73, 98, 52, 93}), | ||
(std::vector<int>{9, 2, 1, 3, 2, 1, 2, 1, 1, 0, 1, 0})); | ||
} | ||
LostPointer marked this conversation as resolved.
Show resolved
Hide resolved
|
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,61 @@ | ||
#include "heap.hpp" | ||
|
||
#include <cstddef> | ||
#include <stdexcept> | ||
#include <vector> | ||
|
||
void Heap::SiftUp(int index) { | ||
while (index >= 0 && heap_[index] < heap_[(index - 1) / 2]) { | ||
std::swap(heap_[index], heap_[(index - 1) / 2]); | ||
index = (index - 1) / 2; | ||
} | ||
} | ||
|
||
void Heap::SiftDown(int index) { | ||
while (2 * index + 1 < heap_.size()) { | ||
int left = 2 * index + 1; | ||
int right = 2 * index + 2; | ||
int small_child_index = left; | ||
if (right < heap_.size() && heap_[right] < heap_[left]) { | ||
small_child_index = right; | ||
} | ||
if (heap_[index] < heap_[small_child_index]) { | ||
break; | ||
} | ||
std::swap(heap_[index], heap_[small_child_index]); | ||
index = small_child_index; | ||
} | ||
} | ||
|
||
int Heap::Min() { | ||
if (Size() == 0) throw std::runtime_error("Empty Heap"); | ||
int m = heap_[0]; | ||
std::swap(heap_[0], heap_[heap_.size() - 1]); | ||
heap_.pop_back(); | ||
SiftDown(0); | ||
return m; | ||
} | ||
|
||
void Heap::Insert(int value) { | ||
heap_.push_back(value); | ||
this->SiftUp(heap_.size() - 1); | ||
} | ||
|
||
void Heap::Build(std::vector<int> data) { | ||
for (auto x : data) { | ||
this->Insert(x); | ||
} | ||
} | ||
|
||
int Heap::Size() { return heap_.size(); } | ||
|
||
int Heap::Top() { | ||
if (Size() == 0) throw std::runtime_error("Empty Heap"); | ||
return heap_[0]; | ||
} | ||
|
||
int FindMin(std::vector<int> data) { | ||
Heap heap; | ||
heap.Build(data); | ||
LostPointer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return heap.Min(); | ||
} |
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,18 @@ | ||
#pragma once | ||
|
||
#include <vector> | ||
class Heap { | ||
public: | ||
int Top(); | ||
int Size(); | ||
int Min(); | ||
void Insert(int value); | ||
void Build(std::vector<int> data); | ||
|
||
private: | ||
std::vector<int> heap_; | ||
void SiftUp(int index); | ||
void SiftDown(int index); | ||
}; | ||
|
||
int FindMin(std::vector<int> data); |
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,33 @@ | ||
|
||
#include <gtest/gtest.h> | ||
|
||
TEST(TopologySort, Simple) { | ||
ASSERT_EQ(1, 1); // Stack [] | ||
#include <stdexcept> | ||
|
||
#include "heap.hpp" | ||
|
||
TEST(HeapMin, Simple) { | ||
ASSERT_EQ(FindMin(std::vector<int>{4, 5, 16, 3, 6}), 3); | ||
ASSERT_EQ(FindMin(std::vector<int>{29, 25, 10, 13, 14, 23, 4, 6}), 4); | ||
ASSERT_EQ(FindMin(std::vector<int>{29, 17, 16, 27, 6, 11}), 6); | ||
} | ||
|
||
LostPointer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
TEST(EmptyHeap, Simple) { | ||
Heap empty_heap; | ||
|
||
ASSERT_EQ(empty_heap.Size(), 0); | ||
EXPECT_THROW(empty_heap.Top(), std::runtime_error); | ||
EXPECT_THROW(empty_heap.Min(), std::runtime_error); | ||
} | ||
|
||
TEST(Heap, Simple) { | ||
Heap heap; | ||
heap.Insert(5); | ||
heap.Insert(3); | ||
|
||
ASSERT_EQ(heap.Size(), 2); | ||
ASSERT_EQ(heap.Top(), 3); | ||
|
||
ASSERT_EQ(heap.Min(), 3); | ||
|
||
ASSERT_EQ(heap.Size(), 1); | ||
ASSERT_EQ(heap.Top(), 5); | ||
} |
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,6 @@ | ||
#include <iostream> | ||
#include <vector> | ||
|
||
#include "qsort.hpp" | ||
|
||
int main() { return 0; } |
Empty file.
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,14 @@ | ||
#pragma once | ||
|
||
#include <iostream> | ||
#include <util.hpp> | ||
#include <vector> | ||
|
||
template <typename T> | ||
void QuickSort(std::vector<T>& arr, int l, int r) { | ||
if (l < r) { | ||
int q = Partition(arr, l, r); | ||
QuickSort(arr, l, q); | ||
QuickSort(arr, q + 1, r); | ||
} | ||
} |
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,36 @@ | ||
|
||
#include <gtest/gtest.h> | ||
|
||
TEST(TopologySort, Simple) { | ||
ASSERT_EQ(1, 1); // Stack [] | ||
#include <vector> | ||
|
||
#include "qsort.hpp" | ||
|
||
TEST(Qsort, Simple1) { | ||
std::vector<int> vec = {77, 42, 19, 53, 18, 20}; | ||
QuickSort<int>(vec, 0, vec.size() - 1); | ||
ASSERT_EQ((vec), (std::vector<int>{18, 19, 20, 42, 53, 77})); | ||
} | ||
|
||
TEST(Qsort, Simple2) { | ||
std::vector<double> vec2{12.75, 5.3, 1.1, 23.223, -13.1, 37.37}; | ||
QuickSort<double>(vec2, 0, vec2.size() - 1); | ||
ASSERT_EQ((vec2), | ||
(std::vector<double>{-13.1, 1.1, 5.3, 12.75, 23.223, 37.37})); | ||
} | ||
|
||
TEST(Qsort, Simple3) { | ||
std::vector<int> vec3 = {-1, 2, -1, 3, 4, 5, 2}; | ||
QuickSort<int>(vec3, 0, vec3.size() - 1); | ||
ASSERT_EQ((vec3), (std::vector<int>{-1, -1, 2, 2, 3, 4, 5})); | ||
} | ||
|
||
TEST(Qsort, Simple4) { | ||
std::vector<int> vec4{2, 4, 2, 1, 3, 4, 1}; | ||
QuickSort<int>(vec4, 0, vec4.size() - 1); | ||
ASSERT_EQ((vec4), (std::vector<int>{1, 1, 2, 2, 3, 4, 4})); | ||
} | ||
|
||
LostPointer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
TEST(Qsort, Simple5) { | ||
std::vector<int> vec5{}; | ||
QuickSort<int>(vec5, 0, vec5.size() - 1); | ||
ASSERT_EQ((vec5), std::vector<int>{}); | ||
} |
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.