diff --git a/task_01/src/main.cpp b/task_01/src/main.cpp index 0e4393ba..4c7e26e5 100644 --- a/task_01/src/main.cpp +++ b/task_01/src/main.cpp @@ -1,3 +1,17 @@ #include -int main() { return 0; } +#include "summandds_in_array.hpp" + +int main() { + int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; + int m = 5; + std::optional> b = SummanddsInArray(a, 9, m); + if (b) { + std::cout << "found" << std::endl; + std::cout << std::get<0>(b.value()) << " : " << *std::get<0>(b.value()) + << std::endl; + std::cout << std::get<1>(b.value()) << " : " << *std::get<1>(b.value()) + << std::endl; + } else + std::cout << "NOT found" << std::endl; +} diff --git a/task_01/src/summandds_in_array.cpp b/task_01/src/summandds_in_array.cpp new file mode 100644 index 00000000..15ad2457 --- /dev/null +++ b/task_01/src/summandds_in_array.cpp @@ -0,0 +1,21 @@ +#include "summandds_in_array.hpp" + +#include + +std::optional> SummanddsInArray(int *arr, int n, + int sum) { + int *begin = arr; + int *end = arr + (n - 1); + while (begin && end && begin < end && *begin + *end != sum) + if ((*begin + *end) > sum) + end--; + else + begin++; + if ((!begin) || (!end) || (begin >= end) || (*begin + *end != sum)) + begin = end = nullptr; + + std::optional> result = std::nullopt; + if (begin != nullptr && end != nullptr) + result = std::tuple{begin, end}; + return result; +} diff --git a/task_01/src/summandds_in_array.hpp b/task_01/src/summandds_in_array.hpp new file mode 100644 index 00000000..c3801ac6 --- /dev/null +++ b/task_01/src/summandds_in_array.hpp @@ -0,0 +1,9 @@ +#ifndef SUMMANDDS_IN_ARRAY_HPP +#define SUMMANDDS_IN_ARRAY_HPP + +#include +#include + +std::optional> SummanddsInArray(int* arr, int n, int m); + +#endif \ No newline at end of file diff --git a/task_01/src/test.cpp b/task_01/src/test.cpp index ef5a86ae..09e953b2 100644 --- a/task_01/src/test.cpp +++ b/task_01/src/test.cpp @@ -1,8 +1,97 @@ #include -#include "topology_sort.hpp" +#include "summandds_in_array.hpp" -TEST(TopologySort, Simple) { - ASSERT_EQ(1, 1); // Stack [] +TEST(SummanddsInArray, Simple1) { + int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; + int n = 9; + int m = 11; + std::optional> a = SummanddsInArray(arr, n, m); + ASSERT_TRUE(a); + ASSERT_EQ(*std::get<0>(a.value()) + *std::get<1>(a.value()), m); } + +TEST(SummanddsInArray, Simple2) { + int arr[] = {1, 2, 3, 4, 5, 8, 9}; + int n = 7; + int m = 5; + std::optional> a = SummanddsInArray(arr, n, m); + ASSERT_TRUE(a); + ASSERT_EQ(*std::get<0>(a.value()) + *std::get<1>(a.value()), m); +} +TEST(SummanddsInArray, Simple3) { + int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; + int n = 9; + int m = 3; + std::optional> a = SummanddsInArray(arr, n, m); + ASSERT_TRUE(a); + ASSERT_EQ(*std::get<0>(a.value()) + *std::get<1>(a.value()), m); +} + +TEST(SummanddsInArray, Simple4) { + int arr[] = {1, 3, 5, 7, 9}; + int n = 5; + int m = 7; + std::optional> a = SummanddsInArray(arr, n, m); + ASSERT_FALSE(a); +} + +TEST(SummanddsInArray, Nullarr) { + int* arr = nullptr; + int n = 9; + int m = 3; + std::optional> a = SummanddsInArray(arr, n, m); + ASSERT_FALSE(a); +} + +TEST(SummanddsInArray, Specific1) { + int arr_ref[] = {10, 2, 3, 4, 5, 5, 7, 8, 0}; + int* arr = arr_ref + 1; + int n = 9; + int m = 5; + std::optional> a = SummanddsInArray(arr, n, m); + ASSERT_TRUE(a); + ASSERT_EQ(*std::get<0>(a.value()) + *std::get<1>(a.value()), m); +} + +TEST(SummanddsInArray, Specific2) { + int arr[] = {1, 2, 4, 5, 8}; + int n = 5; + int m = 8; + std::optional> a = SummanddsInArray(arr, n, m); + ASSERT_FALSE(a); +} + +TEST(SummanddsInArray, Specific3) { + int arr[] = {1, 3, 4, 5, 8}; + int n = 5; + int m = 16; + std::optional> a = SummanddsInArray(arr, n, m); + ASSERT_FALSE(a); +} + +TEST(SummanddsInArray, Specific4) { + int arr[] = {1, 3, 4, 5, 5, 8}; + int n = 6; + int m = 10; + std::optional> a = SummanddsInArray(arr, n, m); + ASSERT_TRUE(a); + ASSERT_EQ(*std::get<0>(a.value()) + *std::get<1>(a.value()), m); +} + +TEST(SummanddsInArray, Specific5) { + int arr[] = {}; + int n = 0; + int m = 10; + std::optional> a = SummanddsInArray(arr, n, m); + ASSERT_FALSE(a); +} + +TEST(SummanddsInArray, Specific6) { + int arr[] = {10}; + int n = 1; + int m = 20; + std::optional> a = SummanddsInArray(arr, n, m); + ASSERT_FALSE(a); +} \ 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