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

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

Към профила на Стефан Якимов

Резултати

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

Код

class Hash
# this does not work for nested arrays
def fetch_deep!(roat)
keys = roat.split '.'
if keys.length == 1
# recursion bottom
self[keys[0].to_sym]
else
key = self[keys[0]] ? keys.shift : keys.shift.to_sym
self[key].fetch_deep! keys.join '.'
end
end
def extract_value(collection, key)
if collection.is_a?(Hash)
collection[key] ? collection[key] : collection[key.to_sym]
else
collection[key.to_i]
end
end
def fetch_deep(path)
keys = path.split '.'
key = keys.shift
new_collecton = extract_value(self, key)
until keys.empty?
key = keys.shift
new_collecton = extract_value(new_collecton, key)
end
new_collecton
end
def reshape(shape)
shape.each do |key, value|
if !value.is_a?(Hash)
# recursion bottom hits when shape is a single lvl hash
shape[key] = fetch_deep(value)
else
reshape(value)
end
end
end
end
class Array
def reshape(shape)
reshaped = []
each do |map|
to_be_added = shape.clone
shape.each { |key, value| to_be_added[key] = map.fetch_deep(value) }
reshaped << to_be_added
end
reshaped
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-dz048m/solution.rb:18:in `extract_value'
     # /tmp/d20161024-13689-dz048m/solution.rb:30:in `fetch_deep'
     # /tmp/d20161024-13689-dz048m/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.00939 seconds
15 examples, 1 failure

Failed examples:

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

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

Стефан обнови решението на 21.10.2016 18:19 (преди около 8 години)

+class Hash
+ # this does not work for nested arrays
+ def fetch_deep!(roat)
+ keys = roat.split '.'
+ if keys.length == 1
+ # recursion bottom
+ self[keys[0].to_sym]
+ else
+ key = self[keys[0]] ? keys.shift : keys.shift.to_sym
+ self[key].fetch_deep! keys.join '.'
+ end
+ end
+
+ def extract_value(collection, key)
+ if collection.is_a?(Hash)
+ collection[key] ? collection[key] : collection[key.to_sym]
+ else
+ collection[key.to_i]
+ end
+ end
+
+ def fetch_deep(path)
+ keys = path.split '.'
+ key = keys.shift
+
+ new_collecton = extract_value(self, key)
+
+ until keys.empty?
+ key = keys.shift
+ new_collecton = extract_value(new_collecton, key)
+ end
+
+ new_collecton
+ end
+end
+
+order = {
+ dessert: {
+ type: 'cake',
+ variant: 'chocolate',
+ rating: 10,
+ comments: [
+ { text: 'So sweet!' },
+ { text: 'A perfect blend of milk chocolate and cookies.' }
+ ]
+ }
+}
+
+unless order.fetch_deep('false path').nil?
+ raise 'order.fetch_deep(dessert.comments.0.text) is wrong!'
+end
+
+unless order.fetch_deep('dessert.comments.0.text') == 'So sweet!'
+ raise 'order.fetch_deep(dessert.comments.0.text) is wrong!'
+end
+
+unless order.fetch_deep('dessert.variant') == 'chocolate'
+ raise 'order.fetch_deep(dessert.comments.0.text) is wrong!'
+end

Стефан обнови решението на 21.10.2016 18:20 (преди около 8 години)

class Hash
# this does not work for nested arrays
def fetch_deep!(roat)
keys = roat.split '.'
if keys.length == 1
# recursion bottom
self[keys[0].to_sym]
else
key = self[keys[0]] ? keys.shift : keys.shift.to_sym
self[key].fetch_deep! keys.join '.'
end
end
def extract_value(collection, key)
if collection.is_a?(Hash)
collection[key] ? collection[key] : collection[key.to_sym]
else
collection[key.to_i]
end
end
def fetch_deep(path)
keys = path.split '.'
key = keys.shift
new_collecton = extract_value(self, key)
until keys.empty?
key = keys.shift
new_collecton = extract_value(new_collecton, key)
end
new_collecton
end
-end
-
+end
-order = {
- dessert: {
- type: 'cake',
- variant: 'chocolate',
- rating: 10,
- comments: [
- { text: 'So sweet!' },
- { text: 'A perfect blend of milk chocolate and cookies.' }
- ]
- }
-}
-
-unless order.fetch_deep('false path').nil?
- raise 'order.fetch_deep(dessert.comments.0.text) is wrong!'
-end
-
-unless order.fetch_deep('dessert.comments.0.text') == 'So sweet!'
- raise 'order.fetch_deep(dessert.comments.0.text) is wrong!'
-end
-
-unless order.fetch_deep('dessert.variant') == 'chocolate'
- raise 'order.fetch_deep(dessert.comments.0.text) is wrong!'
-end

Стефан обнови решението на 21.10.2016 19:39 (преди около 8 години)

class Hash
# this does not work for nested arrays
def fetch_deep!(roat)
keys = roat.split '.'
if keys.length == 1
# recursion bottom
self[keys[0].to_sym]
else
key = self[keys[0]] ? keys.shift : keys.shift.to_sym
self[key].fetch_deep! keys.join '.'
end
end
def extract_value(collection, key)
if collection.is_a?(Hash)
collection[key] ? collection[key] : collection[key.to_sym]
else
collection[key.to_i]
end
end
def fetch_deep(path)
keys = path.split '.'
key = keys.shift
new_collecton = extract_value(self, key)
until keys.empty?
key = keys.shift
new_collecton = extract_value(new_collecton, key)
end
new_collecton
end
-end
+
+ def reshape(shape)
+ shape.each do |key, value|
+ if value.is_a?(Hash)
+
+ else
+ shape[key] = fetch_deep(value)
+ end
+ end
+ shape
+ end
+end
+
+order = {
+ dessert: {type: 'cake', variant: 'chocolate'}
+}
+
+shape = {
+ food: 'dessert.type',
+ taste: 'dessert.variant'
+}
+
+p order.reshape(shape)
+#=> {
+# food: 'cake',
+# taste: 'chocolate'
+# }

Стефан обнови решението на 21.10.2016 19:40 (преди около 8 години)

class Hash
# this does not work for nested arrays
def fetch_deep!(roat)
keys = roat.split '.'
if keys.length == 1
# recursion bottom
self[keys[0].to_sym]
else
key = self[keys[0]] ? keys.shift : keys.shift.to_sym
self[key].fetch_deep! keys.join '.'
end
end
def extract_value(collection, key)
if collection.is_a?(Hash)
collection[key] ? collection[key] : collection[key.to_sym]
else
collection[key.to_i]
end
end
def fetch_deep(path)
keys = path.split '.'
key = keys.shift
new_collecton = extract_value(self, key)
until keys.empty?
key = keys.shift
new_collecton = extract_value(new_collecton, key)
end
new_collecton
end
def reshape(shape)
shape.each do |key, value|
if value.is_a?(Hash)
-
+ # TODO: Go recursively
else
shape[key] = fetch_deep(value)
end
end
shape
end
end
-
-order = {
- dessert: {type: 'cake', variant: 'chocolate'}
-}
-
-shape = {
- food: 'dessert.type',
- taste: 'dessert.variant'
-}
-
-p order.reshape(shape)
-#=> {
-# food: 'cake',
-# taste: 'chocolate'
-# }

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

class Hash
# this does not work for nested arrays
def fetch_deep!(roat)
keys = roat.split '.'
if keys.length == 1
# recursion bottom
self[keys[0].to_sym]
else
key = self[keys[0]] ? keys.shift : keys.shift.to_sym
self[key].fetch_deep! keys.join '.'
end
end
def extract_value(collection, key)
if collection.is_a?(Hash)
collection[key] ? collection[key] : collection[key.to_sym]
else
collection[key.to_i]
end
end
def fetch_deep(path)
keys = path.split '.'
key = keys.shift
new_collecton = extract_value(self, key)
until keys.empty?
key = keys.shift
new_collecton = extract_value(new_collecton, key)
end
new_collecton
end
def reshape(shape)
shape.each do |key, value|
- if value.is_a?(Hash)
- # TODO: Go recursively
- else
+ if !value.is_a?(Hash)
+ # recursion bottom hits when shape is a single lvl hash
shape[key] = fetch_deep(value)
+ else
+ reshape(value)
end
end
- shape
end
end
+
+class Array
+ def reshape(shape)
+ reshaped = []
+ each do |map|
+ to_be_added = shape.clone
+ shape.each { |key, value| to_be_added[key] = map.fetch_deep(value) }
+ reshaped << to_be_added
+ end
+ reshaped
+ end
+end
+
+order = {
+ dessert: { type: 'cake', variant: 'chocolate' }
+}
+
+shape = {
+ food: { type: 'dessert.type', taste: 'dessert.variant' }
+}
+
+puts order.reshape(shape)
+#=> {
+# food: {type: 'cake', taste: 'chocolate'}
+# }

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

class Hash
# this does not work for nested arrays
def fetch_deep!(roat)
keys = roat.split '.'
if keys.length == 1
# recursion bottom
self[keys[0].to_sym]
else
key = self[keys[0]] ? keys.shift : keys.shift.to_sym
self[key].fetch_deep! keys.join '.'
end
end
def extract_value(collection, key)
if collection.is_a?(Hash)
collection[key] ? collection[key] : collection[key.to_sym]
else
collection[key.to_i]
end
end
def fetch_deep(path)
keys = path.split '.'
key = keys.shift
new_collecton = extract_value(self, key)
until keys.empty?
key = keys.shift
new_collecton = extract_value(new_collecton, key)
end
new_collecton
end
def reshape(shape)
shape.each do |key, value|
if !value.is_a?(Hash)
# recursion bottom hits when shape is a single lvl hash
shape[key] = fetch_deep(value)
else
reshape(value)
end
end
end
end
class Array
def reshape(shape)
reshaped = []
each do |map|
to_be_added = shape.clone
shape.each { |key, value| to_be_added[key] = map.fetch_deep(value) }
reshaped << to_be_added
end
reshaped
end
end
-
-order = {
- dessert: { type: 'cake', variant: 'chocolate' }
-}
-
-shape = {
- food: { type: 'dessert.type', taste: 'dessert.variant' }
-}
-
-puts order.reshape(shape)
-#=> {
-# food: {type: 'cake', taste: 'chocolate'}
-# }