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

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

Към профила на Йоан Карадимов

Резултати

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

Код

class Array
def to_hash
result_hash = Hash.new
each { |key, value| result_hash[key] = value }
result_hash
end
def index_by
result_hash = Hash.new
each { |item| result_hash[yield item] = item }
result_hash
end
def subarray_count(subarray)
subarrays(subarray.length).select { |x| x == subarray }.length
end
def occurrence_count
result_hash = Hash.new 0
each { |item| result_hash[item] += 1 }
result_hash
end
alias occurences_count occurrence_count # make rspec shut up (nudge nudge wink wink)
private
def subarrays(subarray_length)
(0.. length - subarray_length).map do |subarray_start|
slice subarray_start, subarray_length
end
end
end

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

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

Finished in 0.01451 seconds
17 examples, 0 failures

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

Йоан обнови решението на 24.10.2011 05:00 (преди над 12 години)

+class Array
+ def to_hash
+ result_hash = Hash.new
+ each { |key, value| result_hash[key] = value }
+ result_hash
+ end
+
+ def index_by
+ result_hash = Hash.new
+ each { |item| result_hash[yield item] = item }
+ result_hash
+ end
+
+ def subarray_count(subarray)
+ subarrays(subarray.length).select { |x| x == subarray }.length
+ end
+
+ def occurrence_count
+ result_hash = Hash.new 0
+ each { |item| result_hash[item] += 1 }
+ result_hash
+ end
+
+ alias occurences_count occurrence_count # make rspec shut up (nudge nudge wink wink)
+
+ private
+
+ def subarrays(subarray_length)
+ (0.. length - subarray_length).map do |subarray_start|
+ slice subarray_start, subarray_length
+ end
+ end
+
+end