Решение на Първа задача от Михаил Стойков

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

Към профила на Михаил Стойков

Резултати

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

Код

# no injects due to presentation QQ
class Array
def index_by
result = {}
self.each {|n| result[ yield n ] = n }
result
end
def to_hash # Hash[*self.flatten] - hash apidock comment
result = {}
self.each {|n| result[n[0]] = n[1]}
result
end
def subarray_count ( sub )
if (sub.length > length) then return 0 end
buf = find_index(sub.first)
if buf == nil then return 0 end
#print self[buf+1..-1]
self[buf+1..-1].subarray_count(sub) + (self[buf, sub.length] == sub ? 1 : 0)
# sorry but I'm too lazy atm to not recurse it :P for me it dies on
# 'Array.new(8166,1).subarray_count([1]).should eq 0' with
# 'stack level too deep'
end
def occurences_count
result = Hash.new(0)
self.each { |n| result [n] +=1}
result
end
end

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

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

Finished in 0.01491 seconds
17 examples, 0 failures

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

Михаил обнови решението на 24.10.2011 00:38 (преди над 12 години)

+# no injects due to presentation QQ
+class Array
+ def index_by
+ result = {}
+ self.each {|n| result[ yield n ] = n }
+ result
+ end
+
+ def to_hash # Hash[*self.flatten] - hash apidock comment
+ result = {}
+ self.each {|n| result[n[0]] = n[1]}
+ result
+ end
+
+ def subarray_count ( sub )
+ if (sub.length > length) then return 0 end
+ buf = find_index(sub.first)
+ if buf == nil then return 0 end
+ #print self[buf+1..-1]
+ self[buf+1..-1].subarray_count(sub) + (self[buf, sub.length] == sub ? 1 : 0)
+ # sorry but I'm too lazy atm to not recurse it :P for me it dies on
+ # 'Array.new(8166,1).subarray_count([1]).should eq 0' with
+ # 'stack level too deep'
+ end
+
+ def occurences_count
+ result = Hash.new(0)
+ self.each { |n| result [n] +=1}
+ result
+ end
+end