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

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

Към профила на Михаил Илиев

Резултати

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

Код

class Array
def to_hash
result = {}
self.each { |a, b| result = result.merge( Hash[a, b]) }
result
end
def occurences_count
result = Hash.new(0)
self.each { |a| result = result.merge( Hash[a, self.count(a)]) }
result
end
end

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

.....FFFFFFFF....

Failures:

  1) 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')
     NoMethodError:
       undefined method `index_by' for ["John Coltrane", "Miles Davis"]:Array
     # /tmp/d20111025-2903-1bgtaun/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)>'

  2) 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)
     NoMethodError:
       undefined method `index_by' for [11, 21, 31, 41]:Array
     # /tmp/d20111025-2903-1bgtaun/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)>'

  3) Array#index_by works on empty arrays
     Failure/Error: [].index_by { |n| :something }.should eq({})
     NoMethodError:
       undefined method `index_by' for []:Array
     # /tmp/d20111025-2903-1bgtaun/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)>'

  4) Array#index_by works with false and nil keys
     Failure/Error: [nil, false].index_by { |n| n }.should eq(nil => nil, false => false)
     NoMethodError:
       undefined method `index_by' for [nil, false]:Array
     # /tmp/d20111025-2903-1bgtaun/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)>'

  5) 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
     NoMethodError:
       undefined method `subarray_count' for [1, 1, 2, 1, 1, 1]:Array
     # /tmp/d20111025-2903-1bgtaun/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)>'

  6) 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
     NoMethodError:
       undefined method `subarray_count' for ["a", "b", "c", "b", "c"]:Array
     # /tmp/d20111025-2903-1bgtaun/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)>'

  7) Array#subarray_count(subarray) work with empty arrays
     Failure/Error: [].subarray_count([1]).should be_zero
     NoMethodError:
       undefined method `subarray_count' for []:Array
     # /tmp/d20111025-2903-1bgtaun/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)>'

  8) Array#subarray_count(subarray) work when the argument is larger than the array
     Failure/Error: [1].subarray_count([1, 2]).should eq 0
     NoMethodError:
       undefined method `subarray_count' for [1]:Array
     # /tmp/d20111025-2903-1bgtaun/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)>'

Finished in 0.01335 seconds
17 examples, 8 failures

Failed examples:

rspec /tmp/d20111025-2903-1bgtaun/spec.rb:32 # Array#index_by indexes the array elemens by a block
rspec /tmp/d20111025-2903-1bgtaun/spec.rb:36 # Array#index_by takes the last element when multiple elements evaluate to the same key
rspec /tmp/d20111025-2903-1bgtaun/spec.rb:40 # Array#index_by works on empty arrays
rspec /tmp/d20111025-2903-1bgtaun/spec.rb:44 # Array#index_by works with false and nil keys
rspec /tmp/d20111025-2903-1bgtaun/spec.rb:50 # Array#subarray_count(subarray) counts the number of times the argument is present as a sub-array
rspec /tmp/d20111025-2903-1bgtaun/spec.rb:54 # Array#subarray_count(subarray) works with arrays with non-numeric keys
rspec /tmp/d20111025-2903-1bgtaun/spec.rb:58 # Array#subarray_count(subarray) work with empty arrays
rspec /tmp/d20111025-2903-1bgtaun/spec.rb:62 # Array#subarray_count(subarray) work when the argument is larger than the array

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

Михаил обнови решението на 24.10.2011 16:26 (преди около 13 години)

+class Array
+ def to_hash
+ result = {}
+ self.each { |a, b| result = result.merge( Hash[a, b]) }
+ result
+ end
+
+ def occurences_count
+ result = Hash.new(0)
+ self.each { |a| result = result.merge( Hash[a, self.count(a)]) }
+ result
+ end
+end

Оправено е - заспах докато чета Markdown :)

/off Исках да изтрия коментара, защото виждах, че няма да го оправя, но нямаше такава възможност. Добавих issue.

/off Не е ли bug, че сегашния ми коментар го показва между нашите два от тази нощ?

В коментара си пропуснал едно end, обозначаващо края на to_hash, а идентацията на end-а на occurances_count е малко счупена.

Иначе — да, по-добре е от предаденото :)

П.П: Редът на коментарите вече е окей, благодаря за отбелязването.