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

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

Към профила на Исмаил Алиджиков

Резултати

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

Код

class Integer
def self.number?(arg)
arg.to_i.to_s == arg
end
end
class Hash
def fetch_deep(path)
path.split('.').reduce(self) do |memo, key|
return unless memo
if memo.is_a? Array
memo[key.to_i] if Integer.number? key
else
memo[key] || memo[key.to_sym]
end
end
end
def reshape(shape)
shape_copy = clone_deep(shape)
new_self = reshape_recursive(shape_copy)
self.replace(new_self)
end
private
def reshape_recursive(shape)
shape.each do |key, value|
if value.is_a? Hash
shape[key] = reshape_recursive(value)
elsif value.is_a? String
shape[key] = self.fetch_deep(value)
end
end
end
def clone_deep(hash)
new_hash = {}
hash.each do |key, value|
new_hash[key] = (value.is_a? Hash) ? clone_deep(value) : value.clone
end
new_hash
end
end
class Array
def reshape(shape)
self.each do |e|
e.reshape(shape)
end
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-16cgiy3/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.01046 seconds
15 examples, 1 failure

Failed examples:

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

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

Исмаил обнови решението на 21.10.2016 22:24 (преди над 7 години)

+class Integer
+ def self.number?(arg)
+ arg.to_i.to_s == arg
+ end
+end
+
+class Hash
+ def fetch_deep(path)
+ path.split('.').reduce(self) do |memo, key|
+ return unless memo
+
+ if memo.is_a? Array
+ memo[key.to_i] if Integer.number? key
+ else
+ memo[key] || memo[key.to_sym]
+ end
+ end
+ end
+
+ def reshape(shape)
+ shape_copy = clone_deep(shape)
+ new_self = reshape_recursive(shape_copy)
+
+ self.replace(new_self)
+ end
+
+ private
+
+ def reshape_recursive(shape)
+ shape.each do |key, value|
+ if value.is_a? Hash
+ shape[key] = reshape_recursive(value)
+ elsif value.is_a? String
+ shape[key] = self.fetch_deep(value)
+ end
+ end
+ end
+
+ def clone_deep(hash)
+ new_hash = {}
+ hash.each do |key, value|
+ new_hash[key] = (value.is_a? Hash) ? clone_deep(value) : value.clone
+ end
+
+ new_hash
+ end
+end
+
+class Array
+ def reshape(shape)
+ self.each do |e|
+ e.reshape(shape)
+ end
+ end
+end