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

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

Към профила на Георги Димов

Резултати

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

Код

module FilterOperators
def call(number)
@predicate.call(number)
end
def &(the_other_filter)
combined_filter = self.class.new(nil)
combined_filter.predicate = Proc.new do |number|
call(number) and the_other_filter.call(number)
end
combined_filter
end
def |(the_other_filter)
combined_filter = self.class.new(nil)
combined_filter.predicate = Proc.new do |number|
call(number) or the_other_filter.call(number)
end
combined_filter
end
def to_s
"#@predicate"
end
end
class NumberSet
include Enumerable
attr_reader :container
def initialize
@container = []
end
def each
@container.each { |member| yield member }
end
def <<(new_member)
@container << new_member if @container.find_index(new_member) == nil
end
def size
@container.size
end
def empty?
@container.empty?
end
def [](filter)
filtered_number_set = NumberSet.new
@container.each do |member|
filtered_number_set << member if filter.call(member)
end
filtered_number_set
end
def to_s
"#@container"
end
end
class Filter
include FilterOperators
attr_accessor :predicate
def initialize(&block)
@predicate = block
end
end
class TypeFilter
include FilterOperators
attr_accessor :predicate
def initialize(numbers_type)
case
when numbers_type == :integer
@predicate = Proc.new { |number| number.is_a?(Integer) }
when numbers_type == :real
@predicate = Proc.new do |number| number.is_a?(Float) or
number.is_a?(Rational)
end
when numbers_type == :complex
@predicate = Proc.new { |number| number.is_a?(Complex) }
end
end
end
class SignFilter
include FilterOperators
attr_accessor :predicate
def initialize(numbers_limitation)
@predicate = case numbers_limitation
when :positive then Proc.new { |number| number > 0 }
when :non_positive then Proc.new { |number| number <= 0 }
when :negative then Proc.new { |number| number < 0 }
when :non_negative then Proc.new { |number| number >= 0 }
end
end
end

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

.................F.FFF.F

Failures:

  1) NumberSet can combine two filters with "or" rule
     Failure/Error: filter = Filter.new { |number| number % 2 == 0 } | Filter.new { |number| number > 5 }
     ArgumentError:
       wrong number of arguments (1 for 0)
     # /tmp/d20141028-18133-frpcy1/solution.rb:74:in `initialize'
     # /tmp/d20141028-18133-frpcy1/solution.rb:17:in `new'
     # /tmp/d20141028-18133-frpcy1/solution.rb:17:in `|'
     # /tmp/d20141028-18133-frpcy1/spec.rb:98:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  2) NumberSet can combine multiple filters with "or" rule
     Failure/Error: filter        = even | negative | more_than_100
     ArgumentError:
       wrong number of arguments (1 for 0)
     # /tmp/d20141028-18133-frpcy1/solution.rb:74:in `initialize'
     # /tmp/d20141028-18133-frpcy1/solution.rb:17:in `new'
     # /tmp/d20141028-18133-frpcy1/solution.rb:17:in `|'
     # /tmp/d20141028-18133-frpcy1/spec.rb:118:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  3) NumberSet can combine multiple filters with "and" and "or" rules
     Failure/Error: filter        = even & negative | mod_3_is_zero
     ArgumentError:
       wrong number of arguments (1 for 0)
     # /tmp/d20141028-18133-frpcy1/solution.rb:74:in `initialize'
     # /tmp/d20141028-18133-frpcy1/solution.rb:7:in `new'
     # /tmp/d20141028-18133-frpcy1/solution.rb:7:in `&'
     # /tmp/d20141028-18133-frpcy1/spec.rb:128:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  4) NumberSet can combine multiple filters with "and", "or" and parenthesis
     Failure/Error: filter        = even & (negative | mod_3_is_zero)
     ArgumentError:
       wrong number of arguments (1 for 0)
     # /tmp/d20141028-18133-frpcy1/solution.rb:74:in `initialize'
     # /tmp/d20141028-18133-frpcy1/solution.rb:7:in `new'
     # /tmp/d20141028-18133-frpcy1/solution.rb:7:in `&'
     # /tmp/d20141028-18133-frpcy1/spec.rb:138:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  5) NumberSet returns enumerable of set's contents if no block is given to each
     Failure/Error: expect(numbers.each.to_a.size).to eq [1, 3, 5].size
     LocalJumpError:
       no block given (yield)
     # /tmp/d20141028-18133-frpcy1/solution.rb:40:in `block in each'
     # /tmp/d20141028-18133-frpcy1/solution.rb:40:in `each'
     # /tmp/d20141028-18133-frpcy1/solution.rb:40:in `each'
     # /tmp/d20141028-18133-frpcy1/spec.rb:164:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

Finished in 0.03307 seconds
24 examples, 5 failures

Failed examples:

rspec /tmp/d20141028-18133-frpcy1/spec.rb:97 # NumberSet can combine two filters with "or" rule
rspec /tmp/d20141028-18133-frpcy1/spec.rb:114 # NumberSet can combine multiple filters with "or" rule
rspec /tmp/d20141028-18133-frpcy1/spec.rb:124 # NumberSet can combine multiple filters with "and" and "or" rules
rspec /tmp/d20141028-18133-frpcy1/spec.rb:134 # NumberSet can combine multiple filters with "and", "or" and parenthesis
rspec /tmp/d20141028-18133-frpcy1/spec.rb:159 # NumberSet returns enumerable of set's contents if no block is given to each

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

Георги обнови решението на 26.10.2014 18:55 (преди около 10 години)

+module FilterOperators
+ def call(number)
+ @predicate.call(number)
+ end
+
+ def &(the_other_filter)
+ combined_filter = self.class.new(nil)
+
+ combined_filter.predicate = Proc.new do |number|
+ call(number) and the_other_filter.call(number)
+ end
+
+ combined_filter
+ end
+
+ def |(the_other_filter)
+ combined_filter = self.class.new(nil)
+
+ combined_filter.predicate = Proc.new do |number|
+ call(number) or the_other_filter.call(number)
+ end
+
+ combined_filter
+ end
+
+ def to_s
+ "#@predicate"
+ end
+end
+
+class NumberSet
+ include Enumerable
+ attr_reader :container
+
+ def initialize
+ @container = []
+ end
+
+ def each
+ @container.each { |member| yield member }
+ end
+
+ def <<(new_member)
+ @container << new_member if @container.find_index(new_member) == nil
+ end
+
+ def size
+ @container.size
+ end
+
+ def empty?
+ @container.empty?
+ end
+
+ def [](filter)
+ filtered_number_set = NumberSet.new
+
+ @container.each do |member|
+ filtered_number_set << member if filter.call(member)
+ end
+
+ filtered_number_set
+ end
+
+ def to_s
+ "#@container"
+ end
+
+end
+
+class Filter
+ include FilterOperators
+ attr_accessor :predicate
+
+ def initialize(&block)
+ @predicate = block
+ end
+end
+
+class TypeFilter
+ include FilterOperators
+ attr_accessor :predicate
+
+ def initialize(numbers_type)
+ case
+ when numbers_type == :integer
+ @predicate = Proc.new { |number| number.is_a?(Integer) }
+ when numbers_type == :real
+ @predicate = Proc.new do |number| number.is_a?(Float) or
+ number.is_a?(Rational)
+ end
+ when numbers_type == :complex
+ @predicate = Proc.new { |number| number.is_a?(Complex) }
+ end
+ end
+end
+
+class SignFilter
+ include FilterOperators
+ attr_accessor :predicate
+
+ def initialize(numbers_limitation)
+ @predicate = case numbers_limitation
+ when :positive then Proc.new { |number| number > 0 }
+ when :non_positive then Proc.new { |number| number <= 0 }
+ when :negative then Proc.new { |number| number < 0 }
+ when :non_negative then Proc.new { |number| number >= 0 }
+ end
+ end
+end

Георги обнови решението на 26.10.2014 20:01 (преди около 10 години)

module FilterOperators
def call(number)
@predicate.call(number)
end
def &(the_other_filter)
combined_filter = self.class.new(nil)
combined_filter.predicate = Proc.new do |number|
call(number) and the_other_filter.call(number)
end
combined_filter
end
def |(the_other_filter)
combined_filter = self.class.new(nil)
combined_filter.predicate = Proc.new do |number|
call(number) or the_other_filter.call(number)
end
combined_filter
end
def to_s
"#@predicate"
end
end
class NumberSet
include Enumerable
attr_reader :container
def initialize
@container = []
end
def each
@container.each { |member| yield member }
end
def <<(new_member)
@container << new_member if @container.find_index(new_member) == nil
end
def size
@container.size
end
def empty?
@container.empty?
end
def [](filter)
filtered_number_set = NumberSet.new
@container.each do |member|
filtered_number_set << member if filter.call(member)
end
filtered_number_set
end
def to_s
"#@container"
end
-
end
class Filter
include FilterOperators
attr_accessor :predicate
def initialize(&block)
@predicate = block
end
end
class TypeFilter
include FilterOperators
attr_accessor :predicate
def initialize(numbers_type)
case
when numbers_type == :integer
@predicate = Proc.new { |number| number.is_a?(Integer) }
when numbers_type == :real
@predicate = Proc.new do |number| number.is_a?(Float) or
number.is_a?(Rational)
end
when numbers_type == :complex
@predicate = Proc.new { |number| number.is_a?(Complex) }
end
end
end
class SignFilter
include FilterOperators
attr_accessor :predicate
def initialize(numbers_limitation)
@predicate = case numbers_limitation
when :positive then Proc.new { |number| number > 0 }
when :non_positive then Proc.new { |number| number <= 0 }
when :negative then Proc.new { |number| number < 0 }
when :non_negative then Proc.new { |number| number >= 0 }
end
end
end