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

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

Към профила на Симеон Гергинов

Резултати

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

Код

module Fetch
def fetch_deep(path_with_keys)
parts = path_with_keys.split('.')
parts.reduce(self) { |result, current| foo(result, current) if result }
end
def integer?(string)
string.to_i.to_s == string
end
def foo(result, current)
integer?(current) ? result[current.to_i] : result[current.to_sym]
end
end
class Hash
include Fetch
def reshape(arg)
hash_to_be_shaped = arg.dup
hash_to_be_shaped.each_pair do |k, v|
if v.is_a?(Hash)
reshape(v)
else
hash_to_be_shaped[k] = fetch_deep(v)
end
end
end
end
class Array
include Fetch
def reshape(shape_array)
self.map! do |item|
item.reshape(shape_array)
end
end
end

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

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

Failures:

  1) Task 2 README works for all examples
     Failure/Error: expect(dessert.fetch_deep('variant')).to eq('chocolate')
       
       expected: "chocolate"
            got: nil
       
       (compared using ==)
     # /tmp/d20161024-13689-wcqm02/spec.rb:25: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 is indifferent to symbols and strings
     Failure/Error: expect(input.fetch_deep('dessert')).to eq 'ice cream'
       
       expected: "ice cream"
            got: nil
       
       (compared using ==)
     # /tmp/d20161024-13689-wcqm02/spec.rb:96: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#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-wcqm02/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)>'

  4) Task 2 Hash#reshape can extract fields to nested objects
     Failure/Error: expect(input.reshape(shape)).to eq output
       
       expected: {:me=>{:first_name=>"Georgi"}}
            got: {:me=>{:first_name=>"profile.name"}}
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -:me => {:first_name=>"Georgi"}
       +:me => {:first_name=>"profile.name"}
     # /tmp/d20161024-13689-wcqm02/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)>'

  5) Task 2 Hash#reshape can create nested objects
     Failure/Error: expect(input.reshape(shape)).to eq output
       
       expected: {:profile=>{:first_name=>"Georgi"}}
            got: {:profile=>{:first_name=>"name"}}
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -:profile => {:first_name=>"Georgi"}
       +:profile => {:first_name=>"name"}
     # /tmp/d20161024-13689-wcqm02/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)>'

  6) Task 2 Hash#reshape can extract fields from arrays by index
     Failure/Error: expect(input.reshape(shape)).to eq output
       
       expected: {:me=>{:first_name=>"Georgi", :second_name=>"Ivan"}}
            got: {:me=>{:first_name=>"users.0.name", :second_name=>"users.1.name"}}
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -:me => {:first_name=>"Georgi", :second_name=>"Ivan"}
       +:me => {:first_name=>"users.0.name", :second_name=>"users.1.name"}
     # /tmp/d20161024-13689-wcqm02/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.00959 seconds
15 examples, 6 failures

Failed examples:

rspec /tmp/d20161024-13689-wcqm02/spec.rb:3 # Task 2 README works for all examples
rspec /tmp/d20161024-13689-wcqm02/spec.rb:92 # Task 2 Hash#fetch_deep is indifferent to symbols and strings
rspec /tmp/d20161024-13689-wcqm02/spec.rb:108 # Task 2 Hash#fetch_deep can fetch integer-like keys from hashes
rspec /tmp/d20161024-13689-wcqm02/spec.rb:125 # Task 2 Hash#reshape can extract fields to nested objects
rspec /tmp/d20161024-13689-wcqm02/spec.rb:136 # Task 2 Hash#reshape can create nested objects
rspec /tmp/d20161024-13689-wcqm02/spec.rb:156 # Task 2 Hash#reshape can extract fields from arrays by index

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

Симеон обнови решението на 19.10.2016 23:42 (преди над 7 години)

+class Hash
+ def fetch_deep(path_with_keys)
+ parts = path_with_keys.split('.')
+ match = self[parts[0]]
+ if !(parts[1]) || match.nil?
+ match
+ else
+ match.fetch_deep(parts[1])
+ end
+ end
+end

Виж този пример от условието на задачата:

dessert = {
  type: 'cake',
  'variant' => 'chocolate'
}

dessert.fetch_deep('type')    #=> 'cake'
dessert.fetch_deep('variant') #=> 'chocolate'

Едното е символ. dessert['type'] връща nil, защото няма такъв ключ. Има dessert[:type]

Симеон обнови решението на 20.10.2016 17:06 (преди над 7 години)

class Hash
def fetch_deep(path_with_keys)
- parts = path_with_keys.split('.')
- match = self[parts[0]]
- if !(parts[1]) || match.nil?
- match
+ parts = path_with_keys.split('.', 2)
+ match = self[parts[0].to_sym]
+ if !parts[1] || match.nil?
+ return match
else
match.fetch_deep(parts[1])
end
end
-end
+end

Симеон обнови решението на 20.10.2016 22:52 (преди над 7 години)

class Hash
def fetch_deep(path_with_keys)
- parts = path_with_keys.split('.', 2)
- match = self[parts[0].to_sym]
- if !parts[1] || match.nil?
- return match
- else
- match.fetch_deep(parts[1])
+ parts = path_with_keys.split('.')
+ parts.reduce(self) { |result, current| foo(result, current) if result }
+ end
+
+ def integer?(string)
+ string.to_i.to_s == string
+ end
+
+ def foo(result, current)
+ integer?(current) ? result[current.to_i] : result[current.to_sym]
+ end
+
+ def reshape(hash_to_be_shaped)
+ hash_to_be_shaped.each_pair do |k, v|
+ if v.is_a?(Hash)
+ reshape(v)
+ else
+ hash_to_be_shaped[k] = fetch_deep(v)
+ end
end
end
end

Симеон обнови решението на 21.10.2016 10:12 (преди над 7 години)

class Hash
def fetch_deep(path_with_keys)
parts = path_with_keys.split('.')
parts.reduce(self) { |result, current| foo(result, current) if result }
end
def integer?(string)
string.to_i.to_s == string
end
def foo(result, current)
integer?(current) ? result[current.to_i] : result[current.to_sym]
end
def reshape(hash_to_be_shaped)
hash_to_be_shaped.each_pair do |k, v|
if v.is_a?(Hash)
reshape(v)
else
hash_to_be_shaped[k] = fetch_deep(v)
end
end
end
end
+
+class Array
+ def convert_to_hash(path_with_keys)
+ self.map do |hash|
+ [hash, hash[path_with_keys]]
+ end.to_h
+ end
+
+ def fetch_deep(path_with_keys)
+ convert_to_hash(path_with_keys)
+ parts = path_with_keys.split('.', 2)
+ match = self[0][parts[0].to_sym]
+ if !parts[1] || match.nil?
+ match
+ else
+ match.fetch_deep(parts[1])
+ end
+ end
+
+ def reshape(hash_to_be_shaped)
+ hash_to_be_shaped.each_pair do |k, v|
+ hash_to_be_shaped[k] = fetch_deep(v)
+ end
+ end
+end

На функцията с масива ми връща само хеш , а не масив от хешове и не съм сигурен къде точно бъркам за да не ми се получава отговора. Връща ми {food: 'musaka', price: 4.0} , вместо да върне [ {food: 'musaka', price: 4.0}, {food: 'cake', price: 3.5} ]

Симеон обнови решението на 22.10.2016 14:43 (преди над 7 години)

-class Hash
+module Fetch
def fetch_deep(path_with_keys)
parts = path_with_keys.split('.')
parts.reduce(self) { |result, current| foo(result, current) if result }
end
def integer?(string)
string.to_i.to_s == string
end
def foo(result, current)
integer?(current) ? result[current.to_i] : result[current.to_sym]
end
+end
- def reshape(hash_to_be_shaped)
+class Hash
+ include Fetch
+ def reshape(arg)
+ hash_to_be_shaped = arg.dup
hash_to_be_shaped.each_pair do |k, v|
if v.is_a?(Hash)
reshape(v)
else
hash_to_be_shaped[k] = fetch_deep(v)
end
end
end
end
class Array
- def convert_to_hash(path_with_keys)
- self.map do |hash|
- [hash, hash[path_with_keys]]
- end.to_h
- end
-
- def fetch_deep(path_with_keys)
- convert_to_hash(path_with_keys)
- parts = path_with_keys.split('.', 2)
- match = self[0][parts[0].to_sym]
- if !parts[1] || match.nil?
- match
- else
- match.fetch_deep(parts[1])
+ include Fetch
+ def reshape(shape_array)
+ self.map! do |item|
+ item.reshape(shape_array)
end
end
-
- def reshape(hash_to_be_shaped)
+end
- hash_to_be_shaped.each_pair do |k, v|
- hash_to_be_shaped[k] = fetch_deep(v)
- end
- end
-end