Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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 = 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;
}
77 changes: 75 additions & 2 deletions task_01/src/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool, int*, int*> 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<bool, int*, int*> 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<bool, int*, int*> 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<bool, int*, int*> 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<bool, int*, int*> 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<bool, int*, int*> 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<bool, int*, int*> 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<bool, int*, int*> 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<bool, int*, int*> a = topology_sort(arr, n, m);
ASSERT_EQ(std::get<0>(a), true);
ASSERT_EQ(*std::get<1>(a) + *std::get<2>(a), m);
}
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 *> topology_sort(int *arr, int n, int m) {
Copy link
Contributor

Choose a reason for hiding this comment

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

имя функции не отображает что она делает, и не в том кодстайле (TopologySort)

int *b = arr, *e = 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.

b и e плохие названия переменных

bool found = 1;
Copy link
Contributor

Choose a reason for hiding this comment

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

не 1 а true

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;
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, b, e};
}
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*> topology_sort(int* arr, int n, int m);

#endif