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

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

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

Резултати

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

Код

#!/home/dari/.rvm/rubies/ruby-1.9.2-p290/bin/ruby
class Array
def to_hash
new_hash = Hash.new
self.each { |x,y| new_hash[x] = y }
new_hash
end
def index_by
new_hash = Hash.new
self.each { |x| new_hash[yield(x)] = x }
new_hash
end
def subarray_count(subarray)
arr_len = self.length
sub_len = subarray.length
i=0
result=0
while i <= arr_len - sub_len do
if self.take(sub_len) == subarray
result+=1
end
self.shift
i+=1
end
result
end
def occurences_count
new_hash = Hash.new(0)
self.each { |x,y| new_hash[x] += 1 }
new_hash
end
end

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

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

Finished in 0.0147 seconds
17 examples, 0 failures

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

Дарина обнови решението на 23.10.2011 19:45 (преди над 12 години)

+ #!/home/dari/.rvm/rubies/ruby-1.9.2-p290/bin/ruby
+class Array
+
+ def to_hash
+ new_hash = Hash.new
+ self.each { |x,y| new_hash[x] = y }
+ new_hash
+ end
+
+ def index_by
+ new_hash = Hash.new
+ self.each { |x| new_hash[yield(x)] = x }
+ new_hash
+ end
+
+ def subarray_count(subarray)
+ arr_len = self.length
+ sub_len = subarray.length
+ i=0
+ result=0
+ while i <= arr_len - sub_len do
+ if self.take(sub_len) == subarray
+ result+=1
+ end
+ self.shift
+ i+=1
+ end
+ result
+ end
+
+ def occurences_count
+ new_hash = Hash.new(0)
+ self.each { |x,y| new_hash[x] += 1 }
+ new_hash
+ end
+
+end