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

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

Към профила на Станислав Венков

Резултати

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

Код

class Filter
def initialize(number)
yield number
end
end
class TypeFilter
def initialize(numbers_type)
integer_filter(@set) if numbers_type == :integer
real_filter(@set) if numbers_type == :real
complex_filter(@set) if numbers_type == :complex
end
def integer_filter(numbers_set)
filtered_set = Array.new
numbers_set.each do |number|
if number.is_a?('Integer')
filtered_set << number
end
end
filtered_set
end
def real_filter(numbers_set)
filtered_set = Array.new
numbers_set.each do |number|
if number.is_a?('Rational')
filtered_set << number
end
end
filtered_set
end
def complex_filter(numbers_set)
filtered_set = Array.new
numbers_set.each do |number|
if number.is_a?('Complex')
filtered_set << number
end
end
filtered_set
end
end
class SignFilter
def initialize(numbers_type)
positive_filter(@set) if numbers_type == :integer
non_positive_filter(@set) if numbers_type == :real
negative_filter(@set) if numbers_type == :complex
non_negative_filter(@set) if numbers_type == :non_negative
end
def positive_filter(numbers_set)
filtered_set = Array.new
numbers_set.each do |number|
if number > 0
filtered_set << number
end
end
filtered_set
end
def non_positive(numbers_set)
filtered_set = Array.new
numbers_set.each do |number|
if number <= 0
filtered_set << number
end
end
filtered_set
end
def negative(numbers_set)
filtered_set = Array.new
numbers_set.each do |number|
if number < 0
filtered_set << number
end
end
filtered_set
end
def non_negative(numbers_set)
filtered_set = Array.new
numbers_set.each do |number|
if number >= 0
filtered_set << number
end
end
filtered_set
end
end
class NumberSet
include Enumerable
def initialize(*numbers)
@set = Array.new
@set = numbers
end
def set
@set
end
def <<(new_number)
if ! @set.include?(new_number)
@set << new_number
end
end
def size
@set.size
end
def empty?
@set.empty?
end
def []
NumberSet.new = yield @set
end
def each(&block)
block or return enum_for(__method__)
@set.each_key(&block)
self
end
end

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

FFFFF...FFFFFFFFFFFFFFFF

Failures:

  1) NumberSet can store integers
     Failure/Error: expect(numbers).to include and_get_it_as
     NoMethodError:
       undefined method `each_key' for [42]:Array
     # /tmp/d20141028-18133-epiwkk/solution.rb:127:in `each'
     # /tmp/d20141028-18133-epiwkk/spec.rb:171:in `can_store'
     # /tmp/d20141028-18133-epiwkk/spec.rb:3: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 store floating point numbers
     Failure/Error: expect(numbers).to include and_get_it_as
     NoMethodError:
       undefined method `each_key' for [3.14]:Array
     # /tmp/d20141028-18133-epiwkk/solution.rb:127:in `each'
     # /tmp/d20141028-18133-epiwkk/spec.rb:171:in `can_store'
     # /tmp/d20141028-18133-epiwkk/spec.rb:7: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 store complex numbers
     Failure/Error: expect(numbers).to include and_get_it_as
     NoMethodError:
       undefined method `each_key' for [(0.3+2i)]:Array
     # /tmp/d20141028-18133-epiwkk/solution.rb:127:in `each'
     # /tmp/d20141028-18133-epiwkk/spec.rb:171:in `can_store'
     # /tmp/d20141028-18133-epiwkk/spec.rb:11: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 store rational numbers
     Failure/Error: expect(numbers).to include and_get_it_as
     NoMethodError:
       undefined method `each_key' for [(22/7)]:Array
     # /tmp/d20141028-18133-epiwkk/solution.rb:127:in `each'
     # /tmp/d20141028-18133-epiwkk/spec.rb:171:in `can_store'
     # /tmp/d20141028-18133-epiwkk/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)>'

  5) NumberSet can compare numbers of different types
     Failure/Error: expect(numbers).to include and_get_it_as
     NoMethodError:
       undefined method `each_key' for [(2/1)]:Array
     # /tmp/d20141028-18133-epiwkk/solution.rb:127:in `each'
     # /tmp/d20141028-18133-epiwkk/spec.rb:171:in `can_store'
     # /tmp/d20141028-18133-epiwkk/spec.rb:19: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 by complex type
     Failure/Error: can_filter [0.3+2i, 3.14, 11], using: TypeFilter.new(:complex),
     NoMethodError:
       undefined method `each' for nil:NilClass
     # /tmp/d20141028-18133-epiwkk/solution.rb:36:in `complex_filter'
     # /tmp/d20141028-18133-epiwkk/solution.rb:11:in `initialize'
     # /tmp/d20141028-18133-epiwkk/spec.rb:43:in `new'
     # /tmp/d20141028-18133-epiwkk/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)>'

  7) NumberSet can filter by integer type
     Failure/Error: can_filter [Rational(5, 2), 8.0, 7, Rational(9, 1)], using: TypeFilter.new(:integer),
     NoMethodError:
       undefined method `each' for nil:NilClass
     # /tmp/d20141028-18133-epiwkk/solution.rb:16:in `integer_filter'
     # /tmp/d20141028-18133-epiwkk/solution.rb:9:in `initialize'
     # /tmp/d20141028-18133-epiwkk/spec.rb:49:in `new'
     # /tmp/d20141028-18133-epiwkk/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)>'

  8) NumberSet can filter by real type
     Failure/Error: can_filter [Rational(5, 2), 8.3, 7], using: TypeFilter.new(:real),
     NoMethodError:
       undefined method `each' for nil:NilClass
     # /tmp/d20141028-18133-epiwkk/solution.rb:26:in `real_filter'
     # /tmp/d20141028-18133-epiwkk/solution.rb:10:in `initialize'
     # /tmp/d20141028-18133-epiwkk/spec.rb:55:in `new'
     # /tmp/d20141028-18133-epiwkk/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)>'

  9) NumberSet can filter by custom filter
     Failure/Error: can_filter [Rational(5, 2), 7.6, 5], using: Filter.new { |number| number > 4 },
     ArgumentError:
       wrong number of arguments (0 for 1)
     # /tmp/d20141028-18133-epiwkk/solution.rb:2:in `initialize'
     # /tmp/d20141028-18133-epiwkk/spec.rb:61:in `new'
     # /tmp/d20141028-18133-epiwkk/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)>'

  10) NumberSet can filter positive numbers
     Failure/Error: filtered_numbers = numbers[using]
     ArgumentError:
       wrong number of arguments (1 for 0)
     # /tmp/d20141028-18133-epiwkk/solution.rb:121:in `[]'
     # /tmp/d20141028-18133-epiwkk/spec.rb:179:in `can_filter'
     # /tmp/d20141028-18133-epiwkk/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)>'

  11) NumberSet can filter non-positive numbers
     Failure/Error: filtered_numbers = numbers[using]
     ArgumentError:
       wrong number of arguments (1 for 0)
     # /tmp/d20141028-18133-epiwkk/solution.rb:121:in `[]'
     # /tmp/d20141028-18133-epiwkk/spec.rb:179:in `can_filter'
     # /tmp/d20141028-18133-epiwkk/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)>'

  12) NumberSet can filter negative numbers
     Failure/Error: filtered_numbers = numbers[using]
     ArgumentError:
       wrong number of arguments (1 for 0)
     # /tmp/d20141028-18133-epiwkk/solution.rb:121:in `[]'
     # /tmp/d20141028-18133-epiwkk/spec.rb:179:in `can_filter'
     # /tmp/d20141028-18133-epiwkk/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)>'

  13) NumberSet can filter non-negative numbers
     Failure/Error: can_filter [Rational(-5, 2), 7.6, 0], using: SignFilter.new(:non_negative),
     NoMethodError:
       undefined method `non_negative_filter' for #<SignFilter:0xba3e97c4>
     # /tmp/d20141028-18133-epiwkk/solution.rb:51:in `initialize'
     # /tmp/d20141028-18133-epiwkk/spec.rb:85:in `new'
     # /tmp/d20141028-18133-epiwkk/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)>'

  14) NumberSet can combine two filters with "and" rule
     Failure/Error: filter = SignFilter.new(:non_negative) & Filter.new { |number| number != 0 }
     NoMethodError:
       undefined method `non_negative_filter' for #<SignFilter:0xba3e8c5c>
     # /tmp/d20141028-18133-epiwkk/solution.rb:51:in `initialize'
     # /tmp/d20141028-18133-epiwkk/spec.rb:91:in `new'
     # /tmp/d20141028-18133-epiwkk/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)>'

  15) NumberSet can combine two filters with "or" rule
     Failure/Error: filter = Filter.new { |number| number % 2 == 0 } | Filter.new { |number| number > 5 }
     ArgumentError:
       wrong number of arguments (0 for 1)
     # /tmp/d20141028-18133-epiwkk/solution.rb:2:in `initialize'
     # /tmp/d20141028-18133-epiwkk/spec.rb:98:in `new'
     # /tmp/d20141028-18133-epiwkk/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)>'

  16) NumberSet can combine multiple filters with "and" rule
     Failure/Error: non_negative  = SignFilter.new(:non_negative)
     NoMethodError:
       undefined method `non_negative_filter' for #<SignFilter:0xba3e357c>
     # /tmp/d20141028-18133-epiwkk/solution.rb:51:in `initialize'
     # /tmp/d20141028-18133-epiwkk/spec.rb:105:in `new'
     # /tmp/d20141028-18133-epiwkk/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)>'

  17) NumberSet can combine multiple filters with "or" rule
     Failure/Error: even          = Filter.new(&:even?)
     ArgumentError:
       wrong number of arguments (0 for 1)
     # /tmp/d20141028-18133-epiwkk/solution.rb:2:in `initialize'
     # /tmp/d20141028-18133-epiwkk/spec.rb:115:in `new'
     # /tmp/d20141028-18133-epiwkk/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)>'

  18) NumberSet can combine multiple filters with "and" and "or" rules
     Failure/Error: even          = Filter.new(&:even?)
     ArgumentError:
       wrong number of arguments (0 for 1)
     # /tmp/d20141028-18133-epiwkk/solution.rb:2:in `initialize'
     # /tmp/d20141028-18133-epiwkk/spec.rb:125:in `new'
     # /tmp/d20141028-18133-epiwkk/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)>'

  19) NumberSet can combine multiple filters with "and", "or" and parenthesis
     Failure/Error: even          = Filter.new(&:even?)
     ArgumentError:
       wrong number of arguments (0 for 1)
     # /tmp/d20141028-18133-epiwkk/solution.rb:2:in `initialize'
     # /tmp/d20141028-18133-epiwkk/spec.rb:135:in `new'
     # /tmp/d20141028-18133-epiwkk/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)>'

  20) NumberSet is enumerable
     Failure/Error: numbers.each do |number|
     NoMethodError:
       undefined method `each_key' for [(5/2), 8.0, 7, (9/1)]:Array
     # /tmp/d20141028-18133-epiwkk/solution.rb:127:in `each'
     # /tmp/d20141028-18133-epiwkk/spec.rb:152: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)>'

  21) 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
     NoMethodError:
       undefined method `each_key' for [1, 3, 5]:Array
     # /tmp/d20141028-18133-epiwkk/solution.rb:127:in `each'
     # /tmp/d20141028-18133-epiwkk/spec.rb:164:in `each'
     # /tmp/d20141028-18133-epiwkk/spec.rb:164:in `to_a'
     # /tmp/d20141028-18133-epiwkk/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.02352 seconds
24 examples, 21 failures

Failed examples:

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

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

Станислав обнови решението на 26.10.2014 21:41 (преди около 10 години)

+class Filter
+ def initialize(number)
+ yield number
+ end
+end
+
+class TypeFilter
+ def initialize(numbers_type)
+ integer_filter(@set) if numbers_type == :integer
+ real_filter(@set) if numbers_type == :real
+ complex_filter(@set) if numbers_type == :complex
+ end
+
+ def integer_filter(numbers_set)
+ filtered_set = Array.new
+ numbers_set.each do |number|
+ if number.is_a?('Integer')
+ filtered_set << number
+ end
+ end
+ filtered_set
+ end
+
+ def real_filter(numbers_set)
+ filtered_set = Array.new
+ numbers_set.each do |number|
+ if number.is_a?('Rational')
+ filtered_set << number
+ end
+ end
+ filtered_set
+ end
+
+ def complex_filter(numbers_set)
+ filtered_set = Array.new
+ numbers_set.each do |number|
+ if number.is_a?('Complex')
+ filtered_set << number
+ end
+ end
+ filtered_set
+ end
+
+end
+
+class SignFilter
+ def initialize(numbers_type)
+ positive_filter(@set) if numbers_type == :integer
+ non_positive_filter(@set) if numbers_type == :real
+ negative_filter(@set) if numbers_type == :complex
+ non_negative_filter(@set) if numbers_type == :non_negative
+ end
+
+ def positive_filter(numbers_set)
+ filtered_set = Array.new
+ numbers_set.each do |number|
+ if number > 0
+ filtered_set << number
+ end
+ end
+ filtered_set
+ end
+
+ def non_positive(numbers_set)
+ filtered_set = Array.new
+ numbers_set.each do |number|
+ if number <= 0
+ filtered_set << number
+ end
+ end
+ filtered_set
+ end
+
+ def negative(numbers_set)
+ filtered_set = Array.new
+ numbers_set.each do |number|
+ if number < 0
+ filtered_set << number
+ end
+ end
+ filtered_set
+ end
+
+ def non_negative(numbers_set)
+ filtered_set = Array.new
+ numbers_set.each do |number|
+ if number >= 0
+ filtered_set << number
+ end
+ end
+ filtered_set
+ end
+end
+
+class NumberSet
+ include Enumerable
+
+ def initialize(*numbers)
+ @set = Array.new
+ @set = numbers
+ end
+
+ def set
+ @set
+ end
+
+ def <<(new_number)
+ if ! @set.include?(new_number)
+ @set << new_number
+ end
+ end
+
+ def size
+ @set.size
+ end
+
+ def empty?
+ @set.empty?
+ end
+
+ def []
+ NumberSet.new = yield @set
+ end
+
+ def each(&block)
+ block or return enum_for(__method__)
+ @set.each_key(&block)
+ self
+ end
+
+end

Здравейте, решението ми покрива част от тестовете, но нямам никаква идея за имплементация на & и | и бих искал да попитам къде мога да погледна примерна имплементация на & и | методи. Също така метода [] така ли трябва да изглежда или съм много далеч от истината с тази имплементация. Моля помогнете! :)