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

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

Към профила на Ивана Йовчева

Резултати

  • 1 точка от тестове
  • 0 бонус точки
  • 1 точка общо
  • 4 успешни тест(а)
  • 13 неуспешни тест(а)

Код

class Array
def to_hash(*array)
h = {}
array.each do |n|
h.merge!({array[n][0] => array[n][1]})
end
h
end
def index_by(*array)
h = Hash.new
array.each do |n|
t = yield n
h[t] = array[n]
end
h
end
def subarray_count(subarray,*array)
l = subarray.length
x = 0
while !(array.empty?)
if subarray == array.first(l)
x += 1
end
array.delete_at(0)
end
x
end
def ocurrences_count(*array)
h = Hash.new
array.each do |n|
el = array[n]
h[n] = array.count(el)
end
h
end
end

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

FFF.FFF.FFF..FFFF

Failures:

  1) Array#to_hash converts an array to a hash
     Failure/Error: [[:one, 1], [:two, 2]].to_hash.should eq(one: 1, two: 2)
       
       expected {:one=>1, :two=>2}
            got {}
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -{:one=>1, :two=>2}
       +{}
     # /tmp/d20111025-2903-1elpqlv/spec.rb:3: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#to_hash uses the last pair when a key is present multiple times
     Failure/Error: [[1, 2], [3, 4], [1, 3]].to_hash.should eq(1 => 3, 3 => 4)
       
       expected {1=>3, 3=>4}
            got {}
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -{1=>3, 3=>4}
       +{}
     # /tmp/d20111025-2903-1elpqlv/spec.rb:7: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#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 {}
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -{1=>[2, 3], [4, 5]=>6}
       +{}
     # /tmp/d20111025-2903-1elpqlv/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)>'

  4) Array#to_hash works with boolean keys
     Failure/Error: [nil, 'nil'],
       
       expected {nil=>"nil", true=>"true", false=>"false"}
            got {}
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -{nil=>"nil", true=>"true", false=>"false"}
       +{}
     # /tmp/d20111025-2903-1elpqlv/spec.rb:20: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)>'

  5) Array#index_by indexes the array elemens by a block
     Failure/Error: ['John Coltrane', 'Miles Davis'].index_by { |name| name.split(' ').last }.should eq('Coltrane' => 'John Coltrane', 'Davis' => 'Miles Davis')
       
       expected {"Coltrane"=>"John Coltrane", "Davis"=>"Miles Davis"}
            got {}
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -{"Coltrane"=>"John Coltrane", "Davis"=>"Miles Davis"}
       +{}
     # /tmp/d20111025-2903-1elpqlv/spec.rb:33: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)>'

  6) Array#index_by takes the last element when multiple elements evaluate to the same key
     Failure/Error: [11, 21, 31, 41].index_by { |n| n % 10 }.should eq(1 => 41)
       
       expected {1=>41}
            got {}
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -{1=>41}
       +{}
     # /tmp/d20111025-2903-1elpqlv/spec.rb:37: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)>'

  7) Array#index_by works with false and nil keys
     Failure/Error: [nil, false].index_by { |n| n }.should eq(nil => nil, false => false)
       
       expected {nil=>nil, false=>false}
            got {}
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -{nil=>nil, false=>false}
       +{}
     # /tmp/d20111025-2903-1elpqlv/spec.rb:45: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)>'

  8) Array#subarray_count(subarray) counts the number of times the argument is present as a sub-array
     Failure/Error: [1, 1, 2, 1, 1, 1].subarray_count([1, 1]).should eq 3
       
       expected 3
            got 0
       
       (compared using ==)
     # /tmp/d20111025-2903-1elpqlv/spec.rb:51: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)>'

  9) Array#subarray_count(subarray) works with arrays with non-numeric keys
     Failure/Error: %w[a b c b c].subarray_count(%w[b c]).should eq 2
       
       expected 2
            got 0
       
       (compared using ==)
     # /tmp/d20111025-2903-1elpqlv/spec.rb:55: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)>'

  10) Array#occurences_count counts how many times an element is present in an array
     Failure/Error: [:foo, :bar, :foo].occurences_count.should eq(foo: 2, bar: 1)
     NoMethodError:
       undefined method `occurences_count' for [:foo, :bar, :foo]:Array
     # /tmp/d20111025-2903-1elpqlv/spec.rb:69: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)>'

  11) Array#occurences_count returns a hash that defaults to 0 when the key is not present
     Failure/Error: [].occurences_count[:something].should eq 0
     NoMethodError:
       undefined method `occurences_count' for []:Array
     # /tmp/d20111025-2903-1elpqlv/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)>'

  12) 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)
     NoMethodError:
       undefined method `occurences_count' for [nil, false, nil, false, true]:Array
     # /tmp/d20111025-2903-1elpqlv/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)>'

  13) Array#occurences_count returns a hash, that does not change when indexed with a non-occuring element
     Failure/Error: hash = %w[a a].occurences_count
     NoMethodError:
       undefined method `occurences_count' for ["a", "a"]:Array
     # /tmp/d20111025-2903-1elpqlv/spec.rb:82: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.01967 seconds
17 examples, 13 failures

Failed examples:

rspec /tmp/d20111025-2903-1elpqlv/spec.rb:2 # Array#to_hash converts an array to a hash
rspec /tmp/d20111025-2903-1elpqlv/spec.rb:6 # Array#to_hash uses the last pair when a key is present multiple times
rspec /tmp/d20111025-2903-1elpqlv/spec.rb:10 # Array#to_hash works when the keys and values are arrays
rspec /tmp/d20111025-2903-1elpqlv/spec.rb:18 # Array#to_hash works with boolean keys
rspec /tmp/d20111025-2903-1elpqlv/spec.rb:32 # Array#index_by indexes the array elemens by a block
rspec /tmp/d20111025-2903-1elpqlv/spec.rb:36 # Array#index_by takes the last element when multiple elements evaluate to the same key
rspec /tmp/d20111025-2903-1elpqlv/spec.rb:44 # Array#index_by works with false and nil keys
rspec /tmp/d20111025-2903-1elpqlv/spec.rb:50 # Array#subarray_count(subarray) counts the number of times the argument is present as a sub-array
rspec /tmp/d20111025-2903-1elpqlv/spec.rb:54 # Array#subarray_count(subarray) works with arrays with non-numeric keys
rspec /tmp/d20111025-2903-1elpqlv/spec.rb:68 # Array#occurences_count counts how many times an element is present in an array
rspec /tmp/d20111025-2903-1elpqlv/spec.rb:73 # Array#occurences_count returns a hash that defaults to 0 when the key is not present
rspec /tmp/d20111025-2903-1elpqlv/spec.rb:77 # Array#occurences_count works with arrays containing nil and false
rspec /tmp/d20111025-2903-1elpqlv/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:28 (преди около 13 години)

+class Array
+ def to_hash(*array)
+ h = {}
+ array.each do |n|
+ h.merge!({array[n][0] => array[n][1]})
+ end
+ h
+ end
+
+ def index_by(*array)
+ h = Hash.new
+ array.each do |n|
+ t = yield n
+ h[t] = array[n]
+ end
+ h
+ end
+ def subarray_count(subarray,*array)
+ l = subarray.length
+ x = 0
+ while !(array.empty?)
+ if subarray == array.first(l)
+ x += 1
+ end
+ array.delete_at(0)
+ end
+ x
+ end
+ def ocurrences_count(*array)
+ h = Hash.new
+ array.each do |n|
+ el = array[n]
+ h[n] = array.count(el)
+ end
+ h
+ end
+end