Skip to content

Homework 1 #9

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 9 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
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;
}
21 changes: 17 additions & 4 deletions task_01/src/test.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@

#include <gtest/gtest.h>

#include "topology_sort.hpp"
#include "utils.hpp"

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.

25 changes: 25 additions & 0 deletions task_01/src/utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#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--;
}
}
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);
Loading