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

Mathew_tasks #45

Open
wants to merge 46 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 41 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
df0c810
asd
Mathew131 Feb 10, 2024
27fa911
a2
Mathew131 Feb 23, 2024
df82b7f
WIP
Mathew131 Feb 24, 2024
8e1f461
proverka
Mathew131 Apr 6, 2024
71b9130
Merge branch 'AlgorithmsDafeMipt2024:main' into main
Mathew131 Apr 6, 2024
acd10e5
Merge branch 'AlgorithmsDafeMipt2024:main' into main
Mathew131 Apr 13, 2024
fde1a86
Merge branch 'AlgorithmsDafeMipt2024:main' into main
Mathew131 Apr 13, 2024
ad66904
proverka 3
Mathew131 Apr 20, 2024
443fe7b
new
Mathew131 May 23, 2024
7059a82
update task_01
Mathew131 May 23, 2024
ec0b265
updates test task_01
Mathew131 May 23, 2024
6fecfd8
format
Mathew131 May 23, 2024
98ce20a
task_03
Mathew131 May 23, 2024
a8121d8
task_04
Mathew131 May 23, 2024
aee6bc4
update task_03
Mathew131 May 23, 2024
004deef
update task_03
Mathew131 May 23, 2024
70cc035
update test in task_03
Mathew131 May 23, 2024
8c354cc
update task_04
Mathew131 May 23, 2024
92029b3
update task_04
Mathew131 May 23, 2024
253a40c
task_05
Mathew131 May 23, 2024
f3b691e
task_06
Mathew131 May 23, 2024
230696f
update task_05
Mathew131 May 23, 2024
fb915bb
update task_06
Mathew131 Jun 7, 2024
c79105f
task_07
Mathew131 Jun 7, 2024
668a692
task_09
Mathew131 Jun 7, 2024
63415fb
remove using std
Mathew131 Jun 7, 2024
5e930f0
remove excess include
Mathew131 Jun 7, 2024
fc2aaeb
task_08
Mathew131 Jun 7, 2024
abc65fa
update task_03
Mathew131 Jun 15, 2024
a763ac3
format
Mathew131 Jun 15, 2024
0619d9d
final update task_03
Mathew131 Jun 15, 2024
6888602
update task_04
Mathew131 Jun 15, 2024
69f7a5f
update task_05
Mathew131 Jun 15, 2024
4fc4760
update task_05
Mathew131 Jun 15, 2024
c7063ba
task_02
Mathew131 Jun 15, 2024
858f42f
format
Mathew131 Jun 15, 2024
ee315c5
delete main in task_01
Mathew131 Jun 15, 2024
65a5f13
update task_06
Mathew131 Jun 15, 2024
47f2029
update task_07:
Mathew131 Jun 15, 2024
2b81cba
update task_08
Mathew131 Jun 15, 2024
d592237
add new test in task_09
Mathew131 Jun 15, 2024
e3f2736
rename task_03
Mathew131 Jun 16, 2024
02e5150
rename and add new variable in task_04
Mathew131 Jun 16, 2024
2cd7fd8
reformat
Mathew131 Jun 16, 2024
a0dfea2
rename in task_06
Mathew131 Jun 16, 2024
c8c9cea
rename in task_07 and task_08
Mathew131 Jun 16, 2024
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
24 changes: 19 additions & 5 deletions task_01/src/test.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@

#include <gtest/gtest.h>

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

#include "utils.h"

TEST(main, Simple) {
ASSERT_EQ(Task1(9, std::vector<int>{1, 2, 4, 5, 6, 8, 10, 12}),
(std::pair<int, int>{1, 8}));

ASSERT_EQ(Task1(39, std::vector<int>{1, 2, 4, 5, 6, 9, 10, 35}),
(std::pair<int, int>{4, 35}));

ASSERT_EQ(Task1(14, std::vector<int>{1, 2, 4, 5, 6, 8, 10, 12}),
(std::pair<int, int>{2, 12}));

ASSERT_EQ(Task1(1338, std::vector<int>{10, 20, 40, 50, 60, 87, 100, 1278}),
(std::pair<int, int>{60, 1278}));

TEST(TopologySort, Simple) {
ASSERT_EQ(1, 1); // Stack []
}
ASSERT_EQ(Task1(22, std::vector<int>{10, 10, 11, 11, 12, 15}),
(std::pair<int, int>{10, 12}));
}
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.

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

std::pair<int, int> Task1(int number, std::vector<int> array_of_numbers) {
int i = 0;
int j = array_of_numbers.size() - 1;
while (array_of_numbers[i] + array_of_numbers[j] != number) {
if (array_of_numbers[i] + array_of_numbers[j] < number) i++;
if (array_of_numbers[i] + array_of_numbers[j] > number) j--;
}
return {array_of_numbers[i], array_of_numbers[j]};
}
3 changes: 3 additions & 0 deletions task_01/src/utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include <vector>

std::pair<int, int> Task1(int, std::vector<int>);
44 changes: 36 additions & 8 deletions task_02/src/stack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,48 @@

#include <algorithm>

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

int Stack::Pop() {
auto result = data_.top();
data_.pop();
if (elements_.size() == 0) {
throw std::out_of_range("Stack is empty");
}
int result = elements_.back();
elements_.pop_back();
return result;
}

void MinStack::Push(int value) { data_.push_back(value); }
int Stack::Size() { return elements_.size(); }

int Stack::Top() {
if (elements_.size() == 0) {
throw std::out_of_range("Stack is empty");
}
return elements_.back();
}

///////////////////

void MinStack::Push(int value) {
main_stack_.Push(value);
if (min_stack_.Size() && min_stack_.Top() > value) {
min_stack_.Push(value);
}
if (min_stack_.Size() == 0) {
min_stack_.Push(value);
}
}

int MinStack::Pop() {
auto result = data_.back();
data_.pop_back();
return result;
if (main_stack_.Size() && min_stack_.Size() &&
main_stack_.Top() == min_stack_.Top()) {
min_stack_.Pop();
}
return main_stack_.Pop();
}

int MinStack::GetMin() { return *std::min_element(data_.begin(), data_.end()); }
int MinStack::GetMin() { return min_stack_.Top(); }

int MinStack::Top() { return main_stack_.Top(); }

int MinStack::Size() { return main_stack_.Size(); }
9 changes: 7 additions & 2 deletions task_02/src/stack.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,22 @@ class Stack {
public:
void Push(int value);
int Pop();
int Top();
int Size();

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

class MinStack {
public:
void Push(int value);
int Pop();
int Top();
int GetMin();
int Size();

private:
std::vector<int> data_;
Stack main_stack_;
Stack min_stack_;
};
46 changes: 18 additions & 28 deletions task_02/src/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,26 @@

TEST(StackTest, Simple) {
Stack stack;
stack.Push(1); // Stack [1]
ASSERT_EQ(stack.Pop(), 1); // Stack []
stack.Push(1); // Stack [1]
stack.Push(2); // Stack [1, 2]
ASSERT_EQ(stack.Pop(), 2); // Stack [1]
ASSERT_EQ(stack.Pop(), 1); // Stack []
stack.Push(1); // Stack [1]
stack.Push(2); // Stack [1, 2]
ASSERT_EQ(stack.Pop(), 2); // Stack [1]
stack.Push(3); // Stack [1, 3]
ASSERT_EQ(stack.Pop(), 3); // Stack [1]
ASSERT_EQ(stack.Pop(), 1); // Stack []
stack.Push(1);
ASSERT_EQ(stack.Pop(), 1);
ASSERT_EQ(stack.Size(), 0);
EXPECT_THROW(stack.Top(), std::out_of_range);
EXPECT_THROW(stack.Pop(), std::out_of_range);
stack.Push(3);
stack.Push(-1);
ASSERT_EQ(stack.Top(), -1);
}

TEST(MinStackTest, Simple) {
MinStack stack;
stack.Push(1); // Stack [1]
ASSERT_EQ(stack.GetMin(), 1);
ASSERT_EQ(stack.Pop(), 1); // Stack []
stack.Push(1); // Stack [1]
stack.Push(2); // Stack [1, 2]
ASSERT_EQ(stack.GetMin(), 1);
ASSERT_EQ(stack.Pop(), 2); // Stack [1]
ASSERT_EQ(stack.Pop(), 1); // Stack []
stack.Push(1); // Stack [1]
stack.Push(2); // Stack [1, 2]
ASSERT_EQ(stack.GetMin(), 1);
ASSERT_EQ(stack.Pop(), 2); // Stack [1]
stack.Push(3); // Stack [1, 3]
ASSERT_EQ(stack.GetMin(), 1);
ASSERT_EQ(stack.Pop(), 3); // Stack [1]
ASSERT_EQ(stack.Pop(), 1); // Stack []
stack.Push(1);
ASSERT_EQ(stack.Pop(), 1);
ASSERT_EQ(stack.Size(), 0);
EXPECT_THROW(stack.Top(), std::out_of_range);
EXPECT_THROW(stack.Pop(), std::out_of_range);
EXPECT_THROW(stack.GetMin(), std::out_of_range);
stack.Push(-2);
stack.Push(5);
ASSERT_EQ(stack.Top(), 5);
ASSERT_EQ(stack.GetMin(), -2);
}
17 changes: 15 additions & 2 deletions task_03/src/test.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@

#include <gtest/gtest.h>

#include <vector>

#include "topology_sort.hpp"

TEST(TopologySort, Simple) {
ASSERT_EQ(1, 1); // Stack []
TEST(TopologySort, MultipleCases) {
ASSERT_EQ(TopologySort(std::vector<int>{73, 74, 75, 71, 69, 72, 76, 73}),
(std::vector<int>{1, 1, 4, 2, 1, 1, 0, 0}));
ASSERT_EQ(TopologySort(std::vector<int>{30, 40, 50, 60}),
(std::vector<int>{1, 1, 1, 0}));
ASSERT_EQ(TopologySort(std::vector<int>{30, 60, 90, 70, 80, 40, 30}),
(std::vector<int>{1, 1, 0, 1, 0, 0, 0}));
ASSERT_EQ(TopologySort(std::vector<int>{55, 55, 55, 55, 55}),
(std::vector<int>{0, 0, 0, 0, 0}));
ASSERT_EQ(TopologySort(std::vector<int>{76, 75, 74, 73, 72, 71, 70}),
(std::vector<int>{0, 0, 0, 0, 0, 0, 0}));
ASSERT_EQ(TopologySort(std::vector<int>{75, 72, 71, 70, 74, 76}),
(std::vector<int>{5, 3, 2, 1, 1, 0}));
}
16 changes: 16 additions & 0 deletions task_03/src/topology_sort.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
#include "topology_sort.hpp"

std::vector<int> TopologySort(std::vector<int> temperature) {
Mathew131 marked this conversation as resolved.
Show resolved Hide resolved
std::vector<int> result(temperature.size(), 0);
std::stack<int> index;

for (int i = 0; i < temperature.size(); ++i) {
while (!index.empty() && temperature[index.top()] < temperature[i]) {
int current_index = index.top();
index.pop();
result[current_index] = i - current_index;
}
index.push(i);
}

return result;
}
4 changes: 4 additions & 0 deletions task_03/src/topology_sort.hpp
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
#pragma once
#include <stack>
#include <vector>

std::vector<int> TopologySort(std::vector<int>);
52 changes: 52 additions & 0 deletions task_04/src/heap.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include "heap.hpp"

void Heap::SiftUp(int i) {
while (a[i] < a[i / 2]) {
std::swap(a[i], a[i / 2]);
i /= 2;
}
}

void Heap::SiftDown(int i) {
while (2 * i < n) {
int j = -1;
if (a[2 * i] < a[i]) {
j = 2 * i;
}
if (2 * i + 1 < n && a[2 * i + 1] < a[i] &&
Mathew131 marked this conversation as resolved.
Show resolved Hide resolved
(j == -1 || a[2 * i] > a[2 * i + 1])) {
j = 2 * i + 1;
}
if (j == -1)
break;
else {
std::swap(a[i], a[j]);
i = j;
}
}
}

int Heap::Size() { return a.size(); }

int Heap::GetMin() {
if (a.size() == 0) {
throw std::out_of_range("Heap is empty");
}
return a[0];
}

void Heap::Insert(int x) {
a.push_back(x);
n++;
SiftUp(n - 1);
}

void Heap::ExtractMin() {
if (a.size() == 0) {
throw std::out_of_range("Heap is empty");
}
a[0] = a[n - 1];
a.erase(a.begin() + n - 1);
n--;
SiftDown(0);
}
16 changes: 16 additions & 0 deletions task_04/src/heap.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <iostream>
#include <vector>

class Heap {
public:
void SiftUp(int i);
void SiftDown(int i);
void Insert(int x);
int GetMin();
void ExtractMin();
int Size();

private:
std::vector<int> a;
Mathew131 marked this conversation as resolved.
Show resolved Hide resolved
int n = 0;
};
25 changes: 23 additions & 2 deletions task_04/src/test.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@

#include <gtest/gtest.h>

#include "heap.hpp"

TEST(TopologySort, Simple) {
ASSERT_EQ(1, 1); // Stack []
}
Heap a;

EXPECT_THROW(a.ExtractMin(), std::out_of_range);
EXPECT_THROW(a.GetMin(), std::out_of_range);

a.Insert(-1);

a.ExtractMin();
EXPECT_THROW(a.ExtractMin(), std::out_of_range);

ASSERT_EQ(a.Size(), 0);
a.Insert(5);
ASSERT_EQ(a.GetMin(), 5);
a.Insert(3);
a.Insert(1);
a.Insert(7);
a.Insert(-2);
a.ExtractMin();
ASSERT_EQ(a.Size(), 4);
ASSERT_EQ(a.GetMin(), 1);
}
Mathew131 marked this conversation as resolved.
Show resolved Hide resolved
43 changes: 43 additions & 0 deletions task_05/src/sort.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#pragma once

#include <vector>

std::vector<int> Merge(const std::vector<int>& left,
const std::vector<int>& right) {
std::vector<int> result;
int i = 0, j = 0;

while (i < left.size() && j < right.size()) {
if (left[i] <= right[j]) {
result.push_back(left[i]);
++i;
} else {
result.push_back(right[j]);
++j;
}
}

while (i < left.size()) {
result.push_back(left[i]);
++i;
}

while (j < right.size()) {
result.push_back(right[j]);
++j;
}

return result;
}

std::vector<int> MergeSort(std::vector<int> arr) {
if (arr.size() <= 1) {
return arr;
}

int mid = arr.size() / 2;
std::vector<int> left(arr.begin(), arr.begin() + mid);
std::vector<int> right(arr.begin() + mid, arr.end());

return Merge(MergeSort(left), MergeSort(right));
}
Loading
Loading