From 687b821ddd7ef2e610de7cb6967738ddc7d8b3fc Mon Sep 17 00:00:00 2001 From: Shanechka <161927448+Glekmofob@users.noreply.github.com> Date: Sat, 28 Feb 2026 09:47:56 +0000 Subject: [PATCH] Task 1,Task2 --- task_01/src/findeq.cpp | 21 +++++++++++++++++++++ task_01/src/findeq.hpp | 5 +++++ task_01/src/main.cpp | 20 +++++++++++++++++++- task_01/src/test.cpp | 20 ++++++++++++++++++-- task_01/tests/no_solution.out | 2 +- task_01/tests/single_element.out | 2 +- task_02/src/main.cpp | 15 ++++++++++++++- task_02/src/search.cpp | 23 +++++++++++++++++++++++ task_02/src/search.hpp | 7 +++++++ task_02/src/test.cpp | 16 ++++++++++++++-- 10 files changed, 123 insertions(+), 8 deletions(-) create mode 100644 task_01/src/findeq.cpp create mode 100644 task_01/src/findeq.hpp create mode 100644 task_02/src/search.cpp create mode 100644 task_02/src/search.hpp diff --git a/task_01/src/findeq.cpp b/task_01/src/findeq.cpp new file mode 100644 index 0000000..50974dd --- /dev/null +++ b/task_01/src/findeq.cpp @@ -0,0 +1,21 @@ +#include + +pair findeq(int goal,int length,vector cont){ + int left{0}; + int right = cont.size() - 1; + while (left < right){ + int target_sum = cont[left] + cont[right]; + + if (target_sum == goal){ + return {cont[left],cont[right]}; + } + else if (target_sum < goal){ + left++; + } + else{ + right--; + } + + } + return {-1,-1}; +} diff --git a/task_01/src/findeq.hpp b/task_01/src/findeq.hpp new file mode 100644 index 0000000..36bdaf2 --- /dev/null +++ b/task_01/src/findeq.hpp @@ -0,0 +1,5 @@ +#pragma once +#include +using namespace std; + +pair findeq(int goal,int length,vector cont); diff --git a/task_01/src/main.cpp b/task_01/src/main.cpp index 0e4393b..a9559cf 100644 --- a/task_01/src/main.cpp +++ b/task_01/src/main.cpp @@ -1,3 +1,21 @@ #include +#include -int main() { return 0; } +#include + +using namespace std; + + +int main() { + + size_t length{0}; + int goal{0}; + cin >> goal; + cin >> length; + vector cont(length); + for (int i{0}; i < length; i++){ + cin >> cont[i]; + } + auto result = findeq(goal,length,cont); + cout << result.first << " "<< result.second; + return 0; } diff --git a/task_01/src/test.cpp b/task_01/src/test.cpp index 87cef73..246a1ab 100644 --- a/task_01/src/test.cpp +++ b/task_01/src/test.cpp @@ -1,5 +1,21 @@ #include -TEST(Test, Simple) { - ASSERT_EQ(1, 1); // Stack [] +#include +#include + +TEST(SumOfTwo, Simple) { + std::vector v ={ 2,7,11,15}; + ASSERT_EQ(findeq(9,4,v),std::make_pair(2,7)); // Stack [] +} + + +TEST(SumOfTwo, WrongAns) { + ASSERT_EQ(findeq(1000,4,{0,4,5,9}),std::make_pair(-1,-1)); +} +TEST(SumOfTwo, EmptyCont){ + ASSERT_EQ(findeq(10,0,{}),std::make_pair(-1,-1)); +} + +TEST(SumOfTwo,Hard){ + ASSERT_EQ(findeq(100,10,{0,10,15,20,25,30,40,50,75,95}),std::make_pair(25,75)); } \ No newline at end of file diff --git a/task_01/tests/no_solution.out b/task_01/tests/no_solution.out index 3a2e3f4..9b56b57 100644 --- a/task_01/tests/no_solution.out +++ b/task_01/tests/no_solution.out @@ -1 +1 @@ --1 +-1 -1 diff --git a/task_01/tests/single_element.out b/task_01/tests/single_element.out index 3a2e3f4..9b56b57 100644 --- a/task_01/tests/single_element.out +++ b/task_01/tests/single_element.out @@ -1 +1 @@ --1 +-1 -1 diff --git a/task_02/src/main.cpp b/task_02/src/main.cpp index 0e4393b..194e005 100644 --- a/task_02/src/main.cpp +++ b/task_02/src/main.cpp @@ -1,3 +1,16 @@ #include +#include -int main() { return 0; } +int main() { + size_t length = 0; + std::cin >> length; + std::vector cont(length); + for (int i = 0; i < length; i++){ + std::cin >> cont[i]; + } + + + auto result = search(cont); + std::cout << result << std::endl; + return 0; + } diff --git a/task_02/src/search.cpp b/task_02/src/search.cpp new file mode 100644 index 0000000..ea751ca --- /dev/null +++ b/task_02/src/search.cpp @@ -0,0 +1,23 @@ +#include "search.hpp" + + +int search(std::vector cont){ + int left = 0; + int right = size(cont) - 1; + while (left <= right){ + int middle = left + (right - left) /2; + if (cont[middle] == 0 && cont[middle + 1] == 1){ + return middle; + } + else if (cont[middle] == 1 && cont[middle - 1] == 0){ + return middle - 1; + } + else if(cont[middle] == 1){ + right = middle -1; + } + else{ + left = middle + 1; + } + } + return 0; //function shouldnt reach here but just in case +} \ No newline at end of file diff --git a/task_02/src/search.hpp b/task_02/src/search.hpp new file mode 100644 index 0000000..88fd597 --- /dev/null +++ b/task_02/src/search.hpp @@ -0,0 +1,7 @@ +#pragma once + +#include + + + +int search(std::vector cont); \ No newline at end of file diff --git a/task_02/src/test.cpp b/task_02/src/test.cpp index ee23770..36d4c2a 100644 --- a/task_02/src/test.cpp +++ b/task_02/src/test.cpp @@ -1,5 +1,17 @@ #include +#include -TEST(Test, Simple) { - ASSERT_EQ(1, 1); // placeholder + +TEST(Test, First_elem) { + ASSERT_EQ(search({0,1,1,1,1}), 0); // placeholder +} + +TEST(Test, Easy) { + ASSERT_EQ(search({0,0,1,1,1}), 1); // placeholder +} +TEST(Test, Last_elem) { + ASSERT_EQ(search({0,0,0,0,1}), 3); // placeholder +} +TEST(Test, Hard) { + ASSERT_EQ(search({0,0,0,0,0,0,0,0,0,0,1,1,1,1}), 9); // placeholder }