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

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

Към профила на Виктор Карагяуров

Резултати

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

Код

class Array
def to_hash
result = {}
self.each do |pair|
result[pair[0]] = pair[1]
end
result
end
def index_by
result = {}
self.each do |v|
result[yield v] = v
end
result
end
def subarray_count subarray
result = 0
self.each_index do |i|
if self.slice(i, subarray.length).eql? subarray
result += 1
end
end
result
end
def occurences_count
result = Hash.new(0)
self.each do |k|
result[k] = result.fetch(k, 0) + 1
end
result
end
end

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

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

Finished in 0.01956 seconds
17 examples, 0 failures

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

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

+class Array
+ def to_hash
+ result = {}
+ self.each do |pair|
+ result[pair[0]] = pair[1]
+ end
+ result
+ end
+
+ def index_by
+ result = {}
+ self.each do |v|
+ result[yield v] = v
+ end
+ result
+ end
+
+ def subarray_count subarray
+ result = 0
+ self.each_index do |i|
+ if self.slice(i, subarray.length).eql? subarray
+ result += 1
+ end
+ end
+ result
+ end
+
+ def occurences_count
+ result = Hash.new(0)
+ self.each do |k|
+ result[k] = result.fetch(k, 0) + 1
+ end
+ result
+ end
+end