Skip to content

HOMEWORK 1 #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion task_01/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
#include <iostream>

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<std::tuple<int*, int*>> 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;
}
21 changes: 21 additions & 0 deletions task_01/src/summandds_in_array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "summandds_in_array.hpp"

#include <optional>

std::optional<std::tuple<int *, int *>> 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<std::tuple<int *, int *>> result = std::nullopt;
if (begin != nullptr && end != nullptr)
result = std::tuple<int *, int *>{begin, end};
return result;
}
9 changes: 9 additions & 0 deletions task_01/src/summandds_in_array.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef SUMMANDDS_IN_ARRAY_HPP
#define SUMMANDDS_IN_ARRAY_HPP

#include <optional>
#include <tuple>

std::optional<std::tuple<int*, int*>> SummanddsInArray(int* arr, int n, int m);

#endif
95 changes: 92 additions & 3 deletions task_01/src/test.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,97 @@

#include <gtest/gtest.h>

#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<std::tuple<int*, int*>> a = SummanddsInArray(arr, n, m);
ASSERT_TRUE(a);
ASSERT_EQ(*std::get<0>(a.value()) + *std::get<1>(a.value()), m);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

имхо так лучше выглядит: ASSERT_EQ(*a->first + *a->second, m);

}

TEST(SummanddsInArray, Simple2) {
int arr[] = {1, 2, 3, 4, 5, 8, 9};
int n = 7;
int m = 5;
std::optional<std::tuple<int*, int*>> 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<std::tuple<int*, int*>> 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<std::tuple<int*, int*>> a = SummanddsInArray(arr, n, m);
ASSERT_FALSE(a);
}

TEST(SummanddsInArray, Nullarr) {
int* arr = nullptr;
int n = 9;
int m = 3;
std::optional<std::tuple<int*, int*>> 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<std::tuple<int*, int*>> 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<std::tuple<int*, int*>> 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<std::tuple<int*, int*>> 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<std::tuple<int*, int*>> 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<std::tuple<int*, int*>> a = SummanddsInArray(arr, n, m);
ASSERT_FALSE(a);
}

TEST(SummanddsInArray, Specific6) {
int arr[] = {10};
int n = 1;
int m = 20;
std::optional<std::tuple<int*, int*>> a = SummanddsInArray(arr, n, m);
ASSERT_FALSE(a);
}
1 change: 0 additions & 1 deletion task_01/src/topology_sort.cpp

This file was deleted.

1 change: 0 additions & 1 deletion task_01/src/topology_sort.hpp

This file was deleted.

Loading