Мартин обнови решението на 21.10.2014 22:57 (преди около 10 години)
+class NumberSet
+ include Enumerable
+
+ def initialize
+ @numbers = []
+ end
+
+ def <<(digit)
+ @numbers.each do |item|
+ if(item == digit)
+ return
+ end
+ end
+ @numbers.push(digit)
+ end
+
+ def size
+ @numbers.length
+ end
+
+ def empty?
+ @numbers.empty?
+ end
+
+ def [](arguments)
+ case arguments[2]
+ when '&'
+ and_filters [arguments[0], arguments[1]]
+ else
+ or_filters [arguments[0], arguments[1]]
+ end
+ end
+
+ def and_filters filters
+ temp_arrays = []
+ filters.each do |filter|
+ if (filter.class == Filter)
+ temp_arrays.push(filter.filter_array(@numbers))
+ elsif (filter.class == TypeFilter)
+ temp_arrays.push(filter.type_array(@numbers))
+ else
+ temp_arrays.push(filter.sign_array(@numbers))
+ end
+ end
+ temp_arrays[0] & temp_arrays[1]
+ end
+
+ def or_filters filters
+ temp_arrays = []
+ filters.each do |filter|
+ if (filter.class == Filter)
+ temp_arrays.push(filter.filter_array(@numbers))
+ elsif (filter.class == TypeFilter)
+ temp_arrays.push(filter.type_array(@numbers))
+ else
+ temp_arrays.push(filter.sign_array(@numbers))
+ end
+ end
+ temp_arrays[0] | temp_arrays[1]
+ end
+end
+
+class BaseFilter
+ def &(second)
+ filter_types = [self, second, '&']
+ end
+
+ def |(second)
+ filter_types = [self, second, '|']
+ end
+end
+
+class SignFilter < BaseFilter
+ def initialize argument
+ @argument = argument
+ end
+
+ def sign_array numbers_array
+ case @argument
+ when :positive then filter_positive_array(numbers_array)
+ when :negative then filter_negative_array(numbers_array)
+ when :non_positive then filter_non_positive_array(numbers_array)
+ when :non_negative then filter_non_negative_array(numbers_array)
+ end
+ end
+
+ def filter_positive_array array
+ filtered_positive_array = []
+ array.each do |item|
+ if (item > 0)
+ filtered_positive_array.push(item)
+ end
+ end
+ return filtered_positive_array
+ end
+
+ def filter_negative_array array
+ filtered_negative_array = []
+ array.each do |item|
+ if (item < 0)
+ filtered_negative_array.push(item)
+ end
+ end
+ return filtered_negative_array
+ end
+
+ def filter_non_positive_array array
+ filtered_non_positive_array = []
+ array.each do |item|
+ if (item <= 0)
+ filtered_non_positive_array.push(item)
+ end
+ end
+ return filtered_non_positive_array
+ end
+
+ def filter_non_negative_array array
+ filtered_non_negative_array = []
+ array.each do |item|
+ if (item >= 0)
+ filtered_non_negative_array.push(item)
+ end
+ end
+ return filtered_non_negative_array
+ end
+end
+
+class TypeFilter < BaseFilter
+ def initialize argument
+ @argument = argument
+ end
+
+ def type_array numbers_array
+ case @argument
+ when :integer
+ filter_type_array numbers_array, Fixnum
+ when :complex
+ filter_type_array numbers_array, Complex
+ else
+ float_arr = filter_type_array numbers_array, Float
+ rat_arr = filter_type_array numbers_array, Rational
+ float_arr + rat_arr
+ end
+ end
+
+ def filter_type_array array, filter_type
+ filtered_array = []
+ array.each do |item|
+ if(item.class == filter_type)
+ filtered_array.push(item)
+ end
+ end
+ return filtered_array
+ end
+end
+
+class Filter < BaseFilter
+ def initialize &block
+ @final_block = block
+ end
+
+ def filter_array numbers_array
+ result = []
+ numbers_array.each do |item|
+ if(@final_block.call(item) == true)
+ result.push(item)
+ end
+ end
+ return result
+ end
+end