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

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

Към профила на Венета Денчева

Резултати

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

Код

class Array
def to_hash
Hash[*self.flatten]
end
def index_by
self.inject({}) do |hash, word|
hash[yield word] = word
hash
end
end
def subarray_count(subarray)
count = 0
subarrayLength = subarray.length
arrayLength = self.length
self.each_index do |i|
break if i + subarrayLength > arrayLength
count += 1 if self.slice(i, subarrayLength) == subarray
end if subarrayLength > 0 && arrayLength > 0
count
end
def occurences_count
self.inject(Hash.new(0)) do |hash, word|
hash[word] = (hash[word] || 0) + 1
hash
end
end
end

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

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

Failures:

  1) Array#to_hash works when the keys and values are arrays
     Failure/Error: [[1, [2, 3]], [[4, 5], 6]].to_hash.should eq(1 => [2, 3], [4, 5] => 6)
       
       expected {1=>[2, 3], [4, 5]=>6}
            got {1=>2, 3=>4, 5=>6}
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -{1=>[2, 3], [4, 5]=>6}
       +{1=>2, 3=>4, 5=>6}
     # /tmp/d20111025-2903-nlmhp3/spec.rb:11: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.01504 seconds
17 examples, 1 failure

Failed examples:

rspec /tmp/d20111025-2903-nlmhp3/spec.rb:10 # Array#to_hash works when the keys and values are arrays

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

Венета обнови решението на 24.10.2011 00:58 (преди над 12 години)

+class Array
+ def to_hash
+ Hash[*self.flatten]
+ end
+
+ def index_by
+ self.inject({}) do |hash, word|
+ hash[yield word] = word
+ hash
+ end
+ end
+
+ def subarray_count(subarray)
+ count = 0
+ subarrayLength = subarray.length
+ arrayLength = self.length
+ self.each_index do |i|
+ break if i + subarrayLength > arrayLength
+ count += 1 if self.slice(i, subarrayLength) == subarray
+ end if subarrayLength > 0 && arrayLength > 0
+ count
+ end
+
+ def occurences_count
+ self.inject(Hash.new(0)) do |hash, word|
+ hash[word] = (hash[word] || 0) + 1
+ hash
+ end
+ end
+end