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

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

Към профила на Недислав Стойчев

Резултати

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

Код

class Array
def to_hash
array = Hash[*self.flatten]
return array
end
def index_by
output = Hash[]
self.each { |s| output[yield(s)] = s }
output
end
def subarray_count(value)
output = 0
self.each_index do |n|
if self[n,value.length] == value
output += 1
end
end
output
end
def occurences_count
output = Hash[]
self.each do |e|
output[e] = self.count(e)
end
output
end
end

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

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

Failures:

  1) Array#to_hash works when the keys and values are arrays
     Failure/Error: [[1, [2, 3]], [[4, 5], 6]].to_hash.should eq(1 => [2, 3], [4, 5] => 6)
       
       expected {1=>[2, 3], [4, 5]=>6}
            got {1=>2, 3=>4, 5=>6}
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -{1=>[2, 3], [4, 5]=>6}
       +{1=>2, 3=>4, 5=>6}
     # /tmp/d20111025-2903-194nlqd/spec.rb:11: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 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-194nlqd/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)>'

  3) 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-194nlqd/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.01522 seconds
17 examples, 3 failures

Failed examples:

rspec /tmp/d20111025-2903-194nlqd/spec.rb:10 # Array#to_hash works when the keys and values are arrays
rspec /tmp/d20111025-2903-194nlqd/spec.rb:73 # Array#occurences_count returns a hash that defaults to 0 when the key is not present
rspec /tmp/d20111025-2903-194nlqd/spec.rb:81 # Array#occurences_count returns a hash, that does not change when indexed with a non-occuring element

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

Недислав обнови решението на 24.10.2011 14:57 (преди около 13 години)

+class Array
+ def to_hash
+ array = Hash[*self.flatten]
+ return array
+ end
+
+ def index_by
+ output = Hash[]
+ self.each { |s| output[yield(s)] = s }
+ output
+ end
+
+ def subarray_count(value)
+ output = 0
+ self.each_index do |n|
+ if self[n,value.length] == value
+ output += 1
+ end
+ end
+ output
+ end
+
+ def occurences_count
+ output = Hash[]
+ self.each do |e|
+ output[e] = self.count(e)
+ end
+ output
+ end
+end