-
Notifications
You must be signed in to change notification settings - Fork 15
Homework 2 #27
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
H0p1ty
wants to merge
92
commits into
AlgorithmsDafeMipt2024:main
Choose a base branch
from
H0p1ty:task_08
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 #27
Changes from 70 commits
Commits
Show all changes
92 commits
Select commit
Hold shift + click to select a range
5723e72
test commit
H0p1ty cf43a75
test_commit
H0p1ty cc0495e
task_01 completed
H0p1ty 40c61e7
lesson3
H0p1ty da7e472
test commit
H0p1ty 7ed00bb
task_01 fixes
H0p1ty 7acfbeb
note fix
H0p1ty 44cbf54
pre-fix commit
H0p1ty 85acfc8
lesson5
H0p1ty 51f832e
task_01 fixed x2
H0p1ty 61a3916
Merge branch 'AlgorithmsDafeMipt2024:main' into main
H0p1ty 814b95d
Merge branch 'lessons'
H0p1ty 36763b5
pilot version of hash_table is done
H0p1ty 566cdf6
task_08 first version done
H0p1ty cc6a9e2
tests for task_08 done
H0p1ty 971c93e
final tweaks
H0p1ty fc6eb9b
task_02 done
H0p1ty a495204
task_03 done
H0p1ty 9cc070a
minor change
H0p1ty 1468b8c
task_04 started
H0p1ty 177c895
deleted obsolete task_01
H0p1ty bfd57c2
task_04 done without tests
H0p1ty 52c5a98
task_04 minor changes
H0p1ty 778bfc9
task_04 minor changes x2
H0p1ty 64f2c24
task_04 added 1 test
H0p1ty 96abce9
Merge branch 'AlgorithmsDafeMipt2024:main' into task_08
H0p1ty 50aea4f
task_04 done, but i don't want to erase my efforts
H0p1ty 84ba125
task_04 done
H0p1ty 207c1d0
task_04 done
H0p1ty 03fcab4
task_05 started
H0p1ty ec8bb96
added pragma once to task_04
H0p1ty 0a81053
task_05 done
H0p1ty 873bac6
task_05 fixed with debugging
H0p1ty 2c582db
task_05 fixed with deleted comments of debugging
H0p1ty 3949b62
task_05 perfected
H0p1ty a1ee568
minor tweaks x3
H0p1ty 09ac4e8
task_05 final tweaks
H0p1ty 7c98135
I don't need these files here
H0p1ty 21aaff5
task_04 important change
H0p1ty 5edc8b0
task_04 done
H0p1ty 6c642c0
task_06 done
H0p1ty 7c10268
task_08 added concept
H0p1ty 8291ee1
task_07 first version done
H0p1ty 2dd8289
task_07 unfinished
H0p1ty 7580986
task_07 fixed
H0p1ty 7195c20
task_07 added few tests
H0p1ty f7cc975
added empty stack test
H0p1ty 249fe5b
this file is obsolete
H0p1ty 97f3b43
task_02 added concepts
H0p1ty 9c6ec83
task_02 added comments
H0p1ty 0751218
task_02 added comments x2
H0p1ty 92e1d72
task_04 added comments
H0p1ty 3b23b55
task_04 added constructable concept
H0p1ty 1bacbf3
task_04 added comments
H0p1ty 25fb15e
task_05 added comments
H0p1ty d68e790
task_07 bugfixes
H0p1ty 4de7a45
task_07 added BFS
H0p1ty df8591a
task_07 tweaks
H0p1ty 1f71dbb
task_09 done
H0p1ty 3d0a44c
task_07 delete breadth first search
H0p1ty aedfad6
task_07 tweaks
H0p1ty f27d3b0
task_02 added new test
H0p1ty be7a244
task_07 added comments
H0p1ty bb82451
task_09 tweaked names
H0p1ty 585d136
task_06 minor change
H0p1ty 33e5080
task_02 fixes
H0p1ty bf06d08
task_02 fixes x2
H0p1ty 0ef8e2b
task_04 fixes
H0p1ty 9d53c25
task_08 fixes
H0p1ty 70b39e9
task_01 returned
H0p1ty 47dbcee
task_07 names changed
H0p1ty 9444408
task_06 names changed
H0p1ty 764daed
task_05 names changed
H0p1ty 285a8d8
task_04 names changed
H0p1ty bdf941f
task_08 names changed
H0p1ty 7f57b8d
task_08 names changed x2
H0p1ty b996969
task_09 suggestion added
H0p1ty fd60337
Merge branch 'AlgorithmsDafeMipt2024:main' into task_08
H0p1ty 80a5e56
task_02 suggested changes done
H0p1ty 2e0ee9e
task_05 ultra minor name change
H0p1ty 36426d5
task_04 names fixed
H0p1ty 6a3d26b
task_08 codestyle fixes
H0p1ty 1802c87
task_07 codestyle fixes
H0p1ty bb17319
task_09 added test
H0p1ty 4183342
task_03 codestyle fixes
H0p1ty ed8b30f
task_02 codestyle changes
H0p1ty 5434781
moved duplicate code lines to "util.hpp"
H0p1ty 4d541aa
Delete sandbox/template/src/main
H0p1ty a30bc2f
revert sandbox/template/src/main.cpp
H0p1ty 414db7b
task_07 final fixes
H0p1ty 5b23365
Merge branch 'task_08' of https://github.com/H0p1ty/spring_homework i…
H0p1ty 57c83a8
task_07 final fixes x2
H0p1ty 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
Binary file not shown.
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> | ||
|
||
int main() { return 0; } | ||
int main() { | ||
std::cout << "Hello world!"; | ||
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,3 +1,3 @@ | ||
# Задача 1 | ||
|
||
Дано целое число и отсортированый массив целых чисел, нужно найти 2 числа из массива которые в сумме дадут заданное число | ||
Дано целое число и массив целых чисел, нужно найти 2 числа из массива которые в сумме дадут заданное число |
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,22 @@ | ||
#include <iostream> | ||
|
||
int main() { return 0; } | ||
#include "solution.h" | ||
|
||
int main() { | ||
int sum, arr_size, t; | ||
std::vector<int> input_vector; | ||
std::unordered_map<int, int> indices_map; // keeps array numbers as keys and | ||
// indices as values behind keys | ||
|
||
std::cin >> sum >> arr_size; | ||
|
||
for (int i = 0; i < arr_size; i++) { | ||
std::cin >> t; | ||
input_vector.push_back(t); | ||
} | ||
|
||
std::pair<int, int> sol = solution(sum, input_vector); | ||
std::cout << sol.first << ' ' << sol.second; | ||
|
||
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include <unordered_map> | ||
#include <vector> | ||
|
||
/* | ||
|
||
Output - indices of two numbers, which sum is equal to needed number, if there's | ||
no such numbers, the output is "-1 -1" | ||
|
||
Input: | ||
|
||
10 | ||
10 | ||
-2 2 3 3 5 8 11 13 14 15 | ||
|
||
Output: | ||
|
||
1 5 | ||
|
||
*/ | ||
|
||
// Solution below has a time complexity of O(n) and memory complexity of O(n) | ||
|
||
std::pair<int, int> solution(int sum, std::vector<int> v) { | ||
std::unordered_map<int, int> indices_map; // keeps array numbers as keys and | ||
// indices as values behind keys | ||
|
||
for (int i = 0; i < v.size(); i++) { | ||
if (indices_map.find(sum - v[i]) != | ||
indices_map | ||
.end()) { // if key "number - t" exists, we have found the solution | ||
return {indices_map[sum - v[i]], i}; | ||
} | ||
|
||
if (indices_map.find(v[i]) == indices_map.end()) | ||
indices_map[v[i]] = | ||
i; // We only add keys that weren't in the map before (that | ||
// way we get the least possible sum of i and j) | ||
} | ||
|
||
return {-1, -1}; // in case there are no such numbers | ||
} |
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,39 @@ | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include "topology_sort.hpp" | ||
#include "solution.h" | ||
|
||
TEST(TopologySort, Simple) { | ||
ASSERT_EQ(1, 1); // Stack [] | ||
} | ||
TEST(solution, simple) { | ||
std::vector<int> v1 = {-2, 2, 3, 3, 5, 9, 11, 13, 14, 15}; | ||
std::pair<int, int> p1 = {-1, -1}; | ||
ASSERT_EQ(p1, solution(10, v1)); | ||
|
||
std::vector<int> v2 = {-2, 2, 3, 3, 5, 8, 11, 13, 14, 15}; | ||
std::pair<int, int> p2 = {1, 5}; | ||
ASSERT_EQ(p2, solution(10, v2)); | ||
|
||
std::vector<int> v3 = {}; | ||
std::pair<int, int> p3 = {-1, -1}; | ||
ASSERT_EQ(p3, solution(0, v3)); | ||
|
||
std::vector<int> v4 = {1}; | ||
std::pair<int, int> p4 = {-1, -1}; | ||
ASSERT_EQ(p4, solution(1, v4)); | ||
|
||
std::vector<int> v5 = {1, 2}; | ||
std::pair<int, int> p5 = {0, 1}; | ||
ASSERT_EQ(p5, solution(3, v5)); | ||
|
||
// if there are multiple solutions, the algorithm | ||
// will pick the one, in which sum of i and j is the least, | ||
// where i and j are indices of numbers, which sum is equal to needed number | ||
std::vector<int> v6 = {1, 1, 1, 1, 1, 1, 1, 1, 1}; | ||
std::pair<int, int> p6 = {0, 1}; | ||
ASSERT_EQ(p6, solution(2, v6)); | ||
|
||
// in case there are multiple solutions in which i+j is the least, | ||
// the algorithm will pick the one, in which i is greater | ||
std::vector<int> v7 = {1, 2, 1, 1, 4, 5, 1, 1, 1}; | ||
std::pair<int, int> p7 = {1, 4}; | ||
ASSERT_EQ(p7, solution(6, v7)); | ||
} |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,23 +1,122 @@ | ||
#pragma once | ||
|
||
#include <algorithm> | ||
#include <stack> | ||
#include <stdexcept> | ||
#include <vector> | ||
|
||
class Stack { | ||
// check if type comparasion operators are defined for a class | ||
template <typename CutsomType> | ||
concept comparable = requires(CutsomType a, CutsomType b) { | ||
a < b; | ||
a <= b; | ||
a == b; | ||
a >= b; | ||
a > b; | ||
a != b; | ||
}; | ||
|
||
// simple structure, helps to form a linked list | ||
template <typename CutsomType> | ||
struct Node { | ||
Node() : prev{nullptr}, data{}, next{nullptr} {} | ||
Node(CutsomType data_) : prev{nullptr}, data{data_}, next{nullptr} {} | ||
void AddNode(CutsomType data_); | ||
Node* next; | ||
Node* prev; | ||
CutsomType data; | ||
}; | ||
|
||
// method, that allows to add a node with a value to an existing one | ||
template <typename CutsomType> | ||
void Node<CutsomType>::AddNode(CutsomType data_) { | ||
next = new Node(data_); | ||
next->prev = this; | ||
} | ||
|
||
// data structure that can store and retrieve data in such fashion: | ||
// last element inserted will be retrieved first (last in first out (LIFO)) | ||
// insertion, deletion and retrieval has time complexity of O(1) | ||
template <typename CutsomType> | ||
class MyStack { | ||
public: | ||
void Push(int value); | ||
int Pop(); | ||
MyStack() : top_{nullptr} {} | ||
|
||
// insert an element at the top of the stack | ||
void Push(CutsomType value); | ||
|
||
// retrieve last inserted element | ||
CutsomType Top(); | ||
// retrieve last inserted element and delete it from the stack | ||
CutsomType Pop(); | ||
// check if stack is empty | ||
bool Empty() const { return top_ == nullptr; } | ||
|
||
private: | ||
std::stack<int> data_; | ||
Node<CutsomType>* top_; | ||
}; | ||
|
||
template <typename CutsomType> | ||
void MyStack<CutsomType>::Push(CutsomType value) { | ||
if (top_ == nullptr) | ||
top_ = new Node(value); | ||
else { | ||
top_->AddNode(value); | ||
top_ = top_->next; | ||
} | ||
} | ||
|
||
template <typename CutsomType> | ||
CutsomType MyStack<CutsomType>::Top() { | ||
if (top_ == nullptr) throw std::runtime_error("out_of_range\n"); | ||
LostPointer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return top_->data; | ||
} | ||
|
||
template <typename CutsomType> | ||
CutsomType MyStack<CutsomType>::Pop() { | ||
if (top_ == nullptr) throw std::runtime_error("out_of_range\n"); | ||
Node<CutsomType>* temp = top_->prev; | ||
CutsomType val = top_->data; | ||
LostPointer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
delete top_; | ||
top_ = temp; | ||
return val; | ||
} | ||
|
||
template <comparable CutsomType> | ||
class MinStack { | ||
public: | ||
void Push(int value); | ||
int Pop(); | ||
int GetMin(); | ||
// insert an element at the top of the stack | ||
void Push(CutsomType value); | ||
// retrieve last inserted element and delete it from the stack | ||
CutsomType Pop(); | ||
// retrieve minimal element | ||
CutsomType Min(); | ||
// retrieve last inserted element | ||
CutsomType Top(); | ||
|
||
private: | ||
std::vector<int> data_; | ||
MyStack<std::pair<CutsomType, CutsomType>> stack; | ||
}; | ||
|
||
template <comparable CutsomType> | ||
void MinStack<CutsomType>::Push(CutsomType value) { | ||
if (stack.Empty()) | ||
stack.Push({value, value}); | ||
else | ||
stack.Push({value, std::min(value, stack.Top().second)}); | ||
} | ||
|
||
template <comparable CutsomType> | ||
CutsomType MinStack<CutsomType>::Pop() { | ||
return stack.Pop().first; | ||
} | ||
|
||
template <comparable CutsomType> | ||
CutsomType MinStack<CutsomType>::Min() { | ||
return stack.Top().second; | ||
} | ||
|
||
template <comparable CutsomType> | ||
CutsomType MinStack<CutsomType>::Top() { | ||
return stack.Top().first; | ||
} |
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,33 @@ | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include "topology_sort.hpp" | ||
#include <cstddef> | ||
|
||
#include "weather.h" | ||
|
||
TEST(weather, Simple) { | ||
std::vector<size_t> data_vector_1 = weather_algorithm({1, 2, 3, 4, 5}); | ||
std::vector<size_t> assert_vector_1 = {1, 1, 1, 1, 0}; | ||
|
||
ASSERT_EQ(data_vector_1, assert_vector_1); | ||
|
||
std::vector<size_t> data_vector_2 = weather_algorithm({5}); | ||
std::vector<size_t> assert_vector_2 = {0}; | ||
|
||
ASSERT_EQ(data_vector_2, assert_vector_2); | ||
|
||
std::vector<size_t> data_vector_3 = weather_algorithm({}); | ||
std::vector<size_t> assert_vector_3 = {}; | ||
|
||
ASSERT_EQ(data_vector_3, assert_vector_3); | ||
|
||
std::vector<size_t> data_vector_4 = | ||
weather_algorithm({-1, -1, 2, 2, -5, 3, 4, 1}); | ||
std::vector<size_t> assert_vector_4 = {2, 1, 3, 2, 1, 1, 0, 0}; | ||
|
||
ASSERT_EQ(data_vector_4, assert_vector_4); | ||
|
||
std::vector<size_t> data_vector_5 = weather_algorithm({6, 5, 4, 3, 2, 1}); | ||
std::vector<size_t> assert_vector_5 = {0, 0, 0, 0, 0, 0}; | ||
|
||
TEST(TopologySort, Simple) { | ||
ASSERT_EQ(1, 1); // Stack [] | ||
ASSERT_EQ(data_vector_5, assert_vector_5); | ||
} |
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.
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.
удали лишний файл из ПРа)