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

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

Към профила на Виктор Маринов

Резултати

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

Код

class Hash
def fetch_deep(path)
fetch_deep_with_cast(path) { |s| s.to_sym }
end
def reshape(shape)
shape.each_with_object({}) do |(k, v), hash|
hash[k] = (v.is_a? String) ? self.fetch_deep(v) : reshape(v)
end
end
end
class Array
def fetch_deep(path)
fetch_deep_with_cast(path) { |s| s.to_i }
end
def reshape(shape)
clone.map { |hash| hash.reshape(shape) }
end
end
def fetch_deep_with_cast(path)
head = path.partition('.').first
tail = path.partition('.').last
if tail.empty?
self[yield(head)] || self[head]
else
self[yield(head)]&.fetch_deep(tail) || self[head]&.fetch_deep(tail)
end
end

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

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

Finished in 0.03859 seconds
15 examples, 0 failures

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

Виктор обнови решението на 22.10.2016 02:42 (преди над 7 години)

+class Hash
+ def fetch_deep(path)
+ fetch_deep_and_parse(path) { |s| s.to_sym }
+ end
+
+ def reshape(shape)
+ shape.each_with_object({}) do |(k, v), hash|
+ hash[k] = if v.is_a? String
+ self.fetch_deep(v)
+ else
+ reshape(v)
+ end
+ end
+ end
+end
+
+class Array
+ def fetch_deep(path)
+ fetch_deep_and_parse(path) { |s| s.to_i }
+ end
+
+ def reshape(shape)
+ clone.map { |hash| hash.reshape(shape) }
+ end
+end
+
+def fetch_deep_and_parse(path)
+ head = path.partition('.').first
+ tail = path.partition('.').last
+ if tail.empty?
+ self[yield(head)] || self[head]
+ else
+ self[yield(head)]&.fetch_deep(tail) || self[head]&.fetch_deep(tail)
+ end
+end

Виктор обнови решението на 22.10.2016 17:20 (преди над 7 години)

class Hash
def fetch_deep(path)
fetch_deep_and_parse(path) { |s| s.to_sym }
end
def reshape(shape)
shape.each_with_object({}) do |(k, v), hash|
- hash[k] = if v.is_a? String
- self.fetch_deep(v)
- else
- reshape(v)
- end
+ hash[k] = (v.is_a? String) ? self.fetch_deep(v) : reshape(v)
end
end
end
class Array
def fetch_deep(path)
fetch_deep_and_parse(path) { |s| s.to_i }
end
def reshape(shape)
clone.map { |hash| hash.reshape(shape) }
end
end
def fetch_deep_and_parse(path)
head = path.partition('.').first
tail = path.partition('.').last
if tail.empty?
self[yield(head)] || self[head]
else
self[yield(head)]&.fetch_deep(tail) || self[head]&.fetch_deep(tail)
end
end

Виктор обнови решението на 24.10.2016 16:39 (преди над 7 години)

class Hash
def fetch_deep(path)
- fetch_deep_and_parse(path) { |s| s.to_sym }
+ fetch_deep_with_cast(path) { |s| s.to_sym }
end
def reshape(shape)
shape.each_with_object({}) do |(k, v), hash|
hash[k] = (v.is_a? String) ? self.fetch_deep(v) : reshape(v)
end
end
end
class Array
def fetch_deep(path)
- fetch_deep_and_parse(path) { |s| s.to_i }
+ fetch_deep_with_cast(path) { |s| s.to_i }
end
def reshape(shape)
clone.map { |hash| hash.reshape(shape) }
end
end
-def fetch_deep_and_parse(path)
+def fetch_deep_with_cast(path)
head = path.partition('.').first
tail = path.partition('.').last
if tail.empty?
self[yield(head)] || self[head]
else
self[yield(head)]&.fetch_deep(tail) || self[head]&.fetch_deep(tail)
end
end