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

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

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

Резултати

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

Код

class Hash
def fetch_deep(path)
k = path.split('.')
cur = self
while cur && k.any?
return nil unless cur.respond_to?(:fetch)
cur = cur.class == Array ? cur[k[0].to_i] : cur[k[0]] || cur[k[0].to_sym]
k.shift
end
cur
end
def reshape(shape)
shape.keys.map do |key|
if shape[key].class != Hash
[key, fetch_deep(shape[key])]
else
[key, reshape(shape[key])]
end
end.to_h
end
end
class Array
def reshape(shape)
map { |x| x.reshape(shape) }
end
end

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

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

Finished in 0.00791 seconds
15 examples, 0 failures

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

Петко обнови решението на 19.10.2016 00:03 (преди над 7 години)

+class Hash
+ def fetch_deep(path)
+ keys = path.split('.').map { |x| x =~ /\A\d+\z/ ? x.to_i : x }
+ return_val = keys.reduce(self) { |a, e| a[e] || a[e.to_sym] || {} }
+ return_val == {} ? nil : return_val
+ end
+
+ def reshape(shape)
+ shape.keys.map do |key|
+ if shape[key].class != Hash
+ [key, fetch_deep(shape[key])]
+ else
+ [key, reshape(shape[key])]
+ end
+ end.to_h
+ end
+end
+
+class Array
+ def reshape(shape)
+ map { |x| x.reshape(shape) }
+ end
+end

Петко обнови решението на 20.10.2016 00:55 (преди над 7 години)

class Hash
def fetch_deep(path)
+ p [path, "path"]
keys = path.split('.').map { |x| x =~ /\A\d+\z/ ? x.to_i : x }
- return_val = keys.reduce(self) { |a, e| a[e] || a[e.to_sym] || {} }
- return_val == {} ? nil : return_val
+ begin
+ keys.reduce(self) { |a, e| a[e] || a[e.to_s.to_sym] }
+ rescue NoMethodError
+ nil
+ end
end
def reshape(shape)
shape.keys.map do |key|
if shape[key].class != Hash
[key, fetch_deep(shape[key])]
else
[key, reshape(shape[key])]
end
end.to_h
end
end
class Array
def reshape(shape)
map { |x| x.reshape(shape) }
end
end

Петко обнови решението на 23.10.2016 02:06 (преди над 7 години)

class Hash
def fetch_deep(path)
- p [path, "path"]
- keys = path.split('.').map { |x| x =~ /\A\d+\z/ ? x.to_i : x }
+ keys = path.split('.').map { |key| key =~ /\A\d+\z/ ? x.to_i : x }
begin
- keys.reduce(self) { |a, e| a[e] || a[e.to_s.to_sym] }
+ keys.reduce(self) { |acc, key| a[key] || acc[key.to_s.to_sym] }
rescue NoMethodError
nil
end
end
def reshape(shape)
shape.keys.map do |key|
if shape[key].class != Hash
[key, fetch_deep(shape[key])]
else
[key, reshape(shape[key])]
end
end.to_h
end
end
class Array
def reshape(shape)
map { |x| x.reshape(shape) }
end
end

Петко обнови решението на 23.10.2016 20:44 (преди над 7 години)

class Hash
def fetch_deep(path)
- keys = path.split('.').map { |key| key =~ /\A\d+\z/ ? x.to_i : x }
+ keys = path.split('.').map { |key| key =~ /\A\d+\z/ ? key.to_i : key }
begin
- keys.reduce(self) { |acc, key| a[key] || acc[key.to_s.to_sym] }
+ keys.reduce(self) { |a, e| a[e] || a[e.to_s.to_sym] || a[e.to_s] }
rescue NoMethodError
nil
end
end
def reshape(shape)
shape.keys.map do |key|
if shape[key].class != Hash
[key, fetch_deep(shape[key])]
else
[key, reshape(shape[key])]
end
end.to_h
end
end
class Array
def reshape(shape)
map { |x| x.reshape(shape) }
end
-end
+end

Петко обнови решението на 23.10.2016 22:11 (преди над 7 години)

class Hash
def fetch_deep(path)
- keys = path.split('.').map { |key| key =~ /\A\d+\z/ ? key.to_i : key }
- begin
- keys.reduce(self) { |a, e| a[e] || a[e.to_s.to_sym] || a[e.to_s] }
- rescue NoMethodError
- nil
+ k = path.split('.')
+ cur = self
+ while cur && k != []
+ cur = cur.class == Array ? cur[k[0].to_i] : cur[k[0]] || cur[k[0].to_sym]
+ k.shift
end
+ cur
end
def reshape(shape)
shape.keys.map do |key|
if shape[key].class != Hash
[key, fetch_deep(shape[key])]
else
[key, reshape(shape[key])]
end
end.to_h
end
end
class Array
def reshape(shape)
map { |x| x.reshape(shape) }
end
end

Петко обнови решението на 24.10.2016 15:23 (преди над 7 години)

class Hash
def fetch_deep(path)
k = path.split('.')
cur = self
while cur && k != []
+ return nil unless cur.respond_to?(:fetch)
cur = cur.class == Array ? cur[k[0].to_i] : cur[k[0]] || cur[k[0].to_sym]
k.shift
end
cur
end
def reshape(shape)
shape.keys.map do |key|
if shape[key].class != Hash
[key, fetch_deep(shape[key])]
else
[key, reshape(shape[key])]
end
end.to_h
end
end
class Array
def reshape(shape)
map { |x| x.reshape(shape) }
end
end

Петко обнови решението на 24.10.2016 15:33 (преди над 7 години)

class Hash
def fetch_deep(path)
k = path.split('.')
cur = self
- while cur && k != []
+ while cur && k.any?
return nil unless cur.respond_to?(:fetch)
cur = cur.class == Array ? cur[k[0].to_i] : cur[k[0]] || cur[k[0].to_sym]
k.shift
end
cur
end
def reshape(shape)
shape.keys.map do |key|
if shape[key].class != Hash
[key, fetch_deep(shape[key])]
else
[key, reshape(shape[key])]
end
end.to_h
end
end
class Array
def reshape(shape)
map { |x| x.reshape(shape) }
end
end