Решение на Първа задача от Деница Генчева

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

Към профила на Деница Генчева

Резултати

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

Код

class Array
def to_hash
result = {}
self.each do |n|
result[n[0]] = n[1]
end
result
end
def index_by(&block)
result = {}
self.each do |value|
key = block.call(value)
result[key] = value
end
result
end
def subarray_count(subarray)
count = 0;
maxStartIndex = self.size - subarray.size;
0.upto(maxStartIndex) do |startIndex|
if self[startIndex, subarray.size] == subarray
count = count + 1
end
end
count;
end
def occurences_count
hash = Hash.new(0);
self.each do |element|
occurences = hash[element]
hash[element] = occurences + 1
end
hash
end
end

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

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

Finished in 0.02019 seconds
17 examples, 0 failures

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

Деница обнови решението на 23.10.2011 14:00 (преди над 12 години)

+class Array
+ def to_hash
+ result = {}
+ self.each do |n|
+ result[n[0]] = n[1]
+ end
+ result
+ end
+
+ def index_by(&block)
+ result = {}
+ self.each do |value|
+ key = block.call(value)
+ result[key] = value
+ end
+ result
+ end
+
+ def subarray_count(subarray)
+ count = 0;
+
+ maxStartIndex = self.size - subarray.size;
+ 0.upto(maxStartIndex) do |startIndex|
+ if self[startIndex, subarray.size] == subarray
+ count = count + 1
+ end
+ end
+
+ count;
+ end
+
+ def occurences_count
+ hash = Hash.new(0);
+ self.each do |element|
+ occurences = hash[element]
+ hash[element] = occurences + 1
+ end
+ hash
+ end
+end