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
1 change: 1 addition & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ jobs:
steps:
- name: Install gtest manually
run: sudo apt-get install libgtest-dev
run: sudo apt install libomp-dev

- uses: actions/checkout@v3

Expand Down
23 changes: 23 additions & 0 deletions lib/src/util.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,24 @@
#include "util.hpp"

void MergeSort(std::vector<int>& array) {
int n = array.size();
std::vector<int> buffer(n);

for (int slice = 1; slice < n; slice *= 2) {
for (int left = 0; left < n; left += 2 * slice) {
int mid = std::min(left + slice, n);
int right = std::min(left + 2 * slice, n);

int i = left, j = mid;
int k = left;
while (i < mid && j < right) {
buffer[k++] = array[i] <= array[j] ? array[i++] : array[j++];
}
while (i < mid) buffer[k++] = array[i++];
while (j < right) buffer[k++] = array[j++];

std::copy(buffer.begin() + left, buffer.begin() + right,
array.begin() + left);
}
}
}
3 changes: 3 additions & 0 deletions lib/src/util.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include <vector>

void MergeSort(std::vector<int>& array);
Binary file added sandbox/template/src/main
Binary file not shown.
42 changes: 40 additions & 2 deletions sandbox/template/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,41 @@
#include <iostream>
// #include <iostream>
// #include <vector>

int main() { return 0; }
// void MergeSort(std ::vector<int>& array) {
// int i{0}, j{1};
// int slice{1};
// do {
// while (j <= array.size() - 1) {
// if (array[i] > array[j]) {
// iter_swap(array.begin() + i, array.begin() + j);

// ++j;
// } else
// ++i;
// if (j - i > slice) {
// i = j;
// j = i + slice;
// } else if (j == i) {
// i += 1;
// j = i + slice;
// }
// }
// slice *= 2;
// slice > array.size() ? j = array.size() - 1 : j = slice;
// i = 0;
// if (j == array.size() - 1) {
// if (i == 0) {
// return;
// }
// }
// } while (true);
// }

int main() {
// std ::vector<int> unsort{3, 2, 4, 5, 1, 9, 7};
// MergeSort(unsort);
// for (int i = 0; i < unsort.size(); ++i) {
// std ::cout << unsort[i] << " ";
// }
// std ::cout << std ::endl;
}
8 changes: 4 additions & 4 deletions sandbox/template/src/test.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

#include <gtest/gtest.h>
// #include <gtest/gtest.h>

#include <stack>
// #include <stack>

#include "utils.hpp"
// #include "utils.hpp"

TEST(Template, Simple) { ASSERT_EQ(true, true); }
// TEST(Template, Simple) { ASSERT_EQ(true, true); }
2 changes: 1 addition & 1 deletion sandbox/template/src/utils.cpp
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#include "utils.hpp"
// #include "utils.hpp"
2 changes: 1 addition & 1 deletion sandbox/template/src/utils.hpp
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#pragma once
// #pragma once
20 changes: 20 additions & 0 deletions task_01/src/find.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "find.hpp"

std ::vector<int> FindNums(int k, std ::vector<int> sequence) {
if (sequence.size() == 0) throw NoSumNum{};

std ::vector<int> answer;
unsigned long left_index{0};
unsigned long right_index{sequence.size() - 1};
while (left_index != right_index) {
if ((sequence[left_index] + sequence[right_index]) == k) {
answer.push_back(sequence[left_index]);
answer.push_back(sequence[right_index]);
return answer;
} else if (sequence[left_index] + sequence[right_index] > k)
--right_index;
else
++left_index;
}
throw NoSumNum{};
}
8 changes: 8 additions & 0 deletions task_01/src/find.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <string>
#include <vector>

struct NoSumNum {
std ::string s{"no possible way to solve task"};
};

std ::vector<int> FindNums(int k, std ::vector<int> sequence);
2 changes: 1 addition & 1 deletion task_01/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#include <iostream>

int main() { return 0; }
int main() { return 0; }
32 changes: 30 additions & 2 deletions task_01/src/test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
#include <gtest/gtest.h>

TEST(Test, Simple) {
ASSERT_EQ(1, 1); // Stack []
#include "find.hpp"

TEST(FindNums, Simple) {
std ::vector<int> seq{1, 2, 3, 4, 5};
std ::vector<int> ans{FindNums(5, seq)};
ASSERT_EQ(5, ans[0] + ans[1]);
}

TEST(FindNums, Negative) {
std ::vector<int> seq{-100, -10, -9, -8, -1};
std ::vector<int> ans{FindNums(-9, seq)};
ASSERT_EQ(-9, ans[0] + ans[1]);
}

TEST(FindNums, WithZeros) {
std ::vector<int> seq{
-15, 0, 0, 2, 8,
};
std ::vector<int> ans{FindNums(10, seq)};
ASSERT_EQ(10, ans[0] + ans[1]);
}

TEST(FindNums, NoValue) {
std ::vector<int> seq{0, 0, 2, 8, -15};
EXPECT_THROW(FindNums(60, seq), NoSumNum);
}

TEST(FindNums, Empty) {
std ::vector<int> seq(0);
EXPECT_THROW(FindNums(60, seq), NoSumNum);
}
21 changes: 0 additions & 21 deletions task_02/src/stack.cpp

This file was deleted.

67 changes: 57 additions & 10 deletions task_02/src/stack.hpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,70 @@
#pragma once

#include <stack>
#include <concepts>
#include <stdexcept>
#include <vector>

template <typename T>
concept Comparable = requires(T a, T b) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: 'T' does not refer to a value [clang-diagnostic-error]

concept Comparable = requires(T a, T b) {
                                   ^
Additional context

task_02/src/stack.hpp:5: declared here

template <typename T>
                   ^

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: 'T' does not refer to a value [clang-diagnostic-error]

concept Comparable = requires(T a, T b) {
                              ^
Additional context

task_02/src/stack.hpp:5: declared here

template <typename T>
                   ^

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: unknown type name 'concept' [clang-diagnostic-error]

concept Comparable = requires(T a, T b) {
^

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: 'T' does not refer to a value [clang-diagnostic-error]

concept Comparable = requires(T a, T b) {
                                   ^
Additional context

task_02/src/stack.hpp:4: declared here

template <typename T>
                   ^

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: 'T' does not refer to a value [clang-diagnostic-error]

concept Comparable = requires(T a, T b) {
                              ^
Additional context

task_02/src/stack.hpp:4: declared here

template <typename T>
                   ^

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: 'T' does not refer to a value [clang-diagnostic-error]

concept Comparable = requires(T a, T b) {
                              ^
Additional context

task_02/src/stack.hpp:4: declared here

template <typename T>
                   ^

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: unknown type name 'concept' [clang-diagnostic-error]

concept Comparable = requires(T a, T b) {
^

{ a < b } -> std::convertible_to<bool>;
};

template <Comparable T>
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: unknown type name 'Comparable' [clang-diagnostic-error]

template <Comparable T>
          ^

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: unknown type name 'Comparable' [clang-diagnostic-error]

template <Comparable T>
          ^

class Stack {
public:
void Push(int value);
int Pop();
void Push(T k);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: unknown type name 'T' [clang-diagnostic-error]

  void Push(T k);
            ^

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: unknown type name 'T' [clang-diagnostic-error]

  void Push(T k);
            ^

T Pop();
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: unknown type name 'T' [clang-diagnostic-error]

  T Pop();
  ^

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: unknown type name 'T' [clang-diagnostic-error]

  T Pop();
  ^


private:
std::stack<int> data_;
std ::vector<T> _data;
Comment thread
Half-Head marked this conversation as resolved.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: template argument for template type parameter must be a type [clang-diagnostic-error]

  std ::vector<T> _data;
               ^
Additional context

/usr/include/c++/13/bits/stl_vector.h:426: template parameter is declared here

  template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
                    ^

};

template <Comparable T>
Comment thread
Half-Head marked this conversation as resolved.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: unknown type name 'Comparable' [clang-diagnostic-error]

template <Comparable T>
          ^

void Stack<T>::Push(T k) {
Comment thread
Half-Head marked this conversation as resolved.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: unknown type name 'T' [clang-diagnostic-error]

void Stack<T>::Push(T k) {
                    ^

_data.push_back(k);
}

template <Comparable T>
Comment thread
Half-Head marked this conversation as resolved.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: unknown type name 'Comparable' [clang-diagnostic-error]

template <Comparable T>
          ^

T Stack<T>::Pop() {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: unknown type name 'T' [clang-diagnostic-error]

T Stack<T>::Pop() {
^

if (_data.size() == 0) throw std::out_of_range("No data in stack");
T pop_val{_data.back()};
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: expected ';' after expression [clang-diagnostic-error]

Suggested change
T pop_val{_data.back()};
T; pop_val{_data.back()};

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: use of undeclared identifier 'pop_val' [clang-diagnostic-error]

  T pop_val{_data.back()};
    ^

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: expected ';' after expression [clang-diagnostic-error]

Suggested change
T pop_val{_data.back()};
T; pop_val{_data.back()};

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: use of undeclared identifier 'pop_val' [clang-diagnostic-error]

  T pop_val{_data.back()};
    ^

_data.pop_back();
return pop_val;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: use of undeclared identifier 'pop_val' [clang-diagnostic-error]

  return pop_val;
         ^

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: use of undeclared identifier 'pop_val' [clang-diagnostic-error]

  return pop_val;
         ^

}

template <Comparable T>
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: unknown type name 'Comparable' [clang-diagnostic-error]

template <Comparable T>
          ^

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: unknown type name 'Comparable' [clang-diagnostic-error]

template <Comparable T>
          ^

class MinStack {
public:
void Push(int value);
int Pop();
int GetMin();
void Push(T k);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: unknown type name 'T' [clang-diagnostic-error]

  void Push(T k);
            ^

T Pop();
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: unknown type name 'T' [clang-diagnostic-error]

  T Pop();
  ^

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: unknown type name 'T' [clang-diagnostic-error]

  T Pop();
  ^

T GetMin();
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: unknown type name 'T' [clang-diagnostic-error]

  T GetMin();
  ^


private:
std::vector<int> data_;
std ::vector<T> _data;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: template argument for template type parameter must be a type [clang-diagnostic-error]

  std ::vector<T> _data;
               ^
Additional context

/usr/include/c++/13/bits/stl_vector.h:426: template parameter is declared here

  template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
                    ^

std ::vector<T> _min_data;
};

template <Comparable T>
void MinStack<T>::Push(T k) {
if (_min_data.size() == 0)
_min_data.push_back(k);
else if (_min_data.back() > k)
_min_data.push_back(k);
else
_min_data.push_back(_min_data.back());
_data.push_back(k);
}

template <Comparable T>
T MinStack<T>::Pop() {
if ((_data.size() == 0) || (_min_data.size() == 0))
throw std::out_of_range("No data in stack");
T pop_val{_data.back()};
_data.pop_back();
_min_data.pop_back();
return pop_val;
}

template <Comparable T>
T MinStack<T>::GetMin() {
if (_min_data.size() == 0) throw std::out_of_range("No data in stack");
return _min_data.back();
}
80 changes: 52 additions & 28 deletions task_02/src/test.cpp
Original file line number Diff line number Diff line change
@@ -1,42 +1,66 @@

#include <gtest/gtest.h>

#include <stack>

#include "stack.hpp"

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<int> stack;
stack.Push(1);
ASSERT_EQ(stack.Pop(), 1);
stack.Push(1);
stack.Push(2);
ASSERT_EQ(stack.Pop(), 2);
ASSERT_EQ(stack.Pop(), 1);
stack.Push(1);
stack.Push(2);
ASSERT_EQ(stack.Pop(), 2);
stack.Push(3);
ASSERT_EQ(stack.Pop(), 3);
ASSERT_EQ(stack.Pop(), 1);
}

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

TEST(Stack, Custom) {
Stack<int> stack;
stack.Push(10);
ASSERT_EQ(10, stack.Pop());

EXPECT_THROW(stack.Pop(), std::out_of_range);
}

TEST(MinStack, Custom) {
MinStack<int> stack;
stack.Push(10);
stack.Push(2);
stack.Push(17);
ASSERT_EQ(2, stack.GetMin());

stack.Pop();
stack.Pop();
stack.Pop();
EXPECT_THROW(stack.Pop(), std::out_of_range);

stack.Push(10);
stack.Push(10);
stack.Push(10);
ASSERT_EQ(10, stack.GetMin());
}
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 <iostream>

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

// void print(std::vector<int>& v) {
// for (auto c : v) std ::cout << c << " ";
// }

int main() {
// std ::vector<int> s{5, 7, 9, 3, 4, 0, 2, -1, 3};
// s = Warming(s);
// print(s);
}
Loading
Loading