Skip to content
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

Homework 1 #8

Open
wants to merge 26 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
6 changes: 5 additions & 1 deletion task_01/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#include <iostream>
#include <vector>

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

int main() { return 0; }
26 changes: 22 additions & 4 deletions task_01/src/test.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@

#include <gtest/gtest.h>

#include "topology_sort.hpp"
#include <stdexcept>
#include <vector>

#include "utils.hpp"

TEST(TopologySort, Simple) {
ASSERT_EQ(1, 1); // Stack []
TEST(utils, Simple) {
ASSERT_EQ(SumOfElements(9, std::vector<int>{1, 2, 4, 5, 6, 8, 10, 12}),
(std::pair<int, int>{1, 8}));
ASSERT_EQ(SumOfElements(39, std::vector<int>{1, 2, 4, 5, 6, 9, 10, 35}),
(std::pair<int, int>{4, 35}));
ASSERT_EQ(SumOfElements(14, std::vector<int>{1, 2, 4, 5, 6, 8, 10, 12}),
(std::pair<int, int>{2, 12}));
EXPECT_THROW(SumOfElements(187, std::vector<int>{1, 2, 4, 6, 8, 10, 12, 15}),
std::logic_error);
EXPECT_THROW(SumOfElements(12, std::vector<int>{0, 1, 1, 2, 2}),
std::logic_error);
ASSERT_EQ(
SumOfElements(1338, std::vector<int>{10, 20, 40, 50, 60, 87, 100, 1278}),
(std::pair<int, int>{60, 1278}));
ASSERT_EQ(SumOfElements(22, std::vector<int>{10, 10, 11, 11, 12, 15}),
(std::pair<int, int>{10, 12}));
EXPECT_THROW(SumOfElements(22, std::vector<int>{11}), WrongVector);
EXPECT_THROW(SumOfElements(1233, std::vector<int>{}), WrongVector);
}
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.

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

#include <iostream>
#include <utility>
#include <vector>

std::pair<int, int> SumOfElements(int num, const std::vector<int> arr) {
if (arr.size() < 2) {
throw WrongVector("Vector is too small");
}
for (int i = 0, j = arr.size() - 1; i < j;) {
auto sum = arr[i] + arr[j];
if (sum < num)
++i;
else if (sum > num)
--j;
else if (sum == num) {
return std::pair<int, int>{arr[i], arr[j]};
}
}
throw std::logic_error("There is no sum of elements equal to number");
}
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 WrongVector : public std::runtime_error {
Matvey-cmd marked this conversation as resolved.
Show resolved Hide resolved
using std::runtime_error::runtime_error;
};

std::pair<int, int> SumOfElements(int num, const std::vector<int> arr);
Loading