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

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

Към профила на Виктор Клисуров

Резултати

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

Код

class Array
def fetch_deep(path)
a = path.split('.')
return self[a[0].to_i] if a.length == 1
self[a[0].to_i].fetch_deep(path[a[0].length + 1..-1])
end
end
class Hash
def fetch_deep(path)
a = path.split('.')
return self[a[0].to_sym] if a.length == 1
self[a[0].to_sym].fetch_deep(path[a[0].length + 1..-1])
end
def reshape(shape)
shape.each do |index, value|
self[index] = self.fetch_deep(value)
end
end
end

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

F...FF.FFFFFFFF

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-fsfpey/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 returns nil for non-existant keys
     Failure/Error: expect(input.fetch_deep('meal.0.type')).to be nil
     NoMethodError:
       undefined method `fetch_deep' for nil:NilClass
     # /tmp/d20161024-13689-fsfpey/solution.rb:14:in `fetch_deep'
     # /tmp/d20161024-13689-fsfpey/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)>'

  3) 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-fsfpey/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)>'

  4) 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-fsfpey/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)>'

  5) 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-fsfpey/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)>'

  6) 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-fsfpey/solution.rb:12:in `fetch_deep'
     # /tmp/d20161024-13689-fsfpey/solution.rb:19:in `block in reshape'
     # /tmp/d20161024-13689-fsfpey/solution.rb:18:in `each'
     # /tmp/d20161024-13689-fsfpey/solution.rb:18:in `reshape'
     # /tmp/d20161024-13689-fsfpey/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)>'

  7) 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-fsfpey/solution.rb:12:in `fetch_deep'
     # /tmp/d20161024-13689-fsfpey/solution.rb:19:in `block in reshape'
     # /tmp/d20161024-13689-fsfpey/solution.rb:18:in `each'
     # /tmp/d20161024-13689-fsfpey/solution.rb:18:in `reshape'
     # /tmp/d20161024-13689-fsfpey/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)>'

  8) 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-fsfpey/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)>'

  9) 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-fsfpey/solution.rb:12:in `fetch_deep'
     # /tmp/d20161024-13689-fsfpey/solution.rb:19:in `block in reshape'
     # /tmp/d20161024-13689-fsfpey/solution.rb:18:in `each'
     # /tmp/d20161024-13689-fsfpey/solution.rb:18:in `reshape'
     # /tmp/d20161024-13689-fsfpey/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)>'

  10) 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: {:menu=>{:order=>"cake", "dessert"=>"ice cream", 3=>4}, :order=>"cake", :dessert=>nil}
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,4 @@
       -:menu => {:order=>"cake", "dessert"=>"ice cream", 3=>4}
       +:dessert => nil,
       +:menu => {:order=>"cake", "dessert"=>"ice cream", 3=>4},
       +:order => "cake"
     # /tmp/d20161024-13689-fsfpey/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)>'

  11) Task 2 Array#reshape maps each element with the result of Hash#fetch_deep
     Failure/Error: expect(input.reshape(shape)).to eq [
     NoMethodError:
       undefined method `reshape' for [{:order=>{:item=>"meal"}}, {:order=>{:item=>"dessert"}}]:Array
     # /tmp/d20161024-13689-fsfpey/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.01313 seconds
15 examples, 11 failures

Failed examples:

rspec /tmp/d20161024-13689-fsfpey/spec.rb:3 # Task 2 README works for all examples
rspec /tmp/d20161024-13689-fsfpey/spec.rb:85 # Task 2 Hash#fetch_deep returns nil for non-existant keys
rspec /tmp/d20161024-13689-fsfpey/spec.rb:92 # Task 2 Hash#fetch_deep is indifferent to symbols and strings
rspec /tmp/d20161024-13689-fsfpey/spec.rb:108 # Task 2 Hash#fetch_deep can fetch integer-like keys from hashes
rspec /tmp/d20161024-13689-fsfpey/spec.rb:117 # Task 2 Hash#reshape can rename fields
rspec /tmp/d20161024-13689-fsfpey/spec.rb:125 # Task 2 Hash#reshape can extract fields to nested objects
rspec /tmp/d20161024-13689-fsfpey/spec.rb:136 # Task 2 Hash#reshape can create nested objects
rspec /tmp/d20161024-13689-fsfpey/spec.rb:149 # Task 2 Hash#reshape assigns nil to unknown keys
rspec /tmp/d20161024-13689-fsfpey/spec.rb:156 # Task 2 Hash#reshape can extract fields from arrays by index
rspec /tmp/d20161024-13689-fsfpey/spec.rb:180 # Task 2 Hash#reshape does not modify the input hash
rspec /tmp/d20161024-13689-fsfpey/spec.rb:205 # Task 2 Array#reshape maps each element with the result of Hash#fetch_deep

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

Виктор обнови решението на 24.10.2016 16:49 (преди над 7 години)

+class Array
+ def fetch_deep(path)
+ a = path.split('.')
+ return self[a[0].to_i] if a.length == 1
+
+ self[a[0].to_i].fetch_deep(path[a[0].length + 1..-1])
+ end
+end
+
+class Hash
+ def fetch_deep(path)
+ a = path.split('.')
+ return self[a[0].to_sym] if a.length == 1
+ self[a[0].to_sym].fetch_deep(path[a[0].length + 1..-1])
+ end
+
+ def reshape(shape)
+ a = {}
+ shape.each do |index, value|
+ a[index] = self.fetch_deep(value)
+ end
+ end
+end

Виктор обнови решението на 24.10.2016 16:52 (преди над 7 години)

class Array
def fetch_deep(path)
a = path.split('.')
return self[a[0].to_i] if a.length == 1
self[a[0].to_i].fetch_deep(path[a[0].length + 1..-1])
end
end
class Hash
def fetch_deep(path)
a = path.split('.')
return self[a[0].to_sym] if a.length == 1
self[a[0].to_sym].fetch_deep(path[a[0].length + 1..-1])
end
def reshape(shape)
- a = {}
shape.each do |index, value|
- a[index] = self.fetch_deep(value)
+ self[index] = self.fetch_deep(value)
+ return self
end
end
end

Виктор обнови решението на 24.10.2016 16:53 (преди над 7 години)

class Array
def fetch_deep(path)
a = path.split('.')
return self[a[0].to_i] if a.length == 1
self[a[0].to_i].fetch_deep(path[a[0].length + 1..-1])
end
end
class Hash
def fetch_deep(path)
a = path.split('.')
return self[a[0].to_sym] if a.length == 1
self[a[0].to_sym].fetch_deep(path[a[0].length + 1..-1])
end
def reshape(shape)
shape.each do |index, value|
self[index] = self.fetch_deep(value)
- return self
end
end
end