Решение на Втора задача от Мария Дулева

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

Към профила на Мария Дулева

Резултати

  • 4 точки от тестове
  • 0 бонус точки
  • 4 точки общо
  • 15 успешни тест(а)
  • 9 неуспешни тест(а)

Код

class NumberSet
include Enumerable
attr_accessor :numbers, :size
def initialize
@numbers = []
@size = 0
end
def each
@numbers.each do |number|
yield number
end
end
def empty?
if @size == 0
return true
end
false
end
def <<(other)
other.to_r
numbers.each do |n|
if (n.to_r == other)
return numbers
end
end
numbers.push(other)
@size += 1
end
def [](filter)
new_numbers = NumberSet.new
@numbers.select{|i| filter.valid_filter? i}.each do |number|
new_numbers << number
end
new_numbers
end
def &(filter, filter_one)
Filter.new do |number|
filter.valid_filter? number and filter_one.valid_filter? number
end
end
def |(filter, filter_one)
Filter.new do |number|
filter.valid_filter? number or filter_one.valid_filter? number
end
end
end
class SignFilter
def initialize(filter_sign)
@filter_sign = filter_sign
end
def valid_filter?(number)
return true if(@filter_sign == :positive and number > 0)
return true if(@filter_sign == :non_positive and number <= 0)
return true if(@filter_sign == :negative and number < 0)
return true if(@filter_sign == :non_negative and number >= 0)
false
end
end
class Filter
def initialize(&block)
@block = block
end
def valid_filter?(number)
return @block.call number
end
end
class TypeFilter
def initialize(type)
@type = type
end
def valid_filter?(number)
return number.is_a? Integer if @type ==:integer
if @type == :real
if number.is_a? Rational or number.is_a? Float
return true
end
end
return number.is_a? Complex if @type == :complex
false
end
end

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

..F.....F.......FFFFFF.F

Failures:

  1) NumberSet can store complex numbers
     Failure/Error: numbers << number
     RangeError:
       can't convert 0.3+2i into Rational
     # /tmp/d20141028-18133-19us0em/solution.rb:24:in `to_r'
     # /tmp/d20141028-18133-19us0em/solution.rb:24:in `<<'
     # /tmp/d20141028-18133-19us0em/spec.rb:170:in `can_store'
     # /tmp/d20141028-18133-19us0em/spec.rb:11: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 filter by complex type
     Failure/Error: numbers << number
     RangeError:
       can't convert 0.3+2i into Rational
     # /tmp/d20141028-18133-19us0em/solution.rb:24:in `to_r'
     # /tmp/d20141028-18133-19us0em/solution.rb:24:in `<<'
     # /tmp/d20141028-18133-19us0em/spec.rb:177:in `block in can_filter'
     # /tmp/d20141028-18133-19us0em/spec.rb:176:in `each'
     # /tmp/d20141028-18133-19us0em/spec.rb:176:in `can_filter'
     # /tmp/d20141028-18133-19us0em/spec.rb:43: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 two filters with "and" rule
     Failure/Error: filter = SignFilter.new(:non_negative) & Filter.new { |number| number != 0 }
     NoMethodError:
       undefined method `&' for #<SignFilter:0xb95e2a2c @filter_sign=:non_negative>
     # /tmp/d20141028-18133-19us0em/spec.rb:91: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 two filters with "or" rule
     Failure/Error: filter = Filter.new { |number| number % 2 == 0 } | Filter.new { |number| number > 5 }
     NoMethodError:
       undefined method `|' for #<Filter:0xb95e1f28>
     # /tmp/d20141028-18133-19us0em/spec.rb:98: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" rule
     Failure/Error: filter        = non_negative & non_zero & mod_3_is_zero
     NoMethodError:
       undefined method `&' for #<SignFilter:0xb95e1424 @filter_sign=:non_negative>
     # /tmp/d20141028-18133-19us0em/spec.rb:108: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 can combine multiple filters with "or" rule
     Failure/Error: filter        = even | negative | more_than_100
     NoMethodError:
       undefined method `|' for #<Filter:0xb95e0858 @block=#<Proc:0xb95e08a8>>
     # /tmp/d20141028-18133-19us0em/spec.rb:118: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)>'

  7) NumberSet can combine multiple filters with "and" and "or" rules
     Failure/Error: filter        = even & negative | mod_3_is_zero
     NoMethodError:
       undefined method `&' for #<Filter:0xb954bb54 @block=#<Proc:0xb95e08a8>>
     # /tmp/d20141028-18133-19us0em/spec.rb:128: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)>'

  8) NumberSet can combine multiple filters with "and", "or" and parenthesis
     Failure/Error: filter        = even & (negative | mod_3_is_zero)
     NoMethodError:
       undefined method `|' for #<SignFilter:0xb954b078 @filter_sign=:negative>
     # /tmp/d20141028-18133-19us0em/spec.rb:138: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)>'

  9) 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-19us0em/solution.rb:12:in `block in each'
     # /tmp/d20141028-18133-19us0em/solution.rb:11:in `each'
     # /tmp/d20141028-18133-19us0em/solution.rb:11:in `each'
     # /tmp/d20141028-18133-19us0em/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.02128 seconds
24 examples, 9 failures

Failed examples:

rspec /tmp/d20141028-18133-19us0em/spec.rb:10 # NumberSet can store complex numbers
rspec /tmp/d20141028-18133-19us0em/spec.rb:42 # NumberSet can filter by complex type
rspec /tmp/d20141028-18133-19us0em/spec.rb:90 # NumberSet can combine two filters with "and" rule
rspec /tmp/d20141028-18133-19us0em/spec.rb:97 # NumberSet can combine two filters with "or" rule
rspec /tmp/d20141028-18133-19us0em/spec.rb:104 # NumberSet can combine multiple filters with "and" rule
rspec /tmp/d20141028-18133-19us0em/spec.rb:114 # NumberSet can combine multiple filters with "or" rule
rspec /tmp/d20141028-18133-19us0em/spec.rb:124 # NumberSet can combine multiple filters with "and" and "or" rules
rspec /tmp/d20141028-18133-19us0em/spec.rb:134 # NumberSet can combine multiple filters with "and", "or" and parenthesis
rspec /tmp/d20141028-18133-19us0em/spec.rb:159 # NumberSet returns enumerable of set's contents if no block is given to each

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

Мария обнови решението на 26.10.2014 19:54 (преди около 10 години)

+class NumberSet
+ include Enumerable
+ attr_accessor :numbers, :size
+
+ def initialize
+ @numbers = []
+ @size = 0
+ end
+
+ def each
+ @numbers.each do |number|
+ yield number
+ end
+ end
+
+ def size
+ @size
+ end
+
+def empty?
+ if @size == 0
+ return true
+ end
+ false
+end
+
+def <<(other)
+ other.to_r
+ numbers.each do |n|
+ if (n.to_r == other)
+ return numbers
+ end
+ end
+ numbers.push(other)
+ @size += 1
+end
+
+def [](filter)
+new_numbers = NumberSet.new
+@numbers.select{|i| filter.valid_filter? i}.each do |number|
+new_numbers << number
+end
+new_numbers
+end
+
+def &(first_filter, second_filter)
+ new_numbers = NumberSet.new
+ @numbers.each do |number|
+ if first_filter.valid_filter? number and second_filter.valid_filter? number
+ new_numbers << number
+ end
+end
+ puts new_numbers.each{|number| puts number}
+ new_numbers
+end
+
+def |(first_filter, second_filter)
+ new_numbers = NumberSet.new
+ @numbers.each do |number|
+ if first_filter.valid_filter? number or second_filter.valid_filter? number
+ new_numbers << number
+ end
+end
+ puts new_numbers.each{|number| puts number}
+ new_numbers
+end
+end
+class SignFilter
+ def initialize(filter_sign)
+ @filter_sign = filter_sign
+ end
+
+ def valid_filter?(number)
+ return true if(@filter_sign == :positive and number > 0)
+ return true if(@filter_sign == :non_positive and number <= 0)
+ return true if(@filter_sign == :negative and number < 0)
+ return true if(@filter_sign == :non_negative and number >= 0)
+ false
+ end
+end
+
+class Filter
+ def initialize(&block)
+ @block = block
+ end
+ def valid_filter?(number)
+ return @block.call number
+ end
+end
+
+class TypeFilter
+ def initialize(type)
+ @type = type
+ end
+
+ def valid_filter?(number)
+ return number.is_a? Integer if @type ==:integer
+ if @type == :real
+ if number.is_a? Rational or number.is_a? Float
+ return true
+ end
+ end
+ return number.is_a? Complex if @type == :complex
+ false
+ end
+end

Мария обнови решението на 27.10.2014 13:20 (преди около 10 години)

class NumberSet
- include Enumerable
- attr_accessor :numbers, :size
+ include Enumerable
+ attr_accessor :numbers, :size
- def initialize
- @numbers = []
- @size = 0
- end
+ def initialize
+ @numbers = []
+ @size = 0
+ end
def each
- @numbers.each do |number|
- yield number
+ @numbers.each do |number|
+ yield number
end
end
- def size
- @size
- end
-
def empty?
- if @size == 0
- return true
+ if @size == 0
+ return true
end
false
end
def <<(other)
- other.to_r
- numbers.each do |n|
- if (n.to_r == other)
- return numbers
- end
- end
+ other.to_r
+ numbers.each do |n|
+ if (n.to_r == other)
+ return numbers
+ end
+ end
numbers.push(other)
@size += 1
end
def [](filter)
new_numbers = NumberSet.new
@numbers.select{|i| filter.valid_filter? i}.each do |number|
new_numbers << number
end
new_numbers
end
-def &(first_filter, second_filter)
- new_numbers = NumberSet.new
- @numbers.each do |number|
- if first_filter.valid_filter? number and second_filter.valid_filter? number
- new_numbers << number
- end
+def &(filter, filter_one)
+ Filter.new do |number|
+ filter.valid_filter? number and filter_one.valid_filter? number
end
- puts new_numbers.each{|number| puts number}
- new_numbers
end
-def |(first_filter, second_filter)
- new_numbers = NumberSet.new
- @numbers.each do |number|
- if first_filter.valid_filter? number or second_filter.valid_filter? number
- new_numbers << number
- end
+def |(filter, filter_one)
+ Filter.new do |number|
+ filter.valid_filter? number or filter_one.valid_filter? number
end
- puts new_numbers.each{|number| puts number}
- new_numbers
end
end
+
class SignFilter
def initialize(filter_sign)
@filter_sign = filter_sign
end
def valid_filter?(number)
return true if(@filter_sign == :positive and number > 0)
return true if(@filter_sign == :non_positive and number <= 0)
- return true if(@filter_sign == :negative and number < 0)
- return true if(@filter_sign == :non_negative and number >= 0)
- false
+ return true if(@filter_sign == :negative and number < 0)
+ return true if(@filter_sign == :non_negative and number >= 0)
+ false
end
end
class Filter
def initialize(&block)
@block = block
end
def valid_filter?(number)
return @block.call number
end
end
class TypeFilter
def initialize(type)
@type = type
end
def valid_filter?(number)
return number.is_a? Integer if @type ==:integer
if @type == :real
if number.is_a? Rational or number.is_a? Float
return true
end
end
return number.is_a? Complex if @type == :complex
false
end
end