Skip to content

homework_1 #7

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 14 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion task_01/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Задача 1

Дано целое число и отсортированый массив целых чисел, нужно найти 2 числа из массива которые в сумме дадут заданное число
Дано целое число и отсортированый массив целых чисел, нужно найти 2 числа из массива которые в сумме дадут заданное число
1 change: 1 addition & 0 deletions task_01/file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
123
65 changes: 65 additions & 0 deletions task_01/src/task.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include "task.hpp"

// O(logn)
int BinSearch(std::vector<int> massive, int num) {
int length = massive.size();
int left = 0;
int right = length - 1;
int index = length / 2;
while (right - left >= 0) {
if (massive[index] == num) {
return index;
} else if (massive[index] > num) {
right = index - 1;
} else if (massive[index] < num) {
left = index + 1;
}
index = (right + left) / 2;
}
return -1;
}

// O(n^2)
std::tuple<int, int> NSquareSearch(std::vector<int> massive, int num) {
int length = massive.size();
for (int i = 0; i < length; i++) {
for (int j = i + 1; j < length; j++) {
if (massive[i] + massive[j] == num) {
return {i, j};
}
}
}
return {-1, -1};
}

// O(nlogn)
std::tuple<int, int> NLogNSearch(std::vector<int> massive, int num) {
int length = massive.size();
for (int i = 0; i < length; i++) {
int el1 = massive[i];
int el2 = num - el1;
int j = BinSearch(massive, el2);
if (j != -1 && i != j) {
return {i, j};
}
}
return {-1, -1};
}

// O(n)
std::tuple<int, int> NSearch(std::vector<int> massive, int num) {
int length = massive.size();
int left = 0;
int right = length - 1;
while (right != left) {
int sum = massive[left] + massive[right];
if (sum == num) {
return {left, right};
} else if (sum < num) {
left++;
} else if (sum > num) {
right--;
}
}
return {-1, -1};
}
17 changes: 17 additions & 0 deletions task_01/src/task.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include <iostream>
#include <tuple>
#include <vector>

// O(logn)
int BinSearch(std::vector<int> mas, int num);

// O(n^2)
std::tuple<int, int> NSquareSearch(std::vector<int> massive, int num);

// O(nlogn)
std::tuple<int, int> NLogNSearch(std::vector<int> massive, int num);

// O(n)
std::tuple<int, int> NSearch(std::vector<int> massive, int num);
81 changes: 78 additions & 3 deletions task_01/src/test.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,83 @@

#include <gtest/gtest.h>

#include "topology_sort.hpp"
#include "task.hpp"

TEST(TopologySort, Simple) {
ASSERT_EQ(1, 1); // Stack []
// BinSearch tests
TEST(BinSearch, Simple) {
// even len
std::vector<int> mas = {1, 2, 3, 4, 5, 6, 7, 8};
ASSERT_EQ(BinSearch(mas, 1), 0);
ASSERT_EQ(BinSearch(mas, 2), 1);
ASSERT_EQ(BinSearch(mas, 3), 2);
ASSERT_EQ(BinSearch(mas, 4), 3);
ASSERT_EQ(BinSearch(mas, 5), 4);
ASSERT_EQ(BinSearch(mas, 6), 5);
ASSERT_EQ(BinSearch(mas, 7), 6);
ASSERT_EQ(BinSearch(mas, 8), 7);

ASSERT_EQ(BinSearch(mas, 31), -1);

// odd len
mas = {1, 2, 3, 4, 5, 6, 7, 8, 9};
ASSERT_EQ(BinSearch(mas, 1), 0);
ASSERT_EQ(BinSearch(mas, 2), 1);
ASSERT_EQ(BinSearch(mas, 3), 2);
ASSERT_EQ(BinSearch(mas, 4), 3);
ASSERT_EQ(BinSearch(mas, 5), 4);
ASSERT_EQ(BinSearch(mas, 6), 5);
ASSERT_EQ(BinSearch(mas, 7), 6);
ASSERT_EQ(BinSearch(mas, 8), 7);

ASSERT_EQ(BinSearch(mas, 31), -1);

// empty mas
mas = {};
ASSERT_EQ(BinSearch(mas, 8), -1);

// mas of 1 elem
mas = {1};
ASSERT_EQ(BinSearch(mas, 1), 0);

ASSERT_EQ(BinSearch(mas, 31), -1);

// mas of 2 elems
mas = {1, 2};
ASSERT_EQ(BinSearch(mas, 1), 0);
ASSERT_EQ(BinSearch(mas, 2), 1);

ASSERT_EQ(BinSearch(mas, 31), -1);
}

// NSquareSearch tests
TEST(NSquareSearch, Simple) {
std::vector<int> mas = {1, 2, 3, 4, 5, 6, 7, 8};
ASSERT_EQ(NSquareSearch(mas, 11), std::make_tuple(2, 7));
ASSERT_EQ(NSquareSearch(mas, 9), std::make_tuple(0, 7));

ASSERT_EQ(NSquareSearch(mas, 1), std::make_tuple(-1, -1));
ASSERT_EQ(NSquareSearch(mas, 16),
std::make_tuple(-1, -1)); // not (7, 7), as indexes cant be same
}

// NLogNSearch tests
TEST(NLogNSearch, Simple) {
std::vector<int> mas = {1, 2, 3, 4, 5, 6, 7, 8};
ASSERT_EQ(NLogNSearch(mas, 11), std::make_tuple(2, 7));
ASSERT_EQ(NLogNSearch(mas, 9), std::make_tuple(0, 7));

ASSERT_EQ(NLogNSearch(mas, 1), std::make_tuple(-1, -1));
ASSERT_EQ(NLogNSearch(mas, 16),
std::make_tuple(-1, -1)); // not (7, 7), as indexes cant be same
}

// NSearch tests
TEST(NSearch, Simple) {
std::vector<int> mas = {1, 2, 3, 4, 5, 6, 7, 8};
ASSERT_EQ(NSearch(mas, 11), std::make_tuple(2, 7));
ASSERT_EQ(NSearch(mas, 9), std::make_tuple(0, 7));

ASSERT_EQ(NSearch(mas, 1), std::make_tuple(-1, -1));
ASSERT_EQ(NSearch(mas, 16),
std::make_tuple(-1, -1)); // not (7, 7), as indexes cant be same
}
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.

30 changes: 22 additions & 8 deletions task_02/src/stack.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
#include "stack.hpp"

#include <algorithm>

void Stack::Push(int value) { data_.push(value); }
#include <cstddef>
#include <utility>

int Stack::Pop() {
auto result = data_.top();
data_.pop();
int result = data_[data_.size() - 1];
data_.pop_back();
return result;
}

void MinStack::Push(int value) { data_.push_back(value); }
void MinStack::Push(int value) {
std::pair<int, int> new_element;
if (current_minimum_ == 1e9) {
new_element = {value, value};
} else if (value < current_minimum_) {
new_element = {value, value};
} else {
new_element = {value, current_minimum_};
}
data_.push_back(new_element);
current_minimum_ = new_element.second;
}

int MinStack::Pop() {
auto result = data_.back();
int result = data_[data_.size() - 1].first;
data_.pop_back();
if (data_.size() == 0) {
current_minimum_ = 1e9;
} else {
current_minimum_ = data_[data_.size() - 1].second;
}
return result;
}

int MinStack::GetMin() { return *std::min_element(data_.begin(), data_.end()); }
10 changes: 6 additions & 4 deletions task_02/src/stack.hpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
#pragma once

#include <stack>
#include <utility>
#include <vector>

class Stack {
public:
void Push(int value);
void Push(int value) { data_.push_back(value); }
int Pop();

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

class MinStack {
public:
void Push(int value);
int Pop();
int GetMin();
int GetMin() { return current_minimum_; };

private:
std::vector<int> data_;
std::vector<std::pair<int, int>> data_;
int current_minimum_ = 1e9;
};
Loading