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

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

Към профила на Пламен Стоев

Резултати

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

Код

class Array
def to_hash
Hash[*flatten]
end
def index_by
map { |x| Array[(yield x), x] }.to_hash
end
def occurences_count
Hash.new(0).merge index_by { |x| count x }.invert
end
def subarray_count(subarray)
if subarray.length > length
return 0
end
top = length - subarray.length
(0..top).map { |x| self[x , subarray.length] }.count subarray
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-1oes2m2/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 counts how many times an element is present in an array
     Failure/Error: %w[a b a b c].occurences_count.should eq('a' => 2, 'b' => 2, 'c' => 1)
       
       expected {"a"=>2, "b"=>2, "c"=>1}
            got {"b"=>2, "c"=>1}
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -{"a"=>2, "b"=>2, "c"=>1}
       +{"b"=>2, "c"=>1}
     # /tmp/d20111025-2903-1oes2m2/spec.rb:70: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 works with arrays containing nil and false
     Failure/Error: [nil, false, nil, false, true].occurences_count.should eq(nil => 2, false => 2, true => 1)
       
       expected {nil=>2, false=>2, true=>1}
            got {false=>2, true=>1}
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -{nil=>2, false=>2, true=>1}
       +{false=>2, true=>1}
     # /tmp/d20111025-2903-1oes2m2/spec.rb:78: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.01964 seconds
17 examples, 3 failures

Failed examples:

rspec /tmp/d20111025-2903-1oes2m2/spec.rb:10 # Array#to_hash works when the keys and values are arrays
rspec /tmp/d20111025-2903-1oes2m2/spec.rb:68 # Array#occurences_count counts how many times an element is present in an array
rspec /tmp/d20111025-2903-1oes2m2/spec.rb:77 # Array#occurences_count works with arrays containing nil and false

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

Пламен обнови решението на 19.10.2011 03:44 (преди около 13 години)

+class Array
+ def to_hash
+ Hash[*flatten]
+ end
+
+ def index_by
+ map { |x| Array[(yield x), x] }.to_hash
+ end
+
+ def occurences_count
+ Hash.new(0).merge index_by { |x| count x }.invert
+ end
+
+ def subarray_count(subarray)
+ if subarray.length > length
+ return 0
+ end
+ top = length - subarray.length
+ (0..top).map { |x| self[x , subarray.length] }.count subarray
+ end
+end