Решение на Втора задача от Константин Димитров

Обратно към всички решения

Към профила на Константин Димитров

Резултати

  • 6 точки от тестове
  • 0 бонус точки
  • 6 точки общо
  • 24 успешни тест(а)
  • 0 неуспешни тест(а)

Код

class NumberSet
include Enumerable
attr_accessor :set
def initialize
@set = []
end
def <<(value)
@set << value unless @set.include? value
end
def size
@set.size
end
def empty?
@set.empty?
end
def each(&block)
@set.each(&block)
end
def [](filter)
@set.inject(NumberSet.new) do |new_set, current_item|
new_set << current_item if filter.block.call(current_item)
new_set
end
end
end
class Filter
attr_reader :block
def initialize(&block)
@block = block
end
def &(other_filter)
Filter.new { |item| @block.call(item) & other_filter.block.call(item) }
end
def |(other_filter)
Filter.new { |item| @block.call(item) | other_filter.block.call(item) }
end
end
class TypeFilter < Filter
def initialize(type)
@block = get_filter(type).block
end
def get_filter(type)
case type
when :integer then Filter.new { |item| item.is_a? Integer }
when :real then Filter.new do
|item| item.is_a? Float or item.is_a? Rational
end
when :complex then Filter.new { |item| item.is_a? Complex }
end
end
end
class SignFilter < Filter
def initialize(type)
@block = get_filter(type).block
end
def get_filter(type)
case type
when :positive then Filter.new { |item| item > 0 }
when :non_positive then Filter.new { |item| item <= 0 }
when :negative then Filter.new { |item| item < 0 }
when :non_negative then Filter.new { |item| item >= 0 }
end
end
end

Лог от изпълнението

........................

Finished in 0.02197 seconds
24 examples, 0 failures

История (1 версия и 1 коментар)

Константин обнови решението на 26.10.2014 17:57 (преди над 9 години)

+class NumberSet
+ include Enumerable
+ attr_accessor :set
+ def initialize
+ @set = []
+ end
+
+ def <<(value)
+ @set << value unless @set.include? value
+ end
+
+ def size
+ @set.size
+ end
+
+ def empty?
+ @set.empty?
+ end
+
+ def each(&block)
+ @set.each(&block)
+ end
+
+ def [](filter)
+ @set.inject(NumberSet.new) do |new_set, current_item|
+ new_set << current_item if filter.block.call(current_item)
+ new_set
+ end
+ end
+end
+
+
+class Filter
+ attr_reader :block
+ def initialize(&block)
+ @block = block
+ end
+
+ def &(other_filter)
+ Filter.new { |item| @block.call(item) & other_filter.block.call(item) }
+ end
+
+ def |(other_filter)
+ Filter.new { |item| @block.call(item) | other_filter.block.call(item) }
+ end
+end
+
+
+class TypeFilter < Filter
+ def initialize(type)
+ @block = get_filter(type).block
+ end
+
+ def get_filter(type)
+ case type
+ when :integer then Filter.new { |item| item.is_a? Integer }
+ when :real then Filter.new do
+ |item| item.is_a? Float or item.is_a? Rational
+ end
+ when :complex then Filter.new { |item| item.is_a? Complex }
+ end
+ end
+end
+
+
+class SignFilter < Filter
+ def initialize(type)
+ @block = get_filter(type).block
+ end
+
+ def get_filter(type)
+ case type
+ when :positive then Filter.new { |item| item > 0 }
+ when :non_positive then Filter.new { |item| item <= 0 }
+ when :negative then Filter.new { |item| item < 0 }
+ when :non_negative then Filter.new { |item| item >= 0 }
+ end
+ end
+end

Здрасти,

Имам няколко коментара по кода ти:

  • attr_accessor :set, attr_reader :block не ти трябват.
  • @set е име на нещо, което всъщност е Array. Можеш ли да измислиш по-добро име в контекста на задачата?
  • Провери в style guide-а конвенцията за наименоване при дефиниране на бинарни оператори.
  • В get_filter можеш да ползваш анонимни функции вместо Filter обекти. Също така можеш директно да подаваш блок на конструктора на Filter чрез super() { |number| ... }, ако изместиш логиката от get_filter в initialize.
  • Направи си метод за block.call(item) във Filter. Не е добре да показваш имплементацията си на всички. Помисли добре за името на този метод. Прецени какво прави и какъв резултат връща.