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

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

Към профила на Беатрис Бонева

Резултати

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

Код

class Hash
def fetch_deep(path)
head, tail = *path.strip.reverse.chomp('.').reverse.chomp('.').split('.', 2)
value = [dig(head), dig(head.to_sym)].find(&:itself)
return value unless tail
if value.is_a?(Hash)
value.fetch_deep(tail)
elsif value.is_a?(Array)
Hash[(0..(value.size - 1)).map { |x| x.to_s }.zip(value)].fetch_deep(tail)
end
end
def reshape(structure)
result = structure.clone
result.each do |key, value|
result[key] = value.is_a?(Hash) ? self.reshape(value) : fetch_deep(value)
end
end
end
class Array
def reshape(structure)
each_with_index { |val, index| self[index] = val.reshape(structure) }
end
end

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

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

Finished in 0.00846 seconds
15 examples, 0 failures

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

Беатрис обнови решението на 24.10.2016 16:58 (преди над 7 години)

+class Hash
+ def fetch_deep(path)
+ head, tail = *path.strip.reverse.chomp('.').reverse.chomp('.').split('.', 2)
+ value = [dig(head), dig(head.to_sym)].find(&:itself)
+ return value unless tail
+ if value.is_a?(Hash)
+ value.fetch_deep(tail)
+ elsif value.is_a?(Array)
+ Hash[(0..(value.size - 1)).map { |x| x.to_s }.zip(value)].fetch_deep(tail)
+ end
+ end
+
+ def reshape(structure)
+ result = structure.clone
+ result.each do |key, value|
+ result[key] = value.is_a?(Hash) ? self.reshape(value) : fetch_deep(value)
+ end
+ end
+end
+
+class Array
+ def reshape(structure)
+ each_with_index { |val, index| self[index] = val.reshape(structure) }
+ end
+end