Skip to content

Feedback #1

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

Open
wants to merge 2 commits into
base: feedback
Choose a base branch
from
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

## 1. Информация о студенте

**Номер группы**: 00-000
**Номер группы**: 11-109

**Фамилия и Имя**: Иванов Иван
**Фамилия и Имя**: Шамсутдинов Рафаэль

## 2. Описание задания

Expand Down
22 changes: 21 additions & 1 deletion src/binary_search_iterative.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,31 @@

namespace assignment {

std::optional<int> BinarySearchIterative::Search(const std::vector<int>& arr, int search_elem) const {
std::optional<int> BinarySearchIterative::Search(const std::vector<int>& data, int search_element) const {

// Tips:
// 1. Заведите две переменные: (а) индекс левой границы и (б) индекс правой границы.
int l = 0;
int r = data.size();
// 2. Поиск ведется пока индекс левой границы не превысил индекс правой.
if (r == 0){
return std::nullopt;
}
while (l<=r){
int m = (l+r)/2;
if ((m<0) || (m>=data.size())){
return std::nullopt;
}
if (data[m] == search_element){
return m;
}
else if (data[m]>search_element){
r = m-1;
}
else {
l = m+1;
}
}
// 3. Каждую итерацию вычисляйте индекс середины внутри области, задаваемой индексами левой и правой границы.
// 4. Рассмотрите 3 случая:
// 1) Целевой элемент равен элементу посередине.
Expand Down
20 changes: 16 additions & 4 deletions src/binary_search_recursive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,26 @@
namespace assignment {

std::optional<int> BinarySearchRecursive::Search(const std::vector<int>& arr, int search_elem) const {
// запускаем рекурсивный метод с границами поиска от начала массива до конца
return search(arr, search_elem, 0, static_cast<int>(arr.size() - 1));
}

std::optional<int> BinarySearchRecursive::search(const std::vector<int>& arr, int search_elem, int start, int stop) const {
std::optional<int> BinarySearchRecursive::search(const std::vector<int>& arr, int search_elem, int left_index, int right_index) const {
int mid_index = (left_index + right_index) / 2;
if (left_index > right_index){
return std::nullopt;
}
if (arr[mid_index] == search_elem){
return mid_index;
}
if (arr[mid_index] > search_elem) {
return search(arr, search_elem, left_index, mid_index - 1);
}
if (arr[mid_index] < search_elem) {
return search(arr, search_elem, mid_index + 1, right_index);
}

// Tips:
// 1. Рассмотрите базовые случаи выхода и рекурсии:
// 1. Рассмотрите базовые случаи выхода из рекурсии:
// 1) индекс левого элемента стал больше индекса правого элемента
// 2) целевой элемент найден
// 2. Вызовите рекурсивный метод, изменив границы поиска
Expand All @@ -23,4 +35,4 @@ namespace assignment {
return std::nullopt;
}

} // namespace assignment
} // namespace assignment
7 changes: 5 additions & 2 deletions src/linear_search_iterative.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ namespace assignment {

std::optional<int> LinearSearchIterative::Search(const std::vector<int>& arr, int search_elem) const {

// Tips: итеративно пройдитесь по элементам массива

for (int i = 0; i<arr.size(); i++) {
if (arr[i] == search_elem) {
return i;
}
}
return std::nullopt;
}

Expand Down
12 changes: 9 additions & 3 deletions src/linear_search_recursive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@ namespace assignment {
}

std::optional<int> LinearSearchRecursive::search(const std::vector<int>& arr, int search_elem, int curr_index) const {
if (curr_index < 0) {
return std::nullopt;
}

if (arr[curr_index] == search_elem) {
return curr_index;
}

return search(arr, search_elem, curr_index - 1);
// Tips:
// 1. Укажите случаи выхода из рекурсии: (а) обошли все элементы и (б) элемент найден.
// 2. Вызовите рекурсивный метод с другим индексом.

return std::nullopt;
}

} // namespace assignment
12 changes: 12 additions & 0 deletions src/linear_search_two_pointers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ namespace assignment {
// 2. Проверяйте наличие целевого элемента по индексам.
// 3. Обновляйте индексы пока левый не станет больше правого.

int left = 0;
int right = static_cast<int>(arr.size() - 1);
while (left <= right) {
if (arr[left] == search_elem) {
return left;
}
if (arr[right] == search_elem) {
return right;
}
left++;
right--;
}
return std::nullopt;
}

Expand Down
20 changes: 17 additions & 3 deletions src/two_sum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,24 @@

namespace assignment {

std::optional<std::pair<int, int>> two_sum(const std::vector<int>& arr, int sum) {

std::optional<std::pair<int, int>> two_sum(const std::vector<int>& data, int sum) {
if (data.empty()) {
return std::nullopt;
}
// Tips: для создания пары (pair) используйте функцию std::make_pair

int left = 0;
int right = static_cast<int>(data.size() - 1);
while (data[left] + data[right] != sum and left != right) {
if (data[left] + data[right] < sum) {
left++;
}
else {
right--;
}
}
if (left != right){
return std::make_pair(left, right);
}
return std::nullopt;
}

Expand Down