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

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

Към профила на Калина Бухлева

Резултати

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

Код

class Hash
def fetch_deep(path)
path_elements = path.split('.')
path_elements.reduce(self) do |hsh, key|
if hsh
hsh = hsh.stringify_keys
hsh[key]
end
end
end
def reshape(shape)
result = shape
shape.each do |(key, value)|
if value.is_a? Hash
reshape(value)
else
result[key] = self.fetch_deep(value)
end
end
end
def stringify_keys
result = {}
each_key do |key|
result[key.to_s] = self[key]
end
result
end
end
class Array
def reshape(shape)
self.each_with_index do |value, index|
self[index] = value.reshape(shape.dup)
end
end
def stringify_keys
result = self.each_with_index.map do |value, index|
[index.to_s, value]
end
result.to_h
end
end

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

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

Finished in 0.0082 seconds
15 examples, 0 failures

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

Калина обнови решението на 19.10.2016 21:19 (преди около 8 години)

+class Hash
+ def fetch_deep(path)
+ path_elements = path.split('.')
+ result = self.deep_stringify_keys
+ path_elements.reduce(result) do |hsh, key|
+ key = Integer(key) if key.number?
+ hsh[key] if hsh
+ end
+ end
+
+ def reshape(shape)
+ shape.each_with_object({}) do |(key, value), result|
+ if value.is_a? Hash
+ reshape(value)
+ else
+ result[key] = fetch_deep(value)
+ end
+ end
+ end
+
+ def deep_stringify_keys
+ deep_transform_keys(self, &:to_s)
+ end
+
+ def deep_transform_keys(object, &block)
+ if object.is_a? Hash
+ object.each_with_object({}) do |(key, value), result|
+ result[yield(key)] = deep_transform_keys(value, &block)
+ end
+ elsif object.is_a? Array
+ object.map { |e| deep_transform_keys(e, &block) }
+ else object
+ end
+ end
+end
+
+class Array
+ def reshape(shape)
+ self.map! do |hsh|
+ hsh.reshape(shape)
+ end
+ end
+end
+
+class String
+ def number?
+ true if Integer(self) rescue false
+ end
+end

Калина обнови решението на 19.10.2016 21:26 (преди около 8 години)

class Hash
def fetch_deep(path)
path_elements = path.split('.')
result = self.deep_stringify_keys
path_elements.reduce(result) do |hsh, key|
key = Integer(key) if key.number?
hsh[key] if hsh
end
end
def reshape(shape)
shape.each_with_object({}) do |(key, value), result|
if value.is_a? Hash
reshape(value)
else
result[key] = fetch_deep(value)
end
end
end
def deep_stringify_keys
deep_transform_keys(self, &:to_s)
end
def deep_transform_keys(object, &block)
if object.is_a? Hash
object.each_with_object({}) do |(key, value), result|
result[yield(key)] = deep_transform_keys(value, &block)
end
elsif object.is_a? Array
object.map { |e| deep_transform_keys(e, &block) }
else object
end
end
end
class Array
def reshape(shape)
- self.map! do |hsh|
+ self.map do |hsh|
hsh.reshape(shape)
end
end
end
class String
def number?
true if Integer(self) rescue false
end
end

Калина обнови решението на 22.10.2016 10:57 (преди около 8 години)

class Hash
def fetch_deep(path)
path_elements = path.split('.')
- result = self.deep_stringify_keys
- path_elements.reduce(result) do |hsh, key|
- key = Integer(key) if key.number?
- hsh[key] if hsh
+ path_elements.reduce(self) do |hsh, key|
+ hsh = hsh.stringify_keys if hsh
+ hsh[key]
end
end
def reshape(shape)
- shape.each_with_object({}) do |(key, value), result|
+ result = shape
+ shape.each do |(key, value)|
if value.is_a? Hash
reshape(value)
else
- result[key] = fetch_deep(value)
+ result[key] = self.fetch_deep(value)
end
end
end
- def deep_stringify_keys
- deep_transform_keys(self, &:to_s)
+ def stringify_keys
+ transform_keys(&:to_s)
end
- def deep_transform_keys(object, &block)
- if object.is_a? Hash
- object.each_with_object({}) do |(key, value), result|
- result[yield(key)] = deep_transform_keys(value, &block)
- end
- elsif object.is_a? Array
- object.map { |e| deep_transform_keys(e, &block) }
- else object
+ def transform_keys
+ return enum_for(:transform_keys) { size } unless block_given?
+ result = {}
+ each_key do |key|
+ result[yield(key)] = self[key]
end
+ result
end
end
class Array
def reshape(shape)
- self.map do |hsh|
- hsh.reshape(shape)
+ self.each_with_index do |value, index|
+ self[index] = value.reshape(shape.dup)
end
end
-end
-class String
- def number?
- true if Integer(self) rescue false
+ def stringify_keys
+ result = self.each_with_index.map do |value, index|
+ [index.to_s, value]
+ end
+ result.to_h
end
-end
+end

Калина обнови решението на 22.10.2016 11:03 (преди около 8 години)

class Hash
def fetch_deep(path)
path_elements = path.split('.')
path_elements.reduce(self) do |hsh, key|
hsh = hsh.stringify_keys if hsh
hsh[key]
end
end
def reshape(shape)
result = shape
shape.each do |(key, value)|
if value.is_a? Hash
reshape(value)
else
result[key] = self.fetch_deep(value)
end
end
end
def stringify_keys
transform_keys(&:to_s)
end
def transform_keys
- return enum_for(:transform_keys) { size } unless block_given?
result = {}
each_key do |key|
result[yield(key)] = self[key]
end
result
end
end
class Array
def reshape(shape)
self.each_with_index do |value, index|
self[index] = value.reshape(shape.dup)
end
end
def stringify_keys
result = self.each_with_index.map do |value, index|
[index.to_s, value]
end
result.to_h
end
end

Калина обнови решението на 22.10.2016 11:11 (преди около 8 години)

class Hash
def fetch_deep(path)
path_elements = path.split('.')
path_elements.reduce(self) do |hsh, key|
hsh = hsh.stringify_keys if hsh
hsh[key]
end
end
def reshape(shape)
result = shape
shape.each do |(key, value)|
if value.is_a? Hash
reshape(value)
else
result[key] = self.fetch_deep(value)
end
end
end
def stringify_keys
- transform_keys(&:to_s)
- end
-
- def transform_keys
result = {}
each_key do |key|
- result[yield(key)] = self[key]
+ result[key.to_s] = self[key]
end
result
end
end
class Array
def reshape(shape)
self.each_with_index do |value, index|
self[index] = value.reshape(shape.dup)
end
end
def stringify_keys
result = self.each_with_index.map do |value, index|
[index.to_s, value]
end
result.to_h
end
end

Калина обнови решението на 23.10.2016 14:41 (преди около 8 години)

class Hash
def fetch_deep(path)
path_elements = path.split('.')
path_elements.reduce(self) do |hsh, key|
- hsh = hsh.stringify_keys if hsh
- hsh[key]
+ if hsh
+ hsh = hsh.stringify_keys
+ hsh[key]
+ end
end
end
def reshape(shape)
result = shape
shape.each do |(key, value)|
if value.is_a? Hash
reshape(value)
else
result[key] = self.fetch_deep(value)
end
end
end
def stringify_keys
result = {}
each_key do |key|
result[key.to_s] = self[key]
end
result
end
end
class Array
def reshape(shape)
self.each_with_index do |value, index|
self[index] = value.reshape(shape.dup)
end
end
def stringify_keys
result = self.each_with_index.map do |value, index|
[index.to_s, value]
end
result.to_h
end
end