diff --git a/task_01/src/main.cpp b/task_01/src/main.cpp index 0e4393ba..5b0faafc 100644 --- a/task_01/src/main.cpp +++ b/task_01/src/main.cpp @@ -1,3 +1,7 @@ #include +#include -int main() { return 0; } +#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 ef5a86ae..3cee3f68 100644 --- a/task_01/src/test.cpp +++ b/task_01/src/test.cpp @@ -1,8 +1,21 @@ #include -#include "topology_sort.hpp" +#include +#include -TEST(TopologySort, Simple) { - ASSERT_EQ(1, 1); // Stack [] +#include "utils.hpp" +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), + NoAnswer); + EXPECT_THROW(FindSummands(std::vector{1, 2, 3, 4, 5, 7, 8, 12}, -1), + 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), NoAnswer); } 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/topology_sort.hpp b/task_01/src/topology_sort.hpp deleted file mode 100644 index 6f70f09b..00000000 --- a/task_01/src/topology_sort.hpp +++ /dev/null @@ -1 +0,0 @@ -#pragma once diff --git a/task_01/src/utils.cpp b/task_01/src/utils.cpp new file mode 100644 index 00000000..7a8ff8f0 --- /dev/null +++ b/task_01/src/utils.cpp @@ -0,0 +1,23 @@ +#include "utils.hpp" + +#include +#include +#include +#include +#include + +std::pair FindSummands(const std::vector array, int number) { + int first_index = 0; + int second_index = array.size() - 1; + + 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--; + } + throw NoAnswer("Unable to find the right elements"); +} diff --git a/task_01/src/utils.hpp b/task_01/src/utils.hpp new file mode 100644 index 00000000..d760cf67 --- /dev/null +++ b/task_01/src/utils.hpp @@ -0,0 +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