Решение на Втора задача - хешове, масиви и структура от Натали Арабаджийска

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

Към профила на Натали Арабаджийска

Резултати

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

Код

class Hash
def key_to_sym(myhash)
myhash.keys.each do |key|
myhash[(key.to_sym rescue key) || key] = myhash.delete(key)
end
myhash
end
def fetch_deep(path)
path.split('.').reduce(key_to_sym(self)) do |memo, key|
if memo.is_a? Array
memo[key.to_i]
else
key_to_sym(memo).fetch key.to_sym
end
end
end
def get_values(order, shape)
shape.map do |key, value|
if value.is_a? Hash
get_values(order, value)
else
shape[key] = order.fetch_deep(value)
end
end
end
def change(shape)
self.replace(shape)
end
def reshape(shape)
get_values(self, shape)
change(shape)
end
end
class Array
def reshape(newshape)
self.map do |arr_hash|
arr_hash.reshape(newshape.dup) if arr_hash.is_a? Hash
end
end
end

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

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

Failures:

  1) Task 2 Hash#fetch_deep returns nil for non-existant keys
     Failure/Error: expect(input.fetch_deep('meal')).to be nil
     KeyError:
       key not found: :meal
     # /tmp/d20161024-13689-1yb8dfv/solution.rb:14:in `fetch'
     # /tmp/d20161024-13689-1yb8dfv/solution.rb:14:in `block in fetch_deep'
     # /tmp/d20161024-13689-1yb8dfv/solution.rb:10:in `each'
     # /tmp/d20161024-13689-1yb8dfv/solution.rb:10:in `reduce'
     # /tmp/d20161024-13689-1yb8dfv/solution.rb:10:in `fetch_deep'
     # /tmp/d20161024-13689-1yb8dfv/spec.rb:88: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 does not modify the input hash
     Failure/Error: expect(input).to eq menu: {order: 'cake', 'dessert' => 'ice cream', 3 => 4}
       
       expected: {:menu=>{:order=>"cake", "dessert"=>"ice cream", 3=>4}}
            got: {:menu=>{:order=>"cake", :dessert=>"ice cream", 3=>4}}
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -:menu => {:order=>"cake", "dessert"=>"ice cream", 3=>4}
       +:menu => {:order=>"cake", :dessert=>"ice cream", 3=>4}
     # /tmp/d20161024-13689-1yb8dfv/spec.rb:105: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#reshape assigns nil to unknown keys
     Failure/Error: expect(input.reshape(shape)).to eq b: nil
     KeyError:
       key not found: :b
     # /tmp/d20161024-13689-1yb8dfv/solution.rb:14:in `fetch'
     # /tmp/d20161024-13689-1yb8dfv/solution.rb:14:in `block in fetch_deep'
     # /tmp/d20161024-13689-1yb8dfv/solution.rb:10:in `each'
     # /tmp/d20161024-13689-1yb8dfv/solution.rb:10:in `reduce'
     # /tmp/d20161024-13689-1yb8dfv/solution.rb:10:in `fetch_deep'
     # /tmp/d20161024-13689-1yb8dfv/solution.rb:24:in `block in get_values'
     # /tmp/d20161024-13689-1yb8dfv/solution.rb:20:in `each'
     # /tmp/d20161024-13689-1yb8dfv/solution.rb:20:in `map'
     # /tmp/d20161024-13689-1yb8dfv/solution.rb:20:in `get_values'
     # /tmp/d20161024-13689-1yb8dfv/solution.rb:34:in `reshape'
     # /tmp/d20161024-13689-1yb8dfv/spec.rb:153: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#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-1yb8dfv/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.00877 seconds
15 examples, 4 failures

Failed examples:

rspec /tmp/d20161024-13689-1yb8dfv/spec.rb:85 # Task 2 Hash#fetch_deep returns nil for non-existant keys
rspec /tmp/d20161024-13689-1yb8dfv/spec.rb:99 # Task 2 Hash#fetch_deep does not modify the input hash
rspec /tmp/d20161024-13689-1yb8dfv/spec.rb:149 # Task 2 Hash#reshape assigns nil to unknown keys
rspec /tmp/d20161024-13689-1yb8dfv/spec.rb:180 # Task 2 Hash#reshape does not modify the input hash

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

Натали обнови решението на 24.10.2016 00:02 (преди над 7 години)

+class Hash
+ def key_to_sym(myhash)
+ myhash.keys.each do |key|
+ myhash[(key.to_sym rescue key) || key] = myhash.delete(key)
+ end
+ myhash
+ end
+
+ def fetch_deep(path)
+ path.split('.').reduce(key_to_sym(self)) do |memo, key|
+ if memo.is_a? Array
+ memo[key.to_i]
+ else
+ key_to_sym(memo).fetch key.to_sym
+ end
+ end
+ end
+
+ def get_values(order, shape)
+ shape.map do |key, value|
+ if value.is_a? Hash
+ get_values(order, value)
+ else
+ shape[key] = order.fetch_deep(value)
+ end
+ end
+ end
+
+ def change(shape)
+ self.replace(shape)
+ end
+
+ def reshape(shape)
+ get_values(self, shape)
+ change(shape)
+ end
+end
+
+class Array
+ def reshape(newshape)
+ self.map do |arr_hash|
+ arr_hash.reshape(newshape.dup) if arr_hash.is_a? Hash
+ end
+ end
+end