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

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

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

Резултати

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

Код

class Hash
def fetch_deep(path)
keys = path.split('.')
object = self
keys.each { |key| object = next_object(object, key) }
object
end
def reshape(shape)
reshaped_hash = shape.deep? ? deep_reshape(shape) : shallow_reshape(shape)
self.clear
self.merge!(reshaped_hash)
end
def deep?
self.values.any? { |value| value.is_a?(Hash) }
end
private
def shallow_reshape(shape)
reshaped_hash = {}
shape.each { |key, value| reshaped_hash[key] = self.fetch_deep(value) }
reshaped_hash
end
def deep_reshape(shape)
reshaped = {}
shape.each { |key, value| reshaped[key] = proper_hash_from_value(value) }
reshaped
end
def proper_hash_from_value(value)
return deep_reshape(value) if value.deep?
shallow_reshape(value)
end
def next_object(object, key)
return object[key.to_i] if object.is_a?(Array)
object.fetch(key) { object.fetch(key.to_sym, nil) }
end
end
class Array
def reshape(shape)
self.each { |hash| hash.reshape(shape) }
end
end

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

....F........F.

Failures:

  1) 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 `fetch' for nil:NilClass
     # /tmp/d20161024-13689-11niqi/solution.rb:43:in `next_object'
     # /tmp/d20161024-13689-11niqi/solution.rb:6:in `block in fetch_deep'
     # /tmp/d20161024-13689-11niqi/solution.rb:6:in `each'
     # /tmp/d20161024-13689-11niqi/solution.rb:6:in `fetch_deep'
     # /tmp/d20161024-13689-11niqi/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)>'

  2) Task 2 Hash#reshape does not modify the input hash
     Failure/Error: expect(input).to eq menu: {
       
       expected: {:menu=>{:order=>"cake", "dessert"=>"ice cream", 3=>4}}
            got: {:order=>"cake", :dessert=>"ice cream"}
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,3 @@
       -:menu => {:order=>"cake", "dessert"=>"ice cream", 3=>4}
       +:dessert => "ice cream",
       +:order => "cake"
     # /tmp/d20161024-13689-11niqi/spec.rb:196: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.00873 seconds
15 examples, 2 failures

Failed examples:

rspec /tmp/d20161024-13689-11niqi/spec.rb:85 # Task 2 Hash#fetch_deep returns nil for non-existant keys
rspec /tmp/d20161024-13689-11niqi/spec.rb:180 # Task 2 Hash#reshape does not modify the input hash

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

Георги обнови решението на 23.10.2016 19:53 (преди над 7 години)

+class Hash
+ def fetch_deep(path)
+
+ keys = path.split('.')
+ object = self
+ keys.each { |key| object = next_object(object, key) }
+
+ object
+ end
+
+ def reshape(shape)
+ reshaped_hash = shape.deep? ? deep_reshape(shape) : shallow_reshape(shape)
+
+ self.clear
+ self.merge!(reshaped_hash)
+ end
+
+ def deep?
+ self.values.any? { |value| value.is_a?(Hash) }
+ end
+
+ private
+ def shallow_reshape(shape)
+ reshaped_hash = {}
+ shape.each { |key, value| reshaped_hash[key] = self.fetch_deep(value) }
+ reshaped_hash
+ end
+
+ def deep_reshape(shape)
+ reshaped = {}
+ shape.each { |key, value| reshaped[key] = proper_hash_from_value(value) }
+
+ reshaped
+ end
+
+ def proper_hash_from_value(value)
+ return deep_reshape(value) if value.deep?
+ shallow_reshape(value)
+ end
+
+ def next_object(object, key)
+ return object[key.to_i] if object.is_a?(Array)
+ object.fetch(key) { object.fetch(key.to_sym, nil) }
+ end
+end
+
+class Array
+ def reshape(shape)
+ self.each { |hash| hash.reshape(shape) }
+ end
+end