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

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

Към профила на Радослав Гайдаров

Резултати

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

Код

class Hash
def fetch_deep(key_path)
path_split = key_path.split('.')
current_key = path_split.shift
symbol_key_result = get_result(self[current_key.to_sym], path_split)
string_key_result = get_result(self[current_key], path_split)
symbol_key_result != nil ? symbol_key_result : string_key_result
end
def reshape(new_shape)
buffer_shape = Marshal.load( Marshal.dump(new_shape) )
shapeshifter(self, buffer_shape)
self.replace(buffer_shape)
end
private
def array_handler(current_object, path_split)
if current_object[to_number_convert(path_split[0])]
get_result(current_object[path_split.shift.to_i], path_split)
end
end
def to_number_convert(string)
num = string.to_i
if num.to_s == string
num
else
999_999
end
end
def get_result(current_object, path_split)
if path_split.empty?
current_object
elsif current_object.is_a? Array
array_handler(current_object, path_split)
elsif !current_object.is_a?Hash
else
current_object.fetch_deep(path_split.join('.'))
end
end
def shapeshifter(old_shape, buffer_shape)
buffer_shape.map do |x, y|
if buffer_shape[x].is_a? Hash
shapeshifter(old_shape, buffer_shape[x])
else
buffer_shape[x] = old_shape.fetch_deep(y)
end
end
end
end
class Array
def reshape(new_shape)
self.map! { |x| x.reshape(new_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-1uvxfhu/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.0093 seconds
15 examples, 1 failure

Failed examples:

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

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

Радослав обнови решението на 24.10.2016 00:58 (преди над 7 години)

+class Hash
+ def fetch_deep(key_path)
+ path_split = key_path.split('.')
+ current_key = path_split.shift
+ symbol_key_result = get_result(self[current_key.to_sym], path_split)
+ string_key_result = get_result(self[current_key], path_split)
+ symbol_key_result != nil ? symbol_key_result : string_key_result
+ end
+
+ def reshape(new_shape)
+ buffer_shape = Marshal.load( Marshal.dump(new_shape) )
+ shapeshifter(self, buffer_shape)
+ self.replace(buffer_shape)
+ end
+
+ private
+
+ def array_handler(current_object, path_split)
+ if current_object[to_number_convert(path_split[0])]
+ get_result(current_object[path_split.shift.to_i], path_split)
+ end
+ end
+
+ def to_number_convert(string)
+ num = string.to_i
+ if num.to_s == string
+ num
+ else
+ 999_999
+ end
+ end
+
+ def get_result(current_object, path_split)
+ if path_split.empty?
+ current_object
+ elsif current_object.is_a? Array
+ array_handler(current_object, path_split)
+ elsif !current_object.is_a?Hash
+ else
+ current_object.fetch_deep(path_split.join('.'))
+ end
+ end
+
+ def shapeshifter(old_shape, buffer_shape)
+ buffer_shape.map do |x, y|
+ if buffer_shape[x].is_a? Hash
+ shapeshifter(old_shape, buffer_shape[x])
+ else
+ buffer_shape[x] = old_shape.fetch_deep(y)
+ end
+ end
+ end
+end
+class Array
+ def reshape(new_shape)
+ self.map! { |x| x.reshape(new_shape) }
+ end
+end