Решение на Втора задача от Любомир Иванов

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

Към профила на Любомир Иванов

Резултати

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

Код

class NumberSet
include Enumerable
def each(&block)
@container.each(&block)
end
def initialize
@container = []
end
def <<(number)
unless @container.include? number
@container.push(number)
end
end
def empty?
@container.size == 0
end
def size
@container.size
end
def [](filter)
filtered_set = NumberSet.new
self.each do |n|
filtered_set << n if (filter.call n)
end
end
end
class Filter
def initialize(&block)
@filter_proc = Proc.new &block
end
def filtrate?(number)
@filter_proc.call number
end
def self.call_new_filter_proc(number)
@new_filter_proc.call number
end
end
class TypeFilter
def initialize(type)
@type = type
end
def filtrate?(number)
case @type
when :integer
number.is_a? Fixnum
when :real
(number.is_a? Float) || (number.is_a? Rational)
when :complex
number.is_a? Complex
end
end
end
class SignFilter
def initialize(sign_type)
@sign_type = sign_type
end
def filtrate?(number)
case @sign_type
when :positive then number > 0
when :non_negative then number >= 0
when :non_positive then number <= 0
when :negative then number < 0
end
end
end

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

........FFFFFFFFFFFFFF..

Failures:

  1) NumberSet can filter by complex type
     Failure/Error: filtered_numbers = numbers[using]
     NoMethodError:
       undefined method `call' for #<TypeFilter:0xb98c66d0 @type=:complex>
     # /tmp/d20141028-18133-11j3717/solution.rb:29:in `block in []'
     # /tmp/d20141028-18133-11j3717/solution.rb:5:in `each'
     # /tmp/d20141028-18133-11j3717/solution.rb:5:in `each'
     # /tmp/d20141028-18133-11j3717/solution.rb:28:in `[]'
     # /tmp/d20141028-18133-11j3717/spec.rb:179:in `can_filter'
     # /tmp/d20141028-18133-11j3717/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: filtered_numbers = numbers[using]
     NoMethodError:
       undefined method `call' for #<TypeFilter:0xb98c5a3c @type=:integer>
     # /tmp/d20141028-18133-11j3717/solution.rb:29:in `block in []'
     # /tmp/d20141028-18133-11j3717/solution.rb:5:in `each'
     # /tmp/d20141028-18133-11j3717/solution.rb:5:in `each'
     # /tmp/d20141028-18133-11j3717/solution.rb:28:in `[]'
     # /tmp/d20141028-18133-11j3717/spec.rb:179:in `can_filter'
     # /tmp/d20141028-18133-11j3717/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: filtered_numbers = numbers[using]
     NoMethodError:
       undefined method `call' for #<TypeFilter:0xb98c4e20 @type=:real>
     # /tmp/d20141028-18133-11j3717/solution.rb:29:in `block in []'
     # /tmp/d20141028-18133-11j3717/solution.rb:5:in `each'
     # /tmp/d20141028-18133-11j3717/solution.rb:5:in `each'
     # /tmp/d20141028-18133-11j3717/solution.rb:28:in `[]'
     # /tmp/d20141028-18133-11j3717/spec.rb:179:in `can_filter'
     # /tmp/d20141028-18133-11j3717/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: filtered_numbers = numbers[using]
     NoMethodError:
       undefined method `call' for #<Filter:0xb98c427c>
     # /tmp/d20141028-18133-11j3717/solution.rb:29:in `block in []'
     # /tmp/d20141028-18133-11j3717/solution.rb:5:in `each'
     # /tmp/d20141028-18133-11j3717/solution.rb:5:in `each'
     # /tmp/d20141028-18133-11j3717/solution.rb:28:in `[]'
     # /tmp/d20141028-18133-11j3717/spec.rb:179:in `can_filter'
     # /tmp/d20141028-18133-11j3717/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: filtered_numbers = numbers[using]
     NoMethodError:
       undefined method `call' for #<SignFilter:0xb99377e0 @sign_type=:positive>
     # /tmp/d20141028-18133-11j3717/solution.rb:29:in `block in []'
     # /tmp/d20141028-18133-11j3717/solution.rb:5:in `each'
     # /tmp/d20141028-18133-11j3717/solution.rb:5:in `each'
     # /tmp/d20141028-18133-11j3717/solution.rb:28:in `[]'
     # /tmp/d20141028-18133-11j3717/spec.rb:179:in `can_filter'
     # /tmp/d20141028-18133-11j3717/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: filtered_numbers = numbers[using]
     NoMethodError:
       undefined method `call' for #<SignFilter:0xb9936bd8 @sign_type=:non_positive>
     # /tmp/d20141028-18133-11j3717/solution.rb:29:in `block in []'
     # /tmp/d20141028-18133-11j3717/solution.rb:5:in `each'
     # /tmp/d20141028-18133-11j3717/solution.rb:5:in `each'
     # /tmp/d20141028-18133-11j3717/solution.rb:28:in `[]'
     # /tmp/d20141028-18133-11j3717/spec.rb:179:in `can_filter'
     # /tmp/d20141028-18133-11j3717/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: filtered_numbers = numbers[using]
     NoMethodError:
       undefined method `call' for #<SignFilter:0xb9935f6c @sign_type=:negative>
     # /tmp/d20141028-18133-11j3717/solution.rb:29:in `block in []'
     # /tmp/d20141028-18133-11j3717/solution.rb:5:in `each'
     # /tmp/d20141028-18133-11j3717/solution.rb:5:in `each'
     # /tmp/d20141028-18133-11j3717/solution.rb:28:in `[]'
     # /tmp/d20141028-18133-11j3717/spec.rb:179:in `can_filter'
     # /tmp/d20141028-18133-11j3717/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: filtered_numbers = numbers[using]
     NoMethodError:
       undefined method `call' for #<SignFilter:0xb99353c8 @sign_type=:non_negative>
     # /tmp/d20141028-18133-11j3717/solution.rb:29:in `block in []'
     # /tmp/d20141028-18133-11j3717/solution.rb:5:in `each'
     # /tmp/d20141028-18133-11j3717/solution.rb:5:in `each'
     # /tmp/d20141028-18133-11j3717/solution.rb:28:in `[]'
     # /tmp/d20141028-18133-11j3717/spec.rb:179:in `can_filter'
     # /tmp/d20141028-18133-11j3717/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 }
     NoMethodError:
       undefined method `&' for #<SignFilter:0xb993416c @sign_type=:non_negative>
     # /tmp/d20141028-18133-11j3717/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:0xb994f1b0>
     # /tmp/d20141028-18133-11j3717/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: filter        = non_negative & non_zero & mod_3_is_zero
     NoMethodError:
       undefined method `&' for #<SignFilter:0xb994e670 @sign_type=:non_negative>
     # /tmp/d20141028-18133-11j3717/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)>'

  12) NumberSet can combine multiple filters with "or" rule
     Failure/Error: filter        = even | negative | more_than_100
     NoMethodError:
       undefined method `|' for #<Filter:0xb994d9dc @filter_proc=#<Proc:0xb994da40>>
     # /tmp/d20141028-18133-11j3717/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)>'

  13) NumberSet can combine multiple filters with "and" and "or" rules
     Failure/Error: filter        = even & negative | mod_3_is_zero
     NoMethodError:
       undefined method `&' for #<Filter:0xb994cd84 @filter_proc=#<Proc:0xb994da40>>
     # /tmp/d20141028-18133-11j3717/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)>'

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

Finished in 0.02053 seconds
24 examples, 14 failures

Failed examples:

rspec /tmp/d20141028-18133-11j3717/spec.rb:42 # NumberSet can filter by complex type
rspec /tmp/d20141028-18133-11j3717/spec.rb:48 # NumberSet can filter by integer type
rspec /tmp/d20141028-18133-11j3717/spec.rb:54 # NumberSet can filter by real type
rspec /tmp/d20141028-18133-11j3717/spec.rb:60 # NumberSet can filter by custom filter
rspec /tmp/d20141028-18133-11j3717/spec.rb:66 # NumberSet can filter positive numbers
rspec /tmp/d20141028-18133-11j3717/spec.rb:72 # NumberSet can filter non-positive numbers
rspec /tmp/d20141028-18133-11j3717/spec.rb:78 # NumberSet can filter negative numbers
rspec /tmp/d20141028-18133-11j3717/spec.rb:84 # NumberSet can filter non-negative numbers
rspec /tmp/d20141028-18133-11j3717/spec.rb:90 # NumberSet can combine two filters with "and" rule
rspec /tmp/d20141028-18133-11j3717/spec.rb:97 # NumberSet can combine two filters with "or" rule
rspec /tmp/d20141028-18133-11j3717/spec.rb:104 # NumberSet can combine multiple filters with "and" rule
rspec /tmp/d20141028-18133-11j3717/spec.rb:114 # NumberSet can combine multiple filters with "or" rule
rspec /tmp/d20141028-18133-11j3717/spec.rb:124 # NumberSet can combine multiple filters with "and" and "or" rules
rspec /tmp/d20141028-18133-11j3717/spec.rb:134 # NumberSet can combine multiple filters with "and", "or" and parenthesis

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

Любомир обнови решението на 26.10.2014 22:06 (преди около 10 години)

+class NumberSet
+ include Enumerable
+
+ def each(&block)
+ @container.each(&block)
+ end
+
+ def initialize
+ @container = []
+ @set_size = 0
+ end
+
+ def <<(number)
+ unless @container.include? number
+ @container.push(number)
+ @set_size += 1
+ end
+ end
+
+ def empty?
+ @set_size == 0
+ end
+
+ def size
+ @set_size
+ end
+
+ def [](proc)
+ new_array = []
+ self.each do |n|
+ new_array << n if (proc.call n)
+ end
+ end
+end
+
+class Filter
+ def initialize(&block)
+ @filter_proc = Proc.new &block
+ end
+
+ def type?(number)
+ @filter_proc.call number
+ end
+end
+
+class TypeFilter
+ def initialize(type)
+ @type = type
+ end
+
+ def type?(number)
+ case @type
+ when :integer
+ number.is_a? Fixnum
+ when :real
+ (number.is_a? Float) || (number.is_a? Rational)
+ when :complex
+ number.is_a? Complex
+ end
+ end
+end
+
+class SignFilter
+ def initialize(sign_type)
+ @sign_type = sign_type
+ end
+
+ def type?(number)
+ case @sign_type
+ when :positive then number > 0
+ when :non_negative then number >= 0
+ when :non_positive then number <= 0
+ when :negative then number < 0
+ end
+ end
+end

Здрасти,

Ето няколко коментара по кода ти:

  • Какъв е тоя @set_size? Не можеш ли да питаш масива колко е голям, вместо да си играеш да броиш?
  • NumberSet#<< не е правилно идентиран.
  • block, Proc.new(&block), Proc.new(&Proc.new(&block)), ... са едно и също нещо. Ти кое би избрал? :)
  • Каква е идеята зад името Filter#type?? Трудно ми е да го разбера.
  • NumberSet#[] трябва да връща NumberSet обект. В момента връщаш Array.

Любомир обнови решението на 27.10.2014 15:53 (преди около 10 години)

class NumberSet
include Enumerable
def each(&block)
@container.each(&block)
end
def initialize
@container = []
- @set_size = 0
end
def <<(number)
unless @container.include? number
- @container.push(number)
- @set_size += 1
+ @container.push(number)
end
end
def empty?
- @set_size == 0
+ @container.size == 0
end
def size
- @set_size
+ @container.size
end
- def [](proc)
- new_array = []
+ def [](filter)
+ filtered_set = NumberSet.new
self.each do |n|
- new_array << n if (proc.call n)
+ filtered_set << n if (filter.call n)
end
end
end
class Filter
def initialize(&block)
@filter_proc = Proc.new &block
end
- def type?(number)
+ def filtrate?(number)
@filter_proc.call number
end
+
+ def self.call_new_filter_proc(number)
+ @new_filter_proc.call number
+ end
end
class TypeFilter
def initialize(type)
@type = type
end
- def type?(number)
+ def filtrate?(number)
case @type
when :integer
number.is_a? Fixnum
when :real
(number.is_a? Float) || (number.is_a? Rational)
when :complex
number.is_a? Complex
end
end
end
class SignFilter
def initialize(sign_type)
@sign_type = sign_type
end
- def type?(number)
+ def filtrate?(number)
case @sign_type
when :positive then number > 0
when :non_negative then number >= 0
when :non_positive then number <= 0
when :negative then number < 0
end
end
-end
+end