Решение на Втора задача от Звездалина Димитрова

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

Към профила на Звездалина Димитрова

Резултати

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

Код

class NumberSet
include Enumerable
def initialize(set = [])
@number_set = set
end
def <<(element_to_add)
unless @number_set.include?(element_to_add)
@number_set << element_to_add
end
end
def size
@number_set.size
end
def empty?
@number_set.empty?
end
def each
@number_set.each { |variable| yield variable }
end
def [](filter)
filtered = NumberSet.new
@number_set.each do |number|
filtered << number if(filter.condition.call number)
end
filtered
end
end
class Filter
attr :condition
def initialize(&condition)
@condition = condition
end
def &(other)
Filter.new { |v| condition.call v and other.condition.call v }
end
def |(other)
Filter.new { |v| condition.call alter or other.condition.call v }
end
end
class SignFilter < Filter
def initialize(condition)
case condition
when :positive then super() { |variable| variable > 0 }
when :non_positive then super() { |variable| variable <= 0 }
when :negative then super() { |variable| variable < 0 }
when :non_negative then super() { |variable| variable >= 0 }
end
end
end
class TypeFilter < Filter
def initialize(type)
case type
when :integer then super() { |v| v.is_a? Intager }
when :real then super() { |v| v.is_a? Float or v.is_a? Rational }
when :complex then super() { |v| v.is_a? Complex }
end
end
end

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

.........F.......F.FFF.F

Failures:

  1) NumberSet can filter by integer type
     Failure/Error: filtered_numbers = numbers[using]
     NameError:
       uninitialized constant TypeFilter::Intager
     # /tmp/d20141028-18133-1w5isik/solution.rb:64:in `block in initialize'
     # /tmp/d20141028-18133-1w5isik/solution.rb:29:in `call'
     # /tmp/d20141028-18133-1w5isik/solution.rb:29:in `block in []'
     # /tmp/d20141028-18133-1w5isik/solution.rb:28:in `each'
     # /tmp/d20141028-18133-1w5isik/solution.rb:28:in `[]'
     # /tmp/d20141028-18133-1w5isik/spec.rb:179:in `can_filter'
     # /tmp/d20141028-18133-1w5isik/spec.rb:49: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)>'

  2) NumberSet can combine two filters with "or" rule
     Failure/Error: filtered_numbers = numbers[using]
     NameError:
       undefined local variable or method `alter' for #<Filter:0xba776180>
     # /tmp/d20141028-18133-1w5isik/solution.rb:46:in `block in |'
     # /tmp/d20141028-18133-1w5isik/solution.rb:29:in `call'
     # /tmp/d20141028-18133-1w5isik/solution.rb:29:in `block in []'
     # /tmp/d20141028-18133-1w5isik/solution.rb:28:in `each'
     # /tmp/d20141028-18133-1w5isik/solution.rb:28:in `[]'
     # /tmp/d20141028-18133-1w5isik/spec.rb:179:in `can_filter'
     # /tmp/d20141028-18133-1w5isik/spec.rb:99: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)>'

  3) NumberSet can combine multiple filters with "or" rule
     Failure/Error: filtered_numbers = numbers[using]
     NameError:
       undefined local variable or method `alter' for #<Filter:0xba7741f0>
     # /tmp/d20141028-18133-1w5isik/solution.rb:46:in `block in |'
     # /tmp/d20141028-18133-1w5isik/solution.rb:29:in `call'
     # /tmp/d20141028-18133-1w5isik/solution.rb:29:in `block in []'
     # /tmp/d20141028-18133-1w5isik/solution.rb:28:in `each'
     # /tmp/d20141028-18133-1w5isik/solution.rb:28:in `[]'
     # /tmp/d20141028-18133-1w5isik/spec.rb:179:in `can_filter'
     # /tmp/d20141028-18133-1w5isik/spec.rb:119: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)>'

  4) NumberSet can combine multiple filters with "and" and "or" rules
     Failure/Error: filtered_numbers = numbers[using]
     NameError:
       undefined local variable or method `alter' for #<Filter:0xba6ff724>
     # /tmp/d20141028-18133-1w5isik/solution.rb:46:in `block in |'
     # /tmp/d20141028-18133-1w5isik/solution.rb:29:in `call'
     # /tmp/d20141028-18133-1w5isik/solution.rb:29:in `block in []'
     # /tmp/d20141028-18133-1w5isik/solution.rb:28:in `each'
     # /tmp/d20141028-18133-1w5isik/solution.rb:28:in `[]'
     # /tmp/d20141028-18133-1w5isik/spec.rb:179:in `can_filter'
     # /tmp/d20141028-18133-1w5isik/spec.rb:129: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)>'

  5) NumberSet can combine multiple filters with "and", "or" and parenthesis
     Failure/Error: filtered_numbers = numbers[using]
     NameError:
       undefined local variable or method `alter' for #<SignFilter:0xba6feb6c>
     # /tmp/d20141028-18133-1w5isik/solution.rb:46:in `block in |'
     # /tmp/d20141028-18133-1w5isik/solution.rb:42:in `call'
     # /tmp/d20141028-18133-1w5isik/solution.rb:42:in `block in &'
     # /tmp/d20141028-18133-1w5isik/solution.rb:29:in `call'
     # /tmp/d20141028-18133-1w5isik/solution.rb:29:in `block in []'
     # /tmp/d20141028-18133-1w5isik/solution.rb:28:in `each'
     # /tmp/d20141028-18133-1w5isik/solution.rb:28:in `[]'
     # /tmp/d20141028-18133-1w5isik/spec.rb:179:in `can_filter'
     # /tmp/d20141028-18133-1w5isik/spec.rb:139: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)>'

  6) NumberSet returns enumerable of set's contents if no block is given to each
     Failure/Error: expect(numbers.each.to_a.size).to eq [1, 3, 5].size
     LocalJumpError:
       no block given (yield)
     # /tmp/d20141028-18133-1w5isik/solution.rb:23:in `block in each'
     # /tmp/d20141028-18133-1w5isik/solution.rb:23:in `each'
     # /tmp/d20141028-18133-1w5isik/solution.rb:23:in `each'
     # /tmp/d20141028-18133-1w5isik/spec.rb:164: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.02331 seconds
24 examples, 6 failures

Failed examples:

rspec /tmp/d20141028-18133-1w5isik/spec.rb:48 # NumberSet can filter by integer type
rspec /tmp/d20141028-18133-1w5isik/spec.rb:97 # NumberSet can combine two filters with "or" rule
rspec /tmp/d20141028-18133-1w5isik/spec.rb:114 # NumberSet can combine multiple filters with "or" rule
rspec /tmp/d20141028-18133-1w5isik/spec.rb:124 # NumberSet can combine multiple filters with "and" and "or" rules
rspec /tmp/d20141028-18133-1w5isik/spec.rb:134 # NumberSet can combine multiple filters with "and", "or" and parenthesis
rspec /tmp/d20141028-18133-1w5isik/spec.rb:159 # NumberSet returns enumerable of set's contents if no block is given to each

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

Звездалина обнови решението на 27.10.2014 16:36 (преди над 9 години)

+class NumberSet
+ include Enumerable
+
+ def initialize(set = [])
+ @number_set = set
+ end
+
+ def <<(element_to_add)
+ unless @number_set.include?(element_to_add)
+ @number_set << element_to_add
+ end
+ end
+
+ def size
+ @number_set.size
+ end
+
+ def empty?
+ @number_set.empty?
+ end
+
+ def each
+ @number_set.each { |variable| yield variable }
+ end
+
+ def [](filter)
+ filtered = NumberSet.new
+ @number_set.each do |number|
+ filtered << number if(filter.condition.call number)
+ end
+ filtered
+ end
+end
+
+class Filter
+ attr :condition
+ def initialize(&condition)
+ @condition = condition
+ end
+
+ def &(other)
+ Filter.new { |v| condition.call v and other.condition.call v }
+ end
+
+ def |(other)
+ Filter.new { |v| condition.call alter or other.condition.call v }
+ end
+end
+
+class SignFilter < Filter
+ def initialize(condition)
+ case condition
+ when :positive then super() { |variable| variable > 0 }
+ when :non_positive then super() { |variable| variable <= 0 }
+ when :negative then super() { |variable| variable < 0 }
+ when :non_negative then super() { |variable| variable >= 0 }
+ end
+ end
+end
+
+class TypeFilter < Filter
+ def initialize(type)
+ case type
+ when :integer then super() { |v| v.is_a? Intager }
+ when :real then super() { |v| v.is_a? Float or v.is_a? Rational }
+ when :complex then super() { |v| v.is_a? Complex }
+ end
+ end
+end