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
27 changes: 27 additions & 0 deletions task_01/src/get_terms.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

std::pair<int, int> GetTerms(const std::vector<int>& array, int sum) {
int n = array.size();

if (n == 0 || n == 1) return std::pair(-1, -1);

int l{0}, r{n - 1};

int current_sum;
while (l != r) {
current_sum = array[l] + array[r];
if (current_sum == sum) {
return std::pair(array[l], array[r]);
} else {
if (current_sum > sum)
--r;
else
++l;
}
}

return std::pair(-1, -1);
}
30 changes: 29 additions & 1 deletion task_01/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,31 @@
#include <get_terms.hpp>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

int main() { return 0; }
// python3 ./scripts/run_cases.py --tasks task_01

int main() {
int S, N;
std::vector<int> v;
std::string line3, line1, line2;

getline(std::cin, line1);
getline(std::cin, line2);
getline(std::cin, line3);

S = stoi(line1);
N = stoi(line2);

std::istringstream is(line3);

int x;
while (is >> x) v.push_back(x);
std::pair<int, int> result = GetTerms(v, S);
if (result == std::pair(-1, -1))
std::cout << -1 << std::endl;
else
std::cout << result.first << " " << result.second << std::endl;
return 0;
}
32 changes: 31 additions & 1 deletion task_01/src/test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,35 @@
#include <gtest/gtest.h>

#include <get_terms.hpp>
#include <stdexcept>
#include <vector>

TEST(Test, Simple) {
ASSERT_EQ(1, 1); // Stack []
std::vector<int> v = {1, 2, 4};
std::vector<int> v1 = {2, 7, 11, 15};
std::vector<int> v2 = {3, 7, 12};
std::vector<int> v3 = {-67, -42, 0, 52, 108, 252};
std::vector<int> v4 = {2, 4};
ASSERT_EQ(GetTerms(v, 52), std::make_pair(-1, -1));
ASSERT_EQ(GetTerms(v, -52), std::make_pair(-1, -1));
ASSERT_EQ(GetTerms(v, 0), std::make_pair(-1, -1));
ASSERT_EQ(GetTerms(v1, 9), std::make_pair(2, 7));
ASSERT_EQ(GetTerms(v2, 19), std::make_pair(7, 12));
ASSERT_EQ(GetTerms(v3, 10), std::make_pair(-42, 52));
ASSERT_EQ(GetTerms(v3, 1), std::make_pair(-1, -1));
ASSERT_EQ(GetTerms(v4, 6), std::make_pair(2, 4));
}

TEST(Test, Empty) {
std::vector<int> v_empty = {};
ASSERT_EQ(GetTerms(v_empty, 52), std::make_pair(-1, -1));
ASSERT_EQ(GetTerms(v_empty, -52), std::make_pair(-1, -1));
ASSERT_EQ(GetTerms(v_empty, 0), std::make_pair(-1, -1));
}

TEST(Test, Single) {
std::vector<int> v_single = {0};
ASSERT_EQ(GetTerms(v_single, 52), std::make_pair(-1, -1));
ASSERT_EQ(GetTerms(v_single, -52), std::make_pair(-1, -1));
ASSERT_EQ(GetTerms(v_single, 0), std::make_pair(-1, -1));
}
20 changes: 20 additions & 0 deletions task_02/src/get_border_index.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <vector>

int GetBorderIndex(std::vector<int> v) {
int N = v.size();

if (v[1] == 1) return 0;
if (v[N - 2] == 0) return N - 2;

int left = 0, right = N - 1;

while (left + 1 < right) {
int mid = (left + right) / 2;

if (v[mid] == 1)
right = mid;
else
left = mid;
}
return left;
}
22 changes: 21 additions & 1 deletion task_02/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
#include <get_border_index.hpp>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

int main() { return 0; }
int main() {
int N;
std::vector<int> v;
std::string line1, line2;

getline(cin, line1);
getline(cin, line2);

N = stoi(line1);

std::istringstream is(line2);
int x;
while (is >> x) v.push_back(x);

std::cout << GetBorderIndex(v) << endl;
return 0;
}
53 changes: 52 additions & 1 deletion task_02/src/test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,56 @@
#include <gtest/gtest.h>

#include <get_border_index.hpp>
#include <vector>

TEST(Test, Simple) {
ASSERT_EQ(1, 1); // placeholder
std::vector<int> v1{0, 1};
std::vector<int> v2{0, 0, 0, 1, 1};
std::vector<int> v3{0, 0, 0, 0, 0, 1};
std::vector<int> v4{0, 1, 1, 1, 1};
std::vector<int> v5{0, 0, 1, 1, 1, 1};
std::vector<int> v6{0, 0, 0, 1};
std::vector<int> v7{0, 0, 1};
std::vector<int> v8{0, 1, 1};
std::vector<int> v9{0, 0, 0, 0, 1, 1, 1};
std::vector<int> v10{0, 0, 0, 0, 0, 0, 0, 0, 1};
std::vector<int> v11{0, 0, 0, 0, 1};
std::vector<int> v12{0, 0, 1, 1};
std::vector<int> v13{0, 1, 1, 1};
std::vector<int> v14{0, 1, 0, 1, 0, 1, 0, 1};

ASSERT_EQ(GetBorderIndex(v1), 0);
ASSERT_EQ(GetBorderIndex(v2), 2);
ASSERT_EQ(GetBorderIndex(v3), 4);
ASSERT_EQ(GetBorderIndex(v4), 0);
ASSERT_EQ(GetBorderIndex(v5), 1);
ASSERT_EQ(GetBorderIndex(v6), 2);
ASSERT_EQ(GetBorderIndex(v7), 1);
ASSERT_EQ(GetBorderIndex(v8), 0);
ASSERT_EQ(GetBorderIndex(v9), 3);
ASSERT_EQ(GetBorderIndex(v10), 7);
ASSERT_EQ(GetBorderIndex(v11), 3);
ASSERT_EQ(GetBorderIndex(v12), 1);
ASSERT_EQ(GetBorderIndex(v13), 0);
}

void CheckAnswer(int i, std::vector<int> v) {
ASSERT_EQ(v[i], 0);
ASSERT_EQ(v[i] + 1, 1);
}

TEST(Test, OutOfSync) {
std::vector<int> v1{0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1};
std::vector<int> v2{0, 1, 0, 1, 0, 1, 0, 1};
std::vector<int> v3{0, 0, 0, 1, 0, 0, 0, 1};
std::vector<int> v4{0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1};
std::vector<int> v5{0, 1, 0, 1};
std::vector<int> v6{0, 1, 1, 1, 1, 0, 1};

CheckAnswer(GetBorderIndex(v1), v1);
CheckAnswer(GetBorderIndex(v2), v2);
CheckAnswer(GetBorderIndex(v3), v3);
CheckAnswer(GetBorderIndex(v4), v4);
CheckAnswer(GetBorderIndex(v5), v5);
CheckAnswer(GetBorderIndex(v6), v6);
}
38 changes: 38 additions & 0 deletions task_03/src/get_combinations.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <string>
#include <vector>

const std::vector<std::string> num_to_let{"abc", "def", "ghi", "jkl",
"mno", "pqrs", "tuv", "wxyz"};
constexpr int shift = 2;

void FillingArray(std::vector<int> nums, int index, std::string combination,
std::vector<std::string> &result) {
if (index == nums.size()) {
result.push_back(combination);
return;
}

std::string new_combination;
for (char c : num_to_let[nums[index] - shift]) {
new_combination = combination + c;
FillingArray(nums, index + 1, new_combination, result);
}
}

std::vector<std::string> GetCombinations(std::string digits) {
std::vector<int> nums;
for (char c : digits) {
if (c >= '2' && c <= '9') {
nums.push_back(c - '0');
}
}
std::vector<std::string> result;

if (nums.empty()) return result;

std::string combination;
int index = 0;

FillingArray(nums, index, combination, result);
return result;
}
12 changes: 11 additions & 1 deletion task_03/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
#include <get_combinations.hpp>
#include <iostream>
#include <string>
#include <vector>

int main() { return 0; }
int main() {
std::string digits;
getline(std::cin, digits);

std::vector<std::string> result = GetCombinations(digits);

for (std::string str : result) std::cout << str << " ";
}
54 changes: 52 additions & 2 deletions task_03/src/test.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,54 @@

#include <gtest/gtest.h>

TEST(TopologySort, Simple) { ASSERT_EQ(1, 1); }
#include <vector>

#include "get_combinations.hpp"

TEST(Test, Single) {
// Одна цифра
std::vector<std::string> empty{};
std::vector<std::string> v2{"a", "b", "c"};
std::vector<std::string> v3{"d", "e", "f"};
std::vector<std::string> v4{"g", "h", "i"};
std::vector<std::string> v5{"j", "k", "l"};
std::vector<std::string> v6{"m", "n", "o"};
std::vector<std::string> v7{"p", "q", "r", "s"};
std::vector<std::string> v8{"t", "u", "v"};
std::vector<std::string> v9{"w", "x", "y", "z"};

ASSERT_EQ(GetCombinations("0"), empty);
ASSERT_EQ(GetCombinations("1"), empty);
ASSERT_EQ(GetCombinations("2"), v2);
ASSERT_EQ(GetCombinations("3"), v3);
ASSERT_EQ(GetCombinations("4"), v4);
ASSERT_EQ(GetCombinations("5"), v5);
ASSERT_EQ(GetCombinations("6"), v6);
ASSERT_EQ(GetCombinations("7"), v7);
ASSERT_EQ(GetCombinations("8"), v8);
ASSERT_EQ(GetCombinations("9"), v9);
}

TEST(Test, TwoDigits) {
// Две цифры
std::vector<std::string> v1{"ad", "ae", "af", "bd", "be",
"bf", "cd", "ce", "cf"};
std::vector<std::string> v2{"pw", "px", "py", "pz", "qw", "qx", "qy", "qz",
"rw", "rx", "ry", "rz", "sw", "sx", "sy", "sz"};
ASSERT_EQ(GetCombinations("23"), v1);
ASSERT_EQ(GetCombinations("79"), v2);
}

TEST(Test, ThreeDigits) {
// Три цифры
std::vector<std::string> v1{"adg", "adh", "adi", "aeg", "aeh", "aei", "afg",
"afh", "afi", "bdg", "bdh", "bdi", "beg", "beh",
"bei", "bfg", "bfh", "bfi", "cdg", "cdh", "cdi",
"ceg", "ceh", "cei", "cfg", "cfh", "cfi"};
ASSERT_EQ(GetCombinations("234"), v1);
}

TEST(Test, Empty) {
// Пустая строка
std::vector<std::string> empty{};
ASSERT_EQ(GetCombinations(""), empty);
}
23 changes: 14 additions & 9 deletions task_04/src/stack.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
#include "stack.hpp"

#include <algorithm>

void Stack::Push(int value) { data_.push(value); }
void Stack::Push(int value) { stack_.push_back(value); }

int Stack::Pop() {
auto result = data_.top();
data_.pop();
auto result = stack_.back();
stack_.pop_back();
return result;
}

void MinStack::Push(int value) { data_.push_back(value); }
void MinStack::Push(int value) {
stack_.push_back(value);
if (min_stack_.size() == 0 || value < min_stack_.back())
min_stack_.push_back(value);
else
min_stack_.push_back(min_stack_.back());
}

int MinStack::Pop() {
auto result = data_.back();
data_.pop_back();
auto result = stack_.back();
stack_.pop_back();
min_stack_.pop_back();
return result;
}

int MinStack::GetMin() { return *std::min_element(data_.begin(), data_.end()); }
int MinStack::GetMin() { return min_stack_.back(); }
5 changes: 3 additions & 2 deletions task_04/src/stack.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Stack {
int Pop();

private:
std::stack<int> data_;
std::vector<int> stack_;
};

class MinStack {
Expand All @@ -19,5 +19,6 @@ class MinStack {
int GetMin();

private:
std::vector<int> data_;
std::vector<int> stack_;
std::vector<int> min_stack_;
};
17 changes: 16 additions & 1 deletion task_05/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
#include <iostream>
#include <vector>

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

int main() {
int N;
std::cin >> N;
std::vector<int> v;

for (int i = 0; i < N; ++i) {
int n;
std::cin >> n;
v.push_back(n);
}
std::vector<int> v2 = TemperatureRise(v);
for (int t : v2) std::cout << t << " ";
}
Loading
Loading