diff --git a/test/exercise/arrays/solution.rb b/test/exercise/arrays/solution.rb index a7d518fc..2d21934e 100644 --- a/test/exercise/arrays/solution.rb +++ b/test/exercise/arrays/solution.rb @@ -2,11 +2,28 @@ module Exercise module Arrays class << self def replace(array) - array + max = array.max + array.map { |v| v.positive? ? max : v } end - def search(_array, _query) - 0 + def search(array, query) + low_index = 0 + high_index = array.size + + while low_index < high_index + mid = (low_index + high_index) / 2 + return mid if array[mid] == query + + if array[low_index] <= query && query < array[mid] + high_index = mid + elsif array[mid] < query && query <= array[high_index - 1] + low_index = mid + else + break + end + end + + -1 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 diff --git a/test/exercise/fp/solution.rb b/test/exercise/fp/solution.rb index 91c40040..811488c8 100644 --- a/test/exercise/fp/solution.rb +++ b/test/exercise/fp/solution.rb @@ -4,12 +4,15 @@ class << self # Обратиться к параметрам фильма можно так: # film["name"], film["rating_kinopoisk"], film["rating_imdb"], # film["genres"], film["year"], film["access_level"], film["country"] - def rating(_array) - 0 + def rating(array) + ratings = array.map { |a| { rating: (a['rating_kinopoisk'] || 0).to_f, countries: (a['country'] || '').split(',').size } } + .select { |a| a[:countries] >= 2 && (a[:rating]).positive? }.map { |a| a[:rating] } + ratings.reduce(:+) / ratings.size end - def chars_count(_films, _threshold) - 0 + def chars_count(films, threshold) + films.select { |m| m['rating_kinopoisk'] && m['rating_kinopoisk'].to_f >= threshold } + .map { |m| m['name'].count('и') }.reduce(:+) end end end diff --git a/test/exercise/fp/test.rb b/test/exercise/fp/test.rb index c1554768..8dbd42da 100644 --- a/test/exercise/fp/test.rb +++ b/test/exercise/fp/test.rb @@ -7,10 +7,10 @@ class Exercise::FpTest < Minitest::Test # Посчитать средний рейтинг фильмов по версии кинопоиска у которых две или больше стран # Фильмы у которых рейтиг не задан или равен 0 не учитывать в расчете среднего. def test_rating - skip array = CSV.readlines('./test/fixtures/films.csv', headers: true) result = Exercise::Fp.rating(array) + # rubocop:disable Lint/FloatComparison assert result == 6.809410385259628 # rubocop:enable Lint/FloatComparison @@ -18,7 +18,6 @@ def test_rating # Посчитать количесвто букв 'и' в названиях всех фильмов с рейтингом кинопоиска больше или равным заданному значению def test_chars_count - skip array = CSV.readlines('./test/fixtures/films.csv', headers: true) result = Exercise::Fp.chars_count(array, 5) diff --git a/test/exercise/fp2/solution.rb b/test/exercise/fp2/solution.rb index 8104f35c..2b4ea87a 100644 --- a/test/exercise/fp2/solution.rb +++ b/test/exercise/fp2/solution.rb @@ -5,16 +5,58 @@ class MyArray < Array # Использовать свои написанные функции для реализации следующих - можно. # Написать свою функцию my_each - def my_each; end + def my_each + return self unless block_given? + + i = 0 + while i < size + yield self[i] + i += 1 + end + self + end # Написать свою функцию my_map - def my_map; end + def my_map + return self unless block_given? + + result = MyArray.new + i = 0 + while i < size + result << (yield self[i]) + i += 1 + end + result + end # Написать свою функцию my_compact - def my_compact; end + def my_compact + result = MyArray.new + i = 0 + while i < size + result << self[i] unless self[i].nil? + i += 1 + end + result + end # Написать свою функцию my_reduce - def my_reduce; end + def my_reduce(acc = nil) + return nil unless block_given? + return nil if empty? + + i = 0 + if acc.nil? + acc = self[0] + i = 1 + end + + while i < size + acc = (yield acc, self[i]) + i += 1 + end + acc + end end end end diff --git a/test/exercise/fp2/test.rb b/test/exercise/fp2/test.rb index accb11fc..9b7d7f17 100644 --- a/test/exercise/fp2/test.rb +++ b/test/exercise/fp2/test.rb @@ -11,7 +11,6 @@ def setup end def test_my_each - skip result = [] my_result = [] @@ -23,14 +22,12 @@ def test_my_each end def test_my_map - skip func = ->(element) { element * @int } assert @array.map(&func) == @my_array.my_map(&func) assert @array.map(&func).map(&func) == @my_array.my_map(&func).my_map(&func) end def test_my_compact - skip func = ->(element) { element if element.even? } func_another = ->(element) { element * @int } func_yet_another = ->(element) { element.even? } @@ -40,7 +37,6 @@ def test_my_compact end def test_my_reduce - skip func = ->(acc, element) { acc * element } assert @array.reduce(&func) == @my_array.my_reduce(&func)