Решение на Първа задача от Стефан Кънев

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

Към профила на Стефан Кънев

Резултати

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

Код

class Array
def to_hash
inject({}) { |hash, pair| hash[pair.first] = pair[1]; hash }
end
def index_by
map { |n| [yield(n), n] }.to_hash
end
def subarray_count(subarray)
each_cons(subarray.length).count(subarray)
end
def occurences_count
Hash.new { |hash, key| 0 }.tap do |result|
each { |item| result[item] += 1 }
end
end
end

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

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

Finished in 0.01643 seconds
17 examples, 0 failures

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

Стефан обнови решението на 17.10.2011 17:52 (преди над 12 години)

+class Array
+ def to_hash
+ inject({}) { |hash, pair| hash[pair.first] = pair[1]; hash }
+ end
+
+ def index_by
+ map { |n| [yield(n), n] }.to_hash
+ end
+
+ def subarray_count(subarray)
+ each_cons(subarray.length).count(subarray)
+ end
+
+ def occurences_count
+ Hash.new { |hash, key| 0 }.tap do |result|
+ each { |item| result[item] += 1 }
+ end
+ end
+end