Skip to content
This repository has been archived by the owner on Jan 31, 2022. It is now read-only.

Arrays solution added #122

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
19 changes: 16 additions & 3 deletions test/exercise/arrays/solution.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,24 @@ module Exercise
module Arrays
class << self
def replace(array)
array
max = array[0]
for x in array do
max = x if x > max
end
array.map { |el| el.positive? ? max : el }
end

def search(_array, _query)
0
def search(array, query)
return -1 if array.empty? || (array.last < query)

mid = array.count / 2
if query == array[mid]
mid
elsif query < array[mid]
search(array[0..mid - 1], query)
elsif query > array[mid]
search(array[mid + 1..-1], query) + mid + 1
end
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