Skip to content
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
21 changes: 21 additions & 0 deletions task_01/src/search_task.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "search_task.hpp"

#include <algorithm>
#include <optional>

std::optional<std::pair<LL, LL>> SearchInList(std::vector<LL> list, LL n) {
if (list.empty()) return std::nullopt;
if (!std::is_sorted(list.cbegin(), list.cend())) return std::nullopt;
LL left = 0;
LL right = list.size() - 1;
while (left != right) {
LL sum = list[left] + list[right];
if (sum == n)
return std::pair<LL, LL>{list[left], list[right]};
else if (sum < n)
left++;
else if (sum > n)
right--;
}
return std::nullopt;
}
9 changes: 9 additions & 0 deletions task_01/src/search_task.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#include <iostream>
#include <optional>
#include <vector>

typedef long long LL;

std::optional<std::pair<LL, LL>> SearchInList(std::vector<LL> list, LL n);
29 changes: 24 additions & 5 deletions task_01/src/test.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@

#include <gtest/gtest.h>

#include "topology_sort.hpp"
#include <optional>

#include "search_task.hpp"

TEST(TopologySort, Simple) {
ASSERT_EQ(1, 1); // Stack []
}
TEST(SearchInList, Simple) {
std::vector<LL> list = {2, 4, 7, 9, 12, 16, 19, 23, 27};
ASSERT_EQ(SearchInList(list, 50), std::pair(23LL, 27LL));
ASSERT_EQ(SearchInList(list, 43), std::pair(16LL, 27LL));
ASSERT_EQ(SearchInList(list, 21), std::pair(2LL, 19LL));
ASSERT_EQ(SearchInList(list, 18), std::pair(2LL, 16LL));
ASSERT_EQ(SearchInList(list, 11), std::pair(2LL, 9LL));
ASSERT_EQ(SearchInList(list, 47), std::nullopt);
ASSERT_EQ(SearchInList(list, 24), std::nullopt);
ASSERT_EQ(SearchInList(list, 2), std::nullopt);
ASSERT_EQ(SearchInList(list, 1), std::nullopt);
ASSERT_EQ(SearchInList(list, -1421532541), std::nullopt);
list = {};
ASSERT_EQ(SearchInList(list, 10), std::nullopt);
list = {1};
ASSERT_EQ(SearchInList(list, 100), std::nullopt);
list = {0};
ASSERT_EQ(SearchInList(list, -142), std::nullopt);
list = {};
ASSERT_EQ(SearchInList(list, 0), std::nullopt);
}
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.