Решение на Втора задача от Ивайло Георгиев

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

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

Резултати

  • 4 точки от тестове
  • 0 бонус точки
  • 4 точки общо
  • 17 успешни тест(а)
  • 7 неуспешни тест(а)

Код

class NumberSet
include Enumerable
def initialize(numbers_list = [])
@numbers_list = numbers_list
end
def each
@numbers_list.each { |member| yield member }
end
def << (number)
@numbers_list.push(number) unless @numbers_list.include?(number)
end
def [](filter)
new_list = NumberSet.new(filter[@numbers_list])
end
def size
@numbers_list.length
end
def empty?
@numbers_list.empty?
end
end
class SignFilter
def initialize(sign)
@sign = sign
end
def [](array)
return array.find_all{ |v| v > 0 } if @sign == :positive
return array.find_all{ |v| v <= 0} if @sign == :non_positive
return array.find_all{ |v| v < 0} if @sign == :negative
array.find_all{ |v| v >= 0} if @sign == :non_negative
end
end
class TypeFilter
def initialize(type)
@type = type
end
def [](array)
return array.find_all{ |i| i.is_a? Integer } if @type == :integer
return array.find_all{ |i| i.is_a? Complex } if @type == :complex
array.find_all{ |i| i.is_a? Float or i.is_a? Rational } if @type == :real
end
end
class Filter
def initialize (&block)
@block = block
end
def [](array)
array.find_all{ |e| @block.call(e) }
end
end

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

................FFFFFF.F

Failures:

  1) NumberSet can combine two filters with "and" rule
     Failure/Error: filter = SignFilter.new(:non_negative) & Filter.new { |number| number != 0 }
     NoMethodError:
       undefined method `&' for #<SignFilter:0xb8e1f434 @sign=:non_negative>
     # /tmp/d20141028-18133-nw5x92/spec.rb:91: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 two filters with "or" rule
     Failure/Error: filter = Filter.new { |number| number % 2 == 0 } | Filter.new { |number| number > 5 }
     NoMethodError:
       undefined method `|' for #<Filter:0xb8e1e8e0>
     # /tmp/d20141028-18133-nw5x92/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)>'

  3) NumberSet can combine multiple filters with "and" rule
     Failure/Error: filter        = non_negative & non_zero & mod_3_is_zero
     NoMethodError:
       undefined method `&' for #<SignFilter:0xb8e1dc10 @sign=:non_negative>
     # /tmp/d20141028-18133-nw5x92/spec.rb:108: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 "or" rule
     Failure/Error: filter        = even | negative | more_than_100
     NoMethodError:
       undefined method `|' for #<Filter:0xb8e1cfe0 @block=#<Proc:0xb8e1d01c>>
     # /tmp/d20141028-18133-nw5x92/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)>'

  5) NumberSet can combine multiple filters with "and" and "or" rules
     Failure/Error: filter        = even & negative | mod_3_is_zero
     NoMethodError:
       undefined method `&' for #<Filter:0xb8e1c4b4 @block=#<Proc:0xb8e1d01c>>
     # /tmp/d20141028-18133-nw5x92/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)>'

  6) NumberSet can combine multiple filters with "and", "or" and parenthesis
     Failure/Error: filter        = even & (negative | mod_3_is_zero)
     NoMethodError:
       undefined method `|' for #<SignFilter:0xb8f07b30 @sign=:negative>
     # /tmp/d20141028-18133-nw5x92/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)>'

  7) 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-nw5x92/solution.rb:9:in `block in each'
     # /tmp/d20141028-18133-nw5x92/solution.rb:9:in `each'
     # /tmp/d20141028-18133-nw5x92/solution.rb:9:in `each'
     # /tmp/d20141028-18133-nw5x92/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.02085 seconds
24 examples, 7 failures

Failed examples:

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

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

Ивайло обнови решението на 27.10.2014 13:55 (преди около 10 години)

+class NumberSet
+ include Enumerable
+
+ def initialize(numbers_list = [])
+ @numbers_list = numbers_list
+ end
+
+ def each
+ @numbers_list.each { |member| yield member }
+ end
+
+ def << (number)
+ @numbers_list.push(number) unless @numbers_list.include?(number)
+ end
+
+ def [](filter)
+ new_list = NumberSet.new(filter[@numbers_list])
+ end
+
+ def size
+ @numbers_list.length
+ end
+
+ def empty?
+ @numbers_list.empty?
+ end
+
+end
+
+class SignFilter
+
+ def initialize(sign)
+ @sign = sign
+ end
+
+ def [](array)
+ return array.find_all{ |v| v > 0 } if @sign == :positive
+ return array.find_all{ |v| v <= 0} if @sign == :non_positive
+ return array.find_all{ |v| v < 0} if @sign == :negative
+ array.find_all{ |v| v >= 0} if @sign == :non_negative
+ end
+
+end
+
+class TypeFilter
+
+ def initialize(type)
+ @type = type
+ end
+
+ def [](array)
+ return array.find_all{ |i| i.is_a? Integer } if @type == :integer
+ return array.find_all{ |i| i.is_a? Complex } if @type == :complex
+ array.find_all{ |i| i.is_a? Float or i.is_a? Rational } if @type == :real
+ end
+
+end
+
+class Filter
+
+ def initialize (&block)
+ @block = block
+ end
+
+ def [](array)
+ array.find_all{ |e| @block.call(e) }
+ end
+
+end