diff --git a/lesson3/heap.cpp b/lesson3/heap.cpp new file mode 100644 index 00000000..307a8c1c --- /dev/null +++ b/lesson3/heap.cpp @@ -0,0 +1,19 @@ +#include +//#include + +// 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 values; + int size; +}; + +void Heap::sift_up(int i) {} \ No newline at end of file diff --git a/lesson3/sorting_algorithms.cpp b/lesson3/sorting_algorithms.cpp new file mode 100644 index 00000000..1b0610d9 --- /dev/null +++ b/lesson3/sorting_algorithms.cpp @@ -0,0 +1,21 @@ +#include + +// 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) + +void bubble_sort(std::vector& v) {} + +void td_merge_sort(std::vector& v) { + for (int l = 1; l < v.size(); l++) { + for (int i = 0; i + l < v.size(); i += 2 * l) { + } + } +} \ No newline at end of file diff --git a/lesson5/search_tree.cpp b/lesson5/search_tree.cpp new file mode 100644 index 00000000..8f66d502 --- /dev/null +++ b/lesson5/search_tree.cpp @@ -0,0 +1,62 @@ +#include "search_tree.hpp" + +#include + +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 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 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) {} \ No newline at end of file diff --git a/lesson5/search_tree.hpp b/lesson5/search_tree.hpp new file mode 100644 index 00000000..fb56cec0 --- /dev/null +++ b/lesson5/search_tree.hpp @@ -0,0 +1,54 @@ +#include +#include +#include +#include +#include + +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& Find(Node* current_node, int key); + std::string& Remove(int key); + void ForEach(std::function); + + private: + std::pair Find_Node(Node* current_node, int key); + + Node* head; +}; \ No newline at end of file diff --git a/sandbox/template/src/main b/sandbox/template/src/main new file mode 100755 index 00000000..9342dd8d Binary files /dev/null and b/sandbox/template/src/main differ diff --git a/sandbox/template/src/main.cpp b/sandbox/template/src/main.cpp index 0e4393ba..0a554f65 100644 --- a/sandbox/template/src/main.cpp +++ b/sandbox/template/src/main.cpp @@ -1,3 +1,6 @@ #include -int main() { return 0; } +int main() { + std::cout << "Hello world!"; + return 0; +} diff --git a/task_01/src/main b/task_01/src/main new file mode 100755 index 00000000..c3c1e2a9 Binary files /dev/null and b/task_01/src/main differ diff --git a/task_01/src/main.cpp b/task_01/src/main.cpp index 0e4393ba..7825a349 100644 --- a/task_01/src/main.cpp +++ b/task_01/src/main.cpp @@ -1,3 +1,22 @@ #include -int main() { return 0; } +#include "solution.h" + +int main() { + int sum, arr_size, t; + std::vector input_vector; + std::unordered_map 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 sol = solution(sum, input_vector); + std::cout << sol.first << ' ' << sol.second; + + return 0; +} \ No newline at end of file diff --git a/task_01/src/solution.h b/task_01/src/solution.h new file mode 100644 index 00000000..c423e0c5 --- /dev/null +++ b/task_01/src/solution.h @@ -0,0 +1,41 @@ +#include +#include + +/* + +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 solution(int sum, std::vector v) { + std::unordered_map 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 +} \ No newline at end of file diff --git a/task_01/src/test.cpp b/task_01/src/test.cpp index ef5a86ae..2908b463 100644 --- a/task_01/src/test.cpp +++ b/task_01/src/test.cpp @@ -1,8 +1,39 @@ #include -#include "topology_sort.hpp" +#include "solution.h" -TEST(TopologySort, Simple) { - ASSERT_EQ(1, 1); // Stack [] -} +TEST(solution, simple) { + std::vector v1 = {-2, 2, 3, 3, 5, 9, 11, 13, 14, 15}; + std::pair p1 = {-1, -1}; + ASSERT_EQ(p1, solution(10, v1)); + + std::vector v2 = {-2, 2, 3, 3, 5, 8, 11, 13, 14, 15}; + std::pair p2 = {1, 5}; + ASSERT_EQ(p2, solution(10, v2)); + + std::vector v3 = {}; + std::pair p3 = {-1, -1}; + ASSERT_EQ(p3, solution(0, v3)); + + std::vector v4 = {1}; + std::pair p4 = {-1, -1}; + ASSERT_EQ(p4, solution(1, v4)); + + std::vector v5 = {1, 2}; + std::pair 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 v6 = {1, 1, 1, 1, 1, 1, 1, 1, 1}; + std::pair 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 v7 = {1, 2, 1, 1, 4, 5, 1, 1, 1}; + std::pair p7 = {1, 4}; + ASSERT_EQ(p7, solution(6, v7)); +} \ No newline at end of file diff --git a/task_01/src/topology_sort.cpp b/task_01/src/topology_sort.cpp deleted file mode 100644 index e53f670c..00000000 --- a/task_01/src/topology_sort.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "topology_sort.hpp" diff --git a/task_01/src/topology_sort.hpp b/task_01/src/topology_sort.hpp deleted file mode 100644 index 6f70f09b..00000000 --- a/task_01/src/topology_sort.hpp +++ /dev/null @@ -1 +0,0 @@ -#pragma once diff --git a/testfile.txt b/testfile.txt new file mode 100644 index 00000000..e69de29b