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

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

Към профила на Кристъфър Коруев

Резултати

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

Код

class Hash
def fetch_deep(path)
all_keys = path.split('.')
get_value_by_keys(all_keys, self)
end
def reshape(shape)
cloned_shape = shape.clone
replace_path_strings(shape.keys, cloned_shape)
self.replace(cloned_shape)
end
private
def replace_path_strings(keys, hash)
return {} if keys.empty?
current_value = hash[keys.first]
if current_value.is_a?(Hash)
replace_path_strings(current_value.keys, current_value)
else
hash[keys.first] = self.fetch_deep(current_value)
replace_path_strings(keys[1, keys.length], hash)
end
end
def get_value_by_keys(keys, hash)
is_array = hash.is_a?(Array)
is_hash = hash.is_a?(Hash)
return nil if hash == nil
return nil if !is_array && !is_hash
value = find_correct_value(is_array, keys.first, hash)
return value if keys.length == 1
get_value_by_keys(keys[1, keys.length], value)
end
def find_correct_value(is_array, next_key, hash)
# easy way to check if String is an Integer
is_int = next_key.to_i.to_s == next_key
return hash[next_key.to_i] if is_array && is_int
return nil if is_array && !is_int
is_key_string = hash.key?(next_key)
is_key_string ? hash[next_key] : hash[next_key.to_sym]
end
end
class Array
def reshape(shape)
replace_hashes(self, shape)
self
end
private
def replace_hashes(array_to_reshape, shape)
return if array_to_reshape.empty?
first_hash = array_to_reshape.first
first_hash.reshape(shape) if first_hash.is_a?(Hash)
replace_hashes(array_to_reshape[1, array_to_reshape.length], shape)
end
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-1jqnb3x/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.00897 seconds
15 examples, 1 failure

Failed examples:

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

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

Кристъфър обнови решението на 24.10.2016 01:00 (преди около 8 години)

+class Hash
+ def fetch_deep(path)
+ all_keys = path.split('.')
+ get_value_by_keys(all_keys, self)
+ end
+
+ def reshape(shape)
+ cloned_shape = shape.clone
+ replace_path_strings(shape.keys, cloned_shape)
+ self.replace(cloned_shape)
+ end
+
+ private
+
+ def replace_path_strings(keys, hash)
+ return {} if keys.empty?
+ current_value = hash[keys.first]
+ if current_value.is_a?(Hash)
+ replace_path_strings(current_value.keys, current_value)
+ else
+ hash[keys.first] = self.fetch_deep(current_value)
+ replace_path_strings(keys[1, keys.length], hash)
+ end
+ end
+
+ def get_value_by_keys(keys, hash)
+ is_array = hash.is_a?(Array)
+ is_hash = hash.is_a?(Hash)
+ return nil if hash == nil
+ return nil if !is_array && !is_hash
+
+ value = find_correct_value(is_array, keys.first, hash)
+ return value if keys.length == 1
+
+ get_value_by_keys(keys[1, keys.length], value)
+ end
+
+ def find_correct_value(is_array, next_key, hash)
+ # easy way to check if String is an Integer
+ is_int = next_key.to_i.to_s == next_key
+ return hash[next_key.to_i] if is_array && is_int
+ return nil if is_array && !is_int
+
+ is_key_string = hash.key?(next_key)
+ is_key_string ? hash[next_key] : hash[next_key.to_sym]
+ end
+end
+
+class Array
+ def reshape(shape)
+ replace_hashes(self, shape)
+ self
+ end
+
+ private
+
+ def replace_hashes(array_to_reshape, shape)
+ return if array_to_reshape.empty?
+ first_hash = array_to_reshape.first
+ first_hash.reshape(shape) if first_hash.is_a?(Hash)
+ replace_hashes(array_to_reshape[1, array_to_reshape.length], shape)
+ end
+end