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

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

Към профила на Константин Димитров

Резултати

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

Код

class Hash
def fetch_deep(key)
y = self
key.split('.').each do |x|
y = [y[x], y[x.to_i], y[x.to_sym]].select { |z| z != nil }[0]
end
y
end
def reshape(shape)
anser = {}
shape.each do |key, value|
if value.class == String
anser[key] = self.fetch_deep(value)
elsif value.class == Hash
anser[key] = self.reshape(value)
end
end
end
end
class Array
def reshape(shape)
anser = []
self.each { |value| anser << value.reshape(shape) }
anser
end
end

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

F..FF...FFFFF.F

Failures:

  1) Task 2 README works for all examples
     Failure/Error: expect(order.fetch_deep('dessert.comments.0.text')).to eq('So sweet!')
     TypeError:
       no implicit conversion of String into Integer
     # /tmp/d20161024-13689-10orhdh/solution.rb:5:in `[]'
     # /tmp/d20161024-13689-10orhdh/solution.rb:5:in `block in fetch_deep'
     # /tmp/d20161024-13689-10orhdh/solution.rb:4:in `each'
     # /tmp/d20161024-13689-10orhdh/solution.rb:4:in `fetch_deep'
     # /tmp/d20161024-13689-10orhdh/spec.rb:17: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 find values in arrays by index
     Failure/Error: expect(input.fetch_deep('orders.0.meal')).to eq 'cake'
     TypeError:
       no implicit conversion of String into Integer
     # /tmp/d20161024-13689-10orhdh/solution.rb:5:in `[]'
     # /tmp/d20161024-13689-10orhdh/solution.rb:5:in `block in fetch_deep'
     # /tmp/d20161024-13689-10orhdh/solution.rb:4:in `each'
     # /tmp/d20161024-13689-10orhdh/solution.rb:4:in `fetch_deep'
     # /tmp/d20161024-13689-10orhdh/spec.rb:81: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 returns nil for non-existant keys
     Failure/Error: expect(input.fetch_deep('meal.0.type')).to be nil
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # /tmp/d20161024-13689-10orhdh/solution.rb:5:in `block in fetch_deep'
     # /tmp/d20161024-13689-10orhdh/solution.rb:4:in `each'
     # /tmp/d20161024-13689-10orhdh/solution.rb:4:in `fetch_deep'
     # /tmp/d20161024-13689-10orhdh/spec.rb:89: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 rename fields
     Failure/Error: expect(input.reshape(shape)).to eq output
       
       expected: {:first_name=>"Georgi"}
            got: {:first_name=>"name"}
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -:first_name => "Georgi"
       +:first_name => "name"
     # /tmp/d20161024-13689-10orhdh/spec.rb:122: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 to nested objects
     Failure/Error: expect(input.reshape(shape)).to eq output
       
       expected: {:me=>{:first_name=>"Georgi"}}
            got: {:me=>{:first_name=>"profile.name"}}
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -:me => {:first_name=>"Georgi"}
       +:me => {:first_name=>"profile.name"}
     # /tmp/d20161024-13689-10orhdh/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)>'

  6) Task 2 Hash#reshape can create nested objects
     Failure/Error: expect(input.reshape(shape)).to eq output
       
       expected: {:profile=>{:first_name=>"Georgi"}}
            got: {:profile=>{:first_name=>"name"}}
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -:profile => {:first_name=>"Georgi"}
       +:profile => {:first_name=>"name"}
     # /tmp/d20161024-13689-10orhdh/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)>'

  7) Task 2 Hash#reshape assigns nil to unknown keys
     Failure/Error: expect(input.reshape(shape)).to eq b: nil
       
       expected: {:b=>nil}
            got: {:b=>"b"}
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -:b => nil
       +:b => "b"
     # /tmp/d20161024-13689-10orhdh/spec.rb:153: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)>'

  8) Task 2 Hash#reshape can extract fields from arrays by index
     Failure/Error: expect(input.reshape(shape)).to eq output
     TypeError:
       no implicit conversion of String into Integer
     # /tmp/d20161024-13689-10orhdh/solution.rb:5:in `[]'
     # /tmp/d20161024-13689-10orhdh/solution.rb:5:in `block in fetch_deep'
     # /tmp/d20161024-13689-10orhdh/solution.rb:4:in `each'
     # /tmp/d20161024-13689-10orhdh/solution.rb:4:in `fetch_deep'
     # /tmp/d20161024-13689-10orhdh/solution.rb:14:in `block in reshape'
     # /tmp/d20161024-13689-10orhdh/solution.rb:12:in `each'
     # /tmp/d20161024-13689-10orhdh/solution.rb:12:in `reshape'
     # /tmp/d20161024-13689-10orhdh/solution.rb:16:in `block in reshape'
     # /tmp/d20161024-13689-10orhdh/solution.rb:12:in `each'
     # /tmp/d20161024-13689-10orhdh/solution.rb:12:in `reshape'
     # /tmp/d20161024-13689-10orhdh/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)>'

  9) 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=>"order.item"}, {:type=>"order.item"}]
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -[{:type=>"meal"}, {:type=>"dessert"}]
       +[{:type=>"order.item"}, {:type=>"order.item"}]
     # /tmp/d20161024-13689-10orhdh/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.0104 seconds
15 examples, 9 failures

Failed examples:

rspec /tmp/d20161024-13689-10orhdh/spec.rb:3 # Task 2 README works for all examples
rspec /tmp/d20161024-13689-10orhdh/spec.rb:73 # Task 2 Hash#fetch_deep can find values in arrays by index
rspec /tmp/d20161024-13689-10orhdh/spec.rb:85 # Task 2 Hash#fetch_deep returns nil for non-existant keys
rspec /tmp/d20161024-13689-10orhdh/spec.rb:117 # Task 2 Hash#reshape can rename fields
rspec /tmp/d20161024-13689-10orhdh/spec.rb:125 # Task 2 Hash#reshape can extract fields to nested objects
rspec /tmp/d20161024-13689-10orhdh/spec.rb:136 # Task 2 Hash#reshape can create nested objects
rspec /tmp/d20161024-13689-10orhdh/spec.rb:149 # Task 2 Hash#reshape assigns nil to unknown keys
rspec /tmp/d20161024-13689-10orhdh/spec.rb:156 # Task 2 Hash#reshape can extract fields from arrays by index
rspec /tmp/d20161024-13689-10orhdh/spec.rb:205 # Task 2 Array#reshape maps each element with the result of Hash#fetch_deep

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

Константин обнови решението на 24.10.2016 12:11 (преди около 8 години)

+class Hash
+ def fetch_deep(key)
+ y = self
+ key.split('.').each do |x|
+ y = [y[x], y[x.to_i], y[x.to_sym]].select { |z| z != nil }[0]
+ end
+ y
+ end
+
+ def reshape(shape)
+ anser = {}
+ shape.each do |key, value|
+ if value.class == String
+ anser[key] = self.fetch_deep(value)
+ elsif value.class == Hash
+ anser[key] = self.reshape(value)
+ end
+ end
+ end
+end
+
+class Array
+ def reshape(shape)
+ anser = []
+ self.each { |value| anser << value.reshape(shape) }
+ anser
+ end
+end