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

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

Към профила на Антон Димов

Резултати

  • 1 точка от тестове
  • 0 бонус точки
  • 1 точка общо
  • 2 успешни тест(а)
  • 22 неуспешни тест(а)

Код

class NumberSet
include Enumerable
def initialize
@numbers = []
@numbers_counter = 0
end
def <<(other)
if( @numbers.include?(other.to_i) != true)
@numbers << other.to_i if check(other)
@numbers_counter = @numbers_counter + 1
end
end
def size
@numbers_counter
end
def empty?
if(size == 0)
true
else
false
end
end
def each
end
def [](filter)
end
private
def check(value)
if(Float(value) == value)
return value.to_i
end
end
end
class Filter
def initialize(&block)
if block == true
return true
else
return false
end
end
end
class TypeFilter
def initialize(type)
case type
when ":integer"
@numbers.delete_if { |number| number != number.to_i}
when ":real"
@numbers.delete_if { |number| number != number.to_f}
when ":complex"
@numbers.delete_if { |number| number != number.to_c}
end
end
end
class SignFilter
def initialize(type)
case type
when ":positive"
@numbers.delete_if { |number| number > 0 }
when ":non_positive"
@numbers.delete_if { |number| number <= 0 }
when ":negative"
@numbers.delete_if { |number| number < 0 }
end
end
end

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

FFFFF.F.FFFFFFFFFFFFFFFF

Failures:

  1) NumberSet can store integers
     Failure/Error: expect(numbers).to include and_get_it_as
       expected #<NumberSet:0xb9eb4768 @numbers=[42], @numbers_counter=1> to include 42
     # /tmp/d20141028-18133-8rcuja/spec.rb:171:in `can_store'
     # /tmp/d20141028-18133-8rcuja/spec.rb:3: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 store floating point numbers
     Failure/Error: expect(numbers).to include and_get_it_as
       expected #<NumberSet:0xb9d90440 @numbers=[3], @numbers_counter=1> to include 3.14
     # /tmp/d20141028-18133-8rcuja/spec.rb:171:in `can_store'
     # /tmp/d20141028-18133-8rcuja/spec.rb:7: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 store complex numbers
     Failure/Error: numbers << number
     RangeError:
       can't convert 0.3+2i into Integer
     # /tmp/d20141028-18133-8rcuja/solution.rb:10:in `to_i'
     # /tmp/d20141028-18133-8rcuja/solution.rb:10:in `<<'
     # /tmp/d20141028-18133-8rcuja/spec.rb:170:in `can_store'
     # /tmp/d20141028-18133-8rcuja/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)>'

  4) NumberSet can store rational numbers
     Failure/Error: expect(numbers).to include and_get_it_as
       expected #<NumberSet:0xb9e5a970 @numbers=[3], @numbers_counter=1> to include (22/7)
     # /tmp/d20141028-18133-8rcuja/spec.rb:171:in `can_store'
     # /tmp/d20141028-18133-8rcuja/spec.rb:15: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 compare numbers of different types
     Failure/Error: expect(numbers).to include and_get_it_as
       expected #<NumberSet:0xb9e599d0 @numbers=[2], @numbers_counter=1> to include 2
     # /tmp/d20141028-18133-8rcuja/spec.rb:171:in `can_store'
     # /tmp/d20141028-18133-8rcuja/spec.rb:19: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 contains multiple numbers
     Failure/Error: expect(numbers.size).to eq 3
       
       expected: 3
            got: 2
       
       (compared using ==)
     # /tmp/d20141028-18133-8rcuja/spec.rb:32: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 filter by complex type
     Failure/Error: numbers << number
     RangeError:
       can't convert 0.3+2i into Integer
     # /tmp/d20141028-18133-8rcuja/solution.rb:10:in `to_i'
     # /tmp/d20141028-18133-8rcuja/solution.rb:10:in `<<'
     # /tmp/d20141028-18133-8rcuja/spec.rb:177:in `block in can_filter'
     # /tmp/d20141028-18133-8rcuja/spec.rb:176:in `each'
     # /tmp/d20141028-18133-8rcuja/spec.rb:176:in `can_filter'
     # /tmp/d20141028-18133-8rcuja/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)>'

  8) NumberSet can filter by integer type
     Failure/Error: expect(filtered_numbers.size).to eq expecting.size
     NoMethodError:
       undefined method `size' for nil:NilClass
     # /tmp/d20141028-18133-8rcuja/spec.rb:180:in `can_filter'
     # /tmp/d20141028-18133-8rcuja/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)>'

  9) NumberSet can filter by real type
     Failure/Error: expect(filtered_numbers.size).to eq expecting.size
     NoMethodError:
       undefined method `size' for nil:NilClass
     # /tmp/d20141028-18133-8rcuja/spec.rb:180:in `can_filter'
     # /tmp/d20141028-18133-8rcuja/spec.rb:55: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)>'

  10) NumberSet can filter by custom filter
     Failure/Error: expect(filtered_numbers.size).to eq expecting.size
     NoMethodError:
       undefined method `size' for nil:NilClass
     # /tmp/d20141028-18133-8rcuja/spec.rb:180:in `can_filter'
     # /tmp/d20141028-18133-8rcuja/spec.rb:61: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)>'

  11) NumberSet can filter positive numbers
     Failure/Error: expect(filtered_numbers.size).to eq expecting.size
     NoMethodError:
       undefined method `size' for nil:NilClass
     # /tmp/d20141028-18133-8rcuja/spec.rb:180:in `can_filter'
     # /tmp/d20141028-18133-8rcuja/spec.rb:67: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)>'

  12) NumberSet can filter non-positive numbers
     Failure/Error: expect(filtered_numbers.size).to eq expecting.size
     NoMethodError:
       undefined method `size' for nil:NilClass
     # /tmp/d20141028-18133-8rcuja/spec.rb:180:in `can_filter'
     # /tmp/d20141028-18133-8rcuja/spec.rb:73: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)>'

  13) NumberSet can filter negative numbers
     Failure/Error: expect(filtered_numbers.size).to eq expecting.size
     NoMethodError:
       undefined method `size' for nil:NilClass
     # /tmp/d20141028-18133-8rcuja/spec.rb:180:in `can_filter'
     # /tmp/d20141028-18133-8rcuja/spec.rb:79: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)>'

  14) NumberSet can filter non-negative numbers
     Failure/Error: expect(filtered_numbers.size).to eq expecting.size
     NoMethodError:
       undefined method `size' for nil:NilClass
     # /tmp/d20141028-18133-8rcuja/spec.rb:180:in `can_filter'
     # /tmp/d20141028-18133-8rcuja/spec.rb:85: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)>'

  15) 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:0xb9d5f0ac>
     # /tmp/d20141028-18133-8rcuja/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)>'

  16) 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:0xb9d5e580>
     # /tmp/d20141028-18133-8rcuja/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)>'

  17) NumberSet can combine multiple filters with "and" rule
     Failure/Error: filter        = non_negative & non_zero & mod_3_is_zero
     NoMethodError:
       undefined method `&' for #<SignFilter:0xb9d5d874>
     # /tmp/d20141028-18133-8rcuja/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)>'

  18) NumberSet can combine multiple filters with "or" rule
     Failure/Error: filter        = even | negative | more_than_100
     NoMethodError:
       undefined method `|' for #<Filter:0xb9d5cc44>
     # /tmp/d20141028-18133-8rcuja/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)>'

  19) NumberSet can combine multiple filters with "and" and "or" rules
     Failure/Error: filter        = even & negative | mod_3_is_zero
     NoMethodError:
       undefined method `&' for #<Filter:0xb9d5c12c>
     # /tmp/d20141028-18133-8rcuja/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)>'

  20) NumberSet can combine multiple filters with "and", "or" and parenthesis
     Failure/Error: filter        = even & (negative | mod_3_is_zero)
     NoMethodError:
       undefined method `|' for #<SignFilter:0xb9dd778c>
     # /tmp/d20141028-18133-8rcuja/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)>'

  21) NumberSet is enumerable
     Failure/Error: expect(values.size).to eq [Rational(5, 2), 8, 7, 9].size
       
       expected: 4
            got: 0
       
       (compared using ==)
     # /tmp/d20141028-18133-8rcuja/spec.rb:155: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)>'

  22) 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
       
       expected: 3
            got: 0
       
       (compared using ==)
     # /tmp/d20141028-18133-8rcuja/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.0218 seconds
24 examples, 22 failures

Failed examples:

rspec /tmp/d20141028-18133-8rcuja/spec.rb:2 # NumberSet can store integers
rspec /tmp/d20141028-18133-8rcuja/spec.rb:6 # NumberSet can store floating point numbers
rspec /tmp/d20141028-18133-8rcuja/spec.rb:10 # NumberSet can store complex numbers
rspec /tmp/d20141028-18133-8rcuja/spec.rb:14 # NumberSet can store rational numbers
rspec /tmp/d20141028-18133-8rcuja/spec.rb:18 # NumberSet can compare numbers of different types
rspec /tmp/d20141028-18133-8rcuja/spec.rb:27 # NumberSet contains multiple numbers
rspec /tmp/d20141028-18133-8rcuja/spec.rb:42 # NumberSet can filter by complex type
rspec /tmp/d20141028-18133-8rcuja/spec.rb:48 # NumberSet can filter by integer type
rspec /tmp/d20141028-18133-8rcuja/spec.rb:54 # NumberSet can filter by real type
rspec /tmp/d20141028-18133-8rcuja/spec.rb:60 # NumberSet can filter by custom filter
rspec /tmp/d20141028-18133-8rcuja/spec.rb:66 # NumberSet can filter positive numbers
rspec /tmp/d20141028-18133-8rcuja/spec.rb:72 # NumberSet can filter non-positive numbers
rspec /tmp/d20141028-18133-8rcuja/spec.rb:78 # NumberSet can filter negative numbers
rspec /tmp/d20141028-18133-8rcuja/spec.rb:84 # NumberSet can filter non-negative numbers
rspec /tmp/d20141028-18133-8rcuja/spec.rb:90 # NumberSet can combine two filters with "and" rule
rspec /tmp/d20141028-18133-8rcuja/spec.rb:97 # NumberSet can combine two filters with "or" rule
rspec /tmp/d20141028-18133-8rcuja/spec.rb:104 # NumberSet can combine multiple filters with "and" rule
rspec /tmp/d20141028-18133-8rcuja/spec.rb:114 # NumberSet can combine multiple filters with "or" rule
rspec /tmp/d20141028-18133-8rcuja/spec.rb:124 # NumberSet can combine multiple filters with "and" and "or" rules
rspec /tmp/d20141028-18133-8rcuja/spec.rb:134 # NumberSet can combine multiple filters with "and", "or" and parenthesis
rspec /tmp/d20141028-18133-8rcuja/spec.rb:144 # NumberSet is enumerable
rspec /tmp/d20141028-18133-8rcuja/spec.rb:159 # NumberSet returns enumerable of set's contents if no block is given to each

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

Антон обнови решението на 27.10.2014 11:58 (преди около 10 години)

+class NumberSet
+ include Enumerable
+
+ def initialize
+ @numbers = []
+ @numbers_counter = 0
+ end
+
+ def <<(other)
+ if( @numbers.include?(other.to_i) != true)
+ @numbers << other.to_i if check(other)
+ @numbers_counter = @numbers_counter + 1
+ end
+ end
+
+ def size
+ @numbers_counter
+ end
+
+ def empty?
+ if(size == 0)
+ true
+ else
+ false
+ end
+ end
+ def each
+
+ end
+
+ def [](filter)
+ end
+
+ private
+ def check(value)
+ if(Float(value) == value)
+ return value.to_i
+ end
+ end
+
+end
+
+class Filter
+ def initialize(&block)
+ if block == true
+ return true
+ else
+ return false
+ end
+ end
+end
+class TypeFilter
+ def initialize(type)
+ case type
+ when ":integer"
+ @numbers.delete_if { |number| number != number.to_i}
+ when ":real"
+ @numbers.delete_if { |number| number != number.to_f}
+ when ":complex"
+ @numbers.delete_if { |number| number != number.to_c}
+ end
+ end
+end
+
+class SignFilter
+ def initialize(type)
+ case type
+ when ":positive"
+ @numbers.delete_if { |number| number > 0 }
+ when ":non_positive"
+ @numbers.delete_if { |number| number <= 0 }
+ when ":negative"
+ @numbers.delete_if { |number| number < 0 }
+ end
+ end
+end