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

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

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

Резултати

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

Код

class NumberSet
include Enumerable
def initialize(*data)
@data = data
end
def each(&block)
@data.each(&block)
end
def << (new_element)
@data << new_element if not @data.include?(new_element)
end
def size
(map { |item| item = 1 }).reduce { |sum, number| sum + number }
end
def empty?
size == 0
end
def [](filter)
@data.select { |item| item if filter.call(item) }
end
end
class Filter
def initialize(block = Proc.new)
@block = block
end
def call(parameter)
@block.call(parameter)
end
def &(other)
Filter.new { |item| self.call(item) && other.call(item) }
end
def |(other)
Filter.new { |item| self.call(item) || other.call(item) }
end
end
class TypeFilter < Filter
def initialize(type)
@type = type
end
def call(parameter)
case @type
when :integer then parameter.class.equal? Fixnum
when :real then parameter.class.equal? Rational or
parameter.class.equal? Float
when :complex then parameter.class.equal? Complex
end
end
end
class SignFilter < Filter
def initialize(sign)
@sign = sign
end
def call(parameter)
case @sign
when :negative then parameter < 0
when :positive then parameter > 0
when :non_positive then parameter <= 0
when :non_negative then parameter >= 0
end
end
end

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

.....F..................

Failures:

  1) NumberSet starts as empty collection
     Failure/Error: expect(numbers.size).to eq 0
       
       expected: 0
            got: nil
       
       (compared using ==)
     # /tmp/d20141028-18133-1274fbe/spec.rb:24: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.02241 seconds
24 examples, 1 failure

Failed examples:

rspec /tmp/d20141028-18133-1274fbe/spec.rb:22 # NumberSet starts as empty collection

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

Даяна обнови решението на 27.10.2014 16:41 (преди около 10 години)

+class NumberSet
+ include Enumerable
+
+ def initialize(*data)
+ @data = data
+ end
+
+ def each(&block)
+ @data.each(&block)
+ end
+
+ def << (new_element)
+ @data << new_element if not @data.include?(new_element)
+ end
+
+ def size
+ (map { |i| i = 1 }).reduce { |sum, n| sum + n }
+ end
+
+ def empty?
+ size == 0
+ end
+
+ def [](filter)
+ @data.select { |i| i if filter.call(i) }
+ end
+end
+
+
+class Filter
+ def initialize(block = Proc.new)
+ @block = block
+ end
+
+ def call(parameter)
+ @block.call(parameter)
+ end
+
+ def &(other)
+ Filter.new { |item| self.call(item) && other.call(item) }
+ end
+
+ def |(other)
+ Filter.new { |item| self.call(item) || other.call(item) }
+ end
+end
+
+
+class TypeFilter < Filter
+
+ def initialize(type)
+ @type = type
+ end
+
+ def call(parameter)
+ case @type
+ when :integer then parameter.class.equal? Fixnum
+ when :real then parameter.class.equal? Rational or
+ parameter.class.equal? Float
+ when :complex then parameter.class.equal? Complex
+ end
+ end
+end
+
+
+class SignFilter < Filter
+
+ def initialize(sign)
+ @sign = sign
+ end
+
+ def call(parameter)
+ case @sign
+ when :negative then parameter < 0
+ when :positive then parameter > 0
+ when :non_positive then parameter <= 0
+ when :non_negative then parameter >= 0
+ end
+ end
+end

Даяна обнови решението на 27.10.2014 16:50 (преди около 10 години)

class NumberSet
include Enumerable
def initialize(*data)
@data = data
end
def each(&block)
@data.each(&block)
end
def << (new_element)
@data << new_element if not @data.include?(new_element)
end
def size
- (map { |i| i = 1 }).reduce { |sum, n| sum + n }
+ (map { |item| item = 1 }).reduce { |sum, number| sum + number }
end
def empty?
size == 0
end
def [](filter)
- @data.select { |i| i if filter.call(i) }
+ @data.select { |item| item if filter.call(item) }
end
end
class Filter
def initialize(block = Proc.new)
@block = block
end
def call(parameter)
@block.call(parameter)
end
def &(other)
Filter.new { |item| self.call(item) && other.call(item) }
end
def |(other)
Filter.new { |item| self.call(item) || other.call(item) }
end
end
class TypeFilter < Filter
def initialize(type)
@type = type
end
def call(parameter)
case @type
when :integer then parameter.class.equal? Fixnum
when :real then parameter.class.equal? Rational or
parameter.class.equal? Float
when :complex then parameter.class.equal? Complex
end
end
end
class SignFilter < Filter
def initialize(sign)
@sign = sign
end
def call(parameter)
case @sign
when :negative then parameter < 0
when :positive then parameter > 0
when :non_positive then parameter <= 0
when :non_negative then parameter >= 0
end
end
end