Skip to content
Open
Show file tree
Hide file tree
Changes from 6 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
7 changes: 6 additions & 1 deletion task_01/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
#include <iostream>

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

int main() {
std::cout << "Hello World\n";
return 0;
}
16 changes: 12 additions & 4 deletions task_01/src/test.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@

#include <gtest/gtest.h>
#include "utils.hpp"

#include "topology_sort.hpp"
#include <bits/stdc++.h>
Comment thread
LostPointer marked this conversation as resolved.
Outdated
#include <gtest/gtest.h>

TEST(TopologySort, Simple) {
ASSERT_EQ(1, 1); // Stack []
TEST(utils, Simple) {
ASSERT_EQ(FindSum(10, std::vector<int>{2, 3, 4, 7, 12}), (std::pair<int, int>{3,7}));
ASSERT_EQ(FindSum(27,std::vector<int>{1,2,4,5,8,10, 13,17, 20, 21, 22, 25}), (std::pair<int, int>{2, 25}));
ASSERT_EQ(FindSum(6, std::vector<int>{1, 5, 13, 21, 22}), (std::pair<int, int>{1, 5}));
EXPECT_THROW(FindSum(7, std::vector<int>{1, 5, 13, 21, 22}), std::logic_error);
EXPECT_THROW(FindSum(57, std::vector<int>{2, 3, 4, 7, 12}), std::logic_error);
EXPECT_THROW(FindSum(-3,std::vector<int>{1,2,4,5,8,10, 13,17, 20, 21, 22, 25}), std::logic_error);
EXPECT_THROW(FindSum(120,std::vector<int>{}),SmallVector);
EXPECT_THROW(FindSum(47,std::vector<int>{1}),SmallVector);
}
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.

27 changes: 27 additions & 0 deletions task_01/src/utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "utils.hpp"

#include <iostream>
#include <stdexcept>
#include <vector>

std::pair<int, int> FindSum(int number, const std::vector<int> array) {
if (array.size() < 2) {
throw SmallVector("vector size is too small");
}
int left_index = 0;
int right_index = array.size() - 1;
while (left_index < right_index) {
int sum = array[left_index] + array[right_index];
if (sum == number) {
return std::make_pair(array[left_index], array[right_index]);
}
if (sum < number) {
left_index++;
} else {
right_index--;
}
}
if (array[left_index] + array[right_index] != number) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

я бы убрал этот if, что-то поменяется и он не всегда работать будет, тогда функция вернет непонятно что если в этот if не зайдет

throw std::logic_error("no indexes found");
}
}
9 changes: 9 additions & 0 deletions task_01/src/utils.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once
#include <iostream>
#include <vector>

class SmallVector : public std::runtime_error {
using std::runtime_error::runtime_error;
};

std::pair<int, int> FindSum(int number, std::vector<int> array);