Решение на Първа задача от Аксения Пенкова

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

Към профила на Аксения Пенкова

Резултати

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

Код

class Array
def to_hash
hash = {}
self.each { |element| hash[element[0]] = element[1] }
hash
end
def index_by &block
hash = {}
self.each { |element| hash[block.call(element)] = element }
hash
end
def subarray_count subarray
return -1 if subarray == [] || self.size < subarray.size
count = 0
0.upto(self.size - 1) { |i| count += 1 if subarray == self[i, subarray.size] }
count
end
def occurences_count
hash = Hash.new(0)
self.each { |element| hash[element] += 1 }
hash
end
end

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

...........FF....

Failures:

  1) Array#subarray_count(subarray) work with empty arrays
     Failure/Error: [].subarray_count([1]).should be_zero
       expected zero? to return true, got false
     # /tmp/d20111025-2903-1msvvwg/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#subarray_count(subarray) work when the argument is larger than the array
     Failure/Error: [1].subarray_count([1, 2]).should eq 0
       
       expected 0
            got -1
       
       (compared using ==)
     # /tmp/d20111025-2903-1msvvwg/spec.rb:63: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.01543 seconds
17 examples, 2 failures

Failed examples:

rspec /tmp/d20111025-2903-1msvvwg/spec.rb:58 # Array#subarray_count(subarray) work with empty arrays
rspec /tmp/d20111025-2903-1msvvwg/spec.rb:62 # Array#subarray_count(subarray) work when the argument is larger than the array

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

Аксения обнови решението на 24.10.2011 03:39 (преди над 12 години)

+class Array
+ def to_hash
+ hash = {}
+ self.each { |element| hash[element[0]] = element[1] }
+ hash
+ end
+
+ def index_by &block
+ hash = {}
+ self.each { |element| hash[block.call(element)] = element }
+ hash
+ end
+
+ def subarray_count subarray
+ return -1 if subarray == [] || self.size < subarray.size
+
+ count = 0
+ 0.upto(self.size - 1) { |i| count += 1 if subarray == self[i, subarray.size] }
+ count
+ end
+
+ def occurences_count
+ hash = Hash.new(0)
+ self.each { |element| hash[element] += 1 }
+ hash
+ end
+end