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
Expand Up @@ -2,11 +2,27 @@ module Exercise
module Arrays
class << self
def replace(array)
array
max = find_max(array)
array.map { |el| el.positive? ? max : el }
end

def search(_array, _query)
0
def find_max(array)
max = array.first
array.each { |el| max = el if el > max }
Comment on lines +10 to +11

Choose a reason for hiding this comment

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

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

Copy link
Owner Author

Choose a reason for hiding this comment

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

запушил новый коммит с исправлениями
вы это имели ввиду?

Choose a reason for hiding this comment

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

да, пойдет. и давай на ты, если не против

Copy link
Owner Author

Choose a reason for hiding this comment

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

окей, давай на ты

max
end

def search(array, query)
binary_search(array, query, 0, array.size)
end

def binary_search(array, query, left, right)
mid = (left + right) / 2
return mid if array[mid] == query
return -1 if left == right
return binary_search(array, query, mid + 1, right) if array[mid] < query

binary_search(array, query, left, mid)
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