diff --git a/task_01/src/main.cpp b/task_01/src/main.cpp index 0e4393ba..3919ad50 100644 --- a/task_01/src/main.cpp +++ b/task_01/src/main.cpp @@ -1,3 +1,8 @@ #include -int main() { return 0; } +#include "utils.hpp" + +int main() { + std::cout << "Hello World\n"; + return 0; +} diff --git a/task_01/src/test.cpp b/task_01/src/test.cpp index ef5a86ae..6b6b60d4 100644 --- a/task_01/src/test.cpp +++ b/task_01/src/test.cpp @@ -1,8 +1,21 @@ - #include -#include "topology_sort.hpp" +#include "utils.hpp" -TEST(TopologySort, Simple) { - ASSERT_EQ(1, 1); // Stack [] +TEST(utils, Simple) { + ASSERT_EQ(FindSum(10, std::vector{2, 3, 4, 7, 12}), + (std::pair{3, 7})); + ASSERT_EQ( + FindSum(27, std::vector{1, 2, 4, 5, 8, 10, 13, 17, 20, 21, 22, 25}), + (std::pair{2, 25})); + ASSERT_EQ(FindSum(6, std::vector{1, 5, 13, 21, 22}), + (std::pair{1, 5})); + EXPECT_THROW(FindSum(7, std::vector{1, 5, 13, 21, 22}), + std::logic_error); + EXPECT_THROW(FindSum(57, std::vector{2, 3, 4, 7, 12}), std::logic_error); + EXPECT_THROW( + FindSum(-3, std::vector{1, 2, 4, 5, 8, 10, 13, 17, 20, 21, 22, 25}), + std::logic_error); + EXPECT_THROW(FindSum(120, std::vector{}), SmallVector); + EXPECT_THROW(FindSum(47, std::vector{1}), SmallVector); } 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..fd5d7d2f --- /dev/null +++ b/task_01/src/utils.cpp @@ -0,0 +1,25 @@ +#include "utils.hpp" + +#include +#include +#include + +std::pair FindSum(int number, const std::vector array) { + if (array.size() < 2) { + throw SmallVector("vector size is too small"); + } + int left_index = 0; + int right_index = array.size() - 1; + while (left_index < right_index) { + int sum = array[left_index] + array[right_index]; + if (sum == number) { + return std::make_pair(array[left_index], array[right_index]); + } + if (sum < number) { + left_index++; + } else { + right_index--; + } + } + throw std::logic_error("no indexes found"); +} \ 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..ebec21d9 --- /dev/null +++ b/task_01/src/utils.hpp @@ -0,0 +1,9 @@ +#pragma once +#include +#include + +class SmallVector : public std::runtime_error { + using std::runtime_error::runtime_error; +}; + +std::pair FindSum(int number, std::vector array); \ No newline at end of file