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

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

Към профила на Добрин Цветков

Резултати

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

Код

def put_in_result(result, current_key)
if result.is_a? Array
result[current_key.to_i]
elsif result.is_a? Hash
result[current_key] || result[current_key.to_sym]
else
nil
end
end
class Hash
def fetch_deep(path)
keys = path.split('.')
return nil if keys.empty?
return nil unless result = self[keys[0]] || self[keys[0].to_sym]
keys[1..-1].each do |current_key|
result = put_in_result(result, current_key)
end
result
end
def reshape(shape)
new_hash = {}
shape.each do |key, path|
take_value_or_use_recursion(key, path, new_hash, self)
end
new_hash
end
end
def take_value_or_use_recursion(key, path, new_hash, old_hash)
if path.is_a? String
new_hash[key] = old_hash.fetch_deep(path)
elsif path.is_a? Hash
new_hash[key] = old_hash.reshape(path)
end
end
class Array
def reshape(shape)
new_arr = []
self.each { |hash_element| new_arr.push hash_element.reshape(shape) }
new_arr
end
end

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

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

Finished in 0.00935 seconds
15 examples, 0 failures

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

Добрин обнови решението на 21.10.2016 20:06 (преди около 8 години)

+def put_in_result(result, current_key)
+ if result.is_a? Array
+ result[current_key.to_i]
+ elsif result.is_a? Hash
+ result[current_key] || result[current_key.to_sym]
+ else
+ nil
+ end
+end
+
+class Hash
+ def fetch_deep(path)
+ keys = path.split('.')
+ return nil if keys.empty?
+ return nil unless result = self[keys[0]] || self[keys[0].to_sym]
+ keys[1..-1].each do |current_key|
+ result = put_in_result(result, current_key)
+ end
+ result
+ end
+
+ def reshape(shape)
+ self_copy = self.dup
+ self.clear
+ shape.each do |key, path|
+ hash_take_value_or_use_recursion(key, path, self, self_copy)
+ end
+ self
+ end
+end
+
+def hash_take_value_or_use_recursion(key, path, current_hash, hash_old_version)
+ if path.is_a? String
+ current_hash[key] = hash_old_version.fetch_deep(path)
+ elsif path.is_a? Hash
+ current_hash[key] = hash_old_version
+ current_hash[key].reshape(path)
+ end
+end
+
+class Array
+ def reshape(shape)
+ self.each { |hash_element| hash_element.reshape(shape) }
+ end
+end

Добрин обнови решението на 24.10.2016 09:10 (преди около 8 години)

def put_in_result(result, current_key)
if result.is_a? Array
result[current_key.to_i]
elsif result.is_a? Hash
result[current_key] || result[current_key.to_sym]
else
nil
end
end
class Hash
def fetch_deep(path)
keys = path.split('.')
return nil if keys.empty?
return nil unless result = self[keys[0]] || self[keys[0].to_sym]
keys[1..-1].each do |current_key|
result = put_in_result(result, current_key)
end
result
end
def reshape(shape)
- self_copy = self.dup
- self.clear
+ new_hash = {}
shape.each do |key, path|
- hash_take_value_or_use_recursion(key, path, self, self_copy)
+ take_value_or_use_recursion(key, path, new_hash, self)
end
- self
+ new_hash
end
end
-def hash_take_value_or_use_recursion(key, path, current_hash, hash_old_version)
+def take_value_or_use_recursion(key, path, new_hash, old_hash)
if path.is_a? String
- current_hash[key] = hash_old_version.fetch_deep(path)
+ new_hash[key] = old_hash.fetch_deep(path)
elsif path.is_a? Hash
- current_hash[key] = hash_old_version
- current_hash[key].reshape(path)
+ new_hash[key] = old_hash.reshape(path)
end
end
class Array
def reshape(shape)
- self.each { |hash_element| hash_element.reshape(shape) }
+ new_arr = []
+ self.each do |hash_element|
+ p hash_element
+ hash_element = hash_element.reshape(shape).dup
+ new_arr.push hash_element
+ end
+ new_arr
end
end

Добрин обнови решението на 24.10.2016 09:15 (преди около 8 години)

def put_in_result(result, current_key)
if result.is_a? Array
result[current_key.to_i]
elsif result.is_a? Hash
result[current_key] || result[current_key.to_sym]
else
nil
end
end
class Hash
def fetch_deep(path)
keys = path.split('.')
return nil if keys.empty?
return nil unless result = self[keys[0]] || self[keys[0].to_sym]
keys[1..-1].each do |current_key|
result = put_in_result(result, current_key)
end
result
end
def reshape(shape)
new_hash = {}
shape.each do |key, path|
take_value_or_use_recursion(key, path, new_hash, self)
end
new_hash
end
end
def take_value_or_use_recursion(key, path, new_hash, old_hash)
if path.is_a? String
new_hash[key] = old_hash.fetch_deep(path)
elsif path.is_a? Hash
new_hash[key] = old_hash.reshape(path)
end
end
class Array
def reshape(shape)
new_arr = []
self.each do |hash_element|
p hash_element
- hash_element = hash_element.reshape(shape).dup
- new_arr.push hash_element
+ new_arr.push hash_element.reshape(shape)
end
new_arr
end
end

Добрин обнови решението на 24.10.2016 13:33 (преди около 8 години)

def put_in_result(result, current_key)
if result.is_a? Array
result[current_key.to_i]
elsif result.is_a? Hash
result[current_key] || result[current_key.to_sym]
else
nil
end
end
class Hash
def fetch_deep(path)
keys = path.split('.')
return nil if keys.empty?
return nil unless result = self[keys[0]] || self[keys[0].to_sym]
keys[1..-1].each do |current_key|
result = put_in_result(result, current_key)
end
result
end
def reshape(shape)
new_hash = {}
shape.each do |key, path|
take_value_or_use_recursion(key, path, new_hash, self)
end
new_hash
end
end
def take_value_or_use_recursion(key, path, new_hash, old_hash)
if path.is_a? String
new_hash[key] = old_hash.fetch_deep(path)
elsif path.is_a? Hash
new_hash[key] = old_hash.reshape(path)
end
end
class Array
def reshape(shape)
new_arr = []
- self.each do |hash_element|
- p hash_element
- new_arr.push hash_element.reshape(shape)
- end
+ self.each { |hash_element| new_arr.push hash_element.reshape(shape) }
new_arr
end
end