Решение на Втора задача от Георги Павлов

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

Към профила на Георги Павлов

Резултати

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

Код

module Operators
def &(other)
Filter.new { |x| self.filter.call x and other.filter.call x }
end
def |(other)
Filter.new { |x| self.filter.call x or other.filter.call x }
end
end
class NumberSet
include Enumerable
def initialize
@range = []
end
def each(&block)
@range.each &block
end
def <<(number)
@range << number unless @range.include?(number)
end
def size
@range.size
end
def empty?
@range.empty?
end
def [](argument)
new_range = NumberSet.new
@range.each do |number|
new_range << number if argument.filter.call number
end
new_range
end
end
class Filter
include Operators
def initialize(&block)
@condition_block = block
end
def filter
@condition_block
end
end
class TypeFilter
include Operators
def initialize(type)
@type = type
end
def filter
case @type
when :integer then -> (x) { x.is_a? Integer }
when :real then -> (x) { x.is_a? Float or x.is_a? Rational }
when :complex then -> (x) { x.is_a? Complex }
end
end
end
class SignFilter
include Operators
def initialize(sign)
@sign = sign
end
def filter
case @sign
when :positive then ->(x) { x > 0 }
when :non_positive then ->(x) { x <= 0 }
when :negative then ->(x) { x < 0 }
when :non_negative then ->(x) { x >= 0 }
end
end
end

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

........................

Finished in 0.02237 seconds
24 examples, 0 failures

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

Георги обнови решението на 27.10.2014 15:19 (преди около 10 години)

+module Operators
+ def &(other)
+ Filter.new { |x| self.filter.call x and other.filter.call x }
+ end
+
+ def |(other)
+ Filter.new { |x| self.filter.call x or other.filter.call x }
+ end
+end
+
+class NumberSet
+ include Enumerable
+
+ def initialize
+ @range = []
+ end
+
+ def each(&block)
+ @range.each &block
+ end
+
+ def <<(number)
+ @range << number unless @range.include?(number)
+ end
+
+ def size
+ @range.size
+ end
+
+ def empty?
+ @range.empty?
+ end
+
+ def [](argument)
+ new_range = NumberSet.new
+ @range.each do |number|
+ new_range << number if argument.filter.call number
+ end
+ new_range
+ end
+end
+
+class Filter
+ include Operators
+
+ def initialize(&block)
+ @condition_block = block
+ end
+
+ def filter
+ @condition_block
+ end
+end
+
+class TypeFilter
+ include Operators
+
+ def initialize(type)
+ @type = type
+ end
+
+ def filter
+ case @type
+ when :integer then -> (x) { x.is_a? Integer }
+ when :real then -> (x) { x.is_a? Float or x.is_a? Rational }
+ when :complex then -> (x) { x.is_a? Complex }
+ end
+ end
+end
+
+class SignFilter
+ include Operators
+ def initialize(sign)
+ @sign = sign
+ end
+
+ def filter
+ case @sign
+ when :positive then ->(x) { x > 0 }
+ when :non_positive then ->(x) { x <= 0 }
+ when :negative then ->(x) { x < 0 }
+ when :non_negative then ->(x) { x >= 0 }
+ end
+ end
+end

Георги обнови решението на 27.10.2014 15:23 (преди около 10 години)

module Operators
def &(other)
Filter.new { |x| self.filter.call x and other.filter.call x }
end
def |(other)
Filter.new { |x| self.filter.call x or other.filter.call x }
end
end
class NumberSet
include Enumerable
def initialize
@range = []
end
def each(&block)
- @range.each &block
+ @range.each &block
end
def <<(number)
@range << number unless @range.include?(number)
end
def size
@range.size
end
def empty?
@range.empty?
end
def [](argument)
new_range = NumberSet.new
@range.each do |number|
new_range << number if argument.filter.call number
end
new_range
end
end
class Filter
include Operators
def initialize(&block)
@condition_block = block
end
def filter
@condition_block
end
end
class TypeFilter
include Operators
def initialize(type)
@type = type
end
def filter
case @type
when :integer then -> (x) { x.is_a? Integer }
when :real then -> (x) { x.is_a? Float or x.is_a? Rational }
when :complex then -> (x) { x.is_a? Complex }
end
end
end
class SignFilter
include Operators
def initialize(sign)
@sign = sign
end
def filter
case @sign
when :positive then ->(x) { x > 0 }
when :non_positive then ->(x) { x <= 0 }
when :negative then ->(x) { x < 0 }
when :non_negative then ->(x) { x >= 0 }
end
end
end