Решение на Първа задача от Тони Ризов

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

Към профила на Тони Ризов

Резултати

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

Код

class Array
def to_hash
self.inject({}) { |hash, el| hash[el[0]] = el[1]; hash }
end
def index_by
self.inject({}) { |hash, el| hash[yield el] = el; hash }
end
def subarray_count(sub_array)
self.inject([0, 0]) { |pair| if pair[0] + sub_array.length - 1 >= self.length then pair[1] elsif self[pair[0]..(pair[0] + sub_array.length - 1)] == sub_array then [pair[0] + 1, pair[1] + 1] else [pair[0] + 1, pair[1]] end}
end
def occurences_count
self.inject({}) { |hash, el| hash[el] = self.count el; hash }
end
end

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

...........F..F.F

Failures:

  1) Array#subarray_count(subarray) work with empty arrays
     Failure/Error: [].subarray_count([1]).should be_zero
     NoMethodError:
       undefined method `zero?' for [0, 0]:Array
     # /tmp/d20111025-2903-lk5xk9/spec.rb:59:in `block (2 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  2) Array#occurences_count returns a hash that defaults to 0 when the key is not present
     Failure/Error: [].occurences_count[:something].should eq 0
       
       expected 0
            got nil
       
       (compared using ==)
     # /tmp/d20111025-2903-lk5xk9/spec.rb:74:in `block (2 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  3) Array#occurences_count returns a hash, that does not change when indexed with a non-occuring element
     Failure/Error: hash['b'].should eq 0
       
       expected 0
            got nil
       
       (compared using ==)
     # /tmp/d20111025-2903-lk5xk9/spec.rb:84:in `block (2 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

Finished in 0.02075 seconds
17 examples, 3 failures

Failed examples:

rspec /tmp/d20111025-2903-lk5xk9/spec.rb:58 # Array#subarray_count(subarray) work with empty arrays
rspec /tmp/d20111025-2903-lk5xk9/spec.rb:73 # Array#occurences_count returns a hash that defaults to 0 when the key is not present
rspec /tmp/d20111025-2903-lk5xk9/spec.rb:81 # Array#occurences_count returns a hash, that does not change when indexed with a non-occuring element

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

Тони обнови решението на 23.10.2011 00:15 (преди около 13 години)

+class Array
+ def to_hash
+ self.inject({}) { |hash, el| hash[el[0]] = el[1]; hash }
+ end
+ def index_by
+ self.inject({}) { |hash, el| hash[yield el] = el; hash }
+ end
+ def subarray_count(sub_array)
+ self.inject([0, 0]) { |pair| if pair[0] + sub_array.length - 1 >= self.length then pair[1] elsif self[pair[0]..(pair[0] + sub_array.length - 1)] == sub_array then [pair[0] + 1, pair[1] + 1] else [pair[0] + 1, pair[1]] end}
+ end
+ def occurences_count
+ self.inject({}) { |hash, el| hash[el] = self.count el; hash }
+ end
+end