From 2ae1bf016266b622ca0069016399404c722ffac0 Mon Sep 17 00:00:00 2001 From: Lynech <144128009+Lynech@users.noreply.github.com> Date: Fri, 23 Feb 2024 12:34:48 +0000 Subject: [PATCH 1/5] HOMEWORK 1 attempt 1 --- task_01/src/main.cpp | 14 ++++++- task_01/src/test.cpp | 77 ++++++++++++++++++++++++++++++++++- task_01/src/topology_sort.cpp | 15 +++++++ task_01/src/topology_sort.hpp | 9 +++- 4 files changed, 111 insertions(+), 4 deletions(-) diff --git a/task_01/src/main.cpp b/task_01/src/main.cpp index 0e4393ba..67b4c272 100644 --- a/task_01/src/main.cpp +++ b/task_01/src/main.cpp @@ -1,3 +1,15 @@ #include -int main() { return 0; } +#include "topology_sort.hpp" + +int main() { + int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; + int m = 5; + std::tuple b = topology_sort(a, 9, m); + if (std::get<0>(b)) { + std::cout << "found" << std::endl; + std::cout << std::get<1>(b) << " : " << *std::get<1>(b) << std::endl; + std::cout << std::get<2>(b) << " : " << *std::get<2>(b) << std::endl; + } else + std::cout << "NOT found" << std::endl; +} diff --git a/task_01/src/test.cpp b/task_01/src/test.cpp index ef5a86ae..e31e9816 100644 --- a/task_01/src/test.cpp +++ b/task_01/src/test.cpp @@ -3,6 +3,79 @@ #include "topology_sort.hpp" -TEST(TopologySort, Simple) { - ASSERT_EQ(1, 1); // Stack [] +TEST(TopologySort, Simple1) { + int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; + int n = 9; + int m = 11; + std::tuple a = topology_sort(arr, n, m); + ASSERT_EQ(std::get<0>(a), true); + ASSERT_EQ(*std::get<1>(a) + *std::get<2>(a), m); } + +TEST(TopologySort, Simple2) { + int arr[] = {1, 2, 3, 4, 5, 8, 9}; + int n = 7; + int m = 5; + std::tuple a = topology_sort(arr, n, m); + ASSERT_EQ(std::get<0>(a), true); + ASSERT_EQ(*std::get<1>(a) + *std::get<2>(a), m); +} +TEST(TopologySort, Simple3) { + int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; + int n = 9; + int m = 3; + std::tuple a = topology_sort(arr, n, m); + ASSERT_EQ(std::get<0>(a), true); + ASSERT_EQ(*std::get<1>(a) + *std::get<2>(a), m); +} + +TEST(TopologySort, Simple4) { + int arr[] = {1, 3, 5, 7, 9}; + int n = 5; + int m = 7; + std::tuple a = topology_sort(arr, n, m); + ASSERT_EQ(std::get<0>(a), false); +} + +TEST(TopologySort, Nullarr) { + int* arr = nullptr; + int n = 9; + int m = 3; + std::tuple a = topology_sort(arr, n, m); + ASSERT_EQ(std::get<0>(a), false); +} + +TEST(TopologySort, Specific1) { + int arr_ref[] = {10, 2, 3, 4, 5, 5, 7, 8, 0}; + int* arr = arr_ref + 1; + int n = 7; + int m = 5; + std::tuple a = topology_sort(arr, n, m); + ASSERT_EQ(std::get<0>(a), true); + ASSERT_EQ(*std::get<1>(a) + *std::get<2>(a), m); +} + +TEST(TopologySort, Specific2) { + int arr[] = {1, 2, 4, 5, 8}; + int n = 7; + int m = 8; + std::tuple a = topology_sort(arr, n, m); + ASSERT_EQ(std::get<0>(a), false); +} + +TEST(TopologySort, Specific3) { + int arr[] = {1, 3, 4, 5, 8}; + int n = 7; + int m = 16; + std::tuple a = topology_sort(arr, n, m); + ASSERT_EQ(std::get<0>(a), false); +} + +TEST(TopologySort, Specific4) { + int arr[] = {1, 3, 4, 5, 5, 8}; + int n = 6; + int m = 10; + std::tuple a = topology_sort(arr, n, m); + ASSERT_EQ(std::get<0>(a), true); + ASSERT_EQ(*std::get<1>(a) + *std::get<2>(a), m); +} \ No newline at end of file diff --git a/task_01/src/topology_sort.cpp b/task_01/src/topology_sort.cpp index e53f670c..32d709a7 100644 --- a/task_01/src/topology_sort.cpp +++ b/task_01/src/topology_sort.cpp @@ -1 +1,16 @@ #include "topology_sort.hpp" + +std::tuple topology_sort(int *arr, int n, int m) { + int *b = arr, *e = arr + (n - 1); + bool found = 1; + while (b && e && b != e && *b + *e != m) + if ((*b + *e) > m) + e--; + else + b++; + if ((!b) || (!e) || (b == e) || (*b + *e != m)) { + b = e = nullptr; + found = 0; + } + return std::tuple{found, b, e}; +} diff --git a/task_01/src/topology_sort.hpp b/task_01/src/topology_sort.hpp index 6f70f09b..12da2e66 100644 --- a/task_01/src/topology_sort.hpp +++ b/task_01/src/topology_sort.hpp @@ -1 +1,8 @@ -#pragma once +#ifndef TOPOLOGY_SORT_HPP +#define TOPOLOGY_SORT_HPP + +#include + +std::tuple topology_sort(int* arr, int n, int m); + +#endif \ No newline at end of file From e223a9de72a6ec3b054631f33d520f554ae8b83c Mon Sep 17 00:00:00 2001 From: Lynech <144128009+Lynech@users.noreply.github.com> Date: Sat, 24 Feb 2024 19:59:26 +0000 Subject: [PATCH 2/5] attempt 2 --- task_01/src/main.cpp | 2 +- task_01/src/test.cpp | 58 ++++++++++++++++++++++------------- task_01/src/topology_sort.cpp | 20 ++++++------ task_01/src/topology_sort.hpp | 2 +- 4 files changed, 49 insertions(+), 33 deletions(-) diff --git a/task_01/src/main.cpp b/task_01/src/main.cpp index 67b4c272..cca29235 100644 --- a/task_01/src/main.cpp +++ b/task_01/src/main.cpp @@ -5,7 +5,7 @@ int main() { int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; int m = 5; - std::tuple b = topology_sort(a, 9, m); + std::tuple b = SummanddsInArray(a, 9, m); if (std::get<0>(b)) { std::cout << "found" << std::endl; std::cout << std::get<1>(b) << " : " << *std::get<1>(b) << std::endl; diff --git a/task_01/src/test.cpp b/task_01/src/test.cpp index e31e9816..12834303 100644 --- a/task_01/src/test.cpp +++ b/task_01/src/test.cpp @@ -3,79 +3,95 @@ #include "topology_sort.hpp" -TEST(TopologySort, Simple1) { +TEST(SummanddsInArray, Simple1) { int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; int n = 9; int m = 11; - std::tuple a = topology_sort(arr, n, m); + std::tuple a = SummanddsInArray(arr, n, m); ASSERT_EQ(std::get<0>(a), true); ASSERT_EQ(*std::get<1>(a) + *std::get<2>(a), m); } -TEST(TopologySort, Simple2) { +TEST(SummanddsInArray, Simple2) { int arr[] = {1, 2, 3, 4, 5, 8, 9}; int n = 7; int m = 5; - std::tuple a = topology_sort(arr, n, m); + std::tuple a = SummanddsInArray(arr, n, m); ASSERT_EQ(std::get<0>(a), true); ASSERT_EQ(*std::get<1>(a) + *std::get<2>(a), m); } -TEST(TopologySort, Simple3) { +TEST(SummanddsInArray, Simple3) { int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; int n = 9; int m = 3; - std::tuple a = topology_sort(arr, n, m); + std::tuple a = SummanddsInArray(arr, n, m); ASSERT_EQ(std::get<0>(a), true); ASSERT_EQ(*std::get<1>(a) + *std::get<2>(a), m); } -TEST(TopologySort, Simple4) { +TEST(SummanddsInArray, Simple4) { int arr[] = {1, 3, 5, 7, 9}; int n = 5; int m = 7; - std::tuple a = topology_sort(arr, n, m); + std::tuple a = SummanddsInArray(arr, n, m); ASSERT_EQ(std::get<0>(a), false); } -TEST(TopologySort, Nullarr) { +TEST(SummanddsInArray, Nullarr) { int* arr = nullptr; int n = 9; int m = 3; - std::tuple a = topology_sort(arr, n, m); + std::tuple a = SummanddsInArray(arr, n, m); ASSERT_EQ(std::get<0>(a), false); } -TEST(TopologySort, Specific1) { +TEST(SummanddsInArray, Specific1) { int arr_ref[] = {10, 2, 3, 4, 5, 5, 7, 8, 0}; int* arr = arr_ref + 1; - int n = 7; + int n = 9; int m = 5; - std::tuple a = topology_sort(arr, n, m); + std::tuple a = SummanddsInArray(arr, n, m); ASSERT_EQ(std::get<0>(a), true); ASSERT_EQ(*std::get<1>(a) + *std::get<2>(a), m); } -TEST(TopologySort, Specific2) { +TEST(SummanddsInArray, Specific2) { int arr[] = {1, 2, 4, 5, 8}; - int n = 7; + int n = 5; int m = 8; - std::tuple a = topology_sort(arr, n, m); + std::tuple a = SummanddsInArray(arr, n, m); ASSERT_EQ(std::get<0>(a), false); } -TEST(TopologySort, Specific3) { +TEST(SummanddsInArray, Specific3) { int arr[] = {1, 3, 4, 5, 8}; - int n = 7; + int n = 5; int m = 16; - std::tuple a = topology_sort(arr, n, m); + std::tuple a = SummanddsInArray(arr, n, m); ASSERT_EQ(std::get<0>(a), false); } -TEST(TopologySort, Specific4) { +TEST(SummanddsInArray, Specific4) { int arr[] = {1, 3, 4, 5, 5, 8}; int n = 6; int m = 10; - std::tuple a = topology_sort(arr, n, m); + std::tuple a = SummanddsInArray(arr, n, m); ASSERT_EQ(std::get<0>(a), true); ASSERT_EQ(*std::get<1>(a) + *std::get<2>(a), m); +} + +TEST(SummanddsInArray, Specific5) { + int arr[] = {}; + int n = 0; + int m = 10; + std::tuple a = SummanddsInArray(arr, n, m); + ASSERT_EQ(std::get<0>(a), false); +} + +TEST(SummanddsInArray, Specific6) { + int arr[] = {10}; + int n = 1; + int m = 20; + std::tuple a = SummanddsInArray(arr, n, m); + ASSERT_EQ(std::get<0>(a), false); } \ No newline at end of file diff --git a/task_01/src/topology_sort.cpp b/task_01/src/topology_sort.cpp index 32d709a7..9a015563 100644 --- a/task_01/src/topology_sort.cpp +++ b/task_01/src/topology_sort.cpp @@ -1,16 +1,16 @@ #include "topology_sort.hpp" -std::tuple topology_sort(int *arr, int n, int m) { - int *b = arr, *e = arr + (n - 1); - bool found = 1; - while (b && e && b != e && *b + *e != m) - if ((*b + *e) > m) - e--; +std::tuple SummanddsInArray(int *arr, int n, int sum) { + int *begin = arr, *end = arr + (n - 1); + bool found = true; + while (begin && end && begin < end && *begin + *end != sum) + if ((*begin + *end) > sum) + end--; else - b++; - if ((!b) || (!e) || (b == e) || (*b + *e != m)) { - b = e = nullptr; + begin++; + if ((!begin) || (!end) || (begin >= end) || (*begin + *end != sum)) { + begin = end = nullptr; found = 0; } - return std::tuple{found, b, e}; + return std::tuple{found, begin, end}; } diff --git a/task_01/src/topology_sort.hpp b/task_01/src/topology_sort.hpp index 12da2e66..978427c3 100644 --- a/task_01/src/topology_sort.hpp +++ b/task_01/src/topology_sort.hpp @@ -3,6 +3,6 @@ #include -std::tuple topology_sort(int* arr, int n, int m); +std::tuple SummanddsInArray(int* arr, int n, int m); #endif \ No newline at end of file From 289a07bb3997ba45b7e7262a74b417cd215e2bcf Mon Sep 17 00:00:00 2001 From: Lynech <144128009+Lynech@users.noreply.github.com> Date: Wed, 13 Mar 2024 20:59:30 +0000 Subject: [PATCH 3/5] attemot 3 HOMEWORK_1 --- task_01/src/main.cpp | 10 ++++--- task_01/src/test.cpp | 54 +++++++++++++++++------------------ task_01/src/topology_sort.cpp | 19 +++++++----- task_01/src/topology_sort.hpp | 3 +- 4 files changed, 47 insertions(+), 39 deletions(-) diff --git a/task_01/src/main.cpp b/task_01/src/main.cpp index cca29235..584b6059 100644 --- a/task_01/src/main.cpp +++ b/task_01/src/main.cpp @@ -5,11 +5,13 @@ int main() { int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; int m = 5; - std::tuple b = SummanddsInArray(a, 9, m); - if (std::get<0>(b)) { + std::optional> b = SummanddsInArray(a, 9, m); + if (b) { std::cout << "found" << std::endl; - std::cout << std::get<1>(b) << " : " << *std::get<1>(b) << std::endl; - std::cout << std::get<2>(b) << " : " << *std::get<2>(b) << 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/test.cpp b/task_01/src/test.cpp index 12834303..fff5125b 100644 --- a/task_01/src/test.cpp +++ b/task_01/src/test.cpp @@ -7,42 +7,42 @@ TEST(SummanddsInArray, Simple1) { int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; int n = 9; int m = 11; - std::tuple a = SummanddsInArray(arr, n, m); - ASSERT_EQ(std::get<0>(a), true); - ASSERT_EQ(*std::get<1>(a) + *std::get<2>(a), m); + 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::tuple a = SummanddsInArray(arr, n, m); - ASSERT_EQ(std::get<0>(a), true); - ASSERT_EQ(*std::get<1>(a) + *std::get<2>(a), m); + 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::tuple a = SummanddsInArray(arr, n, m); - ASSERT_EQ(std::get<0>(a), true); - ASSERT_EQ(*std::get<1>(a) + *std::get<2>(a), m); + 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::tuple a = SummanddsInArray(arr, n, m); - ASSERT_EQ(std::get<0>(a), false); + std::optional> a = SummanddsInArray(arr, n, m); + ASSERT_FALSE(a); } TEST(SummanddsInArray, Nullarr) { int* arr = nullptr; int n = 9; int m = 3; - std::tuple a = SummanddsInArray(arr, n, m); - ASSERT_EQ(std::get<0>(a), false); + std::optional> a = SummanddsInArray(arr, n, m); + ASSERT_FALSE(a); } TEST(SummanddsInArray, Specific1) { @@ -50,48 +50,48 @@ TEST(SummanddsInArray, Specific1) { int* arr = arr_ref + 1; int n = 9; int m = 5; - std::tuple a = SummanddsInArray(arr, n, m); - ASSERT_EQ(std::get<0>(a), true); - ASSERT_EQ(*std::get<1>(a) + *std::get<2>(a), m); + 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::tuple a = SummanddsInArray(arr, n, m); - ASSERT_EQ(std::get<0>(a), false); + 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::tuple a = SummanddsInArray(arr, n, m); - ASSERT_EQ(std::get<0>(a), false); + 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::tuple a = SummanddsInArray(arr, n, m); - ASSERT_EQ(std::get<0>(a), true); - ASSERT_EQ(*std::get<1>(a) + *std::get<2>(a), m); + 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::tuple a = SummanddsInArray(arr, n, m); - ASSERT_EQ(std::get<0>(a), false); + std::optional> a = SummanddsInArray(arr, n, m); + ASSERT_FALSE(a); } TEST(SummanddsInArray, Specific6) { int arr[] = {10}; int n = 1; int m = 20; - std::tuple a = SummanddsInArray(arr, n, m); - ASSERT_EQ(std::get<0>(a), false); + 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 index 9a015563..0fa624cb 100644 --- a/task_01/src/topology_sort.cpp +++ b/task_01/src/topology_sort.cpp @@ -1,16 +1,21 @@ #include "topology_sort.hpp" -std::tuple SummanddsInArray(int *arr, int n, int sum) { - int *begin = arr, *end = arr + (n - 1); - bool found = true; +#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)) { + if ((!begin) || (!end) || (begin >= end) || (*begin + *end != sum)) begin = end = nullptr; - found = 0; - } - return std::tuple{found, begin, end}; + + std::optional> result = std::nullopt; + if (begin != nullptr && end != nullptr) + result = std::tuple{begin, end}; + return result; } diff --git a/task_01/src/topology_sort.hpp b/task_01/src/topology_sort.hpp index 978427c3..521019e6 100644 --- a/task_01/src/topology_sort.hpp +++ b/task_01/src/topology_sort.hpp @@ -1,8 +1,9 @@ #ifndef TOPOLOGY_SORT_HPP #define TOPOLOGY_SORT_HPP +#include #include -std::tuple SummanddsInArray(int* arr, int n, int m); +std::optional> SummanddsInArray(int* arr, int n, int m); #endif \ No newline at end of file From 7eb7a9eb151e49288ec111b37a29a7ad9eb65ca7 Mon Sep 17 00:00:00 2001 From: Lynech <144128009+Lynech@users.noreply.github.com> Date: Sat, 30 Mar 2024 08:56:10 +0000 Subject: [PATCH 4/5] changed file names --- task_01/src/main.cpp | 2 +- task_01/src/{topology_sort.cpp => summandds_in_array.cpp} | 2 +- task_01/src/{topology_sort.hpp => summandds_in_array.hpp} | 0 task_01/src/test.cpp | 2 +- 4 files changed, 3 insertions(+), 3 deletions(-) rename task_01/src/{topology_sort.cpp => summandds_in_array.cpp} (94%) rename task_01/src/{topology_sort.hpp => summandds_in_array.hpp} (100%) diff --git a/task_01/src/main.cpp b/task_01/src/main.cpp index 584b6059..4c7e26e5 100644 --- a/task_01/src/main.cpp +++ b/task_01/src/main.cpp @@ -1,6 +1,6 @@ #include -#include "topology_sort.hpp" +#include "summandds_in_array.hpp" int main() { int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; diff --git a/task_01/src/topology_sort.cpp b/task_01/src/summandds_in_array.cpp similarity index 94% rename from task_01/src/topology_sort.cpp rename to task_01/src/summandds_in_array.cpp index 0fa624cb..15ad2457 100644 --- a/task_01/src/topology_sort.cpp +++ b/task_01/src/summandds_in_array.cpp @@ -1,4 +1,4 @@ -#include "topology_sort.hpp" +#include "summandds_in_array.hpp" #include diff --git a/task_01/src/topology_sort.hpp b/task_01/src/summandds_in_array.hpp similarity index 100% rename from task_01/src/topology_sort.hpp rename to task_01/src/summandds_in_array.hpp diff --git a/task_01/src/test.cpp b/task_01/src/test.cpp index fff5125b..09e953b2 100644 --- a/task_01/src/test.cpp +++ b/task_01/src/test.cpp @@ -1,7 +1,7 @@ #include -#include "topology_sort.hpp" +#include "summandds_in_array.hpp" TEST(SummanddsInArray, Simple1) { int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; From b5399688230741b00858fb025fc6e0145edcdf15 Mon Sep 17 00:00:00 2001 From: Lynech <144128009+Lynech@users.noreply.github.com> Date: Mon, 27 May 2024 15:09:41 +0000 Subject: [PATCH 5/5] renamed #ifnded #define in summandds_in_array.hpp now it's SUMMANDDS_IN_ARRAY_HPP --- task_01/src/summandds_in_array.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/task_01/src/summandds_in_array.hpp b/task_01/src/summandds_in_array.hpp index 521019e6..c3801ac6 100644 --- a/task_01/src/summandds_in_array.hpp +++ b/task_01/src/summandds_in_array.hpp @@ -1,5 +1,5 @@ -#ifndef TOPOLOGY_SORT_HPP -#define TOPOLOGY_SORT_HPP +#ifndef SUMMANDDS_IN_ARRAY_HPP +#define SUMMANDDS_IN_ARRAY_HPP #include #include