-
Notifications
You must be signed in to change notification settings - Fork 15
homework 2 #46
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
TheFueRr
wants to merge
16
commits into
AlgorithmsDafeMipt2024:main
Choose a base branch
from
TheFueRr:task2
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 #46
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
e2c62ef
Initial commit
TheFueRr d4ec182
task_02 done
TheFueRr 9f6b332
task_03 done
TheFueRr 204487b
task_04 done
TheFueRr b7af4b5
task_05 done
TheFueRr 189a8b5
task_06 done
TheFueRr 91beae6
task_07 done
TheFueRr cbce95c
task_08 done
TheFueRr aab84f0
task_09 done
TheFueRr f860d5b
fix segfault
TheFueRr 45de90f
fix private field name
TheFueRr 5b04ec5
fix semantics
TheFueRr 590a11f
move func to utils
TheFueRr 5d6b245
fixed class name && added delete Nodes
TheFueRr 1326673
added test
TheFueRr 14eaf8f
fix
TheFueRr 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,29 @@ | ||
#include <functional> | ||
#include <random> | ||
#include <vector> | ||
|
||
template <typename T> | ||
static std::size_t Partition(std::vector<T>& data, std::size_t left, | ||
std::size_t right) { | ||
std::function<std::size_t(std::size_t, std::size_t)> RandomPivotIndex = | ||
[&](std::size_t left, std::size_t right) { | ||
std::random_device rd; | ||
std::mt19937 gen(rd()); | ||
std::uniform_int_distribution<> dis(left, right); | ||
return dis(gen); | ||
}; | ||
|
||
std::size_t pivot_index = RandomPivotIndex(left, right); | ||
std::swap(data[pivot_index], data[right]); | ||
std::size_t index = left; | ||
|
||
for (std::size_t i = left; i < right; i++) { | ||
if (data[i] < data[right]) { | ||
std::swap(data[i], data[index]); | ||
index++; | ||
} | ||
} | ||
|
||
std::swap(data[right], data[index]); | ||
return index; | ||
} |
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 "list_stacks.hpp" |
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,65 @@ | ||
#pragma once | ||
|
||
#include <list> | ||
#include <stdexcept> | ||
|
||
template <typename T> | ||
class Stack final { | ||
public: | ||
constexpr Stack() noexcept {} | ||
Stack(const Stack& other) : list_(other.list_) {} | ||
Stack(Stack&& other) : list_(std::move(other.list_)) { other.list_.clear(); } | ||
Stack(const std::initializer_list<T>& init_list) : list_(init_list) {} | ||
|
||
~Stack() = default; | ||
|
||
void Push(const T& value) { list_.push_back(value); } | ||
void Pop() { | ||
if (Empty()) throw std::underflow_error("Stack is empty!"); | ||
list_.pop_back(); | ||
} | ||
|
||
T& Top() { | ||
if (Empty()) throw std::underflow_error("Stack is empty!"); | ||
return list_.back(); | ||
} | ||
bool Empty() { return list_.empty(); } | ||
std::size_t Size() { return list_.size(); } | ||
|
||
private: | ||
std::list<T> list_; | ||
}; | ||
|
||
template <typename T> | ||
class StackMin final { | ||
public: | ||
constexpr StackMin() noexcept {} | ||
StackMin(const StackMin& other) : list_(other.list_) {} | ||
StackMin(StackMin&& other) : list_(other.list_) { other.list_.clear(); } | ||
StackMin(const std::initializer_list<T>& init_list_) : list_(init_list_) {} | ||
|
||
~StackMin() = default; | ||
|
||
void Push(const T& value) { | ||
if (min_stack.Empty() || min_stack.Top() > value) min_stack.Push(value); | ||
list_.push_back(value); | ||
} | ||
void Pop() { | ||
if (Empty()) throw std::underflow_error("Stack is empty!"); | ||
if (min_stack.Top() == list_.back()) min_stack.Pop(); | ||
list_.pop_back(); | ||
} | ||
|
||
T& Top() { | ||
if (Empty()) throw std::underflow_error("Stack is empty!"); | ||
return list_.back(); | ||
} | ||
bool Empty() { return list_.empty(); } | ||
std::size_t Size() { return list_.size(); } | ||
|
||
T& Min() { return min_stack.Top(); } | ||
|
||
private: | ||
std::list<T> list_; | ||
Stack<T> min_stack; | ||
}; |
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
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 @@ | ||
#include "temperature.hpp" | ||
|
||
std::vector<int> TemperatureCounter(std::vector<double> temps) { | ||
std::stack<Day> days; | ||
std::vector<int> result(temps.size(), 0); | ||
for (int i = 0; i < temps.size(); i++) { | ||
while (!days.empty() && days.top().value < temps[i]) { | ||
result[days.top().index] = i - days.top().index; | ||
days.pop(); | ||
} | ||
days.push(Day(i, temps[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,11 @@ | ||
#pragma once | ||
#include <stack> | ||
#include <vector> | ||
|
||
struct Day { | ||
int index; | ||
double value; | ||
Day(int index, double value) : index(index), value(value) {} | ||
}; | ||
|
||
std::vector<int> TemperatureCounter(std::vector<double> 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,18 @@ | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include "topology_sort.hpp" | ||
#include "temperature.hpp" | ||
|
||
TEST(TopologySort, Simple) { | ||
ASSERT_EQ(1, 1); // Stack [] | ||
} | ||
TEST(TemperatureCnt, Simple) { | ||
ASSERT_EQ(TemperatureCounter(std::vector<double>{5.8, 7.2, 4.1, 5.999}), | ||
(std::vector<int>{1, 0, 1, 0})); | ||
ASSERT_EQ( | ||
TemperatureCounter(std::vector<double>{5.99, 12, 4, 9.1, 5.15, 4, 2}), | ||
(std::vector<int>{1, 0, 1, 0, 0, 0, 0})); | ||
ASSERT_EQ( | ||
TemperatureCounter(std::vector<double>{2, 6, 17.512512, 7, 3, 4.412151}), | ||
(std::vector<int>{1, 1, 0, 0, 1, 0})); | ||
ASSERT_EQ(TemperatureCounter(std::vector<double>{}), std::vector<int>{}); | ||
ASSERT_EQ(TemperatureCounter(std::vector<double>{2, 6, 5, 4, 3, 7}), | ||
(std::vector<int>{1, 4, 3, 2, 1, 0})); | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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.