Skip to content
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
22 changes: 19 additions & 3 deletions test/exercise/arrays/solution.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
module Exercise
module Arrays
class << self
def max_value(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.

array.each do |item|
max = item if item > max && item.positive?
end
max
end

def replace(array)
array
array.map do |item|
item.positive? ? max_value(array) : item
end
end

def search(_array, _query)
0
def search(array, query)
return -1 if array.size.zero? || query < array.first || query > array.last

mid_index = array.length / 2
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)

end
end
end
Expand Down
2 changes: 0 additions & 2 deletions test/exercise/arrays/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
class Exercise::ArraysTest < Minitest::Test
# Заменить все положительные элементы целочисленного массива на максимальное значение элементов массива.
def test_replace
skip
array = [3, 2, -8, 4, 100, -6, 7, 8, -99]
new_array = Exercise::Arrays.replace(array)

Expand All @@ -14,7 +13,6 @@ def test_replace
# Реализовать двоичный поиск
# Функция должна возвращать индекс элемента
def test_bin_search
skip
assert Exercise::Arrays.search([1], 900) == -1
assert Exercise::Arrays.search([1], 1).zero?
assert Exercise::Arrays.search([], 900) == -1
Expand Down