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

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

Към профила на Дарин Тодоров

Резултати

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

Код

class Hash
def fetch_deep(path)
new_path_style = path.split('.'). clone
current = self.from_array_to_hash.stringify_keys
while new_path_style.size > 1 && current do
v = current[new_path_style.shift]
current = v
end
current ? current[new_path_style.first] : nil
end
end
class Hash
def stringify_keys
new_hash_style = {}
self.map do |key, value|
value = value.stringify_keys if value.is_a?(Hash) || value.is_a?(Array)
new_hash_style[key.to_s] = value
end
new_hash_style
end
end
class Array
def stringify_keys
new_array_style = []
self.map do |value|
value = value.stringify_keys if value.is_a?(Hash) || value.is_a?(Array)
new_array_style = value
end
new_array_style
end
end
class Hash
def from_array_to_hash
current = self
current.each do |k, _|
if current[k].is_a?(Array)
current[k] = current[k].map.with_index { |x, i| [i, x] }.to_h
elsif current[k].is_a?(Hash)
current[k].from_array_to_hash
end
end
end
end
class Hash
def reshape(shape)
current = self.from_array_to_hash
shape.from_array_to_hash.each do |key, value|
shape[key] = current.fetch_deep(value)
end
end
end
class Array
def reshape(shape)
curr = self
curr = curr.map { |i| i.from_array_to_hash }
curr.map { |i| i.reshape(shape) }
end
end

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

F........FF.F.F

Failures:

  1) Task 2 README works for all examples
     Failure/Error: expect(order.reshape(shape)).to eq({food: {type: 'cake', taste: 'chocolate'}})
     NoMethodError:
       undefined method `split' for {:type=>"dessert.type", :taste=>"dessert.variant"}:Hash
     # /tmp/d20161024-13689-1xak3ij/solution.rb:3:in `fetch_deep'
     # /tmp/d20161024-13689-1xak3ij/solution.rb:52:in `block in reshape'
     # /tmp/d20161024-13689-1xak3ij/solution.rb:51:in `each'
     # /tmp/d20161024-13689-1xak3ij/solution.rb:51:in `reshape'
     # /tmp/d20161024-13689-1xak3ij/spec.rb:44: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 can extract fields to nested objects
     Failure/Error: expect(input.reshape(shape)).to eq output
     NoMethodError:
       undefined method `split' for {:first_name=>"profile.name"}:Hash
     # /tmp/d20161024-13689-1xak3ij/solution.rb:3:in `fetch_deep'
     # /tmp/d20161024-13689-1xak3ij/solution.rb:52:in `block in reshape'
     # /tmp/d20161024-13689-1xak3ij/solution.rb:51:in `each'
     # /tmp/d20161024-13689-1xak3ij/solution.rb:51:in `reshape'
     # /tmp/d20161024-13689-1xak3ij/spec.rb:133: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)>'

  3) Task 2 Hash#reshape can create nested objects
     Failure/Error: expect(input.reshape(shape)).to eq output
     NoMethodError:
       undefined method `split' for {:first_name=>"name"}:Hash
     # /tmp/d20161024-13689-1xak3ij/solution.rb:3:in `fetch_deep'
     # /tmp/d20161024-13689-1xak3ij/solution.rb:52:in `block in reshape'
     # /tmp/d20161024-13689-1xak3ij/solution.rb:51:in `each'
     # /tmp/d20161024-13689-1xak3ij/solution.rb:51:in `reshape'
     # /tmp/d20161024-13689-1xak3ij/spec.rb:146: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)>'

  4) Task 2 Hash#reshape can extract fields from arrays by index
     Failure/Error: expect(input.reshape(shape)).to eq output
     NoMethodError:
       undefined method `split' for {:first_name=>"users.0.name", :second_name=>"users.1.name"}:Hash
     # /tmp/d20161024-13689-1xak3ij/solution.rb:3:in `fetch_deep'
     # /tmp/d20161024-13689-1xak3ij/solution.rb:52:in `block in reshape'
     # /tmp/d20161024-13689-1xak3ij/solution.rb:51:in `each'
     # /tmp/d20161024-13689-1xak3ij/solution.rb:51:in `reshape'
     # /tmp/d20161024-13689-1xak3ij/spec.rb:177: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)>'

  5) Task 2 Array#reshape maps each element with the result of Hash#fetch_deep
     Failure/Error: expect(input.reshape(shape)).to eq [
       
       expected: [{:type=>"meal"}, {:type=>"dessert"}]
            got: [{:type=>nil}, {:type=>nil}]
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -[{:type=>"meal"}, {:type=>"dessert"}]
       +[{:type=>nil}, {:type=>nil}]
     # /tmp/d20161024-13689-1xak3ij/spec.rb:213: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.00937 seconds
15 examples, 5 failures

Failed examples:

rspec /tmp/d20161024-13689-1xak3ij/spec.rb:3 # Task 2 README works for all examples
rspec /tmp/d20161024-13689-1xak3ij/spec.rb:125 # Task 2 Hash#reshape can extract fields to nested objects
rspec /tmp/d20161024-13689-1xak3ij/spec.rb:136 # Task 2 Hash#reshape can create nested objects
rspec /tmp/d20161024-13689-1xak3ij/spec.rb:156 # Task 2 Hash#reshape can extract fields from arrays by index
rspec /tmp/d20161024-13689-1xak3ij/spec.rb:205 # Task 2 Array#reshape maps each element with the result of Hash#fetch_deep

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

Дарин обнови решението на 24.10.2016 03:59 (преди над 7 години)

+class Hash
+ def fetch_deep(path)
+ new_path_style = path.split('.'). clone
+ current = self.from_array_to_hash.stringify_keys
+ while new_path_style.size > 1 && current do
+ v = current[new_path_style.shift]
+ current = v
+ end
+ current ? current[new_path_style.first] : nil
+ end
+end
+
+class Hash
+ def stringify_keys
+ new_hash_style = {}
+ self.map do |key, value|
+ value = value.stringify_keys if value.is_a?(Hash) || value.is_a?(Array)
+ new_hash_style[key.to_s] = value
+ end
+ new_hash_style
+ end
+end
+
+class Array
+ def stringify_keys
+ new_array_style = []
+ self.map do |value|
+ value = value.stringify_keys if value.is_a?(Hash) || value.is_a?(Array)
+ new_array_style = value
+ end
+ new_array_style
+ end
+end
+
+class Hash
+ def from_array_to_hash
+ current = self
+ current.each do |k, _|
+ if current[k].is_a?(Array)
+ current[k] = current[k].map.with_index { |x, i| [i, x] }.to_h
+ elsif current[k].is_a?(Hash)
+ current[k].from_array_to_hash
+ end
+ end
+ end
+end
+
+class Hash
+ def reshape(shape)
+ current = self.from_array_to_hash
+ shape.from_array_to_hash.each do |key, value|
+ shape[key] = current.fetch_deep(value)
+ end
+ end
+end
+
+class Array
+ def reshape(shape)
+ curr = self
+ curr = curr.map { |i| i.from_array_to_hash }
+ curr.map { |i| i.reshape(shape) }
+
+ end
+end