Решение на Първа задача от Ростислав Георгиев

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

Към профила на Ростислав Георгиев

Резултати

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

Код

class Array
def to_hash
inject({}) do |hash, pair|
hash[pair[0]]=pair[1]
hash
end
end
def index_by
inject({}) do |hash, elem|
hash[yield elem] = elem
hash
end
end
def subarray_count(sub_array)
result = 0
each_index do |index|
result += 1 if slice(index, sub_array.length) == sub_array
end
result
end
def occurences_count
inject(Hash.new(0)) do |hash, elem|
hash[elem] = count(elem)
hash
end
end
end

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

.................

Finished in 0.21512 seconds
17 examples, 0 failures

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

Ростислав обнови решението на 22.10.2011 21:15 (преди над 12 години)

+class Array
+
+ def to_hash
+ inject({}) do |hash, pair|
+ hash[pair[0]]=pair[1]
+ hash
+ end
+ end
+
+ def index_by
+ inject({}) do |hash, elem|
+ hash[yield elem] = elem
+ hash
+ end
+ end
+
+ def subarray_count(sub_array)
+ result = 0
+ each_index do |index|
+ result += 1 if slice(index, sub_array.length) == sub_array
+ end
+ result
+ end
+
+ def occurences_count
+ inject(Hash.new(0)) do |hash, elem|
+ hash[elem] = count(elem)
+ hash
+ end
+ end
+
+end