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

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

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

Резултати

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

Код

class Hash
def fetch_deep(path)
first, *segmented_path = path.split('.')
value = self[first] || self[first.to_sym]
segmented_path.each do |segment|
break unless value
(value = value[segment.to_i]) && next if segment.to_i.to_s == segment
value = value[segment] || value[segment.to_sym]
end
value
end
def reshape(shape)
result = {}
shape.map do |key, path|
result[key] = self.fetch_deep(path)
end
result
end
end
class Array
def reshape(shape)
map { |element| element.reshape(shape) }
end
end

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

F......F.FF.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-1cri84k/solution.rb:3:in `fetch_deep'
     # /tmp/d20161024-13689-1cri84k/solution.rb:16:in `block in reshape'
     # /tmp/d20161024-13689-1cri84k/solution.rb:15:in `each'
     # /tmp/d20161024-13689-1cri84k/solution.rb:15:in `map'
     # /tmp/d20161024-13689-1cri84k/solution.rb:15:in `reshape'
     # /tmp/d20161024-13689-1cri84k/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#fetch_deep can fetch integer-like keys from hashes
     Failure/Error: expect(input.fetch_deep('nested.1')).to eq :a
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # /tmp/d20161024-13689-1cri84k/solution.rb:8:in `block in fetch_deep'
     # /tmp/d20161024-13689-1cri84k/solution.rb:5:in `each'
     # /tmp/d20161024-13689-1cri84k/solution.rb:5:in `fetch_deep'
     # /tmp/d20161024-13689-1cri84k/spec.rb:111: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 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-1cri84k/solution.rb:3:in `fetch_deep'
     # /tmp/d20161024-13689-1cri84k/solution.rb:16:in `block in reshape'
     # /tmp/d20161024-13689-1cri84k/solution.rb:15:in `each'
     # /tmp/d20161024-13689-1cri84k/solution.rb:15:in `map'
     # /tmp/d20161024-13689-1cri84k/solution.rb:15:in `reshape'
     # /tmp/d20161024-13689-1cri84k/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)>'

  4) 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-1cri84k/solution.rb:3:in `fetch_deep'
     # /tmp/d20161024-13689-1cri84k/solution.rb:16:in `block in reshape'
     # /tmp/d20161024-13689-1cri84k/solution.rb:15:in `each'
     # /tmp/d20161024-13689-1cri84k/solution.rb:15:in `map'
     # /tmp/d20161024-13689-1cri84k/solution.rb:15:in `reshape'
     # /tmp/d20161024-13689-1cri84k/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)>'

  5) 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-1cri84k/solution.rb:3:in `fetch_deep'
     # /tmp/d20161024-13689-1cri84k/solution.rb:16:in `block in reshape'
     # /tmp/d20161024-13689-1cri84k/solution.rb:15:in `each'
     # /tmp/d20161024-13689-1cri84k/solution.rb:15:in `map'
     # /tmp/d20161024-13689-1cri84k/solution.rb:15:in `reshape'
     # /tmp/d20161024-13689-1cri84k/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)>'

Finished in 0.00789 seconds
15 examples, 5 failures

Failed examples:

rspec /tmp/d20161024-13689-1cri84k/spec.rb:3 # Task 2 README works for all examples
rspec /tmp/d20161024-13689-1cri84k/spec.rb:108 # Task 2 Hash#fetch_deep can fetch integer-like keys from hashes
rspec /tmp/d20161024-13689-1cri84k/spec.rb:125 # Task 2 Hash#reshape can extract fields to nested objects
rspec /tmp/d20161024-13689-1cri84k/spec.rb:136 # Task 2 Hash#reshape can create nested objects
rspec /tmp/d20161024-13689-1cri84k/spec.rb:156 # Task 2 Hash#reshape can extract fields from arrays by index

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

Владимир обнови решението на 22.10.2016 22:41 (преди над 7 години)

+class Hash
+ def fetch_deep(path)
+ first, *segmented_path = path.split('.')
+ value = self[first] || self[first.to_sym]
+ segmented_path.each do |segment|
+ break unless value
+ (value = value[segment.to_i]) && next if segment.to_i.to_s == segment
+ value = value[segment] || value[segment.to_sym]
+ end
+ value
+ end
+
+ def reshape(shape)
+ result = {}
+ shape.map do |key, path|
+ result[key] = self.fetch_deep(path)
+ end
+ result
+ end
+end
+
+class Array
+ def reshape(shape)
+ map { |element| element.reshape(shape) }
+ end
+end