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

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

Към профила на Людмил Делчев

Резултати

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

Код

class NumberSet
include Enumerable
def initialize (numbers = [])
@numbers = numbers
end
def each
0.upto(@numbers.length - 1) do |index|
yield @numbers[index]
end
end
def <<(number)
@numbers << number if @numbers.none? { |element| element == number }
self
end
def size
@numbers.size
end
def empty?
@numbers.empty?
end
def [](filter)
NumberSet.new(@numbers.select(&filter.proc))
end
end
class Filter
def initialize(&proc)
@proc = proc
end
attr_reader :proc
end
class TypeFilter
def initialize(type)
case type
when :integer
@proc = Proc.new{|number| number.is_a?(Integer)}
when :real
@proc = Proc.new{|number| number.is_a?(Rational) or number.is_a?(Float)}
when :complex
@proc = Proc.new{|number| number.is_a?(Complex)}
end
end
attr_reader :proc
end
class SignFilter
def initialize(sign)
case sign
when :positive then @proc = Proc.new{|number| number > 0}
when :non_positive then @proc = Proc.new{|number| number <= 0}
when :negative then @proc = Proc.new{|number| number < 0}
when :non_negative then @proc = Proc.new { |number| number >= 0 }
end
end
attr_reader :proc
end

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

................FFFFFF.F

Failures:

  1) 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:0xb901a20c>
     # /tmp/d20141028-18133-3s0u8i/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)>'

  2) 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:0xb9019564>
     # /tmp/d20141028-18133-3s0u8i/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)>'

  3) NumberSet can combine multiple filters with "and" rule
     Failure/Error: filter        = non_negative & non_zero & mod_3_is_zero
     NoMethodError:
       undefined method `&' for #<SignFilter:0xb901895c>
     # /tmp/d20141028-18133-3s0u8i/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)>'

  4) NumberSet can combine multiple filters with "or" rule
     Failure/Error: filter        = even | negative | more_than_100
     NoMethodError:
       undefined method `|' for #<Filter:0xb909be74 @proc=#<Proc:0xb909beb0>>
     # /tmp/d20141028-18133-3s0u8i/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)>'

  5) NumberSet can combine multiple filters with "and" and "or" rules
     Failure/Error: filter        = even & negative | mod_3_is_zero
     NoMethodError:
       undefined method `&' for #<Filter:0xb909b398 @proc=#<Proc:0xb909beb0>>
     # /tmp/d20141028-18133-3s0u8i/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)>'

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

  7) 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-3s0u8i/solution.rb:10:in `block in each'
     # /tmp/d20141028-18133-3s0u8i/solution.rb:9:in `upto'
     # /tmp/d20141028-18133-3s0u8i/solution.rb:9:in `each'
     # /tmp/d20141028-18133-3s0u8i/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.02138 seconds
24 examples, 7 failures

Failed examples:

rspec /tmp/d20141028-18133-3s0u8i/spec.rb:90 # NumberSet can combine two filters with "and" rule
rspec /tmp/d20141028-18133-3s0u8i/spec.rb:97 # NumberSet can combine two filters with "or" rule
rspec /tmp/d20141028-18133-3s0u8i/spec.rb:104 # NumberSet can combine multiple filters with "and" rule
rspec /tmp/d20141028-18133-3s0u8i/spec.rb:114 # NumberSet can combine multiple filters with "or" rule
rspec /tmp/d20141028-18133-3s0u8i/spec.rb:124 # NumberSet can combine multiple filters with "and" and "or" rules
rspec /tmp/d20141028-18133-3s0u8i/spec.rb:134 # NumberSet can combine multiple filters with "and", "or" and parenthesis
rspec /tmp/d20141028-18133-3s0u8i/spec.rb:159 # NumberSet returns enumerable of set's contents if no block is given to each

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

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

+class NumberSet
+ include Enumerable
+
+ attr_reader :numbers
+
+ def initialize (numbers = [])
+ @numbers = numbers
+ end
+
+ def each
+ 0.upto(@numbers.length - 1) do |x|
+ yield @numbers[x]
+ end
+ end
+
+ def <<(number)
+ @numbers.push(number) if @numbers.none? { |x| x == number }
+ return self
+ end
+
+ def size
+ @numbers.size
+ end
+
+ def empty?
+ @numbers.empty?
+ end
+
+ def [](filter)
+ NumberSet.new(@numbers.select(&filter.proc))
+ end
+end
+
+class Filter
+ def initialize(&proc)
+ @proc = proc
+ end
+
+ attr_reader :proc
+end
+
+class TypeFilter
+ def initialize(type)
+ case type
+ when :integer then @proc = Proc.new { |x| x.is_a?(Integer) }
+ when :real then @proc = Proc.new {|x| x.is_a?(Rational) || x.is_a?(Float)}
+ when :complex then @proc = Proc.new { |x| x.is_a?(Complex) }
+ end
+ end
+
+ attr_reader :proc
+end
+
+class SignFilter
+ def initialize(sign)
+ case sign
+ when :positive then @proc = Proc.new { |x| x > 0 }
+ when :non_positive then @proc = Proc.new { |x| x <= 0 }
+ when :negative then @proc = Proc.new { |x| x < 0 }
+ when :non_negative then @proc = Proc.new { |x| x >= 0 }
+ end
+ end
+
+ attr_reader :proc
+end

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

class NumberSet
include Enumerable
- attr_reader :numbers
-
def initialize (numbers = [])
@numbers = numbers
end
def each
- 0.upto(@numbers.length - 1) do |x|
- yield @numbers[x]
+ 0.upto(@numbers.length - 1) do |index|
+ yield @numbers[index]
end
end
def <<(number)
- @numbers.push(number) if @numbers.none? { |x| x == number }
- return self
+ @numbers << number if @numbers.none? { |element| element == number }
+ self
end
def size
@numbers.size
end
def empty?
@numbers.empty?
end
def [](filter)
NumberSet.new(@numbers.select(&filter.proc))
end
end
class Filter
def initialize(&proc)
@proc = proc
end
attr_reader :proc
end
class TypeFilter
def initialize(type)
case type
- when :integer then @proc = Proc.new { |x| x.is_a?(Integer) }
- when :real then @proc = Proc.new {|x| x.is_a?(Rational) || x.is_a?(Float)}
- when :complex then @proc = Proc.new { |x| x.is_a?(Complex) }
+ when :integer
+ @proc = Proc.new{|number| number.is_a?(Integer)}
+ when :real
+ @proc = Proc.new{|number| number.is_a?(Rational) or number.is_a?(Float)}
+ when :complex
+ @proc = Proc.new{|number| number.is_a?(Complex)}
end
end
attr_reader :proc
end
class SignFilter
def initialize(sign)
case sign
- when :positive then @proc = Proc.new { |x| x > 0 }
- when :non_positive then @proc = Proc.new { |x| x <= 0 }
- when :negative then @proc = Proc.new { |x| x < 0 }
- when :non_negative then @proc = Proc.new { |x| x >= 0 }
+ when :positive then @proc = Proc.new{|number| number > 0}
+ when :non_positive then @proc = Proc.new{|number| number <= 0}
+ when :negative then @proc = Proc.new{|number| number < 0}
+ when :non_negative then @proc = Proc.new { |number| number >= 0 }
end
end
attr_reader :proc
end