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

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

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

Резултати

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

Код

class Array
def reshape(shape)
map { |value| value.reshape(shape) }
end
def fetch_deep(key_path)
key, nested_key_path = key_path.split('.', 2)
element = self[key.to_i]
element.fetch_deep(nested_key_path) if element
end
end
class Hash
def reshape(shape)
return fetch_deep(shape) if shape.is_a? String
shape.map do |new_key, shape|
[new_key, reshape(shape)]
end.to_h
end
def fetch_deep(key_path)
key, nested_key_path = key_path.split('.', 2)
value = self[key.to_sym] || self[key.to_s]
return value unless nested_key_path
value.fetch_deep(nested_key_path) if value
end
end

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

...............

Finished in 0.00786 seconds
15 examples, 0 failures

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

Георги обнови решението на 18.10.2016 21:18 (преди над 7 години)

+class Array
+ def reshape(shape)
+ map { |value| value.reshape(shape) }
+ end
+
+ def fetch_deep(key_path)
+ key, nested_key_path = key_path.split('.', 2)
+ element = self[key.to_i]
+
+ element.fetch_deep(nested_key_path) if element
+ end
+end
+
+class Hash
+ def reshape(shape)
+ return fetch_deep(shape) if shape.is_a? String
+
+ shape.map do |new_key, shape|
+ [new_key, reshape(shape)]
+ end.to_h
+ end
+
+ def fetch_deep(key_path)
+ key, nested_key_path = key_path.split('.', 2)
+ value = self[key.to_sym] || self[key.to_s]
+
+ return value unless nested_key_path
+
+ value.fetch_deep(nested_key_path) if value
+ end
+end