Skip to content

Done homework_1 #18

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 15 commits into
base: main
Choose a base branch
from
Open
43 changes: 42 additions & 1 deletion task_01/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,44 @@
#include <iostream>
#include <stdexcept>
#include <vector>

int main() { return 0; }
#include "topology_sort.hpp"

size_t GetSize() {
size_t length;
std::cout << "Enter a length of array: ";
std::cin >> length;
if (length <= 1) {
std::cout << "You need to enter a number greater than 1\n";
GetSize();
}
return length;
};

long long GetNumber() {
long long number;
std::cout << "Enter a number: ";
std::cin >> number;
return number;
};

std::vector<long long> GetArray(size_t length) {
std::vector<long long> nums;
std::cout << "Enter the array: ";
for (int i = 0; i < length; i++) {
int number;
std::cin >> number;
nums.push_back(number);
}
return nums;
}

int main() {
long long number = GetNumber();
size_t length = GetSize();
std::vector<long long> nums = GetArray(length);
std::pair<long long, long long> res = GetTwoNums(number, nums);
std::cout << "Two numbers, which add up to a given number: " << res.first
<< ", " << res.second;
return 0;
}
70 changes: 67 additions & 3 deletions task_01/src/test.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,72 @@

#include <gtest/gtest.h>

#include <iostream>
#include <vector>

#include "topology_sort.hpp"

TEST(TopologySort, Simple) {
ASSERT_EQ(1, 1); // Stack []
TEST(GetTwoNums, Test_1) {
ASSERT_EQ(GetTwoNums(15, std::vector<long long>{5, 10, 15, 20, 25}),
(std::pair<long long, long long>{5, 10}));
}

TEST(GetTwoNums, Test_2) {
ASSERT_EQ(
GetTwoNums(18, std::vector<long long>{1, 2, 3, 5, 7, 8, 10, 14, 19, 25}),
(std::pair<long long, long long>{8, 10}));
}

TEST(GetTwoNums, Test_3) {
ASSERT_EQ(GetTwoNums(-20, std::vector<long long>{-100, -98, -72, -54, -11, 0,
10, 20, 34, 60}),
(std::pair<long long, long long>{-54, 34}));
}

TEST(GetTwoNums, Test_4) {
ASSERT_EQ(
GetTwoNums(-6, std::vector<long long>{-120, -100, -84, -20, -11, -5, -1}),
(std::pair<long long, long long>{-5, -1}));
}

TEST(GetTwoNums, Test_5) {
ASSERT_EQ(
GetTwoNums(21000000000,
std::vector<long long>{1000000000, 2000000000, 4000000000,
8000000000, 11000000000, 13000000000}),
(std::pair<long long, long long>{8000000000, 13000000000}));
}

TEST(GetTwoNums, Test_6) {
ASSERT_EQ(
GetTwoNums(
11, std::vector<long long>{0, 0, 0, 0, 0, 1, 1, 2, 2, 2, 2, 3,
6, 7, 8, 9, 9, 9, 12, 13, 15, 15, 16, 17}),
(std::pair<long long, long long>{2, 9}));
}

TEST(GetTwoNums, Test_7) {
ASSERT_EQ(GetTwoNums(100000000000000,
std::vector<long long>{
10000000000000, 20000000000000, 30000000000000,
30000000000000, 50000000000000, 50000000000000,
60000000000000, 110000000000000, 110000000000000,
110000000000000}),
(std::pair<long long, long long>{50000000000000, 50000000000000}));
}

TEST(GetTwoNums, Test_8) {
ASSERT_EQ(GetTwoNums(84, std::vector<long long>{-12, -6, -4, 1, 23, 25, 26,
33, 38, 46, 52, 58, 64, 69,
76, 82, 97, 101}),
(std::pair<long long, long long>{26, 58}));
}

TEST(GetTwoNums, Test_9) {
ASSERT_EQ(GetTwoNums(12, std::vector<long long>{12}),
(std::pair<long long, long long>{-1, -1}));
}

TEST(GetTwoNums, Test_10) {
ASSERT_EQ(GetTwoNums(100, std::vector<long long>{}),
(std::pair<long long, long long>{-1, -1}));
}
Copy link
Contributor

Choose a reason for hiding this comment

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

не хватает теста если в массиве нет чисел дающие в сумме нужное число

19 changes: 19 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,20 @@
#include "topology_sort.hpp"

std::pair<long long, long long> GetTwoNums(long long number,
std::vector<long long> nums) {
if (nums.size() <= 1) {
return {-1, -1};
Copy link
Contributor

Choose a reason for hiding this comment

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

кажется лучше тогда кинуть исключение

}
size_t left = 0;
size_t right = nums.size() - 1;
while (left != right) {
if (nums[left] + nums[right] == number)
return {nums[left], nums[right]};
else if (nums[left] + nums[right] < number)
left++;
else if (nums[left] + nums[right] > number)
right--;
}
throw std::logic_error(
"There are no two elements, which add up to a given number.");
}
6 changes: 6 additions & 0 deletions task_01/src/topology_sort.hpp
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
#pragma once

#include <iostream>
#include <vector>

std::pair<long long, long long> GetTwoNums(long long number,
std::vector<long long> nums);