Решение на Първа задача от Александър Маринов

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

Към профила на Александър Маринов

Резултати

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

Код

class Array
def to_hash
result_hash = {}
self.each { |i| result_hash[i[0]] = i[1] }
return result_hash
end
def index_by
result_hash = {}
self.each { |i| result_hash[yield(i)] = i }
return result_hash
end
def subarray_count(subarray)
result_count = 0
for i in (0..self.size - subarray.size)
flag = true
for j in (0..subarray.size - 1)
if self[i + j] != subarray[j]
flag = false
break
end
end
if flag
result_count += 1
end
end
return result_count
end
def occurences_count
result_hash = Hash.new(0)
self.each do |i|
if not result_hash.has_key? i
result_hash[i] = self.count i
end
end
return result_hash
end
end

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

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

Finished in 0.01407 seconds
17 examples, 0 failures

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

Александър обнови решението на 24.10.2011 01:32 (преди над 12 години)

+class Array
+ def to_hash
+ result_hash = {}
+ self.each { |i| result_hash[i[0]] = i[1] }
+ return result_hash
+ end
+
+ def index_by
+ result_hash = {}
+ self.each { |i| result_hash[yield(i)] = i }
+ return result_hash
+ end
+
+ def subarray_count(subarray)
+ result_count = 0
+ for i in (0..self.size - subarray.size)
+ flag = true
+ for j in (0..subarray.size - 1)
+ if self[i + j] != subarray[j]
+ flag = false
+ break
+ end
+ end
+ if flag
+ result_count += 1
+ end
+ end
+ return result_count
+ end
+
+ def occurences_count
+ result_hash = Hash.new(0)
+ self.each do |i|
+ if not result_hash.has_key? i
+ result_hash[i] = self.count i
+ end
+ end
+ return result_hash
+ end
+
+end