Решение на Втора задача от София Петрова

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

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

Резултати

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

Код

module LogicalOperators
class Criteria
def initialize(&condition)
@condition = condition
end
def meets?(collection)
@condition.(collection)
end
def |(criteria)
Criteria.new { |collection| meets? collection or criteria.meets? collection}
end
def &(criteria)
Criteria.new { |collection| meets? collection and criteria.meets? collection}
end
end
end
class NumberSet
include Enumerable
include LogicalOperators
attr_accessor :numbers
def initialize(numbers = [])
@numbers = numbers
end
def each
@numbers.each { |iteration| yield iteration }
end
def <<(element)
unless @numbers.include? element
@numbers.push(element)
end
end
def size
numbers.size
end
def empty?
numbers.empty?
end
def [](argument)
case argument
when "Filter" then Filter.new.filter(numbers)
when "TypeFilter" then TypeFilter.new.type_filter(numbers)
when "SignFilter" then SignFilter.new.sign_filter(numbers)
end
end
end
class Filter
def filter(numbers)
result = []
each { |element| result << element if yield(element) }
result
end
end

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

........FFFFFFFFFFFFFF.F

Failures:

  1) NumberSet can filter by complex type
     Failure/Error: can_filter [0.3+2i, 3.14, 11], using: TypeFilter.new(:complex),
     NameError:
       uninitialized constant TypeFilter
     # /tmp/d20141028-18133-gml00c/spec.rb:43: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 filter by integer type
     Failure/Error: can_filter [Rational(5, 2), 8.0, 7, Rational(9, 1)], using: TypeFilter.new(:integer),
     NameError:
       uninitialized constant TypeFilter
     # /tmp/d20141028-18133-gml00c/spec.rb:49: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 filter by real type
     Failure/Error: can_filter [Rational(5, 2), 8.3, 7], using: TypeFilter.new(:real),
     NameError:
       uninitialized constant TypeFilter
     # /tmp/d20141028-18133-gml00c/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)>'

  4) NumberSet can filter by custom filter
     Failure/Error: expect(filtered_numbers.size).to eq expecting.size
     NoMethodError:
       undefined method `size' for nil:NilClass
     # /tmp/d20141028-18133-gml00c/spec.rb:180:in `can_filter'
     # /tmp/d20141028-18133-gml00c/spec.rb:61: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 filter positive numbers
     Failure/Error: can_filter [Rational(-5, 2), 7.6, 0], using: SignFilter.new(:positive),
     NameError:
       uninitialized constant SignFilter
     # /tmp/d20141028-18133-gml00c/spec.rb:67: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 filter non-positive numbers
     Failure/Error: can_filter [Rational(-5, 2), 7.6, 0], using: SignFilter.new(:non_positive),
     NameError:
       uninitialized constant SignFilter
     # /tmp/d20141028-18133-gml00c/spec.rb:73: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 can filter negative numbers
     Failure/Error: can_filter [Rational(-5, 2), 7.6, 0], using: SignFilter.new(:negative),
     NameError:
       uninitialized constant SignFilter
     # /tmp/d20141028-18133-gml00c/spec.rb:79: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)>'

  8) NumberSet can filter non-negative numbers
     Failure/Error: can_filter [Rational(-5, 2), 7.6, 0], using: SignFilter.new(:non_negative),
     NameError:
       uninitialized constant SignFilter
     # /tmp/d20141028-18133-gml00c/spec.rb:85: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)>'

  9) NumberSet can combine two filters with "and" rule
     Failure/Error: filter = SignFilter.new(:non_negative) & Filter.new { |number| number != 0 }
     NameError:
       uninitialized constant SignFilter
     # /tmp/d20141028-18133-gml00c/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)>'

  10) 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:0xba145a90>
     # /tmp/d20141028-18133-gml00c/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)>'

  11) NumberSet can combine multiple filters with "and" rule
     Failure/Error: non_negative  = SignFilter.new(:non_negative)
     NameError:
       uninitialized constant SignFilter
     # /tmp/d20141028-18133-gml00c/spec.rb:105: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)>'

  12) NumberSet can combine multiple filters with "or" rule
     Failure/Error: negative      = SignFilter.new(:negative)
     NameError:
       uninitialized constant SignFilter
     # /tmp/d20141028-18133-gml00c/spec.rb:116: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)>'

  13) NumberSet can combine multiple filters with "and" and "or" rules
     Failure/Error: negative      = SignFilter.new(:negative)
     NameError:
       uninitialized constant SignFilter
     # /tmp/d20141028-18133-gml00c/spec.rb:126: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)>'

  14) NumberSet can combine multiple filters with "and", "or" and parenthesis
     Failure/Error: negative      = SignFilter.new(:negative)
     NameError:
       uninitialized constant SignFilter
     # /tmp/d20141028-18133-gml00c/spec.rb:136: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)>'

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

Failed examples:

rspec /tmp/d20141028-18133-gml00c/spec.rb:42 # NumberSet can filter by complex type
rspec /tmp/d20141028-18133-gml00c/spec.rb:48 # NumberSet can filter by integer type
rspec /tmp/d20141028-18133-gml00c/spec.rb:54 # NumberSet can filter by real type
rspec /tmp/d20141028-18133-gml00c/spec.rb:60 # NumberSet can filter by custom filter
rspec /tmp/d20141028-18133-gml00c/spec.rb:66 # NumberSet can filter positive numbers
rspec /tmp/d20141028-18133-gml00c/spec.rb:72 # NumberSet can filter non-positive numbers
rspec /tmp/d20141028-18133-gml00c/spec.rb:78 # NumberSet can filter negative numbers
rspec /tmp/d20141028-18133-gml00c/spec.rb:84 # NumberSet can filter non-negative numbers
rspec /tmp/d20141028-18133-gml00c/spec.rb:90 # NumberSet can combine two filters with "and" rule
rspec /tmp/d20141028-18133-gml00c/spec.rb:97 # NumberSet can combine two filters with "or" rule
rspec /tmp/d20141028-18133-gml00c/spec.rb:104 # NumberSet can combine multiple filters with "and" rule
rspec /tmp/d20141028-18133-gml00c/spec.rb:114 # NumberSet can combine multiple filters with "or" rule
rspec /tmp/d20141028-18133-gml00c/spec.rb:124 # NumberSet can combine multiple filters with "and" and "or" rules
rspec /tmp/d20141028-18133-gml00c/spec.rb:134 # NumberSet can combine multiple filters with "and", "or" and parenthesis
rspec /tmp/d20141028-18133-gml00c/spec.rb:159 # NumberSet returns enumerable of set's contents if no block is given to each

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

София обнови решението на 27.10.2014 16:50 (преди около 10 години)

+module LogicalOperators
+ class Criteria
+ def initialize(&condition)
+ @condition = condition
+ end
+
+ def meets?(collection)
+ @condition.(collection)
+ end
+
+ def |(criteria)
+ Criteria.new { |collection| meets? collection or criteria.meets? collection}
+ end
+
+ def &(criteria)
+ Criteria.new { |collection| meets? collection and criteria.meets? collection}
+ end
+ end
+end
+
+class NumberSet
+ include Enumerable
+ include LogicalOperators
+ attr_accessor :numbers
+
+ def initialize(numbers = [])
+ @numbers = numbers
+ end
+
+ def each
+ @numbers.each { |iteration| yield iteration }
+ end
+
+ def <<(element)
+ unless @numbers.include? element
+ @numbers.push(element)
+ end
+ end
+
+ def size
+ numbers.size
+ end
+
+ def empty?
+ numbers.empty?
+ end
+
+ def [](argument)
+ case argument
+ when "Filter" then Filter.new.filter(numbers)
+ when "TypeFilter" then TypeFilter.new.type_filter(numbers)
+ when "SignFilter" then SignFilter.new.sign_filter(numbers)
+ end
+ end
+end
+
+class Filter
+ def filter(numbers)
+ result = []
+ each { |element| result << element if yield(element) }
+ result
+ end
+end