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

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

Към профила на Константин Нецов

Резултати

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

Код

class Hash
def fetch_deep(path)
(arr = path.split(".").map(&:to_sym)).each_with_index do |val, i|
each do |k, v|
return nil if val != k
return v if k == val && (i + 1) == arr.size
((t = t(v, arr[(i + 1), arr.size])).nil? ? break : (return t)) if h?(v)
end
end
end
def reshape(shape)
h = {}
if shape.values[0].is_a?(Hash)
f1(shape)
else
shape.each { |k, v| h[k.to_sym] = self.fetch_deep(v) }
end
h
end
end
class Array
def reshape(shape)
array = []
h = {}
self.each do |hash|
shape.each { |k, v| h[k.to_sym] = hash.fetch_deep(v) }
array << h
end
array
end
end
def f1(shape1)
h = {}
shape1.each do |_, v|
v.each do |k1, k2|
h[k1.to_sym] = self.fetch_deep(k2)
end
c[shape1.keys[0].to_sym] = h
end
c
end
def tr(h)
key = h.keys[0]
ha = {}
h.each do |_, v|
v.map { |k1, v1| ha[tran_keys(key, k1)] = v1 }
end
ha
end
def tran_keys(a, b)
(a.to_s << "." << b.to_s).to_sym
end
def h?(hash)
hash.is_a?(Hash)
end
def if_array(arr, paths)
if paths.size == 1
arr
elsif paths.size == 2
arr[paths[1].to_s.to_i].nil? ? nil : arr[paths[1].to_s.to_i]
elsif paths.size == 3
arr[paths[1].to_s.to_i].nil? ? nil : arr[paths[1].to_s.to_i][paths[2]]
end
end
def t(h, e)
h.each do |k, v|
return if_array(v, e) if k == e[0] && v.is_a?(Array)
return v if k == e[0]
end
nil
end

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

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

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-1c29py8/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 can look up deeply nested values
     Failure/Error: expect(input.fetch_deep('order.meal.type')).to eq 'dessert'
       
       expected: "dessert"
            got: {:type=>"dessert"}
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -"dessert"
       +:type => "dessert"
     # /tmp/d20161024-13689-1c29py8/spec.rb:70: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 find values in arrays by index
     Failure/Error: expect(input.fetch_deep('orders.0.meal')).to eq 'cake'
       
       expected: "cake"
            got: nil
       
       (compared using ==)
     # /tmp/d20161024-13689-1c29py8/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)>'

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

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

  6) Task 2 Hash#reshape can extract fields to nested objects
     Failure/Error: expect(input.reshape(shape)).to eq output
     NameError:
       undefined local variable or method `c' for {:profile=>{:name=>"Georgi"}}:Hash
     # /tmp/d20161024-13689-1c29py8/solution.rb:41:in `block in f1'
     # /tmp/d20161024-13689-1c29py8/solution.rb:37:in `each'
     # /tmp/d20161024-13689-1c29py8/solution.rb:37:in `f1'
     # /tmp/d20161024-13689-1c29py8/solution.rb:15:in `reshape'
     # /tmp/d20161024-13689-1c29py8/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
     NameError:
       undefined local variable or method `c' for {:name=>"Georgi"}:Hash
     # /tmp/d20161024-13689-1c29py8/solution.rb:41:in `block in f1'
     # /tmp/d20161024-13689-1c29py8/solution.rb:37:in `each'
     # /tmp/d20161024-13689-1c29py8/solution.rb:37:in `f1'
     # /tmp/d20161024-13689-1c29py8/solution.rb:15:in `reshape'
     # /tmp/d20161024-13689-1c29py8/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 can extract fields from arrays by index
     Failure/Error: expect(input.reshape(shape)).to eq output
     NameError:
       undefined local variable or method `c' for {:users=>[{:name=>"Georgi"}, {:name=>"Ivan"}]}:Hash
     # /tmp/d20161024-13689-1c29py8/solution.rb:41:in `block in f1'
     # /tmp/d20161024-13689-1c29py8/solution.rb:37:in `each'
     # /tmp/d20161024-13689-1c29py8/solution.rb:37:in `f1'
     # /tmp/d20161024-13689-1c29py8/solution.rb:15:in `reshape'
     # /tmp/d20161024-13689-1c29py8/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=>"dessert"}, {:type=>"dessert"}]
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -[{:type=>"meal"}, {:type=>"dessert"}]
       +[{:type=>"dessert"}, {:type=>"dessert"}]
     # /tmp/d20161024-13689-1c29py8/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.00954 seconds
15 examples, 9 failures

Failed examples:

rspec /tmp/d20161024-13689-1c29py8/spec.rb:3 # Task 2 README works for all examples
rspec /tmp/d20161024-13689-1c29py8/spec.rb:63 # Task 2 Hash#fetch_deep can look up deeply nested values
rspec /tmp/d20161024-13689-1c29py8/spec.rb:73 # Task 2 Hash#fetch_deep can find values in arrays by index
rspec /tmp/d20161024-13689-1c29py8/spec.rb:92 # Task 2 Hash#fetch_deep is indifferent to symbols and strings
rspec /tmp/d20161024-13689-1c29py8/spec.rb:108 # Task 2 Hash#fetch_deep can fetch integer-like keys from hashes
rspec /tmp/d20161024-13689-1c29py8/spec.rb:125 # Task 2 Hash#reshape can extract fields to nested objects
rspec /tmp/d20161024-13689-1c29py8/spec.rb:136 # Task 2 Hash#reshape can create nested objects
rspec /tmp/d20161024-13689-1c29py8/spec.rb:156 # Task 2 Hash#reshape can extract fields from arrays by index
rspec /tmp/d20161024-13689-1c29py8/spec.rb:205 # Task 2 Array#reshape maps each element with the result of Hash#fetch_deep

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

Константин обнови решението на 24.10.2016 14:30 (преди над 7 години)

+class Hash
+ def fetch_deep(path)
+ (arr = path.split(".").map(&:to_sym)).each_with_index do |val, i|
+ each do |k, v|
+ return nil if val != k
+ return v if k == val && (i + 1) == arr.size
+ ((t = t(v, arr[(i + 1), arr.size])).nil? ? break : (return t)) if h?(v)
+ end
+ end
+ end
+
+ def reshape(shape)
+ h = {}
+ if shape.values[0].is_a?(Hash)
+ f1(shape)
+ else
+ shape.each { |k, v| h[k.to_sym] = self.fetch_deep(v) }
+ end
+ h
+ end
+end
+
+class Array
+ def reshape(shape)
+ array = []
+ h = {}
+ self.each do |hash|
+ shape.each { |k, v| h[k.to_sym] = hash.fetch_deep(v) }
+ array << h
+ end
+ array
+ end
+end
+
+def f1(shape1)
+ h = {}
+ shape1.each do |_, v|
+ v.each do |k1, k2|
+ h[k1.to_sym] = self.fetch_deep(k2)
+ end
+ c[shape1.keys[0].to_sym] = h
+ end
+ c
+end
+
+def tr(h)
+ key = h.keys[0]
+ ha = {}
+ h.each do |_, v|
+ v.map { |k1, v1| ha[tran_keys(key, k1)] = v1 }
+ end
+ ha
+end
+
+def tran_keys(a, b)
+ (a.to_s << "." << b.to_s).to_sym
+end
+
+def h?(hash)
+ hash.is_a?(Hash)
+end
+
+def if_array(arr, paths)
+ if paths.size == 1
+ arr
+ elsif paths.size == 2
+ arr[paths[1].to_s.to_i].nil? ? nil : arr[paths[1].to_s.to_i]
+ elsif paths.size == 3
+ arr[paths[1].to_s.to_i].nil? ? nil : arr[paths[1].to_s.to_i][paths[2]]
+ end
+end
+
+def t(h, e)
+ h.each do |k, v|
+ return if_array(v, e) if k == e[0] && v.is_a?(Array)
+ return v if k == e[0]
+ end
+ nil
+end