-
Notifications
You must be signed in to change notification settings - Fork 15
task_01 #15
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
15
commits into
AlgorithmsDafeMipt2024:main
Choose a base branch
from
H0p1ty:main
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
task_01 #15
Changes from all commits
Commits
Show all changes
15 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 f387f87
Merge branch 'AlgorithmsDafeMipt2024:main' into main
H0p1ty fb20d8a
Merge branch 'AlgorithmsDafeMipt2024:main' into main
H0p1ty f32c7dc
question_mark
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
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,19 @@ | ||
#include <vector> | ||
//#include <priority_queue> | ||
|
||
// left child: 2*i + 1; | ||
// right child: 2*i + 2; | ||
// parent: [(i-1)/2]; | ||
|
||
struct Heap { | ||
Heap() : size{0}, values{} {} | ||
|
||
void sift_up(int i); | ||
void sift_down(int i); | ||
void append(int x); | ||
|
||
std::vector<int> values; | ||
int size; | ||
}; | ||
|
||
void Heap::sift_up(int i) {} |
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,21 @@ | ||
#include <vector> | ||
|
||
// bubble_sort: O*(n^2), O(n^2) | ||
// bogo_sort: O*(n!), O(+inf) | ||
// insertion_sort: O*(n^2), O(n^2) | ||
// selection_sort: O*(n^2), O(n^2) | ||
// merge_sort (top_down/bottom_up): O*(nlogn), O(nlogn) | ||
// quick_sort: O*(nlogn), (O(n^2) - ineffective, O(nlogn) - effective) | ||
|
||
// qsort [] = [] | ||
// qsort(x : xs) = qsort(filter(<x) xs) ++ [x] (filter(=x) xs) ++ | ||
// qsort(filter(>x) xs) | ||
|
||
void bubble_sort(std::vector<int>& v) {} | ||
|
||
void td_merge_sort(std::vector<int>& v) { | ||
for (int l = 1; l < v.size(); l++) { | ||
for (int i = 0; i + l < v.size(); i += 2 * l) { | ||
} | ||
} | ||
} |
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,62 @@ | ||
#include "search_tree.hpp" | ||
|
||
#include <stdexcept> | ||
|
||
void Node::add_left_node(std::string data_, int key_) { | ||
left_child = new Node(data_, key_, this); | ||
} | ||
|
||
void Node::add_right_node(std::string data_, int key_) { | ||
right_child = new Node(data_, key_, this); | ||
} | ||
|
||
void Node_swap(Node* node1, Node* node2) { | ||
std::swap(node1->data, node2->data); | ||
std::swap(node1->key, node2->key); | ||
} | ||
|
||
std::string& Tree::Find(Node* current_node, int key) { | ||
if (current_node == nullptr) throw std::runtime_error("No such key"); | ||
if (current_node->key == key) | ||
return current_node->data; | ||
else if (key < current_node->key) | ||
return Find(current_node->left_child, key); | ||
else | ||
return Find(current_node->right_child, key); | ||
} | ||
|
||
std::pair<std::string, Node*> Tree::Find_Node(Node* current_node, int key) { | ||
if (current_node == nullptr) return {"no head", current_node}; | ||
if (current_node->key == key) | ||
return {"repeat", current_node}; | ||
else if (key < current_node->key) { | ||
if (current_node->left_child == nullptr) return {"left", current_node}; | ||
return Find_Node(current_node->left_child, key); | ||
} else { | ||
if (current_node->right_child == nullptr) return {"right", current_node}; | ||
return Find_Node(current_node->right_child, key); | ||
} | ||
} | ||
|
||
bool Tree::Add(int key, std::string data_) { | ||
std::pair<std::string, Node*> needed_node_pair = Find_Node(head, key); | ||
if (needed_node_pair.first == "no head") { | ||
head = new Node(data_, key); | ||
return true; | ||
} else if (needed_node_pair.first == "repeat") | ||
return false; | ||
else if (needed_node_pair.first == "left") { | ||
needed_node_pair.second->left_child = | ||
new Node(data_, key, needed_node_pair.second); | ||
return true; | ||
} else if (needed_node_pair.first == "right") { | ||
needed_node_pair.second->left_child = | ||
new Node(data_, key, needed_node_pair.second); | ||
return true; | ||
} | ||
throw std::runtime_error( | ||
"Tree::Add(...) function reaches the end without returning a value, fix " | ||
"this"); | ||
} | ||
|
||
std::string& Tree::Remove(int key) {} |
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,54 @@ | ||
#include <algorithm> | ||
#include <functional> | ||
#include <optional> | ||
#include <stdexcept> | ||
#include <string> | ||
|
||
class Summator { | ||
int sum; | ||
void operator()(int, std::string); | ||
}; | ||
|
||
struct Node { | ||
Node() | ||
: parent{nullptr}, | ||
left_child{nullptr}, | ||
right_child{nullptr}, | ||
data{""}, | ||
key{0} {} | ||
Node(std::string data_, int key_) | ||
: parent{nullptr}, | ||
left_child{nullptr}, | ||
right_child{nullptr}, | ||
data{data_}, | ||
key{key_} {} | ||
Node(std::string data_, int key_, Node* parent_) | ||
: parent{parent_}, | ||
left_child{nullptr}, | ||
right_child{nullptr}, | ||
data{data_}, | ||
key{key_} {} | ||
void add_left_node(std::string data_, int key_); | ||
void add_right_node(std::string data_, int key_); | ||
Node* parent; | ||
Node* left_child; | ||
Node* right_child; | ||
int key; | ||
std::string data; | ||
}; | ||
|
||
class Tree { | ||
public: | ||
Tree() : head{nullptr} {} | ||
|
||
bool Add(int key, std::string data_); | ||
// could be replaced with "std::optional<std::string>" | ||
std::string& Find(Node* current_node, int key); | ||
std::string& Remove(int key); | ||
void ForEach(std::function<void(int, std::string)>); | ||
|
||
private: | ||
std::pair<std::string, Node*> Find_Node(Node* current_node, int key); | ||
|
||
Node* head; | ||
}; |
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; | ||
} |
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,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)); | ||
LostPointer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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.
Empty file.
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.
это наверное нужно утащить в другой ПР со вторым заданием