Skip to content

Hw 1 #48

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 6 commits into
base: main
Choose a base branch
from
Open

Hw 1 #48

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
16 changes: 13 additions & 3 deletions task_01/src/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@

#include "topology_sort.hpp"

TEST(TopologySort, Simple) {
ASSERT_EQ(1, 1); // Stack []
}
TEST(find_pair_with_sum, Simple) {
std::vector<int> test1 = {2, 3, 7, 12, 13, 28, 30, 60};

ASSERT_EQ(find_pair_with_sum(test1, 14), std::make_pair(0, 3));
ASSERT_EQ(find_pair_with_sum(test1, 20), std::make_pair(2, 4));
ASSERT_EQ(find_pair_with_sum(test1, 21), std::make_pair(-1, -1));
ASSERT_EQ(find_pair_with_sum(test1, 58), std::make_pair(5, 6));
ASSERT_EQ(find_pair_with_sum(test1, 70), std::make_pair(-1, -1));
ASSERT_EQ(find_pair_with_sum(test1, -12312), std::make_pair(-1, -1));

std::vector<int> test2 = {};
ASSERT_EQ(find_pair_with_sum(test2, 1), std::make_pair(-1, -1));
}
21 changes: 21 additions & 0 deletions task_01/src/topology_sort.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,22 @@
#include "topology_sort.hpp"

std::pair<int, int> find_pair_with_sum(std::vector<int> mass, int a) {
if (!std::is_sorted(mass.begin(), mass.end()) or mass.size() == 0) {
return std::pair<int, int>(-1, -1);
}
int L = 0;
int R = mass.size() - 1;

while (L != R) {
if (mass[L] + mass[R] == a) {
return std::pair<int, int>(L, R);
}
if (mass[L] + mass[R] < a) {
L += 1;
} else {
R -= 1;
}
}

return std::pair<int, int>(-1, -1);
}
5 changes: 5 additions & 0 deletions task_01/src/topology_sort.hpp
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
#pragma once

#include <algorithm>
#include <vector>

std::pair<int, int> find_pair_with_sum(std::vector<int> mass, int a);
Loading