Светослав обнови решението на 24.10.2014 18:30 (преди около 10 години)
+class NumberSet
+ include Enumerable
+
+ def initialize()
+ @array = Array.new
+ end
+
+ def each
+ @array.each do |current|
+ yield current
+ end
+ end
+
+ def <<(input_number)
+ if not @array.include?(input_number)
+ @array << input_number
+ end
+ end
+
+ def size
+ @array.size
+ end
+
+ def empty?
+ @array.empty?
+ end
+
+ def [](filter)
+ result = filter.filter_array(@array)
+
+ filtered_set = NumberSet.new
+ result.each do |current|
+ filtered_set << current
+ end
+
+ return filtered_set
+ end
+
+ def print_set
+ puts @array
+ end
+
+end
+
+module CustomFilterOperators
+
+ def &(other)
+ Filter.new { |x| get_filter.call(x) && other.get_filter.call(x) }
+ end
+
+ def |(other)
+ Filter.new { |x| get_filter.call(x) || other.get_filter.call(x) }
+ end
+
+end
+
+
+class Filter
+
+ include CustomFilterOperators
+
+ def initialize(&block)
+ @filter = block
+ end
+
+ def filter_array(numbers)
+ @new_arr = Array.new
+ numbers.each do |current|
+ if @filter.call(current)
+ @new_arr << current
+ end
+ end
+
+ return @new_arr
+ end
+
+ def get_filter
+ @filter
+ end
+
+end
+
+class TypeFilter
+
+ include CustomFilterOperators
+
+
+ def initialize(index)
+ is_integer = lambda { |number| number.integer? }
+ is_real = lambda { |number| number.real? }
+ is_complex = lambda { |number| number.eql?(number.to_c) }
+
+ @hash = { :integer => is_integer ,
+ :real => is_real ,
+ :complex => is_complex }
+ @hash_index = index
+ end
+
+ def filter_array(numbers)
+ @new_arr = Array.new
+ numbers.each do |current|
+ if @hash[@hash_index].call(current)
+ @new_arr << current
+ end
+ end
+
+ return @new_arr
+ end
+
+ def get_filter
+ @hash[@hash_index]
+ end
+
+end
+
+class SignFilter
+
+ include CustomFilterOperators
+
+ def initialize(index)
+ is_positive = lambda { |number| number > 0 }
+ is_non_positive = lambda { |number| number <= 0 }
+ is_negative = lambda { |number| number < 0 }
+ is_non_negative = lambda { |number| number >= 0}
+
+ @hash = { :positive => is_positive , :non_positive => is_non_positive ,
+ :negative => is_negative , :non_negative => is_non_negative }
+ @hash_index = index
+ end
+
+ def filter_array(numbers)
+ @new_arr = Array.new
+ numbers.each do |current|
+ if @hash[@hash_index].call(current)
+ @new_arr << current
+ end
+ end
+
+ return @new_arr
+ end
+
+ def get_filter
+ @hash[@hash_index]
+ end
+
+end