Решение на Втора задача от Костадин Петричков

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

Към профила на Костадин Петричков

Резултати

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

Код

class NumberSet
include Enumerable
def initialize
@elements = []
end
#Fix floating point numbers.
def <<(element)
#element = element.to_f
matches = @elements.select { |number| element.to_f == number.to_f }
if matches.size == 0
@elements << element
end
end
def each(&block)
@elements.each(&block)
end
#Write size and empty methods.
def size
@elements.size
end
def empty?
@elements.empty?
end
#This is the idea:
#nums[Proc.new do |num| num > 10 end]
#But it must be wrapped in custom Filter classes.
def [] (filter)
@elements.select{ |number| filter.call(number) }
end
end
class Filter
include Enumerable
extend Enumerable
def initialize(&block)
@filter = block
@filter.call
end
end

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

........FFFFFFFFFFFFFF..

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-1w8t2vt/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-1w8t2vt/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-1w8t2vt/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: can_filter [Rational(5, 2), 7.6, 5], using: Filter.new { |number| number > 4 },
     NoMethodError:
       undefined method `>' for nil:NilClass
     # /tmp/d20141028-18133-1w8t2vt/spec.rb:61:in `block (3 levels) in <top (required)>'
     # /tmp/d20141028-18133-1w8t2vt/solution.rb:45:in `call'
     # /tmp/d20141028-18133-1w8t2vt/solution.rb:45:in `initialize'
     # /tmp/d20141028-18133-1w8t2vt/spec.rb:61:in `new'
     # /tmp/d20141028-18133-1w8t2vt/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-1w8t2vt/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-1w8t2vt/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-1w8t2vt/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-1w8t2vt/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-1w8t2vt/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 nil:NilClass
     # /tmp/d20141028-18133-1w8t2vt/spec.rb:98:in `block (3 levels) in <top (required)>'
     # /tmp/d20141028-18133-1w8t2vt/solution.rb:45:in `call'
     # /tmp/d20141028-18133-1w8t2vt/solution.rb:45:in `initialize'
     # /tmp/d20141028-18133-1w8t2vt/spec.rb:98:in `new'
     # /tmp/d20141028-18133-1w8t2vt/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-1w8t2vt/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: even          = Filter.new(&:even?)
     ArgumentError:
       no receiver given
     # /tmp/d20141028-18133-1w8t2vt/solution.rb:45:in `call'
     # /tmp/d20141028-18133-1w8t2vt/solution.rb:45:in `initialize'
     # /tmp/d20141028-18133-1w8t2vt/spec.rb:115:in `new'
     # /tmp/d20141028-18133-1w8t2vt/spec.rb:115: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: even          = Filter.new(&:even?)
     ArgumentError:
       no receiver given
     # /tmp/d20141028-18133-1w8t2vt/solution.rb:45:in `call'
     # /tmp/d20141028-18133-1w8t2vt/solution.rb:45:in `initialize'
     # /tmp/d20141028-18133-1w8t2vt/spec.rb:125:in `new'
     # /tmp/d20141028-18133-1w8t2vt/spec.rb:125: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: even          = Filter.new(&:even?)
     ArgumentError:
       no receiver given
     # /tmp/d20141028-18133-1w8t2vt/solution.rb:45:in `call'
     # /tmp/d20141028-18133-1w8t2vt/solution.rb:45:in `initialize'
     # /tmp/d20141028-18133-1w8t2vt/spec.rb:135:in `new'
     # /tmp/d20141028-18133-1w8t2vt/spec.rb:135: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.03312 seconds
24 examples, 14 failures

Failed examples:

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

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

Костадин обнови решението на 25.10.2014 00:41 (преди около 10 години)

+class NumberSet
+ include Enumerable
+ extend Enumerable
+
+ def initialize
+ @elements = []
+ end
+
+ #Fix floating point numbers.
+ def << (element)
+ #element = element.to_f
+ matches = @elements.select { |number| element.to_f == number.to_f }
+
+ if matches.size == 0
+ @elements << element
+ end
+ end
+
+ def each(&block)
+ @elements.each(&block)
+ end
+
+ #Write size and empty methods.
+
+ #This is the idea:
+ #nums[Proc.new do |num| num > 10 end]
+ #But it must be wrapped in custom Filter classes.
+ def [] (filter)
+ @elements.select{ |number| filter.call(number) }
+ end
+end
+
+class Filter
+ include Enumerable
+ extend Enumerable
+
+ def initialize(&block)
+ @filter = block
+ @filter.call
+ end
+end

Костадин обнови решението на 25.10.2014 00:48 (преди около 10 години)

class NumberSet
include Enumerable
extend Enumerable
def initialize
@elements = []
end
#Fix floating point numbers.
def << (element)
#element = element.to_f
matches = @elements.select { |number| element.to_f == number.to_f }
if matches.size == 0
@elements << element
end
end
def each(&block)
@elements.each(&block)
end
#Write size and empty methods.
+ def size
+ @elements.size
+ end
+
+ def empty?
+ @elements.empty?
+ end
#This is the idea:
#nums[Proc.new do |num| num > 10 end]
#But it must be wrapped in custom Filter classes.
def [] (filter)
@elements.select{ |number| filter.call(number) }
end
end
class Filter
include Enumerable
extend Enumerable
def initialize(&block)
@filter = block
@filter.call
end
end

Здрасти,

Имам няколко забележки по кода ти:

  • Не знам защо си решил да extend-неш Enumerable. include Enumerable е достатъчно.
  • Тялото на NumberSet#<< можеш да напишеш по доста по-прост начин. Буквално можеш да преведеш "добави element в @elements, овсен ако element се съдържа в @elements" на английски и след това на Ruby. Имаш излишен whitespace в дефиницията на метода. :)
  • NumberSet#each е неправилно идентиран.

Решението ти не е пълно. Давай с оставащата част. :))

Костадин обнови решението на 27.10.2014 12:30 (преди около 10 години)

class NumberSet
include Enumerable
- extend Enumerable
def initialize
@elements = []
end
#Fix floating point numbers.
- def << (element)
+ def <<(element)
#element = element.to_f
matches = @elements.select { |number| element.to_f == number.to_f }
if matches.size == 0
@elements << element
end
end
def each(&block)
- @elements.each(&block)
+ @elements.each(&block)
end
#Write size and empty methods.
def size
@elements.size
end
def empty?
@elements.empty?
end
#This is the idea:
#nums[Proc.new do |num| num > 10 end]
#But it must be wrapped in custom Filter classes.
def [] (filter)
@elements.select{ |number| filter.call(number) }
end
end
class Filter
include Enumerable
extend Enumerable
def initialize(&block)
@filter = block
@filter.call
end
end