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

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

Към профила на Станимира Христова

Резултати

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

Код

class Array
def to_hash(array)
result={}
array.each do |item|
key=item[0]
value=item[1]
result[key]=value
end
return result
end
def index_by(array , block)
result={}
block_new = lambda block
array.each do |item|
key=block_new.call item
result[key]=item
end
return result
end
def subarray_count(array,subaaray)
result=0
count=subarray.count
array.first(count) do |item|
if item==subarray
result+=1
end
array.shift
end
return result
end
def occurences_count(array)
result={}
array.each do |item|
if result.has_key?(item)
result[item]=result[item]+1
else
result[item]=1
end
end
return result
end
end

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

FFFFFFFFFFFFFFFFF

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)
     ArgumentError:
       wrong number of arguments (0 for 1)
     # /tmp/d20111025-2903-1g5hv44/solution.rb:2:in `to_hash'
     # /tmp/d20111025-2903-1g5hv44/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)
     ArgumentError:
       wrong number of arguments (0 for 1)
     # /tmp/d20111025-2903-1g5hv44/solution.rb:2:in `to_hash'
     # /tmp/d20111025-2903-1g5hv44/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)
     ArgumentError:
       wrong number of arguments (0 for 1)
     # /tmp/d20111025-2903-1g5hv44/solution.rb:2:in `to_hash'
     # /tmp/d20111025-2903-1g5hv44/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 on empty arrays
     Failure/Error: [].to_hash.should eq({})
     ArgumentError:
       wrong number of arguments (0 for 1)
     # /tmp/d20111025-2903-1g5hv44/solution.rb:2:in `to_hash'
     # /tmp/d20111025-2903-1g5hv44/spec.rb:15: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#to_hash works with boolean keys
     Failure/Error: [nil, 'nil'],
     ArgumentError:
       wrong number of arguments (0 for 1)
     # /tmp/d20111025-2903-1g5hv44/solution.rb:2:in `to_hash'
     # /tmp/d20111025-2903-1g5hv44/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)>'

  6) 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')
     ArgumentError:
       wrong number of arguments (0 for 2)
     # /tmp/d20111025-2903-1g5hv44/solution.rb:12:in `index_by'
     # /tmp/d20111025-2903-1g5hv44/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)>'

  7) 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)
     ArgumentError:
       wrong number of arguments (0 for 2)
     # /tmp/d20111025-2903-1g5hv44/solution.rb:12:in `index_by'
     # /tmp/d20111025-2903-1g5hv44/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)>'

  8) Array#index_by works on empty arrays
     Failure/Error: [].index_by { |n| :something }.should eq({})
     ArgumentError:
       wrong number of arguments (0 for 2)
     # /tmp/d20111025-2903-1g5hv44/solution.rb:12:in `index_by'
     # /tmp/d20111025-2903-1g5hv44/spec.rb:41: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#index_by works with false and nil keys
     Failure/Error: [nil, false].index_by { |n| n }.should eq(nil => nil, false => false)
     ArgumentError:
       wrong number of arguments (0 for 2)
     # /tmp/d20111025-2903-1g5hv44/solution.rb:12:in `index_by'
     # /tmp/d20111025-2903-1g5hv44/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)>'

  10) 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
     ArgumentError:
       wrong number of arguments (1 for 2)
     # /tmp/d20111025-2903-1g5hv44/solution.rb:22:in `subarray_count'
     # /tmp/d20111025-2903-1g5hv44/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)>'

  11) 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
     ArgumentError:
       wrong number of arguments (1 for 2)
     # /tmp/d20111025-2903-1g5hv44/solution.rb:22:in `subarray_count'
     # /tmp/d20111025-2903-1g5hv44/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)>'

  12) Array#subarray_count(subarray) work with empty arrays
     Failure/Error: [].subarray_count([1]).should be_zero
     ArgumentError:
       wrong number of arguments (1 for 2)
     # /tmp/d20111025-2903-1g5hv44/solution.rb:22:in `subarray_count'
     # /tmp/d20111025-2903-1g5hv44/spec.rb:59: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#subarray_count(subarray) work when the argument is larger than the array
     Failure/Error: [1].subarray_count([1, 2]).should eq 0
     ArgumentError:
       wrong number of arguments (1 for 2)
     # /tmp/d20111025-2903-1g5hv44/solution.rb:22:in `subarray_count'
     # /tmp/d20111025-2903-1g5hv44/spec.rb:63: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)>'

  14) 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)
     ArgumentError:
       wrong number of arguments (0 for 1)
     # /tmp/d20111025-2903-1g5hv44/solution.rb:35:in `occurences_count'
     # /tmp/d20111025-2903-1g5hv44/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)>'

  15) Array#occurences_count returns a hash that defaults to 0 when the key is not present
     Failure/Error: [].occurences_count[:something].should eq 0
     ArgumentError:
       wrong number of arguments (0 for 1)
     # /tmp/d20111025-2903-1g5hv44/solution.rb:35:in `occurences_count'
     # /tmp/d20111025-2903-1g5hv44/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)>'

  16) 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)
     ArgumentError:
       wrong number of arguments (0 for 1)
     # /tmp/d20111025-2903-1g5hv44/solution.rb:35:in `occurences_count'
     # /tmp/d20111025-2903-1g5hv44/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)>'

  17) 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
     ArgumentError:
       wrong number of arguments (0 for 1)
     # /tmp/d20111025-2903-1g5hv44/solution.rb:35:in `occurences_count'
     # /tmp/d20111025-2903-1g5hv44/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.01326 seconds
17 examples, 17 failures

Failed examples:

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

+class Array
+ def to_hash(array)
+ result={}
+ array.each do |item|
+ key=item[0]
+ value=item[1]
+ result[key]=value
+ end
+ return result
+ end
+
+ def index_by(array , block)
+ result={}
+ block_new = lambda block
+ array.each do |item|
+ key=block_new.call item
+ result[key]=item
+ end
+ return result
+ end
+
+ def subarray_count(array,subaaray)
+ result=0
+ count=subarray.count
+ array.first(count) do |item|
+ if item==subarray
+ result+=1
+ end
+ array.shift
+ end
+ return result
+ end
+
+
+ def occurences_count(array)
+ result={}
+ array.each do |item|
+ if result.has_key?(item)
+ result[item]=result[item]+1
+ else
+ result[item]=1
+ end
+ end
+
+ return result
+ end
+end