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

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

Към профила на Ивайло Дончев

Резултати

  • 3 точки от тестове
  • 0 бонус точки
  • 3 точки общо
  • 8 успешни тест(а)
  • 7 неуспешни тест(а)

Код

def number?(str)
# to catch the exception if the 'str' string's 'to_i' method raises exception
true if Float(str) rescue false
end
class Hash
def fetch_deep(path)
keys = path.split('.')
dict = self
keys.each do |key|
dict = number?(key) ? dict[key.to_i] : dict[key.to_sym]
break if dict.nil?
end
dict
end
def reshape(shape)
shape_keys = shape.keys
old_keys = self.keys - shape_keys
# to add the new keys from the shape
shape_keys.map { |key| self[key] = self.fetch_deep(shape[key]) }
# remove old keys
old_keys.each { |k| self.delete k }
self
end
end
class Array
def reshape(shape)
shape_keys = shape.keys
self.each do |element|
old_keys = element.keys - shape_keys
shape_keys.map { |key| element[key] = element.fetch_deep(shape[key]) }
old_keys.each { |k| element.delete k }
end
self
end
end

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

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

Failures:

  1) Task 2 README works for all examples
     Failure/Error: expect(dessert.fetch_deep('variant')).to eq('chocolate')
       
       expected: "chocolate"
            got: nil
       
       (compared using ==)
     # /tmp/d20161024-13689-yu0jr5/spec.rb:25: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 is indifferent to symbols and strings
     Failure/Error: expect(input.fetch_deep('dessert')).to eq 'ice cream'
       
       expected: "ice cream"
            got: nil
       
       (compared using ==)
     # /tmp/d20161024-13689-yu0jr5/spec.rb:96: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#fetch_deep can fetch integer-like keys from hashes
     Failure/Error: expect(input.fetch_deep('nested.1')).to eq :a
       
       expected: :a
            got: nil
       
       (compared using ==)
     # /tmp/d20161024-13689-yu0jr5/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)>'

  4) 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-yu0jr5/solution.rb:8:in `fetch_deep'
     # /tmp/d20161024-13689-yu0jr5/solution.rb:21:in `block in reshape'
     # /tmp/d20161024-13689-yu0jr5/solution.rb:21:in `map'
     # /tmp/d20161024-13689-yu0jr5/solution.rb:21:in `reshape'
     # /tmp/d20161024-13689-yu0jr5/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)>'

  5) 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-yu0jr5/solution.rb:8:in `fetch_deep'
     # /tmp/d20161024-13689-yu0jr5/solution.rb:21:in `block in reshape'
     # /tmp/d20161024-13689-yu0jr5/solution.rb:21:in `map'
     # /tmp/d20161024-13689-yu0jr5/solution.rb:21:in `reshape'
     # /tmp/d20161024-13689-yu0jr5/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)>'

  6) 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-yu0jr5/solution.rb:8:in `fetch_deep'
     # /tmp/d20161024-13689-yu0jr5/solution.rb:21:in `block in reshape'
     # /tmp/d20161024-13689-yu0jr5/solution.rb:21:in `map'
     # /tmp/d20161024-13689-yu0jr5/solution.rb:21:in `reshape'
     # /tmp/d20161024-13689-yu0jr5/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)>'

  7) 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=>nil}
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,3 @@
       -:menu => {:order=>"cake", "dessert"=>"ice cream", 3=>4}
       +:dessert => nil,
       +:order => "cake"
     # /tmp/d20161024-13689-yu0jr5/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.00922 seconds
15 examples, 7 failures

Failed examples:

rspec /tmp/d20161024-13689-yu0jr5/spec.rb:3 # Task 2 README works for all examples
rspec /tmp/d20161024-13689-yu0jr5/spec.rb:92 # Task 2 Hash#fetch_deep is indifferent to symbols and strings
rspec /tmp/d20161024-13689-yu0jr5/spec.rb:108 # Task 2 Hash#fetch_deep can fetch integer-like keys from hashes
rspec /tmp/d20161024-13689-yu0jr5/spec.rb:125 # Task 2 Hash#reshape can extract fields to nested objects
rspec /tmp/d20161024-13689-yu0jr5/spec.rb:136 # Task 2 Hash#reshape can create nested objects
rspec /tmp/d20161024-13689-yu0jr5/spec.rb:156 # Task 2 Hash#reshape can extract fields from arrays by index
rspec /tmp/d20161024-13689-yu0jr5/spec.rb:180 # Task 2 Hash#reshape does not modify the input hash

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

Ивайло обнови решението на 23.10.2016 18:38 (преди над 7 години)

+def number?(str)
+ # to catch the exception if the 'str' string's 'to_i' method raises exception
+ true if Float(str) rescue false
+end
+
+class Hash
+ def fetch_deep(path)
+ keys = path.split('.')
+ dict = self
+ keys.each do |key|
+ dict = number?(key) ? dict[key.to_i] : dict[key.to_sym]
+ break if dict.nil?
+ end
+ dict
+ end
+
+ def reshape(shape)
+ shape_keys = shape.keys
+ old_keys = self.keys - shape_keys
+ # to add the new keys from the shape
+ shape_keys.map { |key| self[key] = self.fetch_deep(shape[key]) }
+ # remove old keys
+ old_keys.each { |k| self.delete k }
+ self
+ end
+end
+
+class Array
+ def reshape(shape)
+ shape_keys = shape.keys
+ self.each do |element|
+ old_keys = element.keys - shape_keys
+ shape_keys.map { |key| element[key] = element.fetch_deep(shape[key]) }
+ old_keys.each { |k| element.delete k }
+ end
+ self
+ end
+end