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

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

Към профила на Ралица Дарджонова

Резултати

  • 5 точки от тестове
  • 0 бонус точки
  • 5 точки общо
  • 13 успешни тест(а)
  • 2 неуспешни тест(а)

Код

def fetch_element_in_array(current_object, key)
key_not_digit_characters = /[^-+\d]/.match(key)
if key_not_digit_characters == nil
if current_object[key.to_i] != nil
current_object[key.to_i]
else nil
end
else nil
end
end
def fetch_element_in_hash(current_object, key)
if current_object[key] != nil
current_object[key]
elsif current_object[key.to_sym] != nil
current_object[key.to_sym]
else
nil
end
end
def get_result_object(current_object, key)
if current_object.is_a?(Numeric)
nil
elsif current_object.is_a?(Array) || current_object.is_a?(String)
fetch_element_in_array(current_object, key)
else
fetch_element_in_hash(current_object, key)
end
end
def follow_path_of_keys(current_object, path_array)
path_array.each do |key|
current_object = get_result_object(current_object, key)
end
current_object
end
class Hash
def fetch_deep(path)
path_array = path.split('.')
current_object = self
current_object = follow_path_of_keys(current_object, path_array)
current_object
end
end
def follow_deep_keys_in_shape(initial_hash, shape_hash, key)
if shape_hash[key].is_a?(String)
shape_hash[key] = initial_hash.fetch_deep(shape_hash[key])
elsif shape_hash[key].is_a?(Numeric)
# shape_hash[key]
else
shape_hash[key].keys.each do |new_key|
follow_deep_keys_in_shape(initial_hash, shape_hash[key], new_key)
end
end
end
class Hash
def reshape(shape)
copy_shape = Marshal.load(Marshal.dump(shape))
copy_shape.keys.each do |key|
follow_deep_keys_in_shape(self, copy_shape, key)
end
self.replace(copy_shape)
end
end
class Array
def reshape(shape)
self.each do |item|
item.reshape(shape)
end
end
end

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

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

Failures:

  1) Task 2 Hash#fetch_deep returns nil for non-existant keys
     Failure/Error: expect(input.fetch_deep('meal.0.type')).to be nil
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # /tmp/d20161024-13689-1yk4ap1/solution.rb:14:in `fetch_element_in_hash'
     # /tmp/d20161024-13689-1yk4ap1/solution.rb:29:in `get_result_object'
     # /tmp/d20161024-13689-1yk4ap1/solution.rb:35:in `block in follow_path_of_keys'
     # /tmp/d20161024-13689-1yk4ap1/solution.rb:34:in `each'
     # /tmp/d20161024-13689-1yk4ap1/solution.rb:34:in `follow_path_of_keys'
     # /tmp/d20161024-13689-1yk4ap1/solution.rb:45:in `fetch_deep'
     # /tmp/d20161024-13689-1yk4ap1/spec.rb:89: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)>'

  2) 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-1yk4ap1/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.00931 seconds
15 examples, 2 failures

Failed examples:

rspec /tmp/d20161024-13689-1yk4ap1/spec.rb:85 # Task 2 Hash#fetch_deep returns nil for non-existant keys
rspec /tmp/d20161024-13689-1yk4ap1/spec.rb:180 # Task 2 Hash#reshape does not modify the input hash

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

Ралица обнови решението на 20.10.2016 23:41 (преди около 8 години)

+def fetch_element_in_array(current_object, key)
+ key_not_digit_characters = /[^-+\d]/.match(key)
+
+ if key_not_digit_characters == nil
+ if current_object[key.to_i] != nil
+ current_object[key.to_i]
+ else nil
+ end
+ else nil
+ end
+end
+
+def fetch_element_in_hash(current_object, key)
+ if current_object[key] != nil
+ current_object[key]
+ elsif current_object[key.to_sym] != nil
+ current_object[key.to_sym]
+ else
+ nil
+ end
+end
+
+def get_result_object(current_object, key)
+ if current_object.is_a?(Numeric)
+ nil
+ elsif current_object.is_a?(Array) || current_object.is_a?(String)
+ fetch_element_in_array(current_object, key)
+ else
+ fetch_element_in_hash(current_object, key)
+ end
+end
+
+def follow_path_of_keys(current_object, path_array)
+ path_array.each do |key|
+ current_object = get_result_object(current_object, key)
+ end
+ current_object
+end
+
+class Hash
+ def fetch_deep(path)
+
+ path_array = path.split('.')
+ current_object = self
+ current_object = follow_path_of_keys(current_object, path_array)
+ current_object
+ end
+end
+
+def follow_deep_keys_in_shape(initial_hash, shape_hash, key)
+ if shape_hash[key].is_a?(String)
+ shape_hash[key] = initial_hash.fetch_deep(shape_hash[key])
+ elsif shape_hash[key].is_a?(Numeric)
+ # shape_hash[key]
+ else
+ shape_hash[key].keys.each do |new_key|
+ follow_deep_keys_in_shape(initial_hash, shape_hash[key], new_key)
+ end
+ end
+end
+
+class Hash
+ def reshape(shape)
+ copy_shape = Marshal.load(Marshal.dump(shape))
+
+ copy_shape.keys.each do |key|
+ follow_deep_keys_in_shape(self, copy_shape, key)
+ end
+ self.replace(copy_shape)
+ end
+end
+
+class Array
+ def reshape(shape)
+ self.each do |item|
+ item.reshape(shape)
+ end
+ end
+end