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

task01 #5

Open
wants to merge 5 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; }
19 changes: 16 additions & 3 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 <stdexcept>
#include <vector>

TEST(TopologySort, Simple) {
ASSERT_EQ(1, 1); // Stack []
#include "utils.hpp"
TEST(utils, Simple) {
ASSERT_EQ(FindSummands(std::vector<int>{1, 2, 3, 4, 5, 7, 8, 12}, 7),
(std::pair<int, int>{2, 5}));
ASSERT_EQ(FindSummands(std::vector<int>{1, 2, 3, 4, 5, 7, 8, 12}, 13),
(std::pair<int, int>{1, 12}));
EXPECT_THROW(FindSummands(std::vector<int>{1, 2, 3, 4, 5, 7, 8, 12}, 21),
NoAnswer);
EXPECT_THROW(FindSummands(std::vector<int>{1, 2, 3, 4, 5, 7, 8, 12}, -1),
NoAnswer);
EXPECT_THROW(FindSummands(std::vector<int>{1, 1, 1, 1}, 3), NoAnswer);
ASSERT_EQ(FindSummands(std::vector<int>{1, 1, 1, 1}, 2),
(std::pair<int, int>{1, 1}));
EXPECT_THROW(FindSummands(std::vector<int>{}, 0), NoAnswer);
}
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.

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

#include <cstddef>
#include <iostream>
#include <stdexcept>
#include <utility>
#include <vector>

std::pair<int, int> FindSummands(const std::vector<int> array, int number) {
int first_index = 0;
int second_index = array.size() - 1;

while (first_index < second_index) {
int first_summand = array[first_index];
int second_summand = array[second_index];
int sum = first_summand + second_summand;
if (sum == number)
return std::pair<int, int>{first_summand, second_summand};
if (sum < number) first_index++;
if (sum > number) second_index--;
}
throw NoAnswer("Unable to find the right elements");
}
11 changes: 11 additions & 0 deletions task_01/src/utils.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once
#include <stdexcept>
#include <utility>
#include <vector>

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

std::pair<int, int> FindSummands(const std::vector<int> array, int number);
Loading