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..d5bdabb3 100644 --- a/task_01/src/test.cpp +++ b/task_01/src/test.cpp @@ -1,8 +1,26 @@ - #include -#include "topology_sort.hpp" +#include +#include + +#include "utils.hpp" -TEST(TopologySort, Simple) { - ASSERT_EQ(1, 1); // Stack [] +TEST(utils, Simple) { + ASSERT_EQ(SumOfElements(9, std::vector{1, 2, 4, 5, 6, 8, 10, 12}), + (std::pair{1, 8})); + ASSERT_EQ(SumOfElements(39, std::vector{1, 2, 4, 5, 6, 9, 10, 35}), + (std::pair{4, 35})); + ASSERT_EQ(SumOfElements(14, std::vector{1, 2, 4, 5, 6, 8, 10, 12}), + (std::pair{2, 12})); + EXPECT_THROW(SumOfElements(187, std::vector{1, 2, 4, 6, 8, 10, 12, 15}), + std::logic_error); + EXPECT_THROW(SumOfElements(12, std::vector{0, 1, 1, 2, 2}), + std::logic_error); + ASSERT_EQ( + SumOfElements(1338, std::vector{10, 20, 40, 50, 60, 87, 100, 1278}), + (std::pair{60, 1278})); + ASSERT_EQ(SumOfElements(22, std::vector{10, 10, 11, 11, 12, 15}), + (std::pair{10, 12})); + EXPECT_THROW(SumOfElements(22, std::vector{11}), WrongVector); + EXPECT_THROW(SumOfElements(1233, std::vector{}), WrongVector); } 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..2f1f57c6 --- /dev/null +++ b/task_01/src/utils.cpp @@ -0,0 +1,22 @@ +#include "utils.hpp" + +#include +#include +#include + +std::pair SumOfElements(int num, const std::vector arr) { + if (arr.size() < 2) { + throw WrongVector("Vector is too small"); + } + for (int i = 0, j = arr.size() - 1; i < j;) { + auto sum = arr[i] + arr[j]; + if (sum < num) + ++i; + else if (sum > num) + --j; + else if (sum == num) { + return std::pair{arr[i], arr[j]}; + } + } + throw std::logic_error("There is no sum of elements equal to number"); +} \ No newline at end of file diff --git a/task_01/src/utils.hpp b/task_01/src/utils.hpp new file mode 100644 index 00000000..eebc80c8 --- /dev/null +++ b/task_01/src/utils.hpp @@ -0,0 +1,9 @@ +#pragma once +#include +#include + +class WrongVector : public std::runtime_error { + using std::runtime_error::runtime_error; +}; + +std::pair SumOfElements(int num, const std::vector arr);