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

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

Към профила на Мариян Асенов

Резултати

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

Код

class Hash
def fetch_deep(path_in_hash)
path_in_array = path_in_hash.split('.')
fetch_deep_recursion(path_in_array, self)
end
def reshape(shape)
shaped(shape, self)
replace(shape)
end
end
class Array
def reshape(shape)
each { |item| item.reshape(shape.dup) }
end
end
def shaped(shape, current_hash)
shape.keys.each do |key|
shaped(shape[key], current_hash) if shape[key].is_a?(Hash)
if shape[key].is_a?(String)
shape[key] = current_hash.fetch_deep(shape[key])
end
end
end
def fetch_deep_recursion(path, hash)
return hash if path.empty? || hash.nil?
hash = convert_array_to_h(hash) if hash.is_a?(Array)
hash = hash.select { |key| key.to_s == path.first || key.is_a?(Array) }
matched_key = hash.keys.first
path.delete_at(0)
fetch_deep_recursion(path, hash[matched_key])
end
def convert_array_to_h(hash)
hash.map.with_index do |item, index|
[index, item]
end.to_h
end

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

.............F.

Failures:

  1) Task 2 Hash#reshape does not modify the input hash
     Failure/Error: expect(input).to eq menu: {
       
       expected: {:menu=>{:order=>"cake", "dessert"=>"ice cream", 3=>4}}
            got: {:order=>"cake", :dessert=>"ice cream"}
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,3 @@
       -:menu => {:order=>"cake", "dessert"=>"ice cream", 3=>4}
       +:dessert => "ice cream",
       +:order => "cake"
     # /tmp/d20161024-13689-cs5jee/spec.rb:196:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

Finished in 0.009 seconds
15 examples, 1 failure

Failed examples:

rspec /tmp/d20161024-13689-cs5jee/spec.rb:180 # Task 2 Hash#reshape does not modify the input hash

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

Мариян обнови решението на 23.10.2016 00:06 (преди над 7 години)

+class Hash
+ def fetch_deep(path_in_hash)
+ path_in_array = path_in_hash.split('.')
+ fetch_deep_recursion(path_in_array, self)
+ end
+
+ def reshape(shape)
+ shaped(shape, self)
+ replace(shape)
+ end
+end
+class Array
+ def reshape(shape)
+ each { |item| item.reshape(shape.dup) }
+ end
+end
+ def shaped(shape, current_hash)
+ shape.keys.each do |key|
+ shaped(shape[key], current_hash) if shape[key].is_a?(Hash)
+ if shape[key].is_a?(String)
+ shape[key] = current_hash.fetch_deep(shape[key])
+ end
+ end
+ end
+
+def fetch_deep_recursion(path, hash)
+ return hash if path.empty? || hash.nil?
+ hash = convert_array_to_h(hash) if hash.is_a?(Array)
+ hash = hash.select { |key| key.to_s == path.first || key.is_a?(Array) }
+ matched_key = hash.keys.first
+ path.delete_at(0)
+ fetch_deep_recursion(path, hash[matched_key])
+end
+
+ def convert_array_to_h(hash)
+ hash.map.with_index do |item, index|
+ [index, item]
+ end.to_h
+ end