Решение на Първа задача от Димитър Илиев

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

Към профила на Димитър Илиев

Резултати

  • 5 точки от тестове
  • 0 бонус точки
  • 5 точки общо
  • 15 успешни тест(а)
  • 2 неуспешни тест(а)

Код

class Array
def to_hash
the_hash = Hash.new
self.each do |n|
if(n.class != Array && n.size != 2) then continue end
the_hash[n[0]]=n[1]
end # self.each
return the_hash
end
def index_by &block
result = Hash.new
self.each do |n|
result[block.call(n)] = n
end
return result
end
def subarray_count arr
counter = 0
i = 0 # interator
while i<self.size
subarray = self[i..(i+arr.size)]
if subarray == [] then
i+=1
next
end
ii =0
maching = 1
while ii < arr.size
if arr[ii] != subarray[ii] then
maching = 0
break;
end
ii+=1
end
if maching ==1 then counter +=1 end
i+=1
end
return counter
end
def occurences_count
result_hash = Hash.new
self.each do |n|
if result_hash.has_key?(n) then
result_hash[n] +=1
else
result_hash[n] = 1
end
end
return result_hash
end
end

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

..............F.F

Failures:

  1) Array#occurences_count returns a hash that defaults to 0 when the key is not present
     Failure/Error: [].occurences_count[:something].should eq 0
       
       expected 0
            got nil
       
       (compared using ==)
     # /tmp/d20111025-2903-bc0nrd/spec.rb:74:in `block (2 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  2) Array#occurences_count returns a hash, that does not change when indexed with a non-occuring element
     Failure/Error: hash['b'].should eq 0
       
       expected 0
            got nil
       
       (compared using ==)
     # /tmp/d20111025-2903-bc0nrd/spec.rb:84:in `block (2 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

Finished in 0.01402 seconds
17 examples, 2 failures

Failed examples:

rspec /tmp/d20111025-2903-bc0nrd/spec.rb:73 # Array#occurences_count returns a hash that defaults to 0 when the key is not present
rspec /tmp/d20111025-2903-bc0nrd/spec.rb:81 # Array#occurences_count returns a hash, that does not change when indexed with a non-occuring element

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

Димитър обнови решението на 23.10.2011 14:41 (преди над 12 години)

+class Array
+
+ def to_hash
+
+ the_hash = Hash.new
+
+ self.each do |n|
+
+ if(n.class != Array && n.size != 2) then continue end
+
+ the_hash[n[0]]=n[1]
+
+ end # self.each
+
+ return the_hash
+ end
+
+
+ def index_by &block
+
+ result = Hash.new
+
+ self.each do |n|
+ result[block.call(n)] = n
+ end
+
+ return result
+
+ end
+
+
+ def subarray_count arr
+ counter = 0
+ i = 0 # interator
+ while i<self.size
+ subarray = self[i..(i+arr.size)]
+ if subarray == [] then
+ i+=1
+ next
+ end
+
+ ii =0
+ maching = 1
+ while ii < arr.size
+
+ if arr[ii] != subarray[ii] then
+ maching = 0
+ break;
+ end
+ ii+=1
+ end
+ if maching ==1 then counter +=1 end
+
+ i+=1
+ end
+ return counter
+ end
+
+ def occurences_count
+
+ result_hash = Hash.new
+ self.each do |n|
+ if result_hash.has_key?(n) then
+ result_hash[n] +=1
+ else
+ result_hash[n] = 1
+ end
+ end
+ return result_hash
+ end
+
+end