Решение на Първа задача от Николай Беличев

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

Към профила на Николай Беличев

Резултати

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

Код

class Array
def to_hash
inject({}) { |hash, i| hash[i[0]] = i[1]; hash }
end
end
class Array
def index_by
inject({}) { |hash, word| hash[yield word] = word; hash }
end
end
class Array
def subarray_count(a)
count = 0
index = -1
while (index = self.join.index(a.join, index + 1))
count += 1
end
count
end
end
class Array
def occurences_count
a = inject({}) { |hash, word| hash[word] = self.count(word); hash }
a.default = 0
a
end
end

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

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

Finished in 0.01863 seconds
17 examples, 0 failures

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

Николай обнови решението на 23.10.2011 20:25 (преди над 12 години)

+class Array
+ def to_hash
+ inject({}) { |hash, i| hash[i[0]] = i[1]; hash }
+ end
+end
+
+class Array
+ def index_by
+ inject({}) { |hash, word| hash[yield word] = word; hash }
+ end
+end
+
+class Array
+ def subarray_count(a)
+ count = 0
+ index = -1
+ while (index = self.join.index(a.join, index + 1))
+ count += 1
+ end
+ count
+ end
+end
+
+class Array
+ def occurences_count
+ a = inject({}) { |hash, word| hash[word] = self.count(word); hash }
+ a.default = 0
+ a
+ end
+end