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
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) {
}
}
}
1 change: 1 addition & 0 deletions sandbox/template/src/commit_message.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
if you see this it means that the commit worked
Binary file added sandbox/template/src/main
Binary file not shown.
6 changes: 5 additions & 1 deletion sandbox/template/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#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.
50 changes: 49 additions & 1 deletion task_01/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,51 @@
#include <iostream>
#include <unordered_map>

int main() { return 0; }
/*

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)

void solution() {
int number, n, t;
bool flag = false; // in case there are no such numbers
std::unordered_map<int, int> mp;

std::cin >> number >> n;

for (int i = 0; i < n; i++) {
std::cin >> t;

if (mp.find(number - t) !=
mp.end()) { // if key "number - t" exists, we have found the solution
std::cout << mp[number - t] << ' ' << i;
flag = true;
break;
}

if (mp.find(t) == mp.end())
mp[t] = i; // We only add keys that weren't in the map before (that way
// we get the least possible sum of i and j)
}

if (!flag) std::cout << -1 << ' ' << -1;
}

int main() {
solution();
return 0;
}
57 changes: 55 additions & 2 deletions task_01/src/test.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,61 @@

#include <gtest/gtest.h>

#include <vector>

#include "topology_sort.hpp"

TEST(TopologySort, Simple) {
ASSERT_EQ(1, 1); // Stack []
std::pair<int, int> solution(int number, std::vector<int> v) {
Copy link
Contributor

Choose a reason for hiding this comment

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

вынеси в отдельный файл

Copy link
Author

Choose a reason for hiding this comment

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

Сделано

bool flag = false; // in case there are no such numbers
std::unordered_map<int, int> mp;

for (int i = 0; i < v.size(); i++) {
if (mp.find(number - v[i]) !=
mp.end()) { // if key "number - t" exists, we have found the solution
return {mp[number - v[i]], i};
flag = true;
break;
}

if (mp.find(v[i]) == mp.end())
mp[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)
}

if (!flag) return {-1, -1};
}

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));
}
Empty file added testfile.txt
Empty file.