Skip to content

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
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions lesson3/heap.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <vector>
//#include <priority_queue>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

это наверное нужно утащить в другой ПР со вторым заданием


// 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) {}
21 changes: 21 additions & 0 deletions lesson3/sorting_algorithms.cpp
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) {
}
}
}
62 changes: 62 additions & 0 deletions lesson5/search_tree.cpp
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) {}
54 changes: 54 additions & 0 deletions lesson5/search_tree.hpp
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 added sandbox/template/src/main
Binary file not shown.
5 changes: 4 additions & 1 deletion sandbox/template/src/main.cpp
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 added task_01/src/main
Binary file not shown.
21 changes: 20 additions & 1 deletion task_01/src/main.cpp
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;
}
41 changes: 41 additions & 0 deletions task_01/src/solution.h
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
}
39 changes: 35 additions & 4 deletions task_01/src/test.cpp
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));
}
1 change: 0 additions & 1 deletion task_01/src/topology_sort.cpp

This file was deleted.

1 change: 0 additions & 1 deletion task_01/src/topology_sort.hpp

This file was deleted.

Empty file added testfile.txt
Empty file.
Loading