Решение на Втора задача - хешове, масиви и структура от Валентин Георгиев

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

Към профила на Валентин Георгиев

Резултати

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

Код

class Hash
def fetch_deep(hash_path)
hash_params = hash_path.split('.')
this = self
hash_params.each do |param|
param = /\A\d+\z/.match(param) ? param.to_i : param.to_sym
this = this[param]
end
this
end
def set_hash_value(result_format, key, keys, value)
keys.map!(&:to_sym)
key = keys.pop
keys.inject(result_format, :fetch)[key] = self.fetch_deep(value)
end
def recursive_traverse(hash, result_format, keys)
hash.each do |key, value|
keys.push(key)
if value.is_a?(Hash) || value.is_a?(Array)
recursive_traverse(value, result_format, keys)
else
self.set_hash_value(result_format, key, keys, value)
end
end
end
def reshape(result_format)
keys = []
returned_hash = result_format.clone
self.recursive_traverse(returned_hash, returned_hash, keys)
returned_hash
end
end
class Array
def reshape(shape)
format_result = []
self.each { |item| format_result.push(item.reshape(shape)) }
format_result
end
end

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

F...FF.F.......

Failures:

  1) Task 2 README works for all examples
     Failure/Error: expect(dessert.fetch_deep('variant')).to eq('chocolate')
       
       expected: "chocolate"
            got: nil
       
       (compared using ==)
     # /tmp/d20161024-13689-1kmop13/spec.rb:25:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  2) Task 2 Hash#fetch_deep returns nil for non-existant keys
     Failure/Error: expect(input.fetch_deep('meal.0.type')).to be nil
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # /tmp/d20161024-13689-1kmop13/solution.rb:8:in `block in fetch_deep'
     # /tmp/d20161024-13689-1kmop13/solution.rb:6:in `each'
     # /tmp/d20161024-13689-1kmop13/solution.rb:6:in `fetch_deep'
     # /tmp/d20161024-13689-1kmop13/spec.rb:89:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  3) Task 2 Hash#fetch_deep is indifferent to symbols and strings
     Failure/Error: expect(input.fetch_deep('dessert')).to eq 'ice cream'
       
       expected: "ice cream"
            got: nil
       
       (compared using ==)
     # /tmp/d20161024-13689-1kmop13/spec.rb:96:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  4) Task 2 Hash#fetch_deep can fetch integer-like keys from hashes
     Failure/Error: expect(input.fetch_deep('nested.1')).to eq :a
       
       expected: :a
            got: nil
       
       (compared using ==)
     # /tmp/d20161024-13689-1kmop13/spec.rb:111:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

Finished in 0.00806 seconds
15 examples, 4 failures

Failed examples:

rspec /tmp/d20161024-13689-1kmop13/spec.rb:3 # Task 2 README works for all examples
rspec /tmp/d20161024-13689-1kmop13/spec.rb:85 # Task 2 Hash#fetch_deep returns nil for non-existant keys
rspec /tmp/d20161024-13689-1kmop13/spec.rb:92 # Task 2 Hash#fetch_deep is indifferent to symbols and strings
rspec /tmp/d20161024-13689-1kmop13/spec.rb:108 # Task 2 Hash#fetch_deep can fetch integer-like keys from hashes

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

Валентин обнови решението на 23.10.2016 16:30 (преди над 7 години)

+class Hash
+ def fetch_deep(hash_path)
+ hash_params = hash_path.split('.')
+ this = self
+
+ hash_params.each do |param|
+ param = /\A\d+\z/.match(param) ? param.to_i : param.to_sym
+ this = this[param]
+ end
+
+ this
+ end
+
+ def set_hash_value(result_format, key, keys, value)
+ keys.map!(&:to_sym)
+ key = keys.pop
+ keys.inject(result_format, :fetch)[key] = self.fetch_deep(value)
+ end
+
+ def recursive_traverse(hash, result_format, keys)
+
+ hash.each do |key, value|
+ keys.push(key)
+ if value.is_a?(Hash) || value.is_a?(Array)
+ recursive_traverse(value, result_format, keys)
+ else
+ self.set_hash_value(result_format, key, keys, value)
+ end
+ end
+
+ end
+
+ def reshape(result_format)
+
+ keys = []
+ returned_hash = result_format.clone
+
+ self.recursive_traverse(returned_hash, returned_hash, keys)
+
+ returned_hash
+ end
+end
+
+class Array
+ def reshape(shape)
+ format_result = []
+ self.each { |item| format_result.push(item.reshape(shape)) }
+ format_result
+ end
+end
+
+