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

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

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

Резултати

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

Код

class Array
def fetch_deep(keys)
return self[keys.first.to_i] if keys.size == 1
self[keys.first.to_i].fetch_deep(keys[1..keys.size])
end
def reshape(shape)
self.map { |element| element.reshape(shape) }
end
end
class Hash
def fetch_deep(keys)
keys = keys.split('.') if keys.is_a? String
key = keys.first.to_sym if self.keys.include? keys.first.to_sym
key = keys.first if self.keys.include? keys.first
return self[key] if keys.size == 1
self[key].fetch_deep(keys[1..keys.size])
end
def reshape(shape)
reshaped = {}
shape.each { |key, value| reshaped[key] = self.fetch_deep(value) }
reshaped
end
end

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

F...F....FF.F..

Failures:

  1) Task 2 README works for all examples
     Failure/Error: expect(order.reshape(shape)).to eq({food: {type: 'cake', taste: 'chocolate'}})
     NoMethodError:
       undefined method `to_sym' for [:type, "dessert.type"]:Array
     # /tmp/d20161024-13689-1wifes1/solution.rb:15:in `fetch_deep'
     # /tmp/d20161024-13689-1wifes1/solution.rb:23:in `block in reshape'
     # /tmp/d20161024-13689-1wifes1/solution.rb:23:in `each'
     # /tmp/d20161024-13689-1wifes1/solution.rb:23:in `reshape'
     # /tmp/d20161024-13689-1wifes1/spec.rb:44: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 returns nil for non-existant keys
     Failure/Error: expect(input.fetch_deep('meal.0.type')).to be nil
     NoMethodError:
       undefined method `fetch_deep' for nil:NilClass
     # /tmp/d20161024-13689-1wifes1/solution.rb:18:in `fetch_deep'
     # /tmp/d20161024-13689-1wifes1/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)>'

  3) Task 2 Hash#reshape can extract fields to nested objects
     Failure/Error: expect(input.reshape(shape)).to eq output
     NoMethodError:
       undefined method `to_sym' for [:first_name, "profile.name"]:Array
     # /tmp/d20161024-13689-1wifes1/solution.rb:15:in `fetch_deep'
     # /tmp/d20161024-13689-1wifes1/solution.rb:23:in `block in reshape'
     # /tmp/d20161024-13689-1wifes1/solution.rb:23:in `each'
     # /tmp/d20161024-13689-1wifes1/solution.rb:23:in `reshape'
     # /tmp/d20161024-13689-1wifes1/spec.rb:133: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)>'

  4) Task 2 Hash#reshape can create nested objects
     Failure/Error: expect(input.reshape(shape)).to eq output
     NoMethodError:
       undefined method `to_sym' for [:first_name, "name"]:Array
     # /tmp/d20161024-13689-1wifes1/solution.rb:15:in `fetch_deep'
     # /tmp/d20161024-13689-1wifes1/solution.rb:23:in `block in reshape'
     # /tmp/d20161024-13689-1wifes1/solution.rb:23:in `each'
     # /tmp/d20161024-13689-1wifes1/solution.rb:23:in `reshape'
     # /tmp/d20161024-13689-1wifes1/spec.rb:146: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)>'

  5) Task 2 Hash#reshape can extract fields from arrays by index
     Failure/Error: expect(input.reshape(shape)).to eq output
     NoMethodError:
       undefined method `to_sym' for [:first_name, "users.0.name"]:Array
     # /tmp/d20161024-13689-1wifes1/solution.rb:15:in `fetch_deep'
     # /tmp/d20161024-13689-1wifes1/solution.rb:23:in `block in reshape'
     # /tmp/d20161024-13689-1wifes1/solution.rb:23:in `each'
     # /tmp/d20161024-13689-1wifes1/solution.rb:23:in `reshape'
     # /tmp/d20161024-13689-1wifes1/spec.rb:177: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.00792 seconds
15 examples, 5 failures

Failed examples:

rspec /tmp/d20161024-13689-1wifes1/spec.rb:3 # Task 2 README works for all examples
rspec /tmp/d20161024-13689-1wifes1/spec.rb:85 # Task 2 Hash#fetch_deep returns nil for non-existant keys
rspec /tmp/d20161024-13689-1wifes1/spec.rb:125 # Task 2 Hash#reshape can extract fields to nested objects
rspec /tmp/d20161024-13689-1wifes1/spec.rb:136 # Task 2 Hash#reshape can create nested objects
rspec /tmp/d20161024-13689-1wifes1/spec.rb:156 # Task 2 Hash#reshape can extract fields from arrays by index

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

Цветан обнови решението на 23.10.2016 22:22 (преди около 8 години)

+class Array
+ def fetch_deep(keys)
+ key = keys.first.to_i
+ return self[key] if keys.size == 1
+ self[key].fetch_deep(keys[1..keys.size])
+ end
+
+ def reshape(shape)
+ reshaped = []
+ self.each do |element|
+ reshaped << element.reshape(shape)
+ end
+ reshaped
+ end
+end
+
+class Hash
+ def fetch_deep(keys)
+ keys = keys.split('.') if keys.is_a? String
+ key = keys.first.to_sym if self.keys.include? keys.first.to_sym
+ key = keys.first if self.keys.include? keys.first
+ return self[key] if keys.size == 1
+ self[key].fetch_deep(keys[1..keys.size])
+ end
+
+ def reshape(other)
+ reshaped = {}
+ other.each { |key, value| reshaped[key] = self.fetch_deep(value) }
+ reshaped
+ end
+end

Цветан обнови решението на 23.10.2016 23:08 (преди около 8 години)

class Array
def fetch_deep(keys)
- key = keys.first.to_i
- return self[key] if keys.size == 1
- self[key].fetch_deep(keys[1..keys.size])
+ return self[keys.first.to_i] if keys.size == 1
+ self[keys.first.to_i].fetch_deep(keys[1..keys.size])
end
def reshape(shape)
reshaped = []
self.each do |element|
reshaped << element.reshape(shape)
end
reshaped
end
end
class Hash
def fetch_deep(keys)
keys = keys.split('.') if keys.is_a? String
key = keys.first.to_sym if self.keys.include? keys.first.to_sym
key = keys.first if self.keys.include? keys.first
return self[key] if keys.size == 1
self[key].fetch_deep(keys[1..keys.size])
end
- def reshape(other)
- reshaped = {}
- other.each { |key, value| reshaped[key] = self.fetch_deep(value) }
- reshaped
+ def reshape(shape)
+ old_keys = self.keys
+ shape.each { |key, value| self[key] = self.fetch_deep(value) }
+ old_keys.each { |key| self.delete(key) }
+ self
end
end

Цветан обнови решението на 23.10.2016 23:13 (преди около 8 години)

class Array
def fetch_deep(keys)
return self[keys.first.to_i] if keys.size == 1
self[keys.first.to_i].fetch_deep(keys[1..keys.size])
end
def reshape(shape)
reshaped = []
- self.each do |element|
- reshaped << element.reshape(shape)
- end
+ self.each { |element| reshaped << element.reshape(shape) }
reshaped
end
end
class Hash
def fetch_deep(keys)
keys = keys.split('.') if keys.is_a? String
key = keys.first.to_sym if self.keys.include? keys.first.to_sym
key = keys.first if self.keys.include? keys.first
return self[key] if keys.size == 1
self[key].fetch_deep(keys[1..keys.size])
end
def reshape(shape)
old_keys = self.keys
shape.each { |key, value| self[key] = self.fetch_deep(value) }
old_keys.each { |key| self.delete(key) }
self
end
end

Цветан обнови решението на 24.10.2016 07:47 (преди около 8 години)

class Array
def fetch_deep(keys)
return self[keys.first.to_i] if keys.size == 1
self[keys.first.to_i].fetch_deep(keys[1..keys.size])
end
def reshape(shape)
- reshaped = []
- self.each { |element| reshaped << element.reshape(shape) }
- reshaped
+ self.map { |element| element.reshape(shape) }
end
end
class Hash
def fetch_deep(keys)
keys = keys.split('.') if keys.is_a? String
key = keys.first.to_sym if self.keys.include? keys.first.to_sym
key = keys.first if self.keys.include? keys.first
return self[key] if keys.size == 1
self[key].fetch_deep(keys[1..keys.size])
end
- def reshape(shape)
- old_keys = self.keys
- shape.each { |key, value| self[key] = self.fetch_deep(value) }
- old_keys.each { |key| self.delete(key) }
- self
+ def reshape(other)
+ reshaped = {}
+ other.each { |key, value| reshaped[key] = self.fetch_deep(value) }
+ reshaped
end
end

Цветан обнови решението на 24.10.2016 12:34 (преди около 8 години)

class Array
def fetch_deep(keys)
return self[keys.first.to_i] if keys.size == 1
self[keys.first.to_i].fetch_deep(keys[1..keys.size])
end
-
+
def reshape(shape)
self.map { |element| element.reshape(shape) }
end
end
class Hash
def fetch_deep(keys)
keys = keys.split('.') if keys.is_a? String
key = keys.first.to_sym if self.keys.include? keys.first.to_sym
key = keys.first if self.keys.include? keys.first
return self[key] if keys.size == 1
self[key].fetch_deep(keys[1..keys.size])
end
- def reshape(other)
+ def reshape(shape)
reshaped = {}
- other.each { |key, value| reshaped[key] = self.fetch_deep(value) }
+ shape.each { |key, value| reshaped[key] = self.fetch_deep(value) }
reshaped
end
end