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

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

Към профила на Йордан Пулов

Резултати

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

Код

def convert_entry_keys_to_sym(entry)
if entry.is_a?(Hash) || entry.is_a?(Array)
entry.convert_keys_to_sym
else
entry
end
end
class Hash
def convert_keys_to_sym
keys.map do |key|
[
key.to_sym,
convert_entry_keys_to_sym(self[key])
]
end.to_h
end
def fetch_deep(path)
path_value = self.convert_keys_to_sym
path.split(".").map(&:to_sym).each do |item|
path_value = path_value[item]
end
path_value
end
def reshape(new_shape)
new_shape.keys.map do |new_key|
path = new_shape[new_key]
[new_key, path.is_a?(Hash) ? reshape(path) : fetch_deep(path) ]
end.to_h
end
end
class Array
def convert_keys_to_sym
map.with_index do |val, index|
[index.to_s.to_sym, val]
end.to_h
end
def reshape(new_shape)
map { |item| item.reshape(new_shape) }
end
end

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

....F.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 `[]' for nil:NilClass
     # /tmp/d20161024-13689-18z5xez/solution.rb:22:in `block in fetch_deep'
     # /tmp/d20161024-13689-18z5xez/solution.rb:21:in `each'
     # /tmp/d20161024-13689-18z5xez/solution.rb:21:in `fetch_deep'
     # /tmp/d20161024-13689-18z5xez/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#fetch_deep does not modify the input hash
     Failure/Error: input.fetch_deep('menu.order')
     NoMethodError:
       undefined method `to_sym' for 3:Fixnum
     # /tmp/d20161024-13689-18z5xez/solution.rb:13:in `block in convert_keys_to_sym'
     # /tmp/d20161024-13689-18z5xez/solution.rb:11:in `map'
     # /tmp/d20161024-13689-18z5xez/solution.rb:11:in `convert_keys_to_sym'
     # /tmp/d20161024-13689-18z5xez/solution.rb:3:in `convert_entry_keys_to_sym'
     # /tmp/d20161024-13689-18z5xez/solution.rb:14:in `block in convert_keys_to_sym'
     # /tmp/d20161024-13689-18z5xez/solution.rb:11:in `map'
     # /tmp/d20161024-13689-18z5xez/solution.rb:11:in `convert_keys_to_sym'
     # /tmp/d20161024-13689-18z5xez/solution.rb:20:in `fetch_deep'
     # /tmp/d20161024-13689-18z5xez/spec.rb:102: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 does not modify the input hash
     Failure/Error: input.reshape(shape)
     NoMethodError:
       undefined method `to_sym' for 3:Fixnum
     # /tmp/d20161024-13689-18z5xez/solution.rb:13:in `block in convert_keys_to_sym'
     # /tmp/d20161024-13689-18z5xez/solution.rb:11:in `map'
     # /tmp/d20161024-13689-18z5xez/solution.rb:11:in `convert_keys_to_sym'
     # /tmp/d20161024-13689-18z5xez/solution.rb:3:in `convert_entry_keys_to_sym'
     # /tmp/d20161024-13689-18z5xez/solution.rb:14:in `block in convert_keys_to_sym'
     # /tmp/d20161024-13689-18z5xez/solution.rb:11:in `map'
     # /tmp/d20161024-13689-18z5xez/solution.rb:11:in `convert_keys_to_sym'
     # /tmp/d20161024-13689-18z5xez/solution.rb:20:in `fetch_deep'
     # /tmp/d20161024-13689-18z5xez/solution.rb:30:in `block in reshape'
     # /tmp/d20161024-13689-18z5xez/solution.rb:28:in `map'
     # /tmp/d20161024-13689-18z5xez/solution.rb:28:in `reshape'
     # /tmp/d20161024-13689-18z5xez/spec.rb:194: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.00821 seconds
15 examples, 3 failures

Failed examples:

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

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

Йордан обнови решението на 19.10.2016 22:37 (преди около 8 години)

+def convert_entry_keys_to_sym(entry)
+ if entry.is_a?(Hash) || entry.is_a?(Array)
+ entry.convert_keys_to_sym
+ else
+ entry
+ end
+end
+
+class Hash
+ def convert_keys_to_sym
+ keys.map do |key|
+ [
+ key.to_sym,
+ convert_entry_keys_to_sym(self[key])
+ ]
+ end.to_h
+ end
+
+ def fetch_deep(path)
+ path_value = self.convert_keys_to_sym
+ path.split(".").map(&:to_sym).each do |item|
+ path_value = path_value[item]
+ end
+ path_value
+ end
+
+ def reshape(new_shape)
+ new_shape.keys.map do |new_key|
+ path = new_shape[new_key]
+ [new_key, path.is_a?(Hash) ? reshape(path) : fetch_deep(path) ]
+ end.to_h
+ end
+end
+
+class Array
+ def convert_keys_to_sym
+ map.with_index do |val, index|
+ [index.to_s.to_sym, val]
+ end.to_h
+ end
+
+ def reshape(new_shape)
+ map { |item| item.reshape(new_shape) }
+ end
+end