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

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

Към профила на Елеонора Кайкова

Резултати

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

Код

class Hash
def fetch_deep(path)
path_array = path.split('.')
find_path(path_array, self)
end
end
def find_path(path_array, object)
return object if path_array.empty? || !object
if object.class == Array
find_path(path_array.drop(1), object[path_array[0].to_i])
else
find_path(path_array.drop(1), object[path_array[0].to_sym]) ||
find_path(path_array.drop(1), object[path_array[0]])
end
end
class Hash
def reshape(shape)
shape.each_key do |key|
shape[key] = if shape[key].class == String
fetch_deep(shape[key])
else
reshape(shape[key])
end
end
shape
end
end
class Array
def reshape(shape)
each.map { |n| n.reshape(shape.clone) }
end
end

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

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

Finished in 0.00815 seconds
15 examples, 0 failures

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

Елеонора обнови решението на 23.10.2016 19:08 (преди над 7 години)

+class Hash
+ def fetch_deep(path)
+ path_array = path.split('.')
+
+ find_path(path_array, self)
+ end
+end
+
+def find_path(path_array, object)
+ return object if path_array.empty? || !object
+
+ if object.class == Array
+ find_path(path_array.drop(1), object[path_array[0].to_i])
+ else
+ find_path(path_array.drop(1), object[path_array[0].to_sym]) ||
+ find_path(path_array.drop(1), object[path_array[0]])
+ end
+end
+
+class Hash
+ def reshape(shape)
+ shape.each_key do |key|
+ shape[key] = if shape[key].class == String
+ fetch_deep(shape[key])
+ else
+ reshape(shape[key])
+ end
+ end
+ shape
+ end
+end
+
+class Array
+ def reshape(shape)
+ each.map { |n| n.reshape(shape.clone) }
+ end
+end