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

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

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

Резултати

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

Код

class String
def integer?
/\A[-+]?\d+\z/ === self
end
end
class Hash
def fetch_deep(path)
keys = path.split(".").map { |key| key.integer? ? key.to_i : key.to_sym }
self[keys[0]] != nil ? fetch_value(keys) : nil
end
def reshape(shape)
shape.each_key do |key|
if shape[key].class == Hash
reshape(shape[key])
else
shape[key] = fetch_deep(shape[key])
end
end
shape
end
def fetch_value(keys)
value = dup
keys.each do |key|
value = value[key] || value[key.to_s]
end
value
end
end
class Array
def reshape(shape)
map.with_index do |_, index|
reshaped = {}
shape.each_key do |key|
reshaped[key] = self[index].fetch_deep(shape[key])
end
reshaped
end
end
end

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

F....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-1ykvgjy/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-1ykvgjy/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.2')).to eq :b
       
       expected: :b
            got: nil
       
       (compared using ==)
     # /tmp/d20161024-13689-1ykvgjy/spec.rb:112: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.00823 seconds
15 examples, 3 failures

Failed examples:

rspec /tmp/d20161024-13689-1ykvgjy/spec.rb:3 # Task 2 README works for all examples
rspec /tmp/d20161024-13689-1ykvgjy/spec.rb:92 # Task 2 Hash#fetch_deep is indifferent to symbols and strings
rspec /tmp/d20161024-13689-1ykvgjy/spec.rb:108 # Task 2 Hash#fetch_deep can fetch integer-like keys from hashes

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

Мартин обнови решението на 19.10.2016 13:23 (преди над 7 години)

+class String
+ def integer?
+ /\A[-+]?\d+\z/ === self
+ end
+end
+
+class Hash
+ def fetch_deep(path)
+ keys = path.split(".").map { |key| key.integer? ? key.to_i : key.to_sym }
+ fetch_value(keys)
+ end
+
+ def reshape(shape)
+ reshaped = {}
+ shape.each_key { |key| reshaped[key] = fetch_deep(shape[key]) }
+ reshaped
+ end
+
+ def fetch_value(keys)
+ value = dup
+ keys.each do |key|
+ value = value[key] || value[key.to_s]
+ end
+ value
+ end
+end
+
+class Array
+ def reshape(shape)
+ map.with_index do |_, index|
+ reshaped = {}
+ shape.each_key do |key|
+ reshaped[key] = self[index].fetch_deep(shape[key])
+ end
+ reshaped
+ end
+ end
+end

Мартин обнови решението на 23.10.2016 18:49 (преди над 7 години)

class String
def integer?
/\A[-+]?\d+\z/ === self
end
end
class Hash
def fetch_deep(path)
keys = path.split(".").map { |key| key.integer? ? key.to_i : key.to_sym }
- fetch_value(keys)
+ self[keys[0]] != nil ? fetch_value(keys) : nil
end
def reshape(shape)
- reshaped = {}
- shape.each_key { |key| reshaped[key] = fetch_deep(shape[key]) }
- reshaped
+ shape.each_key do |key|
+ if shape[key].class == Hash
+ reshape(shape[key])
+ else
+ shape[key] = fetch_deep(shape[key])
+ end
+ end
+ shape
end
def fetch_value(keys)
value = dup
keys.each do |key|
value = value[key] || value[key.to_s]
end
value
end
end
class Array
def reshape(shape)
map.with_index do |_, index|
reshaped = {}
shape.each_key do |key|
reshaped[key] = self[index].fetch_deep(shape[key])
end
reshaped
end
end
end