Skip to content

Finish exercise arrays#1

Open
akmatkulov wants to merge 5 commits into
mainfrom
feature/arrays
Open

Finish exercise arrays#1
akmatkulov wants to merge 5 commits into
mainfrom
feature/arrays

Conversation

@akmatkulov

Copy link
Copy Markdown
Owner

No description provided.

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

^^^

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Comment thread test/exercise/arrays/solution.rb Outdated
max = 0
item = 0
array.size.times { |n| max = array[n] if max < array[n] }
while array.size > item

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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

Comment thread test/exercise/arrays/solution.rb Outdated
def replace(array)
max = 0
item = 0
array.size.times { |n| max = array[n] if max < array[n] }

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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

Comment thread test/exercise/arrays/solution.rb Outdated
mid_index
when 1
subs = search(array.drop(mid_index + 1), query)
subs.nil? ? nil : (mid_index + 1) + subs

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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

@akmatkulov

akmatkulov commented Nov 3, 2023 via email

Copy link
Copy Markdown
Owner Author

@akmatkulov

Copy link
Copy Markdown
Owner Author

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

Comment thread test/exercise/arrays/solution.rb Outdated
def replace(array)
array
array.map do |item|
if item.positive?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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

Comment thread test/exercise/arrays/solution.rb Outdated

mid_index = array.length / 2

case query <=> array[mid_index]

Copy link
Copy Markdown

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
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

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

@YJorge

YJorge commented Nov 7, 2023

Copy link
Copy Markdown

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

@akmatkulov

akmatkulov commented Nov 7, 2023

Copy link
Copy Markdown
Owner Author

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

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

@YJorge

YJorge commented Nov 7, 2023

Copy link
Copy Markdown

@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
Copy Markdown

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