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

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

Към профила на Светослав Годжев

Резултати

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

Код

class Hash
def fetch_deep(path)
split_path = path.split('.')
hash_dup = self.dup
fetch_deep_hash(hash_dup, split_path)
end
def keys_to_strings
result = {}
self.each do |key, value|
result[key.to_s] = value
end
result
end
def reshape(shape)
shape.map do |key, value|
if value.is_a?(Hash)
[ key, self.reshape(value) ]
else
[ key, self.fetch_deep(value) ]
end
end.to_h
end
private
def fetch_deep_helper(input, path)
if input.is_a?(Hash)
fetch_deep_hash(input, path)
elsif input.is_a?(Array)
fetch_deep_arr(input, path)
end
end
def fetch_deep_hash(hash, path)
hash = hash.keys_to_strings
if path.length == 1
hash[ path[0] ]
else
p = path.shift
fetch_deep_helper(hash[ p ], path)
end
end
def fetch_deep_arr(arr, path)
if path.length == 1
arr[ path[0].to_i ]
else
p = path.shift
fetch_deep_helper(arr[ p.to_i ], path)
end
end
end
class Array
def reshape(shape)
result = []
self.each do |n|
n = n.reshape(shape)
result << n
end
result
end
end

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

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

Finished in 0.00972 seconds
15 examples, 0 failures

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

Светослав обнови решението на 20.10.2016 19:30 (преди над 7 години)

+class Hash
+ def fetch_deep(path)
+ split_path = path.split('.')
+ hash_dup = self.dup
+ fetch_deep_hash(hash_dup, split_path)
+ end
+
+ def keys_to_strings
+ result = {}
+ self.each do |key, value|
+ result[key.to_s] = value
+ end
+ result
+ end
+
+ def reshape(shape)
+ shape.map do |key, value|
+ if value.is_a?(Hash)
+ [ key, self.reshape(value) ]
+ else
+ [ key, self.fetch_deep(value) ]
+ end
+ end.to_h
+ end
+
+ private
+
+ def fetch_deep_helper(input, path)
+ if input.is_a?(Hash)
+ fetch_deep_hash(input, path)
+ elsif input.is_a?(Array)
+ fetch_deep_arr(input, path)
+ end
+ end
+
+ def fetch_deep_hash(hash, path)
+ hash = hash.keys_to_strings
+ if path.length == 1
+ hash[ path[0] ]
+ else
+ p = path.shift
+ fetch_deep_helper(hash[ p ], path)
+ end
+ end
+
+ def fetch_deep_arr(arr, path)
+ if path.length == 1
+ arr[ path[0].to_i ]
+ else
+ p = path.shift
+ fetch_deep_helper(arr[ p.to_i ], path)
+ end
+ end
+
+end
+
+class Array
+ def reshape(shape)
+ result = []
+ self.each do |n|
+ n = n.reshape(shape)
+ result << n
+ end
+ result
+ end
+end