diff --git a/task_01/src/search_task.cpp b/task_01/src/search_task.cpp new file mode 100644 index 00000000..b106d6a9 --- /dev/null +++ b/task_01/src/search_task.cpp @@ -0,0 +1,21 @@ +#include "search_task.hpp" + +#include +#include + +std::optional> SearchInList(std::vector list, LL n) { + if (list.empty()) return std::nullopt; + if (!std::is_sorted(list.cbegin(), list.cend())) return std::nullopt; + LL left = 0; + LL right = list.size() - 1; + while (left != right) { + LL sum = list[left] + list[right]; + if (sum == n) + return std::pair{list[left], list[right]}; + else if (sum < n) + left++; + else if (sum > n) + right--; + } + return std::nullopt; +} \ No newline at end of file diff --git a/task_01/src/search_task.hpp b/task_01/src/search_task.hpp new file mode 100644 index 00000000..96db6d98 --- /dev/null +++ b/task_01/src/search_task.hpp @@ -0,0 +1,9 @@ +#pragma once + +#include +#include +#include + +typedef long long LL; + +std::optional> SearchInList(std::vector list, LL n); \ No newline at end of file diff --git a/task_01/src/test.cpp b/task_01/src/test.cpp index ef5a86ae..330c46f2 100644 --- a/task_01/src/test.cpp +++ b/task_01/src/test.cpp @@ -1,8 +1,27 @@ - #include -#include "topology_sort.hpp" +#include + +#include "search_task.hpp" -TEST(TopologySort, Simple) { - ASSERT_EQ(1, 1); // Stack [] -} +TEST(SearchInList, Simple) { + std::vector list = {2, 4, 7, 9, 12, 16, 19, 23, 27}; + ASSERT_EQ(SearchInList(list, 50), std::pair(23LL, 27LL)); + ASSERT_EQ(SearchInList(list, 43), std::pair(16LL, 27LL)); + ASSERT_EQ(SearchInList(list, 21), std::pair(2LL, 19LL)); + ASSERT_EQ(SearchInList(list, 18), std::pair(2LL, 16LL)); + ASSERT_EQ(SearchInList(list, 11), std::pair(2LL, 9LL)); + ASSERT_EQ(SearchInList(list, 47), std::nullopt); + ASSERT_EQ(SearchInList(list, 24), std::nullopt); + ASSERT_EQ(SearchInList(list, 2), std::nullopt); + ASSERT_EQ(SearchInList(list, 1), std::nullopt); + ASSERT_EQ(SearchInList(list, -1421532541), std::nullopt); + list = {}; + ASSERT_EQ(SearchInList(list, 10), std::nullopt); + list = {1}; + ASSERT_EQ(SearchInList(list, 100), std::nullopt); + list = {0}; + ASSERT_EQ(SearchInList(list, -142), std::nullopt); + list = {}; + ASSERT_EQ(SearchInList(list, 0), std::nullopt); +} \ No newline at end of file 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