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

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

Към профила на Милена Монова

Резултати

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

Код

class Hash
def fetch_deep(path)
fetch_recursion(keys_to_s, path.split('.'))
end
def keys_to_s
result = {}
map do |k, v|
result[k.to_s] = v.is_a?(Hash) ? v.keys_to_s : v
if v.is_a?(Array)
result[k.to_s] = v.collect { |o| o.is_a?(Hash) ? o.keys_to_s : o.to_s }
end
end
result
end
def fetch_recursion(hash, path)
return hash if path.empty?
return fetch_recursion(hash[path.shift.to_i], path) if hash.is_a?(Array)
return fetch_recursion(hash.fetch(path.shift), path) if hash.is_a?(Hash)
end
def reshape(shape)
reshape_recursion(shape, {})
end
def reshape_recursion(shape, copy)
shape.each do |k, v|
copy[k] = v.is_a?(Hash) ? reshape_recursion(v, {}) : fetch_deep(v)
end
copy
end
end
class Array
def reshape(shape)
array = []
each { |h| array << h.to_h.reshape(shape) }
array
end
end

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

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

Failures:

  1) Task 2 Hash#fetch_deep returns nil for non-existant keys
     Failure/Error: expect(input.fetch_deep('meal')).to be nil
     KeyError:
       key not found: "meal"
     # /tmp/d20161024-13689-83m9kt/solution.rb:20:in `fetch'
     # /tmp/d20161024-13689-83m9kt/solution.rb:20:in `fetch_recursion'
     # /tmp/d20161024-13689-83m9kt/solution.rb:3:in `fetch_deep'
     # /tmp/d20161024-13689-83m9kt/spec.rb:88: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#reshape assigns nil to unknown keys
     Failure/Error: expect(input.reshape(shape)).to eq b: nil
     KeyError:
       key not found: "b"
     # /tmp/d20161024-13689-83m9kt/solution.rb:20:in `fetch'
     # /tmp/d20161024-13689-83m9kt/solution.rb:20:in `fetch_recursion'
     # /tmp/d20161024-13689-83m9kt/solution.rb:3:in `fetch_deep'
     # /tmp/d20161024-13689-83m9kt/solution.rb:29:in `block in reshape_recursion'
     # /tmp/d20161024-13689-83m9kt/solution.rb:28:in `each'
     # /tmp/d20161024-13689-83m9kt/solution.rb:28:in `reshape_recursion'
     # /tmp/d20161024-13689-83m9kt/solution.rb:24:in `reshape'
     # /tmp/d20161024-13689-83m9kt/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)>'

Finished in 0.00755 seconds
15 examples, 2 failures

Failed examples:

rspec /tmp/d20161024-13689-83m9kt/spec.rb:85 # Task 2 Hash#fetch_deep returns nil for non-existant keys
rspec /tmp/d20161024-13689-83m9kt/spec.rb:149 # Task 2 Hash#reshape assigns nil to unknown keys

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

Милена обнови решението на 23.10.2016 14:06 (преди над 7 години)

+class Hash
+ def fetch_deep(path)
+ fetch_recursion(keys_to_s, path.split('.'))
+ end
+
+ def keys_to_s
+ result = {}
+ map do |key, value|
+ result[key.to_s] = value.is_a?(Hash) ? value.keys_to_s : value
+ if value.is_a?(Array)
+ result[key.to_s] = value.collect { |v| v.keys_to_s if v.is_a?(Hash) }
+ end
+ end
+ result
+ end
+
+ def fetch_recursion(hash, path)
+ return hash.fetch(path.first) if path.size == 1
+ return fetch_recursion(hash[path.shift.to_i], path) if hash.is_a?(Array)
+ fetch_recursion(hash.fetch(path.shift), path)
+ end
+
+ def reshape(shape)
+ reshape_recursion(shape, {})
+ end
+
+ def reshape_recursion(shape, self_copy)
+ shape.each do |key, value|
+ self_copy[key] = if value.is_a?(Hash)
+ reshape_recursion(value, {})
+ else
+ fetch_deep(value)
+ end
+ end
+ self_copy
+ end
+end
+
+class Array
+ def reshape(shape)
+ array = []
+ each { |h| array << h.to_h.reshape(shape) }
+ array
+ end
+end

Милена обнови решението на 23.10.2016 14:08 (преди над 7 години)

class Hash
def fetch_deep(path)
fetch_recursion(keys_to_s, path.split('.'))
end
def keys_to_s
result = {}
map do |key, value|
result[key.to_s] = value.is_a?(Hash) ? value.keys_to_s : value
if value.is_a?(Array)
result[key.to_s] = value.collect { |v| v.keys_to_s if v.is_a?(Hash) }
end
end
result
end
def fetch_recursion(hash, path)
return hash.fetch(path.first) if path.size == 1
return fetch_recursion(hash[path.shift.to_i], path) if hash.is_a?(Array)
fetch_recursion(hash.fetch(path.shift), path)
end
def reshape(shape)
reshape_recursion(shape, {})
end
- def reshape_recursion(shape, self_copy)
- shape.each do |key, value|
- self_copy[key] = if value.is_a?(Hash)
- reshape_recursion(value, {})
- else
- fetch_deep(value)
- end
+ def reshape_recursion(shape, copy)
+ shape.each do |k, v|
+ copy[k] = v.is_a?(Hash) ? reshape_recursion(v, {}) : fetch_deep(v)
end
- self_copy
+ copy
end
end
class Array
def reshape(shape)
array = []
each { |h| array << h.to_h.reshape(shape) }
array
end
end

Милена обнови решението на 23.10.2016 15:14 (преди над 7 години)

class Hash
def fetch_deep(path)
fetch_recursion(keys_to_s, path.split('.'))
end
def keys_to_s
result = {}
- map do |key, value|
- result[key.to_s] = value.is_a?(Hash) ? value.keys_to_s : value
- if value.is_a?(Array)
- result[key.to_s] = value.collect { |v| v.keys_to_s if v.is_a?(Hash) }
+ map do |k, v|
+ result[k.to_s] = v.is_a?(Hash) ? v.keys_to_s : v
+ if v.is_a?(Array)
+ result[k.to_s] = v.collect { |o| o.is_a?(Hash) ? o.keys_to_s : o.to_s }
end
end
result
end
def fetch_recursion(hash, path)
- return hash.fetch(path.first) if path.size == 1
+ return hash if path.empty?
return fetch_recursion(hash[path.shift.to_i], path) if hash.is_a?(Array)
- fetch_recursion(hash.fetch(path.shift), path)
+ return fetch_recursion(hash.fetch(path.shift), path) if hash.is_a?(Hash)
end
def reshape(shape)
reshape_recursion(shape, {})
end
def reshape_recursion(shape, copy)
shape.each do |k, v|
copy[k] = v.is_a?(Hash) ? reshape_recursion(v, {}) : fetch_deep(v)
end
copy
end
end
class Array
def reshape(shape)
array = []
each { |h| array << h.to_h.reshape(shape) }
array
end
end