diff --git a/test/exercise/arrays/solution.rb b/test/exercise/arrays/solution.rb index a7d518fc..a0d34112 100644 --- a/test/exercise/arrays/solution.rb +++ b/test/exercise/arrays/solution.rb @@ -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 } + 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 diff --git a/test/exercise/arrays/test.rb b/test/exercise/arrays/test.rb index 8cd6fb66..4e3e8acf 100644 --- a/test/exercise/arrays/test.rb +++ b/test/exercise/arrays/test.rb @@ -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) @@ -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