From 577fb9f2e4970f9d28088fc3a8fc7d2d244a1d6f Mon Sep 17 00:00:00 2001 From: jingleville Date: Mon, 4 Dec 2023 22:23:57 +0300 Subject: [PATCH 1/2] solved arrays exercises --- test/exercise/arrays/solution.rb | 18 ++++++++++++++++-- test/exercise/arrays/test.rb | 4 ++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/test/exercise/arrays/solution.rb b/test/exercise/arrays/solution.rb index a7d518fc..7b4c6328 100644 --- a/test/exercise/arrays/solution.rb +++ b/test/exercise/arrays/solution.rb @@ -2,11 +2,25 @@ module Exercise module Arrays class << self def replace(array) - array + array.map { |el| el.positive? ? array.max : el } end def search(_array, _query) - 0 + max = _array.length - 1 + min = 0 + + while min <= max + mid = (min + max) / 2 + if _array[mid] == _query + return mid + elsif _array[mid] > _query + max = mid - 1 + else + min = mid + 1 + end + end + + -1 end end end diff --git a/test/exercise/arrays/test.rb b/test/exercise/arrays/test.rb index 6876461e..95ae5370 100644 --- a/test/exercise/arrays/test.rb +++ b/test/exercise/arrays/test.rb @@ -4,7 +4,7 @@ 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 +14,7 @@ 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 From e6ec6385888e6633546de2a876b1d9298ea95917 Mon Sep 17 00:00:00 2001 From: jingleville Date: Mon, 4 Dec 2023 22:26:01 +0300 Subject: [PATCH 2/2] solved fp --- test/exercise/fp/solution.rb | 11 +++++++++-- test/exercise/fp/test.rb | 4 ++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/test/exercise/fp/solution.rb b/test/exercise/fp/solution.rb index 91c40040..e2495e6d 100644 --- a/test/exercise/fp/solution.rb +++ b/test/exercise/fp/solution.rb @@ -5,11 +5,18 @@ class << self # film["name"], film["rating_kinopoisk"], film["rating_imdb"], # film["genres"], film["year"], film["access_level"], film["country"] def rating(_array) - 0 + _array = _array.map { |film| film if film['country'] }.compact + _array = _array.map { |film| film if film['rating_kinopoisk'] }.compact + _array = _array.map { |film| film['rating_kinopoisk'].to_f if film['country'].split(',').size > 1 }.compact + _array = _array.map { |rate| rate if rate.positive? }.compact + _array.reduce { |sum, el| sum + el }.to_f / _array.size end + # Посчитать количесвто букв 'и' в названиях всех фильмов с рейтингом кинопоиска больше или равным заданному значению + def chars_count(_films, _threshold) - 0 + _films = _films.map { |film| film['name'].count('и') if film['rating_kinopoisk'].to_f > _threshold }.compact + _films.reduce { |sum, el| sum + el } end end end diff --git a/test/exercise/fp/test.rb b/test/exercise/fp/test.rb index c1554768..c131c30b 100644 --- a/test/exercise/fp/test.rb +++ b/test/exercise/fp/test.rb @@ -7,7 +7,7 @@ class Exercise::FpTest < Minitest::Test # Посчитать средний рейтинг фильмов по версии кинопоиска у которых две или больше стран # Фильмы у которых рейтиг не задан или равен 0 не учитывать в расчете среднего. def test_rating - skip + array = CSV.readlines('./test/fixtures/films.csv', headers: true) result = Exercise::Fp.rating(array) @@ -18,7 +18,7 @@ def test_rating # Посчитать количесвто букв 'и' в названиях всех фильмов с рейтингом кинопоиска больше или равным заданному значению def test_chars_count - skip + array = CSV.readlines('./test/fixtures/films.csv', headers: true) result = Exercise::Fp.chars_count(array, 5)