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

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

Към профила на Петър Велев

Резултати

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

Код

class Hash
def fetch_deep(path)
path.split('.').reduce(self) do |memo, key|
memo[key.to_i] || memo[key.to_sym] || memo[key.to_s] if memo
end
end
end
class Hash
def reshape(shape)
shape.each do |key, value|
if shape[key].is_a?(String)
shape[key] = self.fetch_deep(value)
else
self.each { shape[key] = self.reshape(shape[key]) }
end
end
end
end
class Array
def reshape(shape)
self.map { |i| i.reshape(shape.clone) }
end
end

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

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

Finished in 0.00944 seconds
15 examples, 0 failures

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

Петър обнови решението на 20.10.2016 08:20 (преди над 7 години)

+class Hash
+ def fetch_deep(path)
+ path.split('.').reduce(self) do |memo, key|
+ memo[key.to_i] || memo[key.to_sym] || memo[key.to_s] if memo
+ end
+ end
+end
+
+# class Hash
+# def fetch_deep(path)
+# cpy = self.clone
+# path.split('.').each do |t|
+# break unless cpy = cpy[t.to_i] || cpy[t.to_sym] || cpy[t.to_s]
+# end
+# return cpy
+# end
+# end
+
+class Hash
+ def reshape(shape)
+ sh = shape.clone
+ sh.each do |key, value|
+ if sh[key].is_a?(String)
+ sh[key] = self.fetch_deep(value)
+ else
+ self.each { sh[key] = self.reshape(sh[key]) }
+ end
+ end
+ end
+end
+
+class Array
+ def reshape(shape)
+ sh = shape.clone
+ self.map { |i| i.reshape(sh) }
+ end
+end

Петър обнови решението на 20.10.2016 21:53 (преди над 7 години)

class Hash
def fetch_deep(path)
path.split('.').reduce(self) do |memo, key|
memo[key.to_i] || memo[key.to_sym] || memo[key.to_s] if memo
end
end
end
-# class Hash
-# def fetch_deep(path)
-# cpy = self.clone
-# path.split('.').each do |t|
-# break unless cpy = cpy[t.to_i] || cpy[t.to_sym] || cpy[t.to_s]
-# end
-# return cpy
-# end
-# end
-
class Hash
def reshape(shape)
- sh = shape.clone
- sh.each do |key, value|
- if sh[key].is_a?(String)
- sh[key] = self.fetch_deep(value)
+ shape_clone = shape.clone
+ shape_clone.each do |key, value|
+ if shape_clone[key].is_a?(String)
+ shape_clone[key] = self.fetch_deep(value)
else
- self.each { sh[key] = self.reshape(sh[key]) }
+ self.each { shape_clone[key] = self.reshape(shape_clone[key]) }
end
end
end
end
class Array
def reshape(shape)
- sh = shape.clone
- self.map { |i| i.reshape(sh) }
+ shape_clone = shape.clone
+ self.map { |i| i.reshape(shape_clone) }
end
end

Петър обнови решението на 21.10.2016 14:50 (преди над 7 години)

class Hash
def fetch_deep(path)
path.split('.').reduce(self) do |memo, key|
memo[key.to_i] || memo[key.to_sym] || memo[key.to_s] if memo
end
end
end
-
class Hash
def reshape(shape)
- shape_clone = shape.clone
- shape_clone.each do |key, value|
- if shape_clone[key].is_a?(String)
- shape_clone[key] = self.fetch_deep(value)
+ shape.each do |key, value|
+ if shape[key].is_a?(String)
+ shape[key] = self.fetch_deep(value)
else
- self.each { shape_clone[key] = self.reshape(shape_clone[key]) }
+ self.each { shape[key] = self.reshape(shape[key]) }
end
end
end
end
-
class Array
def reshape(shape)
- shape_clone = shape.clone
- self.map { |i| i.reshape(shape_clone) }
- end
+ self.map { |i| i.reshape(shape.clone) }
+ end
end