Решение на Първа задача от Емилиян Янков

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

Към профила на Емилиян Янков

Резултати

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

Код

class Array
def to_hash
hash ={}
if self
self.each do |array|
hash[array.shift]= array.shift
end
end
return hash
end
def index_by
hash ={}
if self
self.each do |elem|
hash[yield elem]= elem
end
end
return hash
end
def subarray_count(subarray)
len = subarray.length - 1
i = 0
count = 0
while i <= (self.length - len)
if self[i..(len+i)] == subarray
count += 1
end
i += 1
end
return count
end
def occurences_count
hash = Hash.new(0)
if self
self.each do |elem|
hash[elem] += 1
end
end
return hash
end
end

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

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

Finished in 0.01418 seconds
17 examples, 0 failures

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

Емилиян обнови решението на 24.10.2011 14:29 (преди над 12 години)

+class Array
+ def to_hash
+ hash ={}
+ if self
+ self.each do |array|
+ hash[array.shift]= array.shift
+ end
+ end
+ return hash
+ end
+
+ def index_by
+ hash ={}
+ if self
+ self.each do |elem|
+ hash[yield elem]= elem
+ end
+ end
+ return hash
+ end
+
+ def subarray_count(subarray)
+ len = subarray.length - 1
+ i = 0
+ count = 0
+ while i <= (self.length - len)
+ if self[i..(len+i)] == subarray
+ count += 1
+ end
+ i += 1
+ end
+ return count
+ end
+
+ def occurences_count
+ hash = Hash.new(0)
+ if self
+ self.each do |elem|
+ hash[elem] += 1
+ end
+ end
+ return hash
+ end
+end