From 5c6a8ec4a8a7234e576e2fc72aaab300a1bbbf8f Mon Sep 17 00:00:00 2001 From: Pavel Plutakhin Date: Mon, 27 Mar 2017 17:53:28 +0400 Subject: [PATCH 1/5] prepare plutkhin tests --- .gitignore | 1 + test/plutakhin/arrays/solution.rb | 14 +++++++++ test/plutakhin/arrays/test.rb | 28 ++++++++++++++++++ test/plutakhin/fp/solution.rb | 16 ++++++++++ test/plutakhin/fp/test.rb | 27 +++++++++++++++++ test/plutakhin/fp2/solution.rb | 24 +++++++++++++++ test/plutakhin/fp2/test.rb | 49 +++++++++++++++++++++++++++++++ 7 files changed, 159 insertions(+) create mode 100644 test/plutakhin/arrays/solution.rb create mode 100644 test/plutakhin/arrays/test.rb create mode 100644 test/plutakhin/fp/solution.rb create mode 100644 test/plutakhin/fp/test.rb create mode 100644 test/plutakhin/fp2/solution.rb create mode 100644 test/plutakhin/fp2/test.rb diff --git a/.gitignore b/.gitignore index 469773b..07928dc 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ /spec/reports/ /tmp/ /log/ +/.idea \ No newline at end of file diff --git a/test/plutakhin/arrays/solution.rb b/test/plutakhin/arrays/solution.rb new file mode 100644 index 0000000..bc1f1a2 --- /dev/null +++ b/test/plutakhin/arrays/solution.rb @@ -0,0 +1,14 @@ +module Plutakhin + module Arrays + class << self + def replace(array) + array + end + + def search(array, query) + 0 + end + end + end +end + diff --git a/test/plutakhin/arrays/test.rb b/test/plutakhin/arrays/test.rb new file mode 100644 index 0000000..4bf23ec --- /dev/null +++ b/test/plutakhin/arrays/test.rb @@ -0,0 +1,28 @@ +require './test/test_helper.rb' +require_relative './solution.rb' + +class Plutakhin::ArraysTest < Minitest::Test + # Заменить все положительные элементы целочисленного массива на максимальное значение элементов массива. + def test_replace + skip + 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 + skip + 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 diff --git a/test/plutakhin/fp/solution.rb b/test/plutakhin/fp/solution.rb new file mode 100644 index 0000000..d069dcb --- /dev/null +++ b/test/plutakhin/fp/solution.rb @@ -0,0 +1,16 @@ +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(_array) + 0 + end + + def chars_count(_films, _threshold) + 0 + end + end + end +end diff --git a/test/plutakhin/fp/test.rb b/test/plutakhin/fp/test.rb new file mode 100644 index 0000000..64f70b5 --- /dev/null +++ b/test/plutakhin/fp/test.rb @@ -0,0 +1,27 @@ +require 'csv' +require './test/test_helper.rb' +require_relative './solution.rb' + +class Plutakhin::FpTest < Minitest::Test + # Посчитать средний рейтинг фильмов по версии кинопоиска у которых две или больше стран + # Фильмы у которых рейтиг не задан или равен 0 не учитывать в расчете среднего. + def test_rating + skip + array = CSV.readlines('./test/fixtures/films.csv', headers: true) + + result = Plutakhin::Fp.rating(array) + assert result == 6.809410385259628 + end + + # Посчитать количесвто букв 'и' в названиях всех фильмов с рейтингом кинопоиска больше или равным заданному значению + def test_chars_count + skip + array = CSV.readlines('./test/fixtures/films.csv', headers: true) + + result = Plutakhin::Fp.chars_count(array, 5) + assert result == 891 + + result = Plutakhin::Fp.chars_count(array, 8.5) + assert result == 1 + end +end diff --git a/test/plutakhin/fp2/solution.rb b/test/plutakhin/fp2/solution.rb new file mode 100644 index 0000000..9e2508f --- /dev/null +++ b/test/plutakhin/fp2/solution.rb @@ -0,0 +1,24 @@ +module Plutakhin + module Fp2 + class MyArray < Array + # Использовать стандартные функции массива для решения задач нельзя. + # Использовать свои написанные функции для реализации следующих - можно. + + # Написать свою функцию my_each + def my_each + end + + # Написать свою функцию my_map + def my_map + end + + # Написать свою функцию my_compact + def my_compact + end + + # Написать свою функцию my_reduce + def my_reduce + end + end + end +end diff --git a/test/plutakhin/fp2/test.rb b/test/plutakhin/fp2/test.rb new file mode 100644 index 0000000..c2a8d74 --- /dev/null +++ b/test/plutakhin/fp2/test.rb @@ -0,0 +1,49 @@ +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 + skip + 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 + 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? } + 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 + skip + 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 From 47965e47a49d0218115a738c75881af17a680aac Mon Sep 17 00:00:00 2001 From: Pavel Plutakhin Date: Mon, 27 Mar 2017 17:54:35 +0400 Subject: [PATCH 2/5] resolve array tests --- test/plutakhin/arrays/solution.rb | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/test/plutakhin/arrays/solution.rb b/test/plutakhin/arrays/solution.rb index bc1f1a2..e19cb77 100644 --- a/test/plutakhin/arrays/solution.rb +++ b/test/plutakhin/arrays/solution.rb @@ -2,13 +2,23 @@ module Plutakhin module Arrays class << self def replace(array) - array + max = array.max + array.map! { |a| a >= 0 ? max : a } end - def search(array, query) - 0 + 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 + + if query < value + to = mid + else + from = mid + 1 + end + search(array, query, from, to) end end end end - From eabf285c47e5d79a5965a72b5fe356030f7fc4a0 Mon Sep 17 00:00:00 2001 From: Pavel Plutakhin Date: Wed, 29 Mar 2017 18:42:07 +0400 Subject: [PATCH 3/5] resolve fp tests --- test/plutakhin/fp/solution.rb | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/test/plutakhin/fp/solution.rb b/test/plutakhin/fp/solution.rb index d069dcb..be88481 100644 --- a/test/plutakhin/fp/solution.rb +++ b/test/plutakhin/fp/solution.rb @@ -4,12 +4,19 @@ 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(films) + ratios = films.map do |film| + film['rating_kinopoisk'].to_f if !film['rating_kinopoisk'].to_f.zero? && !film['country'].nil? && film['country'].split(',').count > 1 + end . compact + ratios.reduce(:+) / ratios.count end - def chars_count(_films, _threshold) - 0 + def chars_count(films, threshold) + total = 0 + films.map do |film| + total += film['name'].count('и') if film['rating_kinopoisk'].to_i >= threshold + end + total end end end From 70ebd9ca8be264ec199ee80f7d2faef706aaea78 Mon Sep 17 00:00:00 2001 From: Pavel Plutakhin Date: Thu, 30 Mar 2017 10:49:52 +0400 Subject: [PATCH 4/5] resolve fp2 tests --- test/plutakhin/fp2/solution.rb | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/test/plutakhin/fp2/solution.rb b/test/plutakhin/fp2/solution.rb index 9e2508f..80b851d 100644 --- a/test/plutakhin/fp2/solution.rb +++ b/test/plutakhin/fp2/solution.rb @@ -6,18 +6,41 @@ class MyArray < Array # Написать свою функцию my_each def my_each + for elm in self + yield(elm) + end end # Написать свою функцию my_map def my_map + array = MyArray.new + for elm in self + array << yield(elm) + end + array end # Написать свою функцию my_compact def my_compact + array = MyArray.new + for elm in self + array << elm unless elm.nil? + end + array end # Написать свою функцию my_reduce - def my_reduce + def my_reduce(acc = nil) + if acc.nil? + ignore_first = true + acc = first + end + index = 0 + each do |elm| + acc = yield(acc, elm) unless ignore_first && index == 0 + index += 1 + end + acc end end end From 4fc9f91f44b48b785a877233a8713c74f38c4d4c Mon Sep 17 00:00:00 2001 From: Pavel Plutakhin Date: Fri, 31 Mar 2017 15:15:12 +0400 Subject: [PATCH 5/5] update tests --- .gitignore | 1 - test/plutakhin/arrays/solution.rb | 8 ++------ test/plutakhin/arrays/test.rb | 2 -- test/plutakhin/fp/solution.rb | 13 ++++++------- test/plutakhin/fp/test.rb | 2 -- test/plutakhin/fp2/solution.rb | 6 +++--- test/plutakhin/fp2/test.rb | 4 ---- 7 files changed, 11 insertions(+), 25 deletions(-) diff --git a/.gitignore b/.gitignore index 07928dc..469773b 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,3 @@ /spec/reports/ /tmp/ /log/ -/.idea \ No newline at end of file diff --git a/test/plutakhin/arrays/solution.rb b/test/plutakhin/arrays/solution.rb index e19cb77..ca21730 100644 --- a/test/plutakhin/arrays/solution.rb +++ b/test/plutakhin/arrays/solution.rb @@ -3,7 +3,7 @@ module Arrays class << self def replace(array) max = array.max - array.map! { |a| a >= 0 ? max : a } + array.map { |a| a >= 0 ? max : a } end def search(array, query, from = 0, to = array.length) @@ -12,11 +12,7 @@ def search(array, query, from = 0, to = array.length) value = array[mid] return mid if value == query - if query < value - to = mid - else - from = mid + 1 - end + query < value ? to = mid : from = mid + 1 search(array, query, from, to) end end diff --git a/test/plutakhin/arrays/test.rb b/test/plutakhin/arrays/test.rb index 4bf23ec..e952ec9 100644 --- a/test/plutakhin/arrays/test.rb +++ b/test/plutakhin/arrays/test.rb @@ -4,7 +4,6 @@ class Plutakhin::ArraysTest < Minitest::Test # Заменить все положительные элементы целочисленного массива на максимальное значение элементов массива. def test_replace - skip array = [3, 2, -8, 4, 100, -6, 7, 8, -99] new_array = Plutakhin::Arrays.replace(array) @@ -14,7 +13,6 @@ def test_replace # Реализовать бинарный поиск # Функция должна возвращать индекс элемента def test_bin_search - skip assert Plutakhin::Arrays.search([1], 900) == -1 assert Plutakhin::Arrays.search([1], 1) == 0 assert Plutakhin::Arrays.search([], 900) == -1 diff --git a/test/plutakhin/fp/solution.rb b/test/plutakhin/fp/solution.rb index be88481..58d8581 100644 --- a/test/plutakhin/fp/solution.rb +++ b/test/plutakhin/fp/solution.rb @@ -6,17 +6,16 @@ class << self # film["genres"], film["year"], film["access_level"], film["country"] def rating(films) ratios = films.map do |film| - film['rating_kinopoisk'].to_f if !film['rating_kinopoisk'].to_f.zero? && !film['country'].nil? && film['country'].split(',').count > 1 - end . compact + 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) - total = 0 - films.map do |film| - total += film['name'].count('и') if film['rating_kinopoisk'].to_i >= threshold - end - total + films.inject(0) { |a, e| e['rating_kinopoisk'].to_i >= threshold ? a + e['name'].count('и') : a } end end end diff --git a/test/plutakhin/fp/test.rb b/test/plutakhin/fp/test.rb index 64f70b5..a499d57 100644 --- a/test/plutakhin/fp/test.rb +++ b/test/plutakhin/fp/test.rb @@ -6,7 +6,6 @@ class Plutakhin::FpTest < Minitest::Test # Посчитать средний рейтинг фильмов по версии кинопоиска у которых две или больше стран # Фильмы у которых рейтиг не задан или равен 0 не учитывать в расчете среднего. def test_rating - skip array = CSV.readlines('./test/fixtures/films.csv', headers: true) result = Plutakhin::Fp.rating(array) @@ -15,7 +14,6 @@ def test_rating # Посчитать количесвто букв 'и' в названиях всех фильмов с рейтингом кинопоиска больше или равным заданному значению def test_chars_count - skip array = CSV.readlines('./test/fixtures/films.csv', headers: true) result = Plutakhin::Fp.chars_count(array, 5) diff --git a/test/plutakhin/fp2/solution.rb b/test/plutakhin/fp2/solution.rb index 80b851d..61a5418 100644 --- a/test/plutakhin/fp2/solution.rb +++ b/test/plutakhin/fp2/solution.rb @@ -6,7 +6,7 @@ class MyArray < Array # Написать свою функцию my_each def my_each - for elm in self + each do |elm| yield(elm) end end @@ -14,7 +14,7 @@ def my_each # Написать свою функцию my_map def my_map array = MyArray.new - for elm in self + my_each do |elm| array << yield(elm) end array @@ -23,7 +23,7 @@ def my_map # Написать свою функцию my_compact def my_compact array = MyArray.new - for elm in self + my_each do |elm| array << elm unless elm.nil? end array diff --git a/test/plutakhin/fp2/test.rb b/test/plutakhin/fp2/test.rb index c2a8d74..8a905ab 100644 --- a/test/plutakhin/fp2/test.rb +++ b/test/plutakhin/fp2/test.rb @@ -10,7 +10,6 @@ def setup end def test_my_each - skip result = [] my_result = [] @@ -22,14 +21,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? } @@ -39,7 +36,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)