-
Notifications
You must be signed in to change notification settings - Fork 26
Varinitsa #8
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
base: main
Are you sure you want to change the base?
Varinitsa #8
Changes from all commits
644f198
e6e69d1
a214465
4aab724
c94a85b
c5bd9a4
d11799e
7fe458d
ca7b895
f315548
b1334be
1bf01e0
1380d32
00bef32
e2e5c89
c3598ef
ad2f8c2
411b59f
61e8344
d789268
62eb3b3
762daf4
14a4dc2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| #include <vector> | ||
|
|
||
| void MergeSort(std::vector<int>& array); |
| 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; | ||
| } |
| 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); } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| #include "utils.hpp" | ||
| // #include "utils.hpp" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| #pragma once | ||
| // #pragma once |
| 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{}; | ||
| } |
| 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); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| #include <iostream> | ||
|
|
||
| int main() { return 0; } | ||
| int main() { return 0; } |
| 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); | ||
| } |
This file was deleted.
| 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) { | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 contexttask_02/src/stack.hpp:5: declared here template <typename T>
^There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) {
^There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 contexttask_02/src/stack.hpp:4: declared here template <typename T>
^There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 contexttask_02/src/stack.hpp:4: declared here template <typename T>
^There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 contexttask_02/src/stack.hpp:4: declared here template <typename T>
^There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: unknown type name 'Comparable' [clang-diagnostic-error] template <Comparable T>
^There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: unknown type name 'T' [clang-diagnostic-error] void Push(T k);
^There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: unknown type name 'T' [clang-diagnostic-error] T Pop();
^There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||||||||||
|
Half-Head marked this conversation as resolved.
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> | ||||||||||
|
Half-Head marked this conversation as resolved.
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||||||||||
|
Half-Head marked this conversation as resolved.
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> | ||||||||||
|
Half-Head marked this conversation as resolved.
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() { | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()}; | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: expected ';' after expression [clang-diagnostic-error]
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()};
^There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: expected ';' after expression [clang-diagnostic-error]
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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;
^There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: unknown type name 'Comparable' [clang-diagnostic-error] template <Comparable T>
^There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: unknown type name 'T' [clang-diagnostic-error] T Pop();
^There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: unknown type name 'T' [clang-diagnostic-error] T Pop();
^ |
||||||||||
| T GetMin(); | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||||||||||
| } | ||||||||||
| 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()); | ||
| } |
| 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); | ||
| } |
There was a problem hiding this comment.
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]
Additional context
task_02/src/stack.hpp:5: declared here