From c6e92174209822330b47baf1aadd3a9ad1e42c23 Mon Sep 17 00:00:00 2001 From: dfsavffc <144057175+dfsavffc@users.noreply.github.com> Date: Sat, 10 Feb 2024 08:43:58 +0000 Subject: [PATCH 1/5] test --- task_01/src/main.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/task_01/src/main.cpp b/task_01/src/main.cpp index 0e4393ba..b9aa6904 100644 --- a/task_01/src/main.cpp +++ b/task_01/src/main.cpp @@ -1,3 +1,5 @@ #include -int main() { return 0; } +int main() { + std::cout << "hello world"; + return 0; } \ No newline at end of file From e18de5a27a74c330f8e3b4027749a01fefc144f1 Mon Sep 17 00:00:00 2001 From: dfsavffc <144057175+dfsavffc@users.noreply.github.com> Date: Sat, 10 Feb 2024 09:26:02 +0000 Subject: [PATCH 2/5] test --- task_01/src/main.cpp | 3 ++- task_01/src/test.cpp | 2 +- task_01/src/topology_sort.cpp | 1 - task_01/src/utils.cpp | 1 + task_01/src/{topology_sort.hpp => utils.hpp} | 0 5 files changed, 4 insertions(+), 3 deletions(-) delete mode 100644 task_01/src/topology_sort.cpp create mode 100644 task_01/src/utils.cpp rename task_01/src/{topology_sort.hpp => utils.hpp} (100%) diff --git a/task_01/src/main.cpp b/task_01/src/main.cpp index b9aa6904..1c647f1d 100644 --- a/task_01/src/main.cpp +++ b/task_01/src/main.cpp @@ -2,4 +2,5 @@ int main() { std::cout << "hello world"; - return 0; } \ No newline at end of file + return 0; + } \ No newline at end of file diff --git a/task_01/src/test.cpp b/task_01/src/test.cpp index ef5a86ae..babf52ce 100644 --- a/task_01/src/test.cpp +++ b/task_01/src/test.cpp @@ -1,7 +1,7 @@ #include -#include "topology_sort.hpp" +#include "utils.hpp" TEST(TopologySort, Simple) { ASSERT_EQ(1, 1); // Stack [] 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/utils.cpp b/task_01/src/utils.cpp new file mode 100644 index 00000000..0ee624c5 --- /dev/null +++ b/task_01/src/utils.cpp @@ -0,0 +1 @@ +#include "utils.hpp" diff --git a/task_01/src/topology_sort.hpp b/task_01/src/utils.hpp similarity index 100% rename from task_01/src/topology_sort.hpp rename to task_01/src/utils.hpp From 51c3aa26fdcfd1836232bfb096132e6f0db48e1e Mon Sep 17 00:00:00 2001 From: dfsavffc <144057175+dfsavffc@users.noreply.github.com> Date: Sat, 10 Feb 2024 11:01:26 +0000 Subject: [PATCH 3/5] done --- task_01/src/main.cpp | 9 +++++---- task_01/src/test.cpp | 14 ++++++++++++-- task_01/src/utils.cpp | 18 ++++++++++++++++++ task_01/src/utils.hpp | 4 ++++ 4 files changed, 39 insertions(+), 6 deletions(-) diff --git a/task_01/src/main.cpp b/task_01/src/main.cpp index 1c647f1d..5b0faafc 100644 --- a/task_01/src/main.cpp +++ b/task_01/src/main.cpp @@ -1,6 +1,7 @@ #include +#include -int main() { - std::cout << "hello world"; - return 0; - } \ No newline at end of file +#include "util.hpp" +#include "utils.hpp" + +int main() { return 0; } \ No newline at end of file diff --git a/task_01/src/test.cpp b/task_01/src/test.cpp index babf52ce..e9327fe4 100644 --- a/task_01/src/test.cpp +++ b/task_01/src/test.cpp @@ -1,8 +1,18 @@ #include -#include "utils.hpp" +#include +#include "utils.hpp" +std::vector test_arr_1 = {1, 2, 3, 4, 5, 7, 8, 12}; +std::vector test_arr_2 = {1, 1, 1, 1}; +std::vector test_arr_3 = {}; TEST(TopologySort, Simple) { - ASSERT_EQ(1, 1); // Stack [] + ASSERT_EQ(FindSummands(test_arr_1, 7), (std::pair{2, 5})); + ASSERT_EQ(FindSummands(test_arr_1, 13), (std::pair{1, 12})); + ASSERT_EQ(FindSummands(test_arr_1, 21), (std::pair{NULL, NULL})); + ASSERT_EQ(FindSummands(test_arr_1, -1), (std::pair{NULL, NULL})); + ASSERT_EQ(FindSummands(test_arr_2, 3), (std::pair{NULL, NULL})); + ASSERT_EQ(FindSummands(test_arr_2, 2), (std::pair{1, 1})); + ASSERT_EQ(FindSummands(test_arr_3, 0), (std::pair{NULL, NULL})); } diff --git a/task_01/src/utils.cpp b/task_01/src/utils.cpp index 0ee624c5..7190011a 100644 --- a/task_01/src/utils.cpp +++ b/task_01/src/utils.cpp @@ -1 +1,19 @@ #include "utils.hpp" + +#include +#include + +std::pair FindSummands(const std::vector array, int number) { + int ptr_1 = 0; + int ptr_2 = array.size() - 1; + + while (ptr_1 < ptr_2) { + int summand_1 = array[ptr_1]; + int summand_2 = array[ptr_2]; + int sum = summand_1 + summand_2; + if (sum == number) return std::pair{summand_1, summand_2}; + if (sum < number) ptr_1++; + if (sum > number) ptr_2--; + } + return std::pair{NULL, NULL}; +} \ No newline at end of file diff --git a/task_01/src/utils.hpp b/task_01/src/utils.hpp index 6f70f09b..60ef4424 100644 --- a/task_01/src/utils.hpp +++ b/task_01/src/utils.hpp @@ -1 +1,5 @@ #pragma once +#include +#include + +std::pair FindSummands(const std::vector array, int number); \ No newline at end of file From f21a713f428ebc99eafa774359a06f72abd27f0c Mon Sep 17 00:00:00 2001 From: dfsavffc <144057175+dfsavffc@users.noreply.github.com> Date: Sun, 18 Feb 2024 16:20:17 +0000 Subject: [PATCH 4/5] fixed --- task_01/src/test.cpp | 25 ++++++++++++++----------- task_01/src/utils.cpp | 26 +++++++++++++++----------- 2 files changed, 29 insertions(+), 22 deletions(-) diff --git a/task_01/src/test.cpp b/task_01/src/test.cpp index e9327fe4..936b315d 100644 --- a/task_01/src/test.cpp +++ b/task_01/src/test.cpp @@ -1,18 +1,21 @@ #include +#include #include #include "utils.hpp" -std::vector test_arr_1 = {1, 2, 3, 4, 5, 7, 8, 12}; -std::vector test_arr_2 = {1, 1, 1, 1}; -std::vector test_arr_3 = {}; -TEST(TopologySort, Simple) { - ASSERT_EQ(FindSummands(test_arr_1, 7), (std::pair{2, 5})); - ASSERT_EQ(FindSummands(test_arr_1, 13), (std::pair{1, 12})); - ASSERT_EQ(FindSummands(test_arr_1, 21), (std::pair{NULL, NULL})); - ASSERT_EQ(FindSummands(test_arr_1, -1), (std::pair{NULL, NULL})); - ASSERT_EQ(FindSummands(test_arr_2, 3), (std::pair{NULL, NULL})); - ASSERT_EQ(FindSummands(test_arr_2, 2), (std::pair{1, 1})); - ASSERT_EQ(FindSummands(test_arr_3, 0), (std::pair{NULL, NULL})); +TEST(utils, Simple) { + ASSERT_EQ(FindSummands(std::vector{1, 2, 3, 4, 5, 7, 8, 12}, 7), + (std::pair{2, 5})); + ASSERT_EQ(FindSummands(std::vector{1, 2, 3, 4, 5, 7, 8, 12}, 13), + (std::pair{1, 12})); + EXPECT_THROW(FindSummands(std::vector{1, 2, 3, 4, 5, 7, 8, 12}, 21), + std::logic_error); + EXPECT_THROW(FindSummands(std::vector{1, 2, 3, 4, 5, 7, 8, 12}, -1), + std::logic_error); + EXPECT_THROW(FindSummands(std::vector{1, 1, 1, 1}, 3), std::logic_error); + ASSERT_EQ(FindSummands(std::vector{1, 1, 1, 1}, 2), + (std::pair{1, 1})); + EXPECT_THROW(FindSummands(std::vector{}, 0), std::logic_error); } diff --git a/task_01/src/utils.cpp b/task_01/src/utils.cpp index 7190011a..afabe05f 100644 --- a/task_01/src/utils.cpp +++ b/task_01/src/utils.cpp @@ -1,19 +1,23 @@ #include "utils.hpp" #include +#include +#include #include +#include std::pair FindSummands(const std::vector array, int number) { - int ptr_1 = 0; - int ptr_2 = array.size() - 1; + int first_index = 0; + int second_index = array.size() - 1; - while (ptr_1 < ptr_2) { - int summand_1 = array[ptr_1]; - int summand_2 = array[ptr_2]; - int sum = summand_1 + summand_2; - if (sum == number) return std::pair{summand_1, summand_2}; - if (sum < number) ptr_1++; - if (sum > number) ptr_2--; + while (first_index < second_index) { + int first_summand = array[first_index]; + int second_summand = array[second_index]; + int sum = first_summand + second_summand; + if (sum == number) + return std::pair{first_summand, second_summand}; + if (sum < number) first_index++; + if (sum > number) second_index--; } - return std::pair{NULL, NULL}; -} \ No newline at end of file + throw std::logic_error("Unable to find the right elements"); +} From 72fb4315d79ea1a50e9de162912deaee11a09382 Mon Sep 17 00:00:00 2001 From: dfsavffc <144057175+dfsavffc@users.noreply.github.com> Date: Sat, 24 Feb 2024 10:01:18 +0000 Subject: [PATCH 5/5] fixed_again --- task_01/src/test.cpp | 8 ++++---- task_01/src/utils.cpp | 2 +- task_01/src/utils.hpp | 6 ++++++ 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/task_01/src/test.cpp b/task_01/src/test.cpp index 936b315d..3cee3f68 100644 --- a/task_01/src/test.cpp +++ b/task_01/src/test.cpp @@ -11,11 +11,11 @@ TEST(utils, Simple) { ASSERT_EQ(FindSummands(std::vector{1, 2, 3, 4, 5, 7, 8, 12}, 13), (std::pair{1, 12})); EXPECT_THROW(FindSummands(std::vector{1, 2, 3, 4, 5, 7, 8, 12}, 21), - std::logic_error); + NoAnswer); EXPECT_THROW(FindSummands(std::vector{1, 2, 3, 4, 5, 7, 8, 12}, -1), - std::logic_error); - EXPECT_THROW(FindSummands(std::vector{1, 1, 1, 1}, 3), std::logic_error); + NoAnswer); + EXPECT_THROW(FindSummands(std::vector{1, 1, 1, 1}, 3), NoAnswer); ASSERT_EQ(FindSummands(std::vector{1, 1, 1, 1}, 2), (std::pair{1, 1})); - EXPECT_THROW(FindSummands(std::vector{}, 0), std::logic_error); + EXPECT_THROW(FindSummands(std::vector{}, 0), NoAnswer); } diff --git a/task_01/src/utils.cpp b/task_01/src/utils.cpp index afabe05f..7a8ff8f0 100644 --- a/task_01/src/utils.cpp +++ b/task_01/src/utils.cpp @@ -19,5 +19,5 @@ std::pair FindSummands(const std::vector array, int number) { if (sum < number) first_index++; if (sum > number) second_index--; } - throw std::logic_error("Unable to find the right elements"); + throw NoAnswer("Unable to find the right elements"); } diff --git a/task_01/src/utils.hpp b/task_01/src/utils.hpp index 60ef4424..d760cf67 100644 --- a/task_01/src/utils.hpp +++ b/task_01/src/utils.hpp @@ -1,5 +1,11 @@ #pragma once +#include #include #include +class NoAnswer : public std::runtime_error { + public: + using std::runtime_error::runtime_error; +}; + std::pair FindSummands(const std::vector array, int number); \ No newline at end of file