From df93d95101e7e882d69f655f5957e6ad145265a9 Mon Sep 17 00:00:00 2001 From: XehiDat <63257446+ZetroRetro@users.noreply.github.com> Date: Tue, 4 Jun 2024 18:42:32 +0000 Subject: [PATCH 1/6] homework 1 --- task_01/src/test.cpp | 16 +++++++++++++--- task_01/src/topology_sort.cpp | 21 +++++++++++++++++++++ task_01/src/topology_sort.hpp | 5 +++++ 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/task_01/src/test.cpp b/task_01/src/test.cpp index ef5a86ae..e4d47237 100644 --- a/task_01/src/test.cpp +++ b/task_01/src/test.cpp @@ -3,6 +3,16 @@ #include "topology_sort.hpp" -TEST(TopologySort, Simple) { - ASSERT_EQ(1, 1); // Stack [] -} +TEST(find_pair_with_sum, Simple) { + std::vector test1 = {2, 3, 7, 12, 13, 28, 30, 60}; + + ASSERT_EQ(find_pair_with_sum(test1, 14), {0, 4}); + ASSERT_EQ(find_pair_with_sum(test1, 20), {2, 4}); + ASSERT_EQ(find_pair_with_sum(test1, 21), {-1, -1}); + ASSERT_EQ(find_pair_with_sum(test1, 58), {6, 7}); + ASSERT_EQ(find_pair_with_sum(test1, 70), {-1, -1}); + ASSERT_EQ(find_pair_with_sum(test1, -12312), {-1, -1}); + + std::vector test2 = {}; + ASSERT_EQ(find_pair_with_sum(test2, 1), {-1, -1}); +} \ No newline at end of file diff --git a/task_01/src/topology_sort.cpp b/task_01/src/topology_sort.cpp index e53f670c..aaeffb40 100644 --- a/task_01/src/topology_sort.cpp +++ b/task_01/src/topology_sort.cpp @@ -1 +1,22 @@ #include "topology_sort.hpp" + +std::vector find_pair_with_sum(std::vector mass, int a) { + if (!std::is_sorted(mass.begin(), mass.end())) { + return {-1, -1}; + } + int L = 0; + int R = mass.size() - 1; + + while (L != R) { + if (mass[L] + mass[R] == a) { + return {L, R}; + } + if (mass[L] + mass[R] < a) { + L += 1; + } else { + R -= 1; + } + } + + return {-1, -1}; +} diff --git a/task_01/src/topology_sort.hpp b/task_01/src/topology_sort.hpp index 6f70f09b..b1657442 100644 --- a/task_01/src/topology_sort.hpp +++ b/task_01/src/topology_sort.hpp @@ -1 +1,6 @@ #pragma once + +#include +#include + +std::vector find_pair_with_sum(std::vector mass, int a); \ No newline at end of file From 16dee441fb42c2d072e76f368c294fa603158118 Mon Sep 17 00:00:00 2001 From: XehiDat <63257446+ZetroRetro@users.noreply.github.com> Date: Tue, 4 Jun 2024 19:49:48 +0000 Subject: [PATCH 2/6] hw 1 --- task_01/src/test.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/task_01/src/test.cpp b/task_01/src/test.cpp index e4d47237..66308445 100644 --- a/task_01/src/test.cpp +++ b/task_01/src/test.cpp @@ -6,13 +6,13 @@ TEST(find_pair_with_sum, Simple) { std::vector test1 = {2, 3, 7, 12, 13, 28, 30, 60}; - ASSERT_EQ(find_pair_with_sum(test1, 14), {0, 4}); - ASSERT_EQ(find_pair_with_sum(test1, 20), {2, 4}); - ASSERT_EQ(find_pair_with_sum(test1, 21), {-1, -1}); - ASSERT_EQ(find_pair_with_sum(test1, 58), {6, 7}); - ASSERT_EQ(find_pair_with_sum(test1, 70), {-1, -1}); - ASSERT_EQ(find_pair_with_sum(test1, -12312), {-1, -1}); + ASSERT_EQ(find_pair_with_sum(test1, 14), std::vector(0, 4)); + ASSERT_EQ(find_pair_with_sum(test1, 20), std::vector(2, 4)); + ASSERT_EQ(find_pair_with_sum(test1, 21), std::vector(-1, -1)); + ASSERT_EQ(find_pair_with_sum(test1, 58), std::vector(6, 7)); + ASSERT_EQ(find_pair_with_sum(test1, 70), std::vector(-1, -1)); + ASSERT_EQ(find_pair_with_sum(test1, -12312), std::vector(-1, -1)); std::vector test2 = {}; - ASSERT_EQ(find_pair_with_sum(test2, 1), {-1, -1}); + ASSERT_EQ(find_pair_with_sum(test2, 1), std::vector(-1, -1)); } \ No newline at end of file From 708e1dd0306cb24b6bfd88751b5b0989b06779a7 Mon Sep 17 00:00:00 2001 From: XehiDat <63257446+ZetroRetro@users.noreply.github.com> Date: Tue, 4 Jun 2024 20:03:57 +0000 Subject: [PATCH 3/6] fix --- task_01/src/test.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/task_01/src/test.cpp b/task_01/src/test.cpp index 66308445..ac59b305 100644 --- a/task_01/src/test.cpp +++ b/task_01/src/test.cpp @@ -6,13 +6,13 @@ TEST(find_pair_with_sum, Simple) { std::vector test1 = {2, 3, 7, 12, 13, 28, 30, 60}; - ASSERT_EQ(find_pair_with_sum(test1, 14), std::vector(0, 4)); - ASSERT_EQ(find_pair_with_sum(test1, 20), std::vector(2, 4)); - ASSERT_EQ(find_pair_with_sum(test1, 21), std::vector(-1, -1)); - ASSERT_EQ(find_pair_with_sum(test1, 58), std::vector(6, 7)); - ASSERT_EQ(find_pair_with_sum(test1, 70), std::vector(-1, -1)); - ASSERT_EQ(find_pair_with_sum(test1, -12312), std::vector(-1, -1)); + // ASSERT_EQ(find_pair_with_sum(test1, 14), std::vector(0, 4)); + ASSERT_EQ(find_pair_with_sum(test1, 20), std::vector({2, 4})); + ASSERT_EQ(find_pair_with_sum(test1, 21), std::vector({-1, -1})); + ASSERT_EQ(find_pair_with_sum(test1, 58), std::vector({5, 6})); + ASSERT_EQ(find_pair_with_sum(test1, 70), std::vector({-1, -1})); + ASSERT_EQ(find_pair_with_sum(test1, -12312), std::vector({-1, -1})); std::vector test2 = {}; - ASSERT_EQ(find_pair_with_sum(test2, 1), std::vector(-1, -1)); + ASSERT_EQ(find_pair_with_sum(test2, 1), std::vector({-1, -1})); } \ No newline at end of file From 0019009add2f52d4f6d85e2eb92fa29ff4e01220 Mon Sep 17 00:00:00 2001 From: XehiDat <63257446+ZetroRetro@users.noreply.github.com> Date: Tue, 4 Jun 2024 20:10:38 +0000 Subject: [PATCH 4/6] another fix --- task_01/src/test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/task_01/src/test.cpp b/task_01/src/test.cpp index ac59b305..79e8feec 100644 --- a/task_01/src/test.cpp +++ b/task_01/src/test.cpp @@ -6,7 +6,7 @@ TEST(find_pair_with_sum, Simple) { std::vector test1 = {2, 3, 7, 12, 13, 28, 30, 60}; - // ASSERT_EQ(find_pair_with_sum(test1, 14), std::vector(0, 4)); + ASSERT_EQ(find_pair_with_sum(test1, 14), std::vector({0, 3})); ASSERT_EQ(find_pair_with_sum(test1, 20), std::vector({2, 4})); ASSERT_EQ(find_pair_with_sum(test1, 21), std::vector({-1, -1})); ASSERT_EQ(find_pair_with_sum(test1, 58), std::vector({5, 6})); From caaef567b1ee1d12137f619a29124f53268a7d8b Mon Sep 17 00:00:00 2001 From: XehiDat <63257446+ZetroRetro@users.noreply.github.com> Date: Tue, 4 Jun 2024 20:23:19 +0000 Subject: [PATCH 5/6] changed to pair (from vector) --- task_01/src/test.cpp | 15 ++++++++------- task_01/src/topology_sort.cpp | 10 +++++----- task_01/src/topology_sort.hpp | 2 +- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/task_01/src/test.cpp b/task_01/src/test.cpp index 79e8feec..79a39626 100644 --- a/task_01/src/test.cpp +++ b/task_01/src/test.cpp @@ -6,13 +6,14 @@ TEST(find_pair_with_sum, Simple) { std::vector test1 = {2, 3, 7, 12, 13, 28, 30, 60}; - ASSERT_EQ(find_pair_with_sum(test1, 14), std::vector({0, 3})); - ASSERT_EQ(find_pair_with_sum(test1, 20), std::vector({2, 4})); - ASSERT_EQ(find_pair_with_sum(test1, 21), std::vector({-1, -1})); - ASSERT_EQ(find_pair_with_sum(test1, 58), std::vector({5, 6})); - ASSERT_EQ(find_pair_with_sum(test1, 70), std::vector({-1, -1})); - ASSERT_EQ(find_pair_with_sum(test1, -12312), std::vector({-1, -1})); + 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 test2 = {}; - ASSERT_EQ(find_pair_with_sum(test2, 1), std::vector({-1, -1})); + ASSERT_EQ(find_pair_with_sum(test2, 1), std::make_pair(-1, -1)); + } \ No newline at end of file diff --git a/task_01/src/topology_sort.cpp b/task_01/src/topology_sort.cpp index aaeffb40..6b3429e4 100644 --- a/task_01/src/topology_sort.cpp +++ b/task_01/src/topology_sort.cpp @@ -1,15 +1,15 @@ #include "topology_sort.hpp" -std::vector find_pair_with_sum(std::vector mass, int a) { - if (!std::is_sorted(mass.begin(), mass.end())) { - return {-1, -1}; +std::pair find_pair_with_sum(std::vector mass, int a) { + if (!std::is_sorted(mass.begin(), mass.end()) or mass.size() == 0) { + return std::pair(-1, -1); } int L = 0; int R = mass.size() - 1; while (L != R) { if (mass[L] + mass[R] == a) { - return {L, R}; + return std::pair(L, R); } if (mass[L] + mass[R] < a) { L += 1; @@ -18,5 +18,5 @@ std::vector find_pair_with_sum(std::vector mass, int a) { } } - return {-1, -1}; + return std::pair(-1, -1); } diff --git a/task_01/src/topology_sort.hpp b/task_01/src/topology_sort.hpp index b1657442..033c4f5e 100644 --- a/task_01/src/topology_sort.hpp +++ b/task_01/src/topology_sort.hpp @@ -3,4 +3,4 @@ #include #include -std::vector find_pair_with_sum(std::vector mass, int a); \ No newline at end of file +std::pair find_pair_with_sum(std::vector mass, int a); \ No newline at end of file From 33d3269acd256fc278efa84643477a16be11eccd Mon Sep 17 00:00:00 2001 From: XehiDat <63257446+ZetroRetro@users.noreply.github.com> Date: Tue, 4 Jun 2024 20:26:03 +0000 Subject: [PATCH 6/6] format fix --- task_01/src/test.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/task_01/src/test.cpp b/task_01/src/test.cpp index 79a39626..b086e16e 100644 --- a/task_01/src/test.cpp +++ b/task_01/src/test.cpp @@ -15,5 +15,4 @@ TEST(find_pair_with_sum, Simple) { std::vector test2 = {}; ASSERT_EQ(find_pair_with_sum(test2, 1), std::make_pair(-1, -1)); - } \ No newline at end of file