Решение на Втора задача - хешове, масиви и структура от София Петрова

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

Към профила на София Петрова

Резултати

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

Код

class Hash
def fetch_deep(form)
splitted_keys = form.split('.')
get_value(self, splitted_keys)
end
def get_value(hash, keys)
key = hash[keys[0]] ? keys[0] : keys[0].to_sym
return hash[key] if keys.size == 1
number = next_key(keys[1])
if number
return get_value(hash[key][number], keys.drop(2)) if keys.size > 2
return hash[key][number]
end
get_value(hash[key], keys.drop(1))
end
def next_key(key)
Integer(key || '')
rescue ArgumentError
false
end
def reshape(form)
if !(form.any? { |_, value| value.is_a? Hash })
form.map { |key, value| form[key] = fetch_deep(value) }
return form
else
reshaping(form)
end
end
def reshaping(form)
flatten_hash = form.flatten
flatten_hash.map do |el|
el = el.map { |key, _| el[key] = fetch_deep(el[key]) } if el.is_a? Hash
end
Hash[*flatten_hash]
end
end
class Array
def reshape(form)
self.map { |element| element.reshape(form.dup) }
end
end

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

....F..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-z2iudd/solution.rb:13:in `get_value'
     # /tmp/d20161024-13689-z2iudd/solution.rb:4:in `fetch_deep'
     # /tmp/d20161024-13689-z2iudd/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)>'

  2) 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-z2iudd/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)>'

Finished in 0.00833 seconds
15 examples, 2 failures

Failed examples:

rspec /tmp/d20161024-13689-z2iudd/spec.rb:85 # Task 2 Hash#fetch_deep returns nil for non-existant keys
rspec /tmp/d20161024-13689-z2iudd/spec.rb:108 # Task 2 Hash#fetch_deep can fetch integer-like keys from hashes

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

София обнови решението на 24.10.2016 03:08 (преди около 8 години)

+class Hash
+ def fetch_deep(key)
+ splitted_keys = key.split('.')
+ get_value(self, splitted_keys)
+ end
+
+ def get_value(hash, keys)
+ key = hash[keys[0]] ? keys[0] : keys[0].to_sym
+ return hash[key] if keys.size == 1
+ number = next_key(keys[1])
+
+ if number
+ return get_value(hash[key][number], keys.drop(2)) if keys.size > 2
+ return hash[key][number]
+ end
+
+ get_value(hash[key], keys.drop(1))
+ end
+
+ def next_key(key)
+ Integer(key || '')
+ rescue ArgumentError
+ false
+ end
+end

София обнови решението на 24.10.2016 13:12 (преди около 8 години)

class Hash
- def fetch_deep(key)
- splitted_keys = key.split('.')
+ def fetch_deep(form)
+ splitted_keys = form.split('.')
get_value(self, splitted_keys)
end
def get_value(hash, keys)
key = hash[keys[0]] ? keys[0] : keys[0].to_sym
return hash[key] if keys.size == 1
number = next_key(keys[1])
if number
return get_value(hash[key][number], keys.drop(2)) if keys.size > 2
return hash[key][number]
end
get_value(hash[key], keys.drop(1))
end
def next_key(key)
Integer(key || '')
rescue ArgumentError
false
+ end
+
+ def reshape(form)
+ if !(form.any? { |_, value| value.is_a? Hash })
+ form.map { |key, value| form[key] = fetch_deep(value) }
+ return form
+ else
+ reshaping(form)
+ end
+ end
+
+ def reshaping(form)
+ flatten_hash = form.flatten
+ flatten_hash.map do |el|
+ el = el.map { |key, _| el[key] = fetch_deep(el[key]) } if el.is_a? Hash
+ end
+ Hash[*flatten_hash]
+ end
+end
+
+class Array
+ def reshape(form)
+ self.map do |element|
+ element.reshape(form.dup)
+ end
end
end

София обнови решението на 24.10.2016 16:02 (преди около 8 години)

class Hash
def fetch_deep(form)
splitted_keys = form.split('.')
get_value(self, splitted_keys)
end
def get_value(hash, keys)
key = hash[keys[0]] ? keys[0] : keys[0].to_sym
return hash[key] if keys.size == 1
number = next_key(keys[1])
if number
return get_value(hash[key][number], keys.drop(2)) if keys.size > 2
return hash[key][number]
end
get_value(hash[key], keys.drop(1))
end
def next_key(key)
Integer(key || '')
rescue ArgumentError
false
end
def reshape(form)
if !(form.any? { |_, value| value.is_a? Hash })
form.map { |key, value| form[key] = fetch_deep(value) }
return form
else
reshaping(form)
end
end
def reshaping(form)
flatten_hash = form.flatten
flatten_hash.map do |el|
el = el.map { |key, _| el[key] = fetch_deep(el[key]) } if el.is_a? Hash
end
Hash[*flatten_hash]
end
end
class Array
def reshape(form)
- self.map do |element|
- element.reshape(form.dup)
- end
+ self.map { |element| element.reshape(form.dup) }
end
end