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

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

Към профила на Костадин Самарджиев

Резултати

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

Код

def prepare_key(hash_object, key)
if hash_object.key? key.to_sym
key.to_sym
elsif hash_object.key? key.to_s
key.to_s
else
nil
end
end
def process_value_for_reshape(hash_object, shape_value)
if shape_value.is_a?(Hash)
hash_object.reshape shape_value
else
hash_object.fetch_deep shape_value
end
end
def process_path_containing_array(hash_object, keys)
counter = -1
build_hash = hash_object[keys[0].to_sym].map do |element|
[(counter += 1).to_s.to_sym, element]
end.to_h
build_hash.fetch_deep(keys[1..-1].join('.'))
end
def process_path(keys)
if prepare_key self, keys[0]
if self[prepare_key self, keys[0]].is_a?(Hash)
self[prepare_key self, keys[0]].fetch_deep(keys[1..-1].join('.'))
else
process_path_containing_array self, keys
end
else nil
end
end
class Hash
def fetch_deep(path)
if path.include? '.'
keys = path.split('.')
process_path keys
elsif prepare_key self, path
self[prepare_key self, path]
end
end
def reshape(shape)
result = {}
shape.keys.each do |key|
result[key] = process_value_for_reshape self, shape[key]
end
result
end
end
class Array
def reshape(shape)
result = []
self.each do |element|
target = {}
shape.keys.each { |key| target[key] = element.fetch_deep shape[key] }
result << target
end
result
end
end

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

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

Finished in 0.00953 seconds
15 examples, 0 failures

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

Костадин обнови решението на 21.10.2016 13:12 (преди около 8 години)

+def process_path_containing_array(hash_object, keys)
+ counter = -1
+ build_hash = hash_object[keys[0].to_sym].map do |element|
+ [(counter += 1).to_s.to_sym, element]
+ end.to_h
+ build_hash.fetch_deep(keys[1..-1].join('.'))
+end
+
+def process_path(keys)
+ if self.key? keys[0].to_sym
+ if self[keys[0].to_sym].is_a?(Hash)
+ self[keys[0].to_sym].fetch_deep(keys[1..-1].join('.'))
+ else
+ process_path_containing_array self, keys
+ end
+ end
+end
+
+class Hash
+ def fetch_deep(path)
+ if path.include? '.'
+ keys = path.split('.')
+ process_path keys
+ elsif self.key? path.to_sym
+ self[path.to_sym]
+ else
+ nil
+ end
+ end
+
+ def reshape(shape)
+ result = {}
+ shape.keys.each do |key|
+ result[key] = self.fetch_deep shape[key]
+ end
+ result
+ end
+end
+
+class Array
+ def reshape(shape)
+ result = []
+ self.each do |element|
+ target = {}
+ shape.keys.each { |key| target[key] = element.fetch_deep shape[key] }
+ result << target
+ end
+ result
+ end
+end

Костадин обнови решението на 21.10.2016 16:53 (преди около 8 години)

+def prepare_key(hash_object, key)
+ if hash_object.key? key.to_sym
+ key.to_sym
+ elsif hash_object.key? key.to_s
+ key.to_s
+ else
+ nil
+ end
+end
+
def process_path_containing_array(hash_object, keys)
counter = -1
build_hash = hash_object[keys[0].to_sym].map do |element|
[(counter += 1).to_s.to_sym, element]
end.to_h
build_hash.fetch_deep(keys[1..-1].join('.'))
end
def process_path(keys)
- if self.key? keys[0].to_sym
- if self[keys[0].to_sym].is_a?(Hash)
- self[keys[0].to_sym].fetch_deep(keys[1..-1].join('.'))
+ if prepare_key self, keys[0]
+ if self[prepare_key self, keys[0]].is_a?(Hash)
+ self[prepare_key self, keys[0]].fetch_deep(keys[1..-1].join('.'))
else
process_path_containing_array self, keys
end
+ else nil
end
end
class Hash
def fetch_deep(path)
if path.include? '.'
keys = path.split('.')
process_path keys
- elsif self.key? path.to_sym
- self[path.to_sym]
- else
- nil
+ elsif prepare_key self, path
+ self[prepare_key self, path]
end
end
def reshape(shape)
result = {}
shape.keys.each do |key|
result[key] = self.fetch_deep shape[key]
end
result
end
end
class Array
def reshape(shape)
result = []
self.each do |element|
target = {}
shape.keys.each { |key| target[key] = element.fetch_deep shape[key] }
result << target
end
result
end
-end
+end

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

def prepare_key(hash_object, key)
if hash_object.key? key.to_sym
key.to_sym
elsif hash_object.key? key.to_s
key.to_s
else
nil
end
end
+def process_value_for_reshape(hash_object, shape_value)
+ if shape_value.is_a?(Hash)
+ hash_object.reshape shape_value
+ else
+ hash_object.fetch_deep shape_value
+ end
+end
+
def process_path_containing_array(hash_object, keys)
counter = -1
build_hash = hash_object[keys[0].to_sym].map do |element|
[(counter += 1).to_s.to_sym, element]
end.to_h
build_hash.fetch_deep(keys[1..-1].join('.'))
end
def process_path(keys)
if prepare_key self, keys[0]
if self[prepare_key self, keys[0]].is_a?(Hash)
self[prepare_key self, keys[0]].fetch_deep(keys[1..-1].join('.'))
else
process_path_containing_array self, keys
end
else nil
end
end
class Hash
def fetch_deep(path)
if path.include? '.'
keys = path.split('.')
process_path keys
elsif prepare_key self, path
self[prepare_key self, path]
end
end
def reshape(shape)
result = {}
shape.keys.each do |key|
- result[key] = self.fetch_deep shape[key]
+ result[key] = process_value_for_reshape self, shape[key]
end
result
end
end
class Array
def reshape(shape)
result = []
self.each do |element|
target = {}
shape.keys.each { |key| target[key] = element.fetch_deep shape[key] }
result << target
end
result
end
end