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

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

Към профила на Никола Жишев

Резултати

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

Код

module FetchDeep
def fetch_deep(keys)
keys.split('.').reduce(self) do |m, key|
m[key.to_i] || m[key] || m[key.to_sym]
end
end
end
class Hash
include FetchDeep
def reshape(shape)
{}.tap do |h|
shape.each do |key, val|
h[key] = pick_reshape_step(val)
end
end
end
private
def pick_reshape_step(value)
if value.is_a?(Hash)
reshape(value)
elsif value.is_a?(Array)
value.map { |v| reshape(v) }
else
fetch_deep(value)
end
end
end
class Array
include FetchDeep
def reshape(shape)
[].tap do |arr|
each do |input|
arr << input.reshape(shape)
end
end
end
end

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

....F..........

Failures:

  1) 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-1td96dt/solution.rb:4:in `block in fetch_deep'
     # /tmp/d20161024-13689-1td96dt/solution.rb:3:in `each'
     # /tmp/d20161024-13689-1td96dt/solution.rb:3:in `reduce'
     # /tmp/d20161024-13689-1td96dt/solution.rb:3:in `fetch_deep'
     # /tmp/d20161024-13689-1td96dt/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)>'

Finished in 0.00796 seconds
15 examples, 1 failure

Failed examples:

rspec /tmp/d20161024-13689-1td96dt/spec.rb:85 # Task 2 Hash#fetch_deep returns nil for non-existant keys

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

Никола обнови решението на 19.10.2016 15:04 (преди около 8 години)

+module FetchDeep
+ def fetch_deep(keys)
+ keys.split('.').reduce(self) do |m, key|
+ m[key.to_i] || m[key] || m[key.to_sym]
+ end
+ end
+end
+
+class Hash
+ include FetchDeep
+
+ def reshape(shape)
+ {}.tap do |h|
+ shape.each do |key, val|
+ h[key] = val.is_a?(Hash) ? reshape(val) : fetch_deep(val)
+ end
+ end
+ end
+end
+
+class Array
+ include FetchDeep
+
+ def reshape(shape)
+ [].tap do |arr|
+ each do |input|
+ arr << input.reshape(shape)
+ end
+ end
+ end
+end

Никола обнови решението на 19.10.2016 15:49 (преди около 8 години)

module FetchDeep
def fetch_deep(keys)
keys.split('.').reduce(self) do |m, key|
m[key.to_i] || m[key] || m[key.to_sym]
end
end
end
class Hash
include FetchDeep
def reshape(shape)
{}.tap do |h|
shape.each do |key, val|
- h[key] = val.is_a?(Hash) ? reshape(val) : fetch_deep(val)
+ h[key] = pick_reshape_step(val)
end
end
end
+
+ private
+
+ def pick_reshape_step(value)
+ if value.is_a?(Hash)
+ reshape(value)
+ elsif value.is_a?(Array)
+ value.map { |v| reshape(v) }
+ else
+ fetch_deep(value)
+ end
+ end
end
class Array
include FetchDeep
def reshape(shape)
[].tap do |arr|
each do |input|
arr << input.reshape(shape)
end
end
end
-end
+end