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

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

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

Резултати

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

Код

class NumberSet
include Enumerable
def initialize
@numbers = []
end
def <<(number)
if not contains_number?(number)
@numbers << number
end
end
def [](filter)
filter_matches = @numbers.select { |number| filter.is_match? number }
NumberSet.new.merge filter_matches
end
def merge(array)
array.each { |number| @numbers << number }
end
def size
@numbers.size
end
def empty?
@numbers.size === 0
end
def each
@numbers.each { |number| yield number }
end
private
def contains_number?(number)
@numbers.any? { |current_number| number == current_number }
end
end
class Filter
def initialize(&block)
@block = block
end
def is_match?(number)
@block.call number
end
def &(other)
Filter.new { |number| self.is_match? number and other.is_match? number }
end
def |(other)
Filter.new { |number| self.is_match? number or other.is_match? number }
end
end
class TypeFilter < Filter
@@types = {
integer: Integer,
real: Float,
complex: Complex,
}
def initialize(type)
type = @@types.fetch type
super() { |number| number.is_a? type }
end
end
class SignFilter < Filter
@@sign_filters = {
positive: Proc.new { |number| number > 0 },
negative: Proc.new { |number| number < 0 },
non_negative: Proc.new { |number| number >= 0 },
non_positive: Proc.new { |number| number <= 0 },
}
def initialize(sign)
sign_filter = @@sign_filters.fetch(sign)
super(&sign_filter)
end
end

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

..........F............F

Failures:

  1) NumberSet can filter by real type
     Failure/Error: expect(filtered_numbers.size).to eq expecting.size
       
       expected: 2
            got: 1
       
       (compared using ==)
     # /tmp/d20141028-18133-1xl7g5w/spec.rb:180:in `can_filter'
     # /tmp/d20141028-18133-1xl7g5w/spec.rb:55: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 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-1xl7g5w/solution.rb:32:in `block in each'
     # /tmp/d20141028-18133-1xl7g5w/solution.rb:32:in `each'
     # /tmp/d20141028-18133-1xl7g5w/solution.rb:32:in `each'
     # /tmp/d20141028-18133-1xl7g5w/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.02225 seconds
24 examples, 2 failures

Failed examples:

rspec /tmp/d20141028-18133-1xl7g5w/spec.rb:54 # NumberSet can filter by real type
rspec /tmp/d20141028-18133-1xl7g5w/spec.rb:159 # NumberSet returns enumerable of set's contents if no block is given to each

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

Делян обнови решението на 27.10.2014 15:12 (преди около 10 години)

+class NumberSet
+ include Enumerable
+
+ def initialize
+ @numbers = []
+ end
+
+ def <<(number)
+ if not contains_number?(number)
+ @numbers << number
+ end
+ end
+
+ def [](filter)
+ filter_matches = @numbers.select { |number| filter.is_match? number }
+ NumberSet.new.merge filter_matches
+ end
+
+ def merge(array)
+ array.each { |number| @numbers << number }
+ end
+
+ def size
+ @numbers.size
+ end
+
+ def empty?
+ @numbers.size === 0
+ end
+
+ def each
+ @numbers.each { |number| yield number }
+ end
+
+ private
+
+ def contains_number?(number)
+ @numbers.any? { |current_number| number == current_number }
+ end
+end
+
+class Filter
+ def initialize(&block)
+ @block = block
+ end
+
+ def is_match?(number)
+ @block.call number
+ end
+
+ def &(other)
+ Filter.new { |number| self.is_match? number and other.is_match? number }
+ end
+
+ def |(other)
+ Filter.new { |number| self.is_match? number or other.is_match? number }
+ end
+end
+
+class TypeFilter < Filter
+ @@types = {
+ integer: Integer,
+ real: Float,
+ complex: Complex,
+ }
+
+ def initialize(type)
+ type = @@types.fetch type
+ super() { |number| number.is_a? type }
+ end
+end
+
+class SignFilter < Filter
+ @@sign_filters = {
+ positive: Proc.new { |number| number > 0 },
+ negative: Proc.new { |number| number < 0 },
+ non_negative: Proc.new { |number| number >= 0 },
+ non_positive: Proc.new { |number| number <= 0 },
+ }
+
+ def initialize(sign)
+ sign_filter = @@sign_filters.fetch(sign)
+ super(&sign_filter)
+ end
+end