Решение на Втора задача от Нели Василева

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

Към профила на Нели Василева

Резултати

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

Код

require 'set'
class NumberSet < Set
def [](filter)
self.dup.keep_if(&filter.filter_condition)
end
def <<(number)
number = number.real if number.class.to_s == 'Complex' && number.imag == 0
case
when number.class.to_s == 'Complex' then self.add(number)
when number.class.to_s == 'Rational'
then self.add(number.to_f.denominator == 1 ? number.to_i : number.to_f)
when number.class.to_s == 'Float'
then self.add(number.denominator == 1 ? number.to_i : number)
else self.add(number)
end
end
end
class BasicFilter
attr_reader :filter_condition
#not working
def &(filter)
self && filter
end
def |(filter)
self || filter
end
end
class Filter < BasicFilter
def initialize(&block)
@filter_condition = block
end
end
class TypeFilter < BasicFilter
def initialize(filter)
@filter_condition = case
when filter == :integer
then lambda { |number| number.class == Integer.class ||
number.class == Fixnum.class }
when filter == :real
then lambda { |number| number.class == Rational.class ||
number.class == Float.class }
when filter == :complex then lambda{|number| number.class == Complex.class}
end
end
end
class SignFilter < BasicFilter
def initialize(filter)
@filter_condition = case
when filter == :positive then lambda { |number| number > 0 }
when filter == :non_positive then lambda { |number| number <= 0 }
when filter == :negative then lambda { |number| number < 0 }
when filter == :non_negative then lambda { |number| number >= 0 }
end
end
end

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

...F....FFF.....FFFFFF..

Failures:

  1) NumberSet can store rational numbers
     Failure/Error: expect(numbers).to include and_get_it_as
       expected #<NumberSet: {3.142857142857143}> to include (22/7)
     # /tmp/d20141028-18133-1o63gpu/spec.rb:171:in `can_store'
     # /tmp/d20141028-18133-1o63gpu/spec.rb:15: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 complex type
     Failure/Error: expect(filtered_numbers.size).to eq expecting.size
       
       expected: 1
            got: 0
       
       (compared using ==)
     # /tmp/d20141028-18133-1o63gpu/spec.rb:180:in `can_filter'
     # /tmp/d20141028-18133-1o63gpu/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)>'

  3) NumberSet can filter by integer type
     Failure/Error: expect(filtered_numbers.size).to eq expecting.size
       
       expected: 1
            got: 0
       
       (compared using ==)
     # /tmp/d20141028-18133-1o63gpu/spec.rb:180:in `can_filter'
     # /tmp/d20141028-18133-1o63gpu/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)>'

  4) NumberSet can filter by real type
     Failure/Error: expect(filtered_numbers.size).to eq expecting.size
       
       expected: 2
            got: 0
       
       (compared using ==)
     # /tmp/d20141028-18133-1o63gpu/spec.rb:180:in `can_filter'
     # /tmp/d20141028-18133-1o63gpu/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)>'

  5) NumberSet can combine two filters with "and" rule
     Failure/Error: expect(filtered_numbers.size).to eq expecting.size
       
       expected: 1
            got: 2
       
       (compared using ==)
     # /tmp/d20141028-18133-1o63gpu/spec.rb:180:in `can_filter'
     # /tmp/d20141028-18133-1o63gpu/spec.rb:92: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 two filters with "or" rule
     Failure/Error: expect(filtered_numbers.size).to eq expecting.size
       
       expected: 7
            got: 5
       
       (compared using ==)
     # /tmp/d20141028-18133-1o63gpu/spec.rb:180:in `can_filter'
     # /tmp/d20141028-18133-1o63gpu/spec.rb:99: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 combine multiple filters with "and" rule
     Failure/Error: expect(filtered_numbers.size).to eq expecting.size
       
       expected: 5
            got: 7
       
       (compared using ==)
     # /tmp/d20141028-18133-1o63gpu/spec.rb:180:in `can_filter'
     # /tmp/d20141028-18133-1o63gpu/spec.rb:109: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 combine multiple filters with "or" rule
     Failure/Error: expect(filtered_numbers.size).to eq expecting.size
       
       expected: 11
            got: 7
       
       (compared using ==)
     # /tmp/d20141028-18133-1o63gpu/spec.rb:180:in `can_filter'
     # /tmp/d20141028-18133-1o63gpu/spec.rb:119: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 multiple filters with "and" and "or" rules
     Failure/Error: expect(filtered_numbers.size).to eq expecting.size
       
       expected: 8
            got: 3
       
       (compared using ==)
     # /tmp/d20141028-18133-1o63gpu/spec.rb:180:in `can_filter'
     # /tmp/d20141028-18133-1o63gpu/spec.rb:129: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 multiple filters with "and", "or" and parenthesis
     Failure/Error: expect(filtered_numbers.size).to eq expecting.size
       
       expected: 4
            got: 3
       
       (compared using ==)
     # /tmp/d20141028-18133-1o63gpu/spec.rb:180:in `can_filter'
     # /tmp/d20141028-18133-1o63gpu/spec.rb:139: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.02493 seconds
24 examples, 10 failures

Failed examples:

rspec /tmp/d20141028-18133-1o63gpu/spec.rb:14 # NumberSet can store rational numbers
rspec /tmp/d20141028-18133-1o63gpu/spec.rb:42 # NumberSet can filter by complex type
rspec /tmp/d20141028-18133-1o63gpu/spec.rb:48 # NumberSet can filter by integer type
rspec /tmp/d20141028-18133-1o63gpu/spec.rb:54 # NumberSet can filter by real type
rspec /tmp/d20141028-18133-1o63gpu/spec.rb:90 # NumberSet can combine two filters with "and" rule
rspec /tmp/d20141028-18133-1o63gpu/spec.rb:97 # NumberSet can combine two filters with "or" rule
rspec /tmp/d20141028-18133-1o63gpu/spec.rb:104 # NumberSet can combine multiple filters with "and" rule
rspec /tmp/d20141028-18133-1o63gpu/spec.rb:114 # NumberSet can combine multiple filters with "or" rule
rspec /tmp/d20141028-18133-1o63gpu/spec.rb:124 # NumberSet can combine multiple filters with "and" and "or" rules
rspec /tmp/d20141028-18133-1o63gpu/spec.rb:134 # NumberSet can combine multiple filters with "and", "or" and parenthesis

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

Нели обнови решението на 27.10.2014 16:00 (преди около 10 години)

+require 'set'
+class NumberSet < Set
+ def [](filter)
+ self.dup.keep_if(&filter.filter_condition)
+ end
+
+ def <<(number)
+ number = number.real if number.class.to_s == 'Complex' && number.imag == 0
+ case
+ when number.class.to_s == 'Complex' then self.add(number)
+ when number.class.to_s == 'Rational'
+ then self.add(number.to_f.denominator == 1 ? number.to_i : number.to_f)
+ when number.class.to_s == 'Float'
+ then self.add(number.denominator == 1 ? number.to_i : number)
+ else self.add(number)
+ end
+ end
+end
+
+class BasicFilter
+ attr_reader :filter_condition
+ #not working
+ def &(filter)
+ self && filter
+ end
+ def |(filter)
+ self || filter
+ end
+end
+
+class Filter < BasicFilter
+ def initialize(&block)
+ @filter_condition = block
+ end
+end
+
+class TypeFilter < BasicFilter
+ def initialize(filter)
+ @filter_condition = case
+ when filter == :integer
+ then lambda { |number| number.class == Integer.class ||
+ number.class == Fixnum.class }
+ when filter == :real
+ then lambda { |number| number.class == Rational.class ||
+ number.class == Float.class }
+ when filter == :complex then lambda{|number| number.class == Complex.class}
+ end
+ end
+end
+
+class SignFilter < BasicFilter
+ def initialize(filter)
+ @filter_condition = case
+ when filter == :positive then lambda { |number| number > 0 }
+ when filter == :non_positive then lambda { |number| number <= 0 }
+ when filter == :negative then lambda { |number| number < 0 }
+ when filter == :non_negative then lambda { |number| number >= 0 }
+ end
+ end
+end