Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve Plutakhin tests #42

Open
wants to merge 5 commits 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
20 changes: 20 additions & 0 deletions test/plutakhin/arrays/solution.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module Plutakhin
module Arrays
class << self
def replace(array)
max = array.max
array.map { |a| a >= 0 ? max : a }
end

def search(array, query, from = 0, to = array.length)
return -1 if from == to
mid = (from + to) / 2
value = array[mid]
return mid if value == query

query < value ? to = mid : from = mid + 1
search(array, query, from, to)
end
end
end
end
26 changes: 26 additions & 0 deletions test/plutakhin/arrays/test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require './test/test_helper.rb'
require_relative './solution.rb'

class Plutakhin::ArraysTest < Minitest::Test
# Заменить все положительные элементы целочисленного массива на максимальное значение элементов массива.
def test_replace
array = [3, 2, -8, 4, 100, -6, 7, 8, -99]
new_array = Plutakhin::Arrays.replace(array)

assert new_array == [100, 100, -8, 100, 100, -6, 100, 100, -99]
end

# Реализовать бинарный поиск
# Функция должна возвращать индекс элемента
def test_bin_search
assert Plutakhin::Arrays.search([1], 900) == -1
assert Plutakhin::Arrays.search([1], 1) == 0
assert Plutakhin::Arrays.search([], 900) == -1
assert Plutakhin::Arrays.search([1, 4, 5, 7, 8, 9], 9) == 5
assert Plutakhin::Arrays.search([1, 4, 5, 7, 8, 9], 1) == 0
assert Plutakhin::Arrays.search([1, 4, 5, 7, 8, 9], 6) == -1

array = (1..10000).to_a
assert Plutakhin::Arrays.search(array, array[1000]) == 1000
end
end
22 changes: 22 additions & 0 deletions test/plutakhin/fp/solution.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module Plutakhin
module Fp
class << self
# Обратиться к параметрам фильма можно так:
# film["name"], film["rating_kinopoisk"], film["rating_imdb"],
# film["genres"], film["year"], film["access_level"], film["country"]
def rating(films)
ratios = films.map do |film|
ratio = film['rating_kinopoisk'].to_f
country = film['country']
ratio if !ratio.zero? && !country.nil? && country.split(',').count > 1
end
ratios.compact!
ratios.reduce(:+) / ratios.count
end

def chars_count(films, threshold)
films.inject(0) { |a, e| e['rating_kinopoisk'].to_i >= threshold ? a + e['name'].count('и') : a }
end
end
end
end
25 changes: 25 additions & 0 deletions test/plutakhin/fp/test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'csv'
require './test/test_helper.rb'
require_relative './solution.rb'

class Plutakhin::FpTest < Minitest::Test
# Посчитать средний рейтинг фильмов по версии кинопоиска у которых две или больше стран
# Фильмы у которых рейтиг не задан или равен 0 не учитывать в расчете среднего.
def test_rating
array = CSV.readlines('./test/fixtures/films.csv', headers: true)

result = Plutakhin::Fp.rating(array)
assert result == 6.809410385259628
end

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

result = Plutakhin::Fp.chars_count(array, 5)
assert result == 891
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

да, значения в тесте неправильные, нужны 3966 и 42


result = Plutakhin::Fp.chars_count(array, 8.5)
assert result == 1
end
end
47 changes: 47 additions & 0 deletions test/plutakhin/fp2/solution.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module Plutakhin
module Fp2
class MyArray < Array
# Использовать стандартные функции массива для решения задач нельзя.
# Использовать свои написанные функции для реализации следующих - можно.

# Написать свою функцию my_each
def my_each
each do |elm|
yield(elm)
end
end

# Написать свою функцию my_map
def my_map
array = MyArray.new
my_each do |elm|
array << yield(elm)
end
array
end

# Написать свою функцию my_compact
def my_compact
array = MyArray.new
my_each do |elm|
array << elm unless elm.nil?
end
array
end

# Написать свою функцию my_reduce
def my_reduce(acc = nil)
if acc.nil?
ignore_first = true
acc = first
end
index = 0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

можно порефакторить и не использовать никаких счетчиков. также предлагаю пользоваться своими функциями типа my_each

each do |elm|
acc = yield(acc, elm) unless ignore_first && index == 0
index += 1
end
acc
end
end
end
end
45 changes: 45 additions & 0 deletions test/plutakhin/fp2/test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require 'csv'
require './test/test_helper.rb'
require_relative './solution.rb'

class Plutakhin::Fp2Test < Minitest::Test
def setup
@array = generate :array
@my_array = Plutakhin::Fp2::MyArray.new(@array)
@int = generate :int
end

def test_my_each
result = []
my_result = []

func = -> (element) { result << element if element.odd? }
my_func = -> (element) { my_result << element if element.odd? }

assert @array.each(&func) == @my_array.my_each(&my_func)
assert result == my_result
end

def test_my_map
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
func = -> (element) { element if element.even? }
func_another = -> (element) { element * @int }
func_yet_another = -> (element) { element.even? }
assert @array.map(&func).compact == @my_array.my_map(&func).my_compact
assert @array.map(&func).compact.map(&func_another) == @my_array.my_map(&func).my_compact.my_map(&func_another)
assert @array.map(&func_yet_another).compact == @my_array.my_map(&func_yet_another).my_compact
end

def test_my_reduce
func = -> (acc, element) { acc * element }

assert @array.reduce(&func) == @my_array.my_reduce(&func)
assert @array.reduce(2, &func) == @my_array.my_reduce(2, &func)
assert @array.reduce(&:+) == @my_array.my_reduce(&:+)
end
end