Решение на Първа задача от Константин Добрев

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

Към профила на Константин Добрев

Резултати

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

Код

class Array
#1
def to_hash
hash = {}
self.each do |el|
hash[el[0]] = el[1]
end
hash
end
#2
def index_by
hash = {}
self.each do |elem|
hash[yield(elem)] = elem
end
hash
end
#3
def subarray_count array
count=0
self.each_index do |x|
if self.slice(x, array.length).eql? array
count += 1
end
end
count
end
#4
def occurences_count
occ_hash = {}
occ_hash.default = 0
self.each do |el|
occ_hash[el] = self.count(el)
end
occ_hash
end
end

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

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

Finished in 0.02382 seconds
17 examples, 0 failures

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

Константин обнови решението на 23.10.2011 15:58 (преди над 12 години)

+class Array
+
+#1
+def to_hash
+ hash = {}
+ self.each do |el|
+ hash[el[0]] = el[1]
+ end
+ hash
+end
+
+#2
+def index_by
+ hash = {}
+ self.each do |elem|
+ hash[yield(elem)] = elem
+ end
+ hash
+end
+
+#3
+def subarray_count array
+ count=0
+ self.each_index do |x|
+ if self.slice(x, array.length).eql? array
+ count += 1
+ end
+ end
+ count
+end
+
+#4
+def occurences_count
+ occ_hash = {}
+ occ_hash.default = 0
+ self.each do |el|
+ occ_hash[el] = self.count(el)
+ end
+ occ_hash
+end
+
+end