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

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

Към профила на Васил Гочев

Резултати

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

Код

class Array
def to_hash
self.inject({}) do |hash, element|
hash[element.first] = element.last
hash
end
end
def index_by
self.map { |element| [yield(element), element] }.to_hash
end
def subarray_count(subarray)
count = 0
self.each_index { |element| count += 1 if self.slice(element, subarray.length) == subarray }
count
end
def occurences_count
result = {}
result.default = 0
self.each do |element|
unless result.key?(element)
result[element] = 1
else
result[element] += 1
end
end
result
end
end

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

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

Finished in 0.01389 seconds
17 examples, 0 failures

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

Васил обнови решението на 24.10.2011 01:19 (преди над 12 години)

+class Array
+ def to_hash
+ self.inject({}) do |hash, element|
+ hash[element.first] = element.last
+ hash
+ end
+ end
+
+ def index_by
+ self.map { |element| [yield(element), element] }.to_hash
+ end
+
+ def subarray_count(subarray)
+ count = 0
+ self.each_index { |element| count += 1 if self.slice(element, subarray.length) == subarray }
+ count
+ end
+
+ def occurences_count
+ result = {}
+ result.default = 0
+ self.each do |element|
+ unless result.key?(element)
+ result[element] = 1
+ else
+ result[element] += 1
+ end
+ end
+ result
+ end
+end