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

Homework 2 #27

Open
wants to merge 92 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
92 commits
Select commit Hold shift + click to select a range
5723e72
test commit
H0p1ty Feb 10, 2024
cf43a75
test_commit
H0p1ty Feb 10, 2024
cc0495e
task_01 completed
H0p1ty Feb 11, 2024
40c61e7
lesson3
H0p1ty Feb 19, 2024
da7e472
test commit
H0p1ty Feb 24, 2024
7ed00bb
task_01 fixes
H0p1ty Feb 24, 2024
7acfbeb
note fix
H0p1ty Feb 24, 2024
44cbf54
pre-fix commit
H0p1ty Mar 2, 2024
85acfc8
lesson5
H0p1ty Mar 9, 2024
51f832e
task_01 fixed x2
H0p1ty Mar 9, 2024
61a3916
Merge branch 'AlgorithmsDafeMipt2024:main' into main
H0p1ty Mar 30, 2024
814b95d
Merge branch 'lessons'
H0p1ty Mar 30, 2024
36763b5
pilot version of hash_table is done
H0p1ty Mar 31, 2024
566cdf6
task_08 first version done
H0p1ty Apr 3, 2024
cc6a9e2
tests for task_08 done
H0p1ty Apr 4, 2024
971c93e
final tweaks
H0p1ty Apr 4, 2024
fc6eb9b
task_02 done
H0p1ty Apr 4, 2024
a495204
task_03 done
H0p1ty Apr 5, 2024
9cc070a
minor change
H0p1ty Apr 5, 2024
1468b8c
task_04 started
H0p1ty Apr 13, 2024
177c895
deleted obsolete task_01
H0p1ty Apr 13, 2024
bfd57c2
task_04 done without tests
H0p1ty Apr 13, 2024
52c5a98
task_04 minor changes
H0p1ty Apr 13, 2024
778bfc9
task_04 minor changes x2
H0p1ty Apr 13, 2024
64f2c24
task_04 added 1 test
H0p1ty Apr 13, 2024
96abce9
Merge branch 'AlgorithmsDafeMipt2024:main' into task_08
H0p1ty Apr 14, 2024
50aea4f
task_04 done, but i don't want to erase my efforts
H0p1ty Apr 14, 2024
84ba125
task_04 done
H0p1ty Apr 14, 2024
207c1d0
task_04 done
H0p1ty Apr 14, 2024
03fcab4
task_05 started
H0p1ty Apr 15, 2024
ec8bb96
added pragma once to task_04
H0p1ty Apr 15, 2024
0a81053
task_05 done
H0p1ty Apr 15, 2024
873bac6
task_05 fixed with debugging
H0p1ty Apr 17, 2024
2c582db
task_05 fixed with deleted comments of debugging
H0p1ty Apr 17, 2024
3949b62
task_05 perfected
H0p1ty Apr 17, 2024
a1ee568
minor tweaks x3
H0p1ty Apr 17, 2024
09ac4e8
task_05 final tweaks
H0p1ty Apr 17, 2024
7c98135
I don't need these files here
H0p1ty Apr 17, 2024
21aaff5
task_04 important change
H0p1ty Apr 17, 2024
5edc8b0
task_04 done
H0p1ty Apr 17, 2024
6c642c0
task_06 done
H0p1ty Apr 17, 2024
7c10268
task_08 added concept
H0p1ty Apr 17, 2024
8291ee1
task_07 first version done
H0p1ty Apr 18, 2024
2dd8289
task_07 unfinished
H0p1ty Apr 18, 2024
7580986
task_07 fixed
H0p1ty Apr 18, 2024
7195c20
task_07 added few tests
H0p1ty Apr 18, 2024
f7cc975
added empty stack test
H0p1ty Apr 20, 2024
249fe5b
this file is obsolete
H0p1ty Apr 20, 2024
97f3b43
task_02 added concepts
H0p1ty Apr 20, 2024
9c6ec83
task_02 added comments
H0p1ty Apr 20, 2024
0751218
task_02 added comments x2
H0p1ty Apr 20, 2024
92e1d72
task_04 added comments
H0p1ty Apr 20, 2024
3b23b55
task_04 added constructable concept
H0p1ty Apr 20, 2024
1bacbf3
task_04 added comments
H0p1ty Apr 20, 2024
25fb15e
task_05 added comments
H0p1ty Apr 20, 2024
d68e790
task_07 bugfixes
H0p1ty Apr 21, 2024
4de7a45
task_07 added BFS
H0p1ty Apr 21, 2024
df8591a
task_07 tweaks
H0p1ty Apr 21, 2024
1f71dbb
task_09 done
H0p1ty May 2, 2024
3d0a44c
task_07 delete breadth first search
H0p1ty May 2, 2024
aedfad6
task_07 tweaks
H0p1ty May 3, 2024
f27d3b0
task_02 added new test
H0p1ty May 3, 2024
be7a244
task_07 added comments
H0p1ty May 3, 2024
bb82451
task_09 tweaked names
H0p1ty May 3, 2024
585d136
task_06 minor change
H0p1ty May 3, 2024
33e5080
task_02 fixes
H0p1ty May 4, 2024
bf06d08
task_02 fixes x2
H0p1ty May 4, 2024
0ef8e2b
task_04 fixes
H0p1ty May 4, 2024
9d53c25
task_08 fixes
H0p1ty May 4, 2024
70b39e9
task_01 returned
H0p1ty May 4, 2024
47dbcee
task_07 names changed
H0p1ty May 17, 2024
9444408
task_06 names changed
H0p1ty May 17, 2024
764daed
task_05 names changed
H0p1ty May 17, 2024
285a8d8
task_04 names changed
H0p1ty May 17, 2024
bdf941f
task_08 names changed
H0p1ty May 17, 2024
7f57b8d
task_08 names changed x2
H0p1ty May 17, 2024
b996969
task_09 suggestion added
H0p1ty May 17, 2024
fd60337
Merge branch 'AlgorithmsDafeMipt2024:main' into task_08
H0p1ty May 17, 2024
80a5e56
task_02 suggested changes done
H0p1ty May 17, 2024
2e0ee9e
task_05 ultra minor name change
H0p1ty May 17, 2024
36426d5
task_04 names fixed
H0p1ty Jun 7, 2024
6a3d26b
task_08 codestyle fixes
H0p1ty Jun 7, 2024
1802c87
task_07 codestyle fixes
H0p1ty Jun 7, 2024
bb17319
task_09 added test
H0p1ty Jun 7, 2024
4183342
task_03 codestyle fixes
H0p1ty Jun 7, 2024
ed8b30f
task_02 codestyle changes
H0p1ty Jun 7, 2024
5434781
moved duplicate code lines to "util.hpp"
H0p1ty Jun 8, 2024
4d541aa
Delete sandbox/template/src/main
H0p1ty Jun 13, 2024
a30bc2f
revert sandbox/template/src/main.cpp
H0p1ty Jun 13, 2024
414db7b
task_07 final fixes
H0p1ty Jun 15, 2024
5b23365
Merge branch 'task_08' of https://github.com/H0p1ty/spring_homework i…
H0p1ty Jun 15, 2024
57c83a8
task_07 final fixes x2
H0p1ty Jun 15, 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
3 changes: 3 additions & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
cmake_minimum_required(VERSION 3.10)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

file(GLOB_RECURSE lib_source_list "src/*.cpp" "src/*.hpp")

add_library(Utils ${lib_source_list})
Expand Down
38 changes: 38 additions & 0 deletions lib/src/util.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <concepts>
#include <iostream>

// check if type comparasion operators are defined for a class
template <typename CustomType>
concept comparable = requires(CustomType a, CustomType b) {
a < b;
a <= b;
a == b;
a >= b;
a > b;
a != b;
};

// concept to check if the type has basic and copy constructor
template <typename CustomType>
concept constructable = requires(CustomType a, CustomType b) {
a = CustomType{};
b = CustomType{a};
};

// concept to check if the type is comparable with a concrete function
template <typename Function, typename CustomType>
concept comparing = requires(CustomType a, CustomType b, Function f) {
f(a, b);
};

// concept to check if the type is printable
template <typename CustomType>
concept printable = requires(CustomType a) {
std::cout << a;
};

// concept to check if the type has a hash function tied to it
template <typename K>
concept hashable = requires(K a) {
{ std::hash<K>{}(a) } -> std::convertible_to<std::size_t>;
};
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 числа из массива которые в сумме дадут заданное число
21 changes: 20 additions & 1 deletion task_01/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
#include <iostream>

int main() { return 0; }
#include "solution.h"

int main() {
int sum, arr_size, t;
std::vector<int> input_vector;
std::unordered_map<int, int> indices_map; // keeps array numbers as keys and
// indices as values behind keys

std::cin >> sum >> arr_size;

for (int i = 0; i < arr_size; i++) {
std::cin >> t;
input_vector.push_back(t);
}

std::pair<int, int> sol = solution(sum, input_vector);
std::cout << sol.first << ' ' << sol.second;

return 0;
}
41 changes: 41 additions & 0 deletions task_01/src/solution.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <unordered_map>
#include <vector>

/*

Output - indices of two numbers, which sum is equal to needed number, if there's
no such numbers, the output is "-1 -1"

Input:

10
10
-2 2 3 3 5 8 11 13 14 15

Output:

1 5

*/

// Solution below has a time complexity of O(n) and memory complexity of O(n)

std::pair<int, int> solution(int sum, std::vector<int> v) {
std::unordered_map<int, int> indices_map; // keeps array numbers as keys and
// indices as values behind keys

for (int i = 0; i < v.size(); i++) {
if (indices_map.find(sum - v[i]) !=
indices_map
.end()) { // if key "number - t" exists, we have found the solution
return {indices_map[sum - v[i]], i};
}

if (indices_map.find(v[i]) == indices_map.end())
indices_map[v[i]] =
i; // We only add keys that weren't in the map before (that
// way we get the least possible sum of i and j)
}

return {-1, -1}; // in case there are no such numbers
}
39 changes: 35 additions & 4 deletions task_01/src/test.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,39 @@

#include <gtest/gtest.h>

#include "topology_sort.hpp"
#include "solution.h"

TEST(TopologySort, Simple) {
ASSERT_EQ(1, 1); // Stack []
}
TEST(solution, simple) {
std::vector<int> v1 = {-2, 2, 3, 3, 5, 9, 11, 13, 14, 15};
std::pair<int, int> p1 = {-1, -1};
ASSERT_EQ(p1, solution(10, v1));

std::vector<int> v2 = {-2, 2, 3, 3, 5, 8, 11, 13, 14, 15};
std::pair<int, int> p2 = {1, 5};
ASSERT_EQ(p2, solution(10, v2));

std::vector<int> v3 = {};
std::pair<int, int> p3 = {-1, -1};
ASSERT_EQ(p3, solution(0, v3));

std::vector<int> v4 = {1};
std::pair<int, int> p4 = {-1, -1};
ASSERT_EQ(p4, solution(1, v4));

std::vector<int> v5 = {1, 2};
std::pair<int, int> p5 = {0, 1};
ASSERT_EQ(p5, solution(3, v5));

// if there are multiple solutions, the algorithm
// will pick the one, in which sum of i and j is the least,
// where i and j are indices of numbers, which sum is equal to needed number
std::vector<int> v6 = {1, 1, 1, 1, 1, 1, 1, 1, 1};
std::pair<int, int> p6 = {0, 1};
ASSERT_EQ(p6, solution(2, v6));

// in case there are multiple solutions in which i+j is the least,
// the algorithm will pick the one, in which i is greater
std::vector<int> v7 = {1, 2, 1, 1, 4, 5, 1, 1, 1};
std::pair<int, int> p7 = {1, 4};
ASSERT_EQ(p7, solution(6, v7));
}
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.

21 changes: 0 additions & 21 deletions task_02/src/stack.cpp

This file was deleted.

105 changes: 97 additions & 8 deletions task_02/src/stack.hpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,112 @@
#pragma once

#include <algorithm>
#include <stack>
#include <stdexcept>
#include <util.hpp>
#include <vector>

class Stack {
// simple structure, helps to form a linked list
template <typename CutsomType>
struct Node {
Node() : prev{nullptr}, data{}, next{nullptr} {}
Node(CutsomType data_) : prev{nullptr}, data{data_}, next{nullptr} {}
void AddNode(CutsomType data_);
Node* next;
Node* prev;
CutsomType data;
};

// method, that allows to add a node with a value to an existing one
template <typename CutsomType>
void Node<CutsomType>::AddNode(CutsomType data_) {
next = new Node(data_);
next->prev = this;
}

// data structure that can store and retrieve data in such fashion:
// last element inserted will be retrieved first (last in first out (LIFO))
// insertion, deletion and retrieval has time complexity of O(1)
template <typename CutsomType>
class MyStack {
public:
void Push(int value);
int Pop();
MyStack() : top_{nullptr} {}

// insert an element at the top of the stack
void Push(CutsomType value);

// retrieve last inserted element
CutsomType Top();
// retrieve last inserted element and delete it from the stack
CutsomType Pop();
// check if stack is empty
bool Empty() const { return top_ == nullptr; }

private:
std::stack<int> data_;
Node<CutsomType>* top_;
};

template <typename CutsomType>
void MyStack<CutsomType>::Push(CutsomType value) {
if (top_ == nullptr)
top_ = new Node(value);
else {
top_->AddNode(value);
top_ = top_->next;
}
}

template <typename CutsomType>
CutsomType MyStack<CutsomType>::Top() {
if (top_ == nullptr) throw std::runtime_error("Empty stack\n");
return top_->data;
}

template <typename CutsomType>
CutsomType MyStack<CutsomType>::Pop() {
if (top_ == nullptr) throw std::runtime_error("Empty stack\n");
Node<CutsomType>* temp = top_->prev;
CutsomType val = top_->data;
LostPointer marked this conversation as resolved.
Show resolved Hide resolved
delete top_;
top_ = temp;
return val;
}

template <comparable CutsomType>
class MinStack {
public:
void Push(int value);
int Pop();
int GetMin();
// insert an element at the top of the stack
void Push(CutsomType value);
// retrieve last inserted element and delete it from the stack
CutsomType Pop();
// retrieve minimal element
CutsomType Min();
// retrieve last inserted element
CutsomType Top();

private:
std::vector<int> data_;
MyStack<std::pair<CutsomType, CutsomType>> stack;
};

template <comparable CutsomType>
void MinStack<CutsomType>::Push(CutsomType value) {
if (stack.Empty())
stack.Push({value, value});
else
stack.Push({value, std::min(value, stack.Top().second)});
}

template <comparable CutsomType>
CutsomType MinStack<CutsomType>::Pop() {
return stack.Pop().first;
}

template <comparable CutsomType>
CutsomType MinStack<CutsomType>::Min() {
return stack.Top().second;
}

template <comparable CutsomType>
CutsomType MinStack<CutsomType>::Top() {
return stack.Top().first;
}
32 changes: 26 additions & 6 deletions task_02/src/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,22 @@
#include <gtest/gtest.h>

#include <stack>
#include <stdexcept>

#include "stack.hpp"

TEST(EmptyStack, Simple) {
MyStack<int> stack;
EXPECT_THROW(stack.Top(), std::runtime_error);
EXPECT_THROW(stack.Pop(), std::runtime_error);
stack.Push(1);
EXPECT_NO_THROW(stack.Pop());
LostPointer marked this conversation as resolved.
Show resolved Hide resolved
EXPECT_THROW(stack.Top(), std::runtime_error);
EXPECT_THROW(stack.Pop(), std::runtime_error);
}

TEST(StackTest, Simple) {
Stack stack;
MyStack<int> stack;
stack.Push(1); // Stack [1]
ASSERT_EQ(stack.Pop(), 1); // Stack []
stack.Push(1); // Stack [1]
Expand All @@ -22,21 +33,30 @@ TEST(StackTest, Simple) {
}

TEST(MinStackTest, Simple) {
MinStack stack;
MinStack<int> stack;
stack.Push(1); // Stack [1]
ASSERT_EQ(stack.GetMin(), 1);
ASSERT_EQ(stack.Min(), 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.Min(), 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.Min(), 1);
ASSERT_EQ(stack.Pop(), 2); // Stack [1]
stack.Push(3); // Stack [1, 3]
ASSERT_EQ(stack.GetMin(), 1);
ASSERT_EQ(stack.Min(), 1);
ASSERT_EQ(stack.Pop(), 3); // Stack [1]
ASSERT_EQ(stack.Pop(), 1); // Stack []

EXPECT_THROW(stack.Top(), std::runtime_error);
EXPECT_THROW(stack.Pop(), std::runtime_error);
}

TEST(StackTest, Empty) {
MyStack<int> stack;
EXPECT_THROW(stack.Top(), std::runtime_error);
EXPECT_THROW(stack.Pop(), std::runtime_error);
}
Loading
Loading