Skip to content
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
23 changes: 20 additions & 3 deletions test/exercise/arrays/solution.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
11 changes: 7 additions & 4 deletions test/exercise/fp/solution.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions test/exercise/fp/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,17 @@ 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
end

# Посчитать количесвто букв 'и' в названиях всех фильмов с рейтингом кинопоиска больше или равным заданному значению
def test_chars_count
skip
array = CSV.readlines('./test/fixtures/films.csv', headers: true)

result = Exercise::Fp.chars_count(array, 5)
Expand Down
50 changes: 46 additions & 4 deletions test/exercise/fp2/solution.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 0 additions & 4 deletions test/exercise/fp2/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ def setup
end

def test_my_each
skip
result = []
my_result = []

Expand All @@ -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? }
Expand All @@ -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)
Expand Down