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 2 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
14 changes: 13 additions & 1 deletion task_01/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
#include <iostream>

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<bool, int*, int*> 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;
std::cout << std::get<2>(b) << " : " << *std::get<2>(b) << std::endl;
} else
std::cout << "NOT found" << std::endl;
}
93 changes: 91 additions & 2 deletions task_01/src/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,95 @@

#include "topology_sort.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::tuple<bool, int*, int*> 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, Simple2) {
int arr[] = {1, 2, 3, 4, 5, 8, 9};
int n = 7;
int m = 5;
std::tuple<bool, int*, int*> 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, Simple3) {
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int n = 9;
int m = 3;
std::tuple<bool, int*, int*> 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, Simple4) {
int arr[] = {1, 3, 5, 7, 9};
int n = 5;
int m = 7;
std::tuple<bool, int*, int*> a = SummanddsInArray(arr, n, m);
ASSERT_EQ(std::get<0>(a), false);
}

TEST(SummanddsInArray, Nullarr) {
int* arr = nullptr;
int n = 9;
int m = 3;
std::tuple<bool, int*, int*> a = SummanddsInArray(arr, n, m);
ASSERT_EQ(std::get<0>(a), false);
}

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::tuple<bool, int*, int*> 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, Specific2) {
int arr[] = {1, 2, 4, 5, 8};
int n = 5;
int m = 8;
std::tuple<bool, int*, int*> a = SummanddsInArray(arr, n, m);
ASSERT_EQ(std::get<0>(a), false);
}

TEST(SummanddsInArray, Specific3) {
int arr[] = {1, 3, 4, 5, 8};
int n = 5;
int m = 16;
std::tuple<bool, int*, int*> a = SummanddsInArray(arr, n, m);
ASSERT_EQ(std::get<0>(a), false);
}

TEST(SummanddsInArray, Specific4) {
int arr[] = {1, 3, 4, 5, 5, 8};
int n = 6;
int m = 10;
std::tuple<bool, int*, int*> 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<bool, int*, int*> 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<bool, int*, int*> a = SummanddsInArray(arr, n, m);
ASSERT_EQ(std::get<0>(a), false);
}
15 changes: 15 additions & 0 deletions task_01/src/topology_sort.cpp
Copy link
Contributor

Choose a reason for hiding this comment

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

давай файл переименуем

Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
#include "topology_sort.hpp"

std::tuple<bool, int *, int *> SummanddsInArray(int *arr, int n, int sum) {
Copy link
Contributor

Choose a reason for hiding this comment

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

сомнительный конечно тип возвращаемого значения, может тогда лучше std::optional?

int *begin = arr, *end = arr + (n - 1);
Copy link
Contributor

Choose a reason for hiding this comment

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

давай делать так: одна строка одно объявление

bool found = true;
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;
found = 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

found = true;

}
return std::tuple<bool, int *, int *>{found, begin, end};
}
9 changes: 8 additions & 1 deletion task_01/src/topology_sort.hpp
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
#pragma once
#ifndef TOPOLOGY_SORT_HPP
#define TOPOLOGY_SORT_HPP

#include <tuple>

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

#endif