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

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

Към профила на Красимир Тренчев

Резултати

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

Код

class Array
def fetch_deep(keys_list)
head, _, tail = keys_list.partition(".")
return self[head.to_i] if tail.empty?
self[head.to_i].fetch_deep(tail)
end
end
class Hash
def fetch_deep(keys_list)
head, _, tail = keys_list.partition(".")
head = head.to_sym unless self.key?(head)
return self[head] if tail.empty?
self[head].fetch_deep(tail)
end
end
class Object
def fetch_deep(*)
nil
end
end
class Hash
def reshape(shape)
reshaped_hash = {}
shape.keys.each do |x|
reshaped_hash.update({x => self.fetch_deep(shape[x])})
end
self.clear.update(reshaped_hash)
end
end
class Array
def reshape(shape)
reshaped_array = []
self.each do |x|
reshaped_array.push(x.reshape(shape))
end
self.replace(reshaped_array)
end
end

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

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

Failures:

  1) Task 2 README works for all examples
     Failure/Error: expect(order.reshape(shape)).to eq({food: {type: 'cake', taste: 'chocolate'}})
     ArgumentError:
       wrong number of arguments (given 1, expected 0)
     # /tmp/d20161024-13689-16zg807/solution.rb:11:in `partition'
     # /tmp/d20161024-13689-16zg807/solution.rb:11:in `fetch_deep'
     # /tmp/d20161024-13689-16zg807/solution.rb:28:in `block in reshape'
     # /tmp/d20161024-13689-16zg807/solution.rb:27:in `each'
     # /tmp/d20161024-13689-16zg807/solution.rb:27:in `reshape'
     # /tmp/d20161024-13689-16zg807/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#reshape can extract fields to nested objects
     Failure/Error: expect(input.reshape(shape)).to eq output
     ArgumentError:
       wrong number of arguments (given 1, expected 0)
     # /tmp/d20161024-13689-16zg807/solution.rb:11:in `partition'
     # /tmp/d20161024-13689-16zg807/solution.rb:11:in `fetch_deep'
     # /tmp/d20161024-13689-16zg807/solution.rb:28:in `block in reshape'
     # /tmp/d20161024-13689-16zg807/solution.rb:27:in `each'
     # /tmp/d20161024-13689-16zg807/solution.rb:27:in `reshape'
     # /tmp/d20161024-13689-16zg807/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)>'

  3) Task 2 Hash#reshape can create nested objects
     Failure/Error: expect(input.reshape(shape)).to eq output
     ArgumentError:
       wrong number of arguments (given 1, expected 0)
     # /tmp/d20161024-13689-16zg807/solution.rb:11:in `partition'
     # /tmp/d20161024-13689-16zg807/solution.rb:11:in `fetch_deep'
     # /tmp/d20161024-13689-16zg807/solution.rb:28:in `block in reshape'
     # /tmp/d20161024-13689-16zg807/solution.rb:27:in `each'
     # /tmp/d20161024-13689-16zg807/solution.rb:27:in `reshape'
     # /tmp/d20161024-13689-16zg807/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)>'

  4) Task 2 Hash#reshape can extract fields from arrays by index
     Failure/Error: expect(input.reshape(shape)).to eq output
     ArgumentError:
       wrong number of arguments (given 1, expected 0)
     # /tmp/d20161024-13689-16zg807/solution.rb:11:in `partition'
     # /tmp/d20161024-13689-16zg807/solution.rb:11:in `fetch_deep'
     # /tmp/d20161024-13689-16zg807/solution.rb:28:in `block in reshape'
     # /tmp/d20161024-13689-16zg807/solution.rb:27:in `each'
     # /tmp/d20161024-13689-16zg807/solution.rb:27:in `reshape'
     # /tmp/d20161024-13689-16zg807/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)>'

  5) Task 2 Hash#reshape does not modify the input hash
     Failure/Error: expect(input).to eq menu: {
       
       expected: {:menu=>{:order=>"cake", "dessert"=>"ice cream", 3=>4}}
            got: {:order=>"cake", :dessert=>"ice cream"}
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,3 @@
       -:menu => {:order=>"cake", "dessert"=>"ice cream", 3=>4}
       +:dessert => "ice cream",
       +:order => "cake"
     # /tmp/d20161024-13689-16zg807/spec.rb:196: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.00875 seconds
15 examples, 5 failures

Failed examples:

rspec /tmp/d20161024-13689-16zg807/spec.rb:3 # Task 2 README works for all examples
rspec /tmp/d20161024-13689-16zg807/spec.rb:125 # Task 2 Hash#reshape can extract fields to nested objects
rspec /tmp/d20161024-13689-16zg807/spec.rb:136 # Task 2 Hash#reshape can create nested objects
rspec /tmp/d20161024-13689-16zg807/spec.rb:156 # Task 2 Hash#reshape can extract fields from arrays by index
rspec /tmp/d20161024-13689-16zg807/spec.rb:180 # Task 2 Hash#reshape does not modify the input hash

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

Красимир обнови решението на 22.10.2016 21:21 (преди около 8 години)

+class Array
+ def fetch_deep(keys_list)
+ partition_output = keys_list.partition(".")
+ return self[partition_output[0].to_i] if partition_output[1].empty?
+ self[partition_output[0].to_i].fetch_deep(partition_output[2])
+ end
+end
+
+class Hash
+ def fetch_deep(keys_list)
+ partition_output = keys_list.partition(".")
+ return self[partition_output[0].to_sym] if partition_output[1].empty?
+ if self.member?(partition_output[0].to_s)
+ return self[partition_output[0].to_s].fetch_deep(partition_output[2])
+ end
+ self[partition_output[0].to_sym].fetch_deep(partition_output[2])
+ end
+end
+
+class Object
+ def fetch_deep(*)
+ nil
+ end
+end
+
+

Красимир обнови решението на 23.10.2016 14:36 (преди около 8 години)

class Array
def fetch_deep(keys_list)
partition_output = keys_list.partition(".")
- return self[partition_output[0].to_i] if partition_output[1].empty?
+ return self[partition_output[0].to_i] if partition_output[2].empty?
self[partition_output[0].to_i].fetch_deep(partition_output[2])
end
end
class Hash
def fetch_deep(keys_list)
partition_output = keys_list.partition(".")
- return self[partition_output[0].to_sym] if partition_output[1].empty?
+ return self[partition_output[0].to_sym] if partition_output[2].empty?
if self.member?(partition_output[0].to_s)
return self[partition_output[0].to_s].fetch_deep(partition_output[2])
end
self[partition_output[0].to_sym].fetch_deep(partition_output[2])
end
end
class Object
def fetch_deep(*)
nil
end
end
+class Hash
+ def reshape(shape)
+ reshaped_hash = {}
+ shape.keys.each do |x|
+ reshaped_hash.update({x => self.fetch_deep(shape[x])})
+ puts x
+ end
+ self.clear.update(reshaped_hash)
+ end
+end
+class Array
+ def reshape(shape)
+ reshaped_array = []
+ self.each do |x|
+ reshaped_array.push(x.reshape(shape))
+ end
+ self.replace(reshaped_array)
+ end
+end

Красимир обнови решението на 23.10.2016 16:40 (преди около 8 години)

class Array
def fetch_deep(keys_list)
partition_output = keys_list.partition(".")
return self[partition_output[0].to_i] if partition_output[2].empty?
self[partition_output[0].to_i].fetch_deep(partition_output[2])
end
end
class Hash
def fetch_deep(keys_list)
partition_output = keys_list.partition(".")
- return self[partition_output[0].to_sym] if partition_output[2].empty?
- if self.member?(partition_output[0].to_s)
- return self[partition_output[0].to_s].fetch_deep(partition_output[2])
+ unless self.key?(partition_output[0])
+ partition_output[0] = partition_output[0].to_sym
end
- self[partition_output[0].to_sym].fetch_deep(partition_output[2])
+ return self[partition_output[0]] if partition_output[2].empty?
+ self[partition_output[0]].fetch_deep(partition_output[2])
end
end
class Object
def fetch_deep(*)
nil
end
end
class Hash
def reshape(shape)
reshaped_hash = {}
shape.keys.each do |x|
reshaped_hash.update({x => self.fetch_deep(shape[x])})
puts x
end
self.clear.update(reshaped_hash)
end
end
class Array
def reshape(shape)
reshaped_array = []
self.each do |x|
reshaped_array.push(x.reshape(shape))
end
self.replace(reshaped_array)
end
end

Красимир обнови решението на 24.10.2016 14:08 (преди около 8 години)

class Array
def fetch_deep(keys_list)
- partition_output = keys_list.partition(".")
- return self[partition_output[0].to_i] if partition_output[2].empty?
- self[partition_output[0].to_i].fetch_deep(partition_output[2])
+ head, _, tail = keys_list.partition(".")
+ return self[head.to_i] if tail.empty?
+ self[head.to_i].fetch_deep(tail)
end
end
class Hash
def fetch_deep(keys_list)
- partition_output = keys_list.partition(".")
- unless self.key?(partition_output[0])
- partition_output[0] = partition_output[0].to_sym
- end
- return self[partition_output[0]] if partition_output[2].empty?
- self[partition_output[0]].fetch_deep(partition_output[2])
+ head, _, tail = keys_list.partition(".")
+ head = head.to_sym unless self.key?(head)
+ return self[head] if tail.empty?
+ self[head].fetch_deep(tail)
end
end
class Object
def fetch_deep(*)
nil
end
end
class Hash
def reshape(shape)
reshaped_hash = {}
shape.keys.each do |x|
reshaped_hash.update({x => self.fetch_deep(shape[x])})
- puts x
end
self.clear.update(reshaped_hash)
end
end
class Array
def reshape(shape)
reshaped_array = []
self.each do |x|
reshaped_array.push(x.reshape(shape))
end
self.replace(reshaped_array)
end
-end
+end