diff --git a/test/exercise/arrays/solution.rb b/test/exercise/arrays/solution.rb index a7d518fc..6b8620b5 100644 --- a/test/exercise/arrays/solution.rb +++ b/test/exercise/arrays/solution.rb @@ -1,12 +1,28 @@ module Exercise module Arrays class << self + def max_value(array) + max = 0 + 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 end end end diff --git a/test/exercise/arrays/test.rb b/test/exercise/arrays/test.rb index 6876461e..da91370a 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