Решение на Първа задача от Томислав Дюлгеров

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

Към профила на Томислав Дюлгеров

Резултати

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

Код

class Array
def to_hash
result = {}
unless self == []
self.each do |item|
result[item[0]] = item[1]
end
end
result
end
def index_by
result = {}
self.each do |item|
result[yield item] = item
end
result
end
def subarray_count(rhs_array)
count = 0
if self == []
return count
end
if rhs_array == []
raise ArgumentError
end
self.each_index do |i|
if i > (self.length - rhs_array.length)
break
elsif self[i..i + rhs_array.length - 1] == rhs_array
count += 1
end
end
count
end
def occurences_count
result = Hash.new(0)
self.each do |item|
result[item] = self.count(item)
end
result
end
end

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

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

Finished in 0.014 seconds
17 examples, 0 failures

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

Томислав обнови решението на 24.10.2011 11:29 (преди над 12 години)

+class Array
+
+ def to_hash
+ result = {}
+ unless self == []
+ self.each do |item|
+ result[item[0]] = item[1]
+ end
+ end
+ result
+ end
+
+ def index_by
+ result = {}
+ self.each do |item|
+ result[yield item] = item
+ end
+ result
+ end
+
+ def subarray_count(rhs_array)
+ count = 0
+ if self == []
+ return count
+ end
+ if rhs_array == []
+ raise ArgumentError
+ end
+ self.each_index do |i|
+ if i > (self.length - rhs_array.length)
+ break
+ elsif self[i..i + rhs_array.length - 1] == rhs_array
+ count += 1
+ end
+ end
+ count
+ end
+
+ def occurences_count
+ result = Hash.new(0)
+ self.each do |item|
+ result[item] = self.count(item)
+ end
+ result
+ end
+end