Решение на Първа задача от Евгени Кунев

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

Към профила на Евгени Кунев

Резултати

  • 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(&block)
((map &block).zip self).to_hash
end
def subarray_count(subarray)
cnt = 0
0.upto(length - subarray.length) do |first|
cnt += 1 if self[first, subarray.length] == subarray
end
cnt
end
def occurences_count
occurences = (zip collect {|x| count x}).to_hash
occurences.default = 0
occurences
end
end

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

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

Finished in 0.0192 seconds
17 examples, 0 failures

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

Евгени обнови решението на 20.10.2011 00:28 (преди над 12 години)

+class Array
+
+ def to_hash
+ inject({}) do |hash, pair|
+ hash[pair[0]] = pair[1]
+ hash
+ end
+ end
+
+ def index_by(&block)
+ ((map &block).zip self).to_hash
+ end
+
+ def subarray_count(subarray)
+ cnt = 0
+ 0.upto(length - subarray.length) do |first|
+ cnt += 1 if self[first, subarray.length] == subarray
+ end
+ cnt
+ end
+
+ def occurences_count
+ occurences = (zip collect {|x| count x}).to_hash
+ occurences.default = 0
+ occurences
+ end
+end