Явор обнови решението на 24.10.2014 04:00 (преди около 10 години)
+class NumberSet
+ include Enumerable
+
+ def initialize
+ @numbers = []
+ end
+
+ def <<(value)
+ @numbers << value unless @numbers.include?(value)
+ end
+
+ def each
+ if block_given?
+ @numbers.each { |number| yield number }
+ else
+ @numbers.each
+ end
+ end
+
+ def size
+ @numbers.size
+ end
+
+ def empty?
+ @numbers.empty?
+ end
+
+ def [](filter)
+ filtered_array = []
+
+ filter.blocks.each do |current_filter|
+ filtered_array << @numbers.select(¤t_filter)
+ end
+
+ filtered_array.reduce(&:&)
+ end
+end
+
+class Filter
+ attr_accessor :blocks
+
+ def initialize(&block)
+ @blocks = []
+ @blocks << block if block_given?
+ end
+
+ def &(other)
+ new_filter = Filter.new
+ new_filter.blocks = @blocks.concat(other.blocks)
+ new_filter
+ end
+end
+
+class TypeFilter < Filter
+ def initialize(type_of_filter)
+ @blocks = []
+
+ block = ->(n) { n.is_a? Complex } if type_of_filter == :complex
+ block = ->(n) { n.is_a? Integer } if type_of_filter == :integer
+ if type_of_filter == :real
+ block = ->(n) { n.is_a? Float or n.is_a? Rational }
+ end
+
+ @blocks << block
+ end
+end
+
+class SignFilter < Filter
+ def initialize(type_of_filter)
+ block = ->(n) { n > 0 } if type_of_filter == :positive
+ block = ->(n) { n <= 0 } if type_of_filter == :non_positive
+ block = ->(n) { n < 0 } if type_of_filter == :negative
+ block = ->(n) { n >= 0 } if type_of_filter == :non_negative
+
+ @blocks = []
+ @blocks << block
+ end
+end