Skip to content

Conversation

@akmatkulov
Copy link
Owner

No description provided.

module Arrays
class << self
def replace(array)
max = 0
Copy link

Choose a reason for hiding this comment

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

если массив будет состоять из отрицательных элементов, в переменной max будет храниться некорректное значение

Copy link

Choose a reason for hiding this comment

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

^^^

Copy link

Choose a reason for hiding this comment

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

max = 0
item = 0
array.size.times { |n| max = array[n] if max < array[n] }
while array.size > item
Copy link

Choose a reason for hiding this comment

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

в руби есть готовые методы, делающие примерно то же. Посмотри про методы итерации each, map и тп

def replace(array)
max = 0
item = 0
array.size.times { |n| max = array[n] if max < array[n] }
Copy link

Choose a reason for hiding this comment

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

вынеси поиск максимума в отдельный метод

mid_index
when 1
subs = search(array.drop(mid_index + 1), query)
subs.nil? ? nil : (mid_index + 1) + subs
Copy link

Choose a reason for hiding this comment

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

что такое subs? возможно не оч подходящее название

@akmatkulov
Copy link
Owner Author

akmatkulov commented Nov 3, 2023 via email

@akmatkulov
Copy link
Owner Author

@YJorge Привет! Проверь пожалуйста я по фиксил!

def replace(array)
array
array.map do |item|
if item.positive?
Copy link

Choose a reason for hiding this comment

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

вложенность ухудшает читаемость. Перепиши через guard clause/тернарный


mid_index = array.length / 2

case query <=> array[mid_index]
Copy link

Choose a reason for hiding this comment

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

  1. Не используй <=> оператор. Он усложняет восприятие и чтение кода.
  2. вложенность ухудшает читаемость. Перепиши через guard clause/тернарный

Copy link
Owner Author

Choose a reason for hiding this comment

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

@YJorge Привет! Исправил.

@YJorge
Copy link

YJorge commented Nov 7, 2023

@akmatkulov 1. Если делаешь исправления и считаешь, что можно проводить ревью заново. Отправляй запрос через https://learn.dualboot.ru/ . Оповещение через комментарий может затеряться и ревью не произойдет до тех пор, пока на сайте не будет запроса
2. Не все комментарии исправлены
3. Заменив <=> на if/else/elsif ты не избавился от вложенности

@akmatkulov
Copy link
Owner Author

akmatkulov commented Nov 7, 2023

@YJorge
Не совсем понял про вложенность.

if
elsif query > array[mid_index]
....
min_index.nil? ? nil : (mid_index + 1) + min_index <===== ты про это?
end

@YJorge
Copy link

YJorge commented Nov 7, 2023

@YJorge Не совсем понял про вложенность.

if elsif query > array[mid_index] .... min_index.nil? ? nil : (mid_index + 1) + min_index <===== ты про это? end

в данном случае вложенность - наличие кода вида

if
...
else
...
end

такие конструкции лучше дробить до вида одиночных условий(guard clause) или записывать в виде тернарных. За счет этого должна улучшиться читабельность кода. Да, не всегда получается сходу полностью избавиться от if/else, тогда нужно поискать то, что можно упростить

return search(array.take(mid_index), query) if query < array[mid_index]

min_index = search(array.drop(mid_index + 1), query)
min_index.nil? ? nil : (mid_index + 1) + min_index
Copy link

Choose a reason for hiding this comment

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

  1. почему возвращается nil? если элемент не найден, должно вернуться -1
  2. min_index = search(array.drop(mid_index + 1), query) почему это минимальный индекс? Кажется название не оч соответствует
  3. функция не работает. Попробуй проверить search([1,2,4], 3) или search([1,2,4,5,7], 6)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants