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

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

Към профила на Христина Тодорова

Резултати

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

Код

class Hash
def fetch_deeper(curr, result)
result = if result.is_a?(Array)
result.fetch(curr.to_i)
else
result = result.fetch(curr.to_sym) { result.fetch(curr, nil) }
end
end
def fetch_deep(path)
current_hash = self
path_keys_array = path.split('.')
until path_keys_array.empty?
current_key = path_keys_array.shift
current_hash = fetch_deeper(current_key, current_hash)
return nil if current_hash == nil
end
current_hash
end
def reshape(shape_hash)
h = shape_hash.clone
h.update(h) { |_, v| v.is_a?(String) ? fetch_deep(v) : reshape(v) }
end
end
class Array
def reshape(shape_hash)
map! { |element| element.reshape(shape_hash) }
end
end

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

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

Finished in 0.00801 seconds
15 examples, 0 failures

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

Христина обнови решението на 18.10.2016 22:01 (преди над 7 години)

+class Hash
+ def fetch_deeper(curr, result)
+ result = if result.is_a?(Array)
+ result.fetch(curr.to_i)
+ else
+ result = result.fetch(curr.to_sym) { result.fetch(curr, nil) }
+ end
+ end
+
+ def fetch_deep(path)
+ result = self
+ path_array = path.split('.')
+ until path_array.empty?
+ current_key = path_array.shift
+ result = fetch_deeper(current_key, result)
+ end
+ result
+ end
+
+ def reshape(shape_hash)
+ shape_hash.update(shape_hash) { |_, value| fetch_deep(value) }
+ end
+end
+
+class Array
+ def reshape(shape_hash)
+ map! { |element| element.reshape(shape_hash) }
+ end
+end

Христина обнови решението на 20.10.2016 14:56 (преди над 7 години)

class Hash
def fetch_deeper(curr, result)
result = if result.is_a?(Array)
result.fetch(curr.to_i)
else
result = result.fetch(curr.to_sym) { result.fetch(curr, nil) }
end
end
def fetch_deep(path)
+ return path unless path.is_a?(String)
result = self
path_array = path.split('.')
until path_array.empty?
current_key = path_array.shift
result = fetch_deeper(current_key, result)
end
result
end
def reshape(shape_hash)
- shape_hash.update(shape_hash) { |_, value| fetch_deep(value) }
+ h = shape_hash.clone
+ h.update(h) { |_, v| v.is_a?(String) ? fetch_deep(v) : reshape(v) }
end
end
class Array
def reshape(shape_hash)
map! { |element| element.reshape(shape_hash) }
end
end

Христина обнови решението на 20.10.2016 23:16 (преди над 7 години)

class Hash
def fetch_deeper(curr, result)
result = if result.is_a?(Array)
result.fetch(curr.to_i)
else
result = result.fetch(curr.to_sym) { result.fetch(curr, nil) }
end
end
def fetch_deep(path)
- return path unless path.is_a?(String)
- result = self
- path_array = path.split('.')
- until path_array.empty?
- current_key = path_array.shift
- result = fetch_deeper(current_key, result)
+ current_hash = self
+ path_keys_array = path.split('.')
+ until path_keys_array.empty?
+ current_key = path_keys_array.shift
+ current_hash = fetch_deeper(current_key, current_hash)
end
- result
+ current_hash
end
def reshape(shape_hash)
h = shape_hash.clone
h.update(h) { |_, v| v.is_a?(String) ? fetch_deep(v) : reshape(v) }
end
end
class Array
def reshape(shape_hash)
map! { |element| element.reshape(shape_hash) }
end
end

Христина обнови решението на 23.10.2016 18:45 (преди над 7 години)

class Hash
def fetch_deeper(curr, result)
result = if result.is_a?(Array)
result.fetch(curr.to_i)
else
result = result.fetch(curr.to_sym) { result.fetch(curr, nil) }
end
end
def fetch_deep(path)
current_hash = self
path_keys_array = path.split('.')
until path_keys_array.empty?
current_key = path_keys_array.shift
current_hash = fetch_deeper(current_key, current_hash)
+ return nil if current_hash == nil
end
current_hash
end
def reshape(shape_hash)
h = shape_hash.clone
h.update(h) { |_, v| v.is_a?(String) ? fetch_deep(v) : reshape(v) }
end
end
class Array
def reshape(shape_hash)
map! { |element| element.reshape(shape_hash) }
end
end
+