-
Notifications
You must be signed in to change notification settings - Fork 26
Pogodaeva_homework #3
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?
Changes from all commits
cf6becd
3fbabbc
6024697
476444e
c8bb5ea
8a93757
4a73676
165d1f5
e78dc8d
9aa106b
7b9f0f4
fcd91b4
168a03b
8e042f8
0b3f5e6
3e39813
d8dcaa5
2044a75
77c447f
ff90882
d2bae7d
f8bea2b
dbdf4f5
0fd87f9
c2a3bdb
0290228
2157390
290f8d5
4a19ead
e746323
646fe0f
2cdb451
ed441a4
234d344
a1d8b0e
dd61cea
37ea51c
73ef56d
796b51c
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,31 @@ | ||
| #include "util.hpp" | ||
|
|
||
| std::vector<double> Smaller(double elem, const std::vector<double>& input) { | ||
| std::vector<double> smaller{}; | ||
| for (auto i : input) { | ||
| if (i < elem) { | ||
| smaller.push_back(i); | ||
| } | ||
| } | ||
| return smaller; | ||
| } | ||
|
|
||
| std::vector<double> Bigger(double elem, const std::vector<double>& input) { | ||
| std::vector<double> bigger{}; | ||
| for (auto i : input) { | ||
| if (i > elem) { | ||
| bigger.push_back(i); | ||
| } | ||
| } | ||
| return bigger; | ||
| } | ||
|
|
||
| std::vector<double> Eq(double elem, const std::vector<double>& input) { | ||
| std::vector<double> eq{}; | ||
| for (auto i : input) { | ||
| if (i == elem) { | ||
| eq.push_back(i); | ||
| } | ||
| } | ||
| return eq; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| #include <vector> | ||
|
|
||
| std::vector<double> Smaller(double elem, const std::vector<double>& input); | ||
|
|
||
| std::vector<double> Bigger(double elem, const std::vector<double>& input); | ||
|
|
||
| std::vector<double> Eq(double elem, const std::vector<double>& input); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| #include "task.hpp" | ||
|
|
||
| #include <stdexcept> | ||
|
|
||
| std::pair<int, int> SearchNumbers(int search_num, | ||
| const std::vector<int>& data) { | ||
| if (data.size() <= 1) { | ||
| throw std::invalid_argument("vector is too small"); | ||
| } | ||
| size_t bottom{0}; | ||
| size_t top{data.size() - 1}; | ||
| while (bottom != top) { | ||
| if (data[bottom] + data[top] == search_num) { | ||
| std::pair<int, int> ans{data[bottom], data[top]}; | ||
| return ans; | ||
| } else if (data[bottom] + data[top] > search_num) { | ||
| --top; | ||
| } else { | ||
| ++bottom; | ||
| } | ||
| } | ||
| throw std::invalid_argument("no suitable pair"); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| #include <utility> | ||
| #include <vector> | ||
|
|
||
| std::pair<int, int> SearchNumbers(int search_num, const std::vector<int>& data); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,48 @@ | ||
| #include <gtest/gtest.h> | ||
|
|
||
| TEST(Test, Simple) { | ||
| ASSERT_EQ(1, 1); // Stack [] | ||
| } | ||
| #include "task.hpp" | ||
|
|
||
| TEST(Task1, Simple) { | ||
| std::vector<int> data{1, 3, 4, 4, 7, 11}; | ||
| std::pair<int, int> ans{3, 7}; | ||
| ASSERT_EQ(SearchNumbers(10, data), ans); | ||
| } | ||
|
|
||
| TEST(Task1, FirstAndLast) { | ||
| std::vector<int> data{1, 2, 3, 4, 5, 99}; | ||
| std::pair<int, int> ans{1, 99}; | ||
| ASSERT_EQ(SearchNumbers(100, data), ans); | ||
| } | ||
|
|
||
| TEST(Task1, Negative) { | ||
| std::vector<int> data{-5, -3, 0, 1, 2, 8}; | ||
| std::pair<int, int> ans{-3, 8}; | ||
| ASSERT_EQ(SearchNumbers(5, data), ans); | ||
| } | ||
|
|
||
| TEST(Task1, Simple2) { | ||
| std::vector<int> data{10, 11, 12, 13, 14, 15}; | ||
| std::pair<int, int> ans{10, 15}; | ||
| ASSERT_EQ(SearchNumbers(25, data), ans); | ||
| } | ||
|
|
||
| TEST(Task1, SmallVector) { | ||
| std::vector<int> data{1, 2}; | ||
| std::pair<int, int> ans{1, 2}; | ||
| ASSERT_EQ(SearchNumbers(3, data), ans); | ||
| } | ||
|
|
||
| TEST(Task1, NoPair) { | ||
| std::vector<int> data{1, 2, 3, 4, 5}; | ||
| ASSERT_THROW(SearchNumbers(100, data), std::invalid_argument); | ||
| } | ||
|
|
||
| TEST(Task1, Empty) { | ||
| std::vector<int> data; | ||
| ASSERT_THROW(SearchNumbers(10, data), std::invalid_argument); | ||
| } | ||
|
|
||
| TEST(Task1, SingleElementVector) { | ||
| std::vector<int> data{5}; | ||
| ASSERT_THROW(SearchNumbers(5, data), std::invalid_argument); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| #include <iostream> | ||
|
|
||
| int main() { return 0; } | ||
| #include "stack.hpp" | ||
|
|
||
| int main() {} |
This file was deleted.
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,23 +1,128 @@ | ||||||
| #pragma once | ||||||
|
|
||||||
| #include <stack> | ||||||
| #include <vector> | ||||||
| #include <algorithm> | ||||||
| #include <concepts> | ||||||
|
|
||||||
| struct EmptyStackError : std::exception { | ||||||
| using std::exception::exception; | ||||||
| }; | ||||||
|
|
||||||
| template <typename T> | ||||||
| struct Node { | ||||||
| T value; | ||||||
| Node<T>* prev = nullptr; | ||||||
| }; | ||||||
|
|
||||||
| template <typename T> | ||||||
| class Stack { | ||||||
| public: | ||||||
| void Push(int value); | ||||||
| int Pop(); | ||||||
| Stack(){}; | ||||||
| ~Stack() { | ||||||
| while (head != nullptr) { | ||||||
| Node<T>* old_head = head; | ||||||
| head = head->prev; | ||||||
| delete old_head; | ||||||
| } | ||||||
| } | ||||||
| void Push(T value); | ||||||
| T Pop(); | ||||||
|
|
||||||
| private: | ||||||
| std::stack<int> data_; | ||||||
| Node<T>* head = nullptr; | ||||||
| }; | ||||||
|
|
||||||
| template <typename T> | ||||||
| requires(std::totally_ordered<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: a type specifier is required for all declarations [clang-diagnostic-error] requires(std::totally_ordered<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: expected ';' after top level declarator [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: expected expression [clang-diagnostic-error] requires(std::totally_ordered<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: no member named 'totally_ordered' in namespace 'std' [clang-diagnostic-error] requires(std::totally_ordered<T>)
^ |
||||||
| class MinStack { | ||||||
| public: | ||||||
| void Push(int value); | ||||||
| int Pop(); | ||||||
| int GetMin(); | ||||||
| MinStack(){}; | ||||||
| ~MinStack() { | ||||||
| while (head != nullptr) { | ||||||
| Node<T>* old_head = head; | ||||||
|
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 'T' [clang-diagnostic-error] Node<T>* old_head = head;
^ |
||||||
| Node<T>* old_head_min = head_min; | ||||||
|
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 'T' [clang-diagnostic-error] Node<T>* old_head_min = head_min;
^ |
||||||
|
|
||||||
| head = head->prev; | ||||||
| head_min = head_min->prev; | ||||||
|
|
||||||
| delete old_head; | ||||||
| delete old_head_min; | ||||||
| } | ||||||
| } | ||||||
| void Push(T value); | ||||||
|
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 value);
^ |
||||||
| 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 const 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 const GetMin();
^ |
||||||
|
|
||||||
| private: | ||||||
| std::vector<int> data_; | ||||||
| Node<T>* head = nullptr; | ||||||
|
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 'T' [clang-diagnostic-error] Node<T>* head = nullptr;
^ |
||||||
| Node<T>* head_min = nullptr; | ||||||
|
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 'T' [clang-diagnostic-error] Node<T>* head_min = nullptr;
^ |
||||||
| }; | ||||||
|
|
||||||
| template <typename T> | ||||||
| void Stack<T>::Push(T value) { | ||||||
| if (!head) { | ||||||
| head = new Node<T>; | ||||||
| head->value = value; | ||||||
| } else { | ||||||
| Node<T>* new_head = new Node<T>; | ||||||
| new_head->prev = head; | ||||||
| new_head->value = value; | ||||||
| head = new_head; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| template <typename T> | ||||||
| T Stack<T>::Pop() { | ||||||
| if (head == nullptr) { | ||||||
| throw EmptyStackError(); | ||||||
| } | ||||||
| Node<T>* del_node = head; | ||||||
| T val = head->value; | ||||||
| head = head->prev; | ||||||
|
Contributor
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. тут утечка памяти, данные в "старой голове" не удаляются и они будут до завершения работы висеть в памяти
Contributor
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. во втором стеке тоже |
||||||
| delete del_node; | ||||||
| return val; | ||||||
| } | ||||||
|
|
||||||
| template <typename T> | ||||||
| requires(std::totally_ordered<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] requires(std::totally_ordered<T>)
^Additional contexttask_02/src/stack.hpp:84: 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: a type specifier is required for all declarations [clang-diagnostic-error] requires(std::totally_ordered<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: expected ';' after top level declarator [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: expected expression [clang-diagnostic-error] requires(std::totally_ordered<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: no member named 'totally_ordered' in namespace 'std' [clang-diagnostic-error] requires(std::totally_ordered<T>)
^ |
||||||
| void MinStack<T>::Push(T value) { | ||||||
|
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 top level declarator [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: variable has incomplete type 'void' [clang-diagnostic-error] void MinStack<T>::Push(T value) {
^ |
||||||
| if (!head) { | ||||||
| head = new Node<T>; | ||||||
| head->value = value; | ||||||
| head_min = new Node<T>; | ||||||
| head_min->value = value; | ||||||
| } else { | ||||||
| Node<T>* new_head = new Node<T>; | ||||||
| Node<T>* new_head_min = new Node<T>; | ||||||
| new_head->prev = head; | ||||||
| new_head->value = value; | ||||||
| new_head_min->prev = head_min; | ||||||
| new_head_min->value = std::min(head_min->value, value); | ||||||
| head = new_head; | ||||||
| head_min = new_head_min; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| template <typename T> | ||||||
| requires(std::totally_ordered<T>) | ||||||
| T MinStack<T>::Pop() { | ||||||
| if (!head || !head_min) { | ||||||
| throw EmptyStackError(); | ||||||
| } | ||||||
| Node<T>* del_node = head; | ||||||
| Node<T>* del_node_min = head_min; | ||||||
| T val = head->value; | ||||||
| head = head->prev; | ||||||
| head_min = head_min->prev; | ||||||
| delete del_node; | ||||||
| delete del_node_min; | ||||||
| return val; | ||||||
| } | ||||||
|
|
||||||
| template <typename T> | ||||||
| requires(std::totally_ordered<T>) | ||||||
| T const MinStack<T>::GetMin() { | ||||||
| if (!head_min) { | ||||||
| throw EmptyStackError(); | ||||||
| } | ||||||
| return head_min->value; | ||||||
| } | ||||||
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]
requires(std::totally_ordered<T>) ^Additional context
task_02/src/stack.hpp:33: declared here