Skip to content
Open
Show file tree
Hide file tree
Changes from 9 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
2 changes: 1 addition & 1 deletion task_01/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Задача 1

Дано целое число и отсортированый массив целых чисел, нужно найти 2 числа из массива которые в сумме дадут заданное число
Дано целое число и отсортированый массив целых чисел, нужно найти 2 числа из массива которые в сумме дадут заданное число
1 change: 1 addition & 0 deletions task_01/file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
123
79 changes: 77 additions & 2 deletions task_01/src/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,81 @@

#include "topology_sort.hpp"

TEST(TopologySort, Simple) {
ASSERT_EQ(1, 1); // Stack []
// BinSearch tests
TEST(BinSearch, Simple) {
// even len
std::vector<int> mas = {1, 2, 3, 4, 5, 6, 7, 8};
ASSERT_EQ(BinSearch(mas, 1), 0);
ASSERT_EQ(BinSearch(mas, 2), 1);
ASSERT_EQ(BinSearch(mas, 3), 2);
ASSERT_EQ(BinSearch(mas, 4), 3);
ASSERT_EQ(BinSearch(mas, 5), 4);
ASSERT_EQ(BinSearch(mas, 6), 5);
ASSERT_EQ(BinSearch(mas, 7), 6);
ASSERT_EQ(BinSearch(mas, 8), 7);

ASSERT_EQ(BinSearch(mas, 31), -1);

// odd len
mas = {1, 2, 3, 4, 5, 6, 7, 8, 9};
ASSERT_EQ(BinSearch(mas, 1), 0);
ASSERT_EQ(BinSearch(mas, 2), 1);
ASSERT_EQ(BinSearch(mas, 3), 2);
ASSERT_EQ(BinSearch(mas, 4), 3);
ASSERT_EQ(BinSearch(mas, 5), 4);
ASSERT_EQ(BinSearch(mas, 6), 5);
ASSERT_EQ(BinSearch(mas, 7), 6);
ASSERT_EQ(BinSearch(mas, 8), 7);

ASSERT_EQ(BinSearch(mas, 31), -1);

// empty mas
mas = {};
ASSERT_EQ(BinSearch(mas, 8), -1);

// mas of 1 elem
mas = {1};
ASSERT_EQ(BinSearch(mas, 1), 0);

ASSERT_EQ(BinSearch(mas, 31), -1);

// mas of 2 elems
mas = {1, 2};
ASSERT_EQ(BinSearch(mas, 1), 0);
ASSERT_EQ(BinSearch(mas, 2), 1);

ASSERT_EQ(BinSearch(mas, 31), -1);
}

// NSquareSearch tests
TEST(NSquareSearch, Simple) {
std::vector<int> mas = {1, 2, 3, 4, 5, 6, 7, 8};
ASSERT_EQ(NSquareSearch(mas, 11), std::make_tuple(2, 7));
ASSERT_EQ(NSquareSearch(mas, 9), std::make_tuple(0, 7));

ASSERT_EQ(NSquareSearch(mas, 1), std::make_tuple(-1, -1));
ASSERT_EQ(NSquareSearch(mas, 16),
std::make_tuple(-1, -1)); // not (7, 7), as indexes cant be same
}

// NLogNSearch tests
TEST(NLogNSearch, Simple) {
std::vector<int> mas = {1, 2, 3, 4, 5, 6, 7, 8};
ASSERT_EQ(NLogNSearch(mas, 11), std::make_tuple(2, 7));
ASSERT_EQ(NLogNSearch(mas, 9), std::make_tuple(0, 7));

ASSERT_EQ(NLogNSearch(mas, 1), std::make_tuple(-1, -1));
ASSERT_EQ(NLogNSearch(mas, 16),
std::make_tuple(-1, -1)); // not (7, 7), as indexes cant be same
}

// NSearch tests
TEST(NSearch, Simple) {
std::vector<int> mas = {1, 2, 3, 4, 5, 6, 7, 8};
ASSERT_EQ(NSearch(mas, 11), std::make_tuple(2, 7));
ASSERT_EQ(NSearch(mas, 9), std::make_tuple(0, 7));

ASSERT_EQ(NSearch(mas, 1), std::make_tuple(-1, -1));
ASSERT_EQ(NSearch(mas, 16),
std::make_tuple(-1, -1)); // not (7, 7), as indexes cant be same
}
64 changes: 64 additions & 0 deletions task_01/src/topology_sort.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,65 @@
#include "topology_sort.hpp"

// O(logn)
int BinSearch(std::vector<int> massive, int num) {
int length = massive.size();
int left = 0;
int right = length - 1;
int index = length / 2;
while (right - left >= 0) {
if (massive[index] == num) {
return index;
} else if (massive[index] > num) {
right = index - 1;
} else if (massive[index] < num) {
left = index + 1;
}
index = (right + left) / 2;
}
return -1;
}

// O(n^2)
std::tuple<int, int> NSquareSearch(std::vector<int> massive, int num) {
int length = massive.size();
for (int i = 0; i < length; i++) {
for (int j = i + 1; j < length; j++) {
if (massive[i] + massive[j] == num) {
return {i, j};
}
}
}
return {-1, -1};
}

// O(nlogn)
std::tuple<int, int> NLogNSearch(std::vector<int> massive, int num) {
int length = massive.size();
for (int i = 0; i < length; i++) {
int el1 = massive[i];
int el2 = num - el1;
int j = BinSearch(massive, el2);
if (j != -1 && i != j) {
return {i, j};
}
}
return {-1, -1};
}

// O(n)
std::tuple<int, int> NSearch(std::vector<int> massive, int num) {
int length = massive.size();
int left = 0;
int right = length - 1;
while (right != left) {
int sum = massive[left] + massive[right];
if (sum == num) {
return {left, right};
} else if (sum < num) {
left++;
} else if (sum > num) {
right--;
}
}
return {-1, -1};
Copy link
Contributor

Choose a reason for hiding this comment

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

как отличить результат (-1, -1) от отсутствия результата?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

такого результата не может быть ни в каком случае, оба числа это индексы от 0 до len-1

Copy link
Contributor

Choose a reason for hiding this comment

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

ок

}
16 changes: 16 additions & 0 deletions task_01/src/topology_sort.hpp
Copy link
Contributor

Choose a reason for hiding this comment

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

давай файлы переименуем с тем что в них содержится

Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
#pragma once

#include <iostream>
#include <tuple>
#include <vector>

// O(logn)
int BinSearch(std::vector<int> mas, int num);

// O(n^2)
std::tuple<int, int> NSquareSearch(std::vector<int> massive, int num);

// O(nlogn)
std::tuple<int, int> NLogNSearch(std::vector<int> massive, int num);

// O(n)
std::tuple<int, int> NSearch(std::vector<int> massive, int num);