Решение на Втора задача от Димитър Мутаров

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

Към профила на Димитър Мутаров

Резултати

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

Код

class NumberSet
include Enumerable
attr_accessor :numbers
def initialize(numbers = [])
@numbers = numbers
end
def <<(number)
if ! is_existing?(number)
@numbers << number
end
end
def is_existing? number
if @numbers.count == 0 then return false
elsif @numbers.include? number then return true
end
@numbers.each do |item|
if(item / number == 1 && (number.is_a? Complex || item % number == 0))
return true
end
end
false
end
def [] filters
@numbers.select(&filters.filter)
end
def each
if block_given?
@numbers.each { |e| yield(e) }
end
end
def empty?
@numbers.empty?
end
def size
@numbers.count
end
end
class BaseFilter
attr_accessor :filter
def initialize(&filter)
@filter = filter
end
def &(other)
BaseFilter.new { self.filter && other.filter }
end
def |(other)
BaseFilter.new { self.filter || other.filter }
end
end
class SignFilter < BaseFilter
def initialize(sign)
case sign
when :positive then @filter = Proc.new { |n| n > 0 }
when :non_positive then @filter = Proc.new { |n| n <= 0 }
when :negative then @filter = Proc.new { |n| n < 0 }
when :non_negative then @filter = Proc.new { |n| n >= 0 }
end
end
end
class TypeFilter < BaseFilter
def initialize(type)
case type
when :integer
@filter = Proc.new { |n| n.is_a?(Integer) }
when :real
@filter = Proc.new { |n| (n.is_a?(Float) || n.is_a?(Rational))}
when :complex
@filter = Proc.new { |n| n.is_a?(Complex) }
end
end
end
class Filter < BaseFilter
def initialize(&block)
@filter = Proc.new block
end
end

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

...........FFFFFFFFFFF.F

Failures:

  1) 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 (1 for 0)
     # /tmp/d20141028-18133-1a6mdck/solution.rb:90:in `initialize'
     # /tmp/d20141028-18133-1a6mdck/solution.rb:90:in `new'
     # /tmp/d20141028-18133-1a6mdck/solution.rb:90:in `initialize'
     # /tmp/d20141028-18133-1a6mdck/spec.rb:61:in `new'
     # /tmp/d20141028-18133-1a6mdck/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)>'

  2) NumberSet can filter positive numbers
     Failure/Error: numbers << number
     ZeroDivisionError:
       divided by 0
     # /tmp/d20141028-18133-1a6mdck/solution.rb:21:in `/'
     # /tmp/d20141028-18133-1a6mdck/solution.rb:21:in `block in is_existing?'
     # /tmp/d20141028-18133-1a6mdck/solution.rb:20:in `each'
     # /tmp/d20141028-18133-1a6mdck/solution.rb:20:in `is_existing?'
     # /tmp/d20141028-18133-1a6mdck/solution.rb:10:in `<<'
     # /tmp/d20141028-18133-1a6mdck/spec.rb:177:in `block in can_filter'
     # /tmp/d20141028-18133-1a6mdck/spec.rb:176:in `each'
     # /tmp/d20141028-18133-1a6mdck/spec.rb:176:in `can_filter'
     # /tmp/d20141028-18133-1a6mdck/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)>'

  3) NumberSet can filter non-positive numbers
     Failure/Error: numbers << number
     ZeroDivisionError:
       divided by 0
     # /tmp/d20141028-18133-1a6mdck/solution.rb:21:in `/'
     # /tmp/d20141028-18133-1a6mdck/solution.rb:21:in `block in is_existing?'
     # /tmp/d20141028-18133-1a6mdck/solution.rb:20:in `each'
     # /tmp/d20141028-18133-1a6mdck/solution.rb:20:in `is_existing?'
     # /tmp/d20141028-18133-1a6mdck/solution.rb:10:in `<<'
     # /tmp/d20141028-18133-1a6mdck/spec.rb:177:in `block in can_filter'
     # /tmp/d20141028-18133-1a6mdck/spec.rb:176:in `each'
     # /tmp/d20141028-18133-1a6mdck/spec.rb:176:in `can_filter'
     # /tmp/d20141028-18133-1a6mdck/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)>'

  4) NumberSet can filter negative numbers
     Failure/Error: numbers << number
     ZeroDivisionError:
       divided by 0
     # /tmp/d20141028-18133-1a6mdck/solution.rb:21:in `/'
     # /tmp/d20141028-18133-1a6mdck/solution.rb:21:in `block in is_existing?'
     # /tmp/d20141028-18133-1a6mdck/solution.rb:20:in `each'
     # /tmp/d20141028-18133-1a6mdck/solution.rb:20:in `is_existing?'
     # /tmp/d20141028-18133-1a6mdck/solution.rb:10:in `<<'
     # /tmp/d20141028-18133-1a6mdck/spec.rb:177:in `block in can_filter'
     # /tmp/d20141028-18133-1a6mdck/spec.rb:176:in `each'
     # /tmp/d20141028-18133-1a6mdck/spec.rb:176:in `can_filter'
     # /tmp/d20141028-18133-1a6mdck/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)>'

  5) NumberSet can filter non-negative numbers
     Failure/Error: numbers << number
     ZeroDivisionError:
       divided by 0
     # /tmp/d20141028-18133-1a6mdck/solution.rb:21:in `/'
     # /tmp/d20141028-18133-1a6mdck/solution.rb:21:in `block in is_existing?'
     # /tmp/d20141028-18133-1a6mdck/solution.rb:20:in `each'
     # /tmp/d20141028-18133-1a6mdck/solution.rb:20:in `is_existing?'
     # /tmp/d20141028-18133-1a6mdck/solution.rb:10:in `<<'
     # /tmp/d20141028-18133-1a6mdck/spec.rb:177:in `block in can_filter'
     # /tmp/d20141028-18133-1a6mdck/spec.rb:176:in `each'
     # /tmp/d20141028-18133-1a6mdck/spec.rb:176:in `can_filter'
     # /tmp/d20141028-18133-1a6mdck/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)>'

  6) NumberSet can combine two filters with "and" rule
     Failure/Error: filter = SignFilter.new(:non_negative) & Filter.new { |number| number != 0 }
     ArgumentError:
       wrong number of arguments (1 for 0)
     # /tmp/d20141028-18133-1a6mdck/solution.rb:90:in `initialize'
     # /tmp/d20141028-18133-1a6mdck/solution.rb:90:in `new'
     # /tmp/d20141028-18133-1a6mdck/solution.rb:90:in `initialize'
     # /tmp/d20141028-18133-1a6mdck/spec.rb:91:in `new'
     # /tmp/d20141028-18133-1a6mdck/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)>'

  7) 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 (1 for 0)
     # /tmp/d20141028-18133-1a6mdck/solution.rb:90:in `initialize'
     # /tmp/d20141028-18133-1a6mdck/solution.rb:90:in `new'
     # /tmp/d20141028-18133-1a6mdck/solution.rb:90:in `initialize'
     # /tmp/d20141028-18133-1a6mdck/spec.rb:98:in `new'
     # /tmp/d20141028-18133-1a6mdck/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)>'

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

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

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

  12) 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
       
       expected: 3
            got: 0
       
       (compared using ==)
     # /tmp/d20141028-18133-1a6mdck/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.02072 seconds
24 examples, 12 failures

Failed examples:

rspec /tmp/d20141028-18133-1a6mdck/spec.rb:60 # NumberSet can filter by custom filter
rspec /tmp/d20141028-18133-1a6mdck/spec.rb:66 # NumberSet can filter positive numbers
rspec /tmp/d20141028-18133-1a6mdck/spec.rb:72 # NumberSet can filter non-positive numbers
rspec /tmp/d20141028-18133-1a6mdck/spec.rb:78 # NumberSet can filter negative numbers
rspec /tmp/d20141028-18133-1a6mdck/spec.rb:84 # NumberSet can filter non-negative numbers
rspec /tmp/d20141028-18133-1a6mdck/spec.rb:90 # NumberSet can combine two filters with "and" rule
rspec /tmp/d20141028-18133-1a6mdck/spec.rb:97 # NumberSet can combine two filters with "or" rule
rspec /tmp/d20141028-18133-1a6mdck/spec.rb:104 # NumberSet can combine multiple filters with "and" rule
rspec /tmp/d20141028-18133-1a6mdck/spec.rb:114 # NumberSet can combine multiple filters with "or" rule
rspec /tmp/d20141028-18133-1a6mdck/spec.rb:124 # NumberSet can combine multiple filters with "and" and "or" rules
rspec /tmp/d20141028-18133-1a6mdck/spec.rb:134 # NumberSet can combine multiple filters with "and", "or" and parenthesis
rspec /tmp/d20141028-18133-1a6mdck/spec.rb:159 # NumberSet returns enumerable of set's contents if no block is given to each

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

Димитър обнови решението на 27.10.2014 15:38 (преди около 10 години)

+class NumberSet
+ include Enumerable
+ attr_accessor :numbers
+
+ def initialize(numbers = [])
+ @numbers = numbers
+ end
+
+ def <<(number)
+ if ! is_existing?(number)
+ @numbers << number
+ end
+ end
+
+ def is_existing? number
+ if @numbers.count == 0 then return false
+ elsif @numbers.include? number then return true
+ end
+
+ @numbers.each do |item|
+ if(item / number == 1 && (number.is_a? Complex || item % number == 0))
+ return true
+ end
+ end
+ false
+ end
+
+ def [] filters
+ @numbers.select(&filters.filter)
+end
+
+def each
+ if block_given?
+ @numbers.each { |e| yield(e) }
+ else
+ Enumerable.new(self, :each)
+ end
+end
+
+def empty?
+ @numbers.empty?
+end
+
+def size
+ @numbers.count
+end
+end
+
+class BaseFilter
+ attr_accessor :filter
+
+ def initialize(&filter)
+ @filter = filter
+ end
+
+ def &(other)
+ BaseFilter.new { self.filter && other.filter }
+ end
+
+ def |(other)
+ BaseFilter.new { self.filter || other.filter }
+ end
+end
+
+class SignFilter < BaseFilter
+
+ def initialize(sign)
+ case sign
+ when :positive then @filter = Proc.new { |n| n > 0 }
+ when :non_positive then @filter = Proc.new { |n| n <= 0 }
+ when :negative then @filter = Proc.new { |n| n < 0 }
+ when :non_negative then @filter = Proc.new { |n| n >= 0 }
+ end
+ end
+end
+
+class TypeFilter < BaseFilter
+ def initialize(type)
+ case type
+ when :integer
+ @filter = Proc.new { |n| n.is_a?(Integer) }
+ when :real
+ @filter = Proc.new { |n| (n.is_a?(Float) || n.is_a?(Rational))}
+ when :complex
+ @filter = Proc.new { |n| n.is_a?(Complex) }
+ end
+ end
+end
+
+class Filter < BaseFilter
+ def initialize(&block)
+ @filter = Proc.new block
+ end
+end

Димитър обнови решението на 27.10.2014 15:44 (преди около 10 години)

class NumberSet
include Enumerable
attr_accessor :numbers
def initialize(numbers = [])
@numbers = numbers
end
def <<(number)
if ! is_existing?(number)
@numbers << number
end
end
def is_existing? number
if @numbers.count == 0 then return false
elsif @numbers.include? number then return true
end
@numbers.each do |item|
if(item / number == 1 && (number.is_a? Complex || item % number == 0))
return true
end
end
false
end
def [] filters
@numbers.select(&filters.filter)
end
def each
if block_given?
@numbers.each { |e| yield(e) }
- else
- Enumerable.new(self, :each)
end
end
def empty?
@numbers.empty?
end
def size
@numbers.count
end
end
class BaseFilter
attr_accessor :filter
def initialize(&filter)
@filter = filter
end
def &(other)
BaseFilter.new { self.filter && other.filter }
end
def |(other)
BaseFilter.new { self.filter || other.filter }
end
end
class SignFilter < BaseFilter
def initialize(sign)
case sign
when :positive then @filter = Proc.new { |n| n > 0 }
when :non_positive then @filter = Proc.new { |n| n <= 0 }
when :negative then @filter = Proc.new { |n| n < 0 }
when :non_negative then @filter = Proc.new { |n| n >= 0 }
end
end
end
class TypeFilter < BaseFilter
def initialize(type)
case type
when :integer
@filter = Proc.new { |n| n.is_a?(Integer) }
when :real
@filter = Proc.new { |n| (n.is_a?(Float) || n.is_a?(Rational))}
when :complex
@filter = Proc.new { |n| n.is_a?(Complex) }
end
end
end
class Filter < BaseFilter
def initialize(&block)
@filter = Proc.new block
end
end