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

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

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

Резултати

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

Код

def when_is_array(hash, keys_arr, limiter, position)
if position < limiter - 1
position += 1
fun(hash[keys_arr[position - 1].to_i], keys_arr, limiter, position)
else hash[keys_arr[position].to_i]
end
end
def when_is_not_array(hash, keys_arr, limiter, position)
if position < limiter - 1
position += 1
fun(hash[keys_arr[position - 1].to_s], keys_arr, limiter, position)
else hash[keys_arr[position].to_s]
end
end
def when_is_indeed_number(hash, keys_arr, limiter, position)
if hash[keys_arr[position].to_i].class != NilClass
when_is_array(hash, keys_arr, limiter, position)
else
when_is_not_array(hash, keys_arr, limiter, position)
end
end
def when_is_symbol(hash, keys_arr, limiter, position)
if hash[keys_arr[position].to_sym].class != NilClass
if position < limiter - 1 then position += 1
fun(hash[keys_arr[position - 1].to_sym], keys_arr, limiter, position)
else hash[keys_arr[position].to_sym]
end
else nil
end
end
def when_is_string(hash, keys_arr, limiter, position)
hash = Hash[hash.map { |( k, v)| [k.to_sym, v] }]
when_is_symbol(hash, keys_arr, limiter, position)
end
def fun(hash, keys_arr, limiter, position)
if keys_arr[position].integer?
when_is_indeed_number(hash, keys_arr, limiter, position)
else
when_is_string(hash, keys_arr, limiter, position)
end
end
class Hash
def fetch_deep(string)
keys = string.split('.')
limiter = keys.count
begin fun(self, keys, limiter, 0)
end
end
end
class String
def integer?
self.to_i.to_s == self
end
end
# #########################################
def value_modifier(shape, order)
shape.map do |k, v|
if v.is_a? Hash then value_modifier(v, order)
elsif v.is_a? Array then v.map { |index| value_modifier(index, order) }
else shape[k] = order.fetch_deep(v.to_s)
end
end
shape
end
class Hash
def reshape(shape)
value_modifier(shape, self)
end
end
# #######################################
class Array
def reshape(shape)
self.map do |index|
shape2 = shape.clone
value_modifier(shape2, index)
end
end
end

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

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

Failures:

  1) Task 2 Hash#fetch_deep does not modify the input hash
     Failure/Error: input.fetch_deep('menu.order')
     NoMethodError:
       undefined method `to_sym' for 3:Fixnum
     # /tmp/d20161024-13689-jzz5mf/solution.rb:36:in `block in when_is_string'
     # /tmp/d20161024-13689-jzz5mf/solution.rb:36:in `each'
     # /tmp/d20161024-13689-jzz5mf/solution.rb:36:in `map'
     # /tmp/d20161024-13689-jzz5mf/solution.rb:36:in `when_is_string'
     # /tmp/d20161024-13689-jzz5mf/solution.rb:44:in `fun'
     # /tmp/d20161024-13689-jzz5mf/solution.rb:28:in `when_is_symbol'
     # /tmp/d20161024-13689-jzz5mf/solution.rb:37:in `when_is_string'
     # /tmp/d20161024-13689-jzz5mf/solution.rb:44:in `fun'
     # /tmp/d20161024-13689-jzz5mf/solution.rb:52:in `fetch_deep'
     # /tmp/d20161024-13689-jzz5mf/spec.rb:102: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.2')).to eq :b
       
       expected: :b
            got: nil
       
       (compared using ==)
     # /tmp/d20161024-13689-jzz5mf/spec.rb:112: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 does not modify the input hash
     Failure/Error: input.reshape(shape)
     NoMethodError:
       undefined method `to_sym' for 3:Fixnum
     # /tmp/d20161024-13689-jzz5mf/solution.rb:36:in `block in when_is_string'
     # /tmp/d20161024-13689-jzz5mf/solution.rb:36:in `each'
     # /tmp/d20161024-13689-jzz5mf/solution.rb:36:in `map'
     # /tmp/d20161024-13689-jzz5mf/solution.rb:36:in `when_is_string'
     # /tmp/d20161024-13689-jzz5mf/solution.rb:44:in `fun'
     # /tmp/d20161024-13689-jzz5mf/solution.rb:28:in `when_is_symbol'
     # /tmp/d20161024-13689-jzz5mf/solution.rb:37:in `when_is_string'
     # /tmp/d20161024-13689-jzz5mf/solution.rb:44:in `fun'
     # /tmp/d20161024-13689-jzz5mf/solution.rb:52:in `fetch_deep'
     # /tmp/d20161024-13689-jzz5mf/solution.rb:69:in `block in value_modifier'
     # /tmp/d20161024-13689-jzz5mf/solution.rb:66:in `each'
     # /tmp/d20161024-13689-jzz5mf/solution.rb:66:in `map'
     # /tmp/d20161024-13689-jzz5mf/solution.rb:66:in `value_modifier'
     # /tmp/d20161024-13689-jzz5mf/solution.rb:77:in `reshape'
     # /tmp/d20161024-13689-jzz5mf/spec.rb:194: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.00848 seconds
15 examples, 3 failures

Failed examples:

rspec /tmp/d20161024-13689-jzz5mf/spec.rb:99 # Task 2 Hash#fetch_deep does not modify the input hash
rspec /tmp/d20161024-13689-jzz5mf/spec.rb:108 # Task 2 Hash#fetch_deep can fetch integer-like keys from hashes
rspec /tmp/d20161024-13689-jzz5mf/spec.rb:180 # Task 2 Hash#reshape does not modify the input hash

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

Христо обнови решението на 20.10.2016 21:00 (преди около 8 години)

+class Hash
+ def fetch_deep(string)
+ keys = string.split('.')
+ limiter = keys.count
+ fun(self, keys, limiter, 0)
+ end
+end
+
+class String
+ def integer?
+ self.to_i.to_s == self
+ end
+end
+
+def when_is_not_number(hash, keys_arr, limiter, position)
+ if hash[keys_arr[position].to_sym].class != NilClass
+ if position < limiter - 1
+ position += 1
+ fun(hash[keys_arr[position - 1].to_sym], keys_arr, limiter, position)
+ else hash[keys_arr[position].to_sym]
+ end
+ else nil
+ end
+end
+
+def when_is_indeed_number(hash, keys_arr, limiter, position)
+ if hash[keys_arr[position].to_i].class != NilClass
+ if position < limiter - 1
+ position += 1
+ fun(hash[keys_arr[position - 1].to_i], keys_arr, limiter, position)
+ else hash[keys_arr[position].to_i]
+ end
+ else nil
+ end
+end
+
+def fun(hash, keys_arr, limiter, position)
+ if keys_arr[position].integer?
+ hash = Hash[hash.map { |( k, v)| [k.to_sym, v] }]
+ when_is_indeed_number(hash, keys_arr, limiter, position)
+ else
+ when_is_not_number(hash, keys_arr, limiter, position)
+ end
+end
+# puts order.fetch_deep('dessert.itso.itso3.c')

Христо обнови решението на 22.10.2016 18:57 (преди около 8 години)

class Hash
def fetch_deep(string)
keys = string.split('.')
limiter = keys.count
fun(self, keys, limiter, 0)
end
end
class String
def integer?
self.to_i.to_s == self
end
end
def when_is_not_number(hash, keys_arr, limiter, position)
+ hash = Hash[hash.map { |( k, v)| [k.to_sym, v] }]
if hash[keys_arr[position].to_sym].class != NilClass
- if position < limiter - 1
- position += 1
+ if position < limiter - 1 then position += 1
fun(hash[keys_arr[position - 1].to_sym], keys_arr, limiter, position)
else hash[keys_arr[position].to_sym]
end
else nil
end
end
def when_is_indeed_number(hash, keys_arr, limiter, position)
if hash[keys_arr[position].to_i].class != NilClass
if position < limiter - 1
position += 1
fun(hash[keys_arr[position - 1].to_i], keys_arr, limiter, position)
else hash[keys_arr[position].to_i]
end
else nil
end
end
def fun(hash, keys_arr, limiter, position)
if keys_arr[position].integer?
- hash = Hash[hash.map { |( k, v)| [k.to_sym, v] }]
when_is_indeed_number(hash, keys_arr, limiter, position)
else
when_is_not_number(hash, keys_arr, limiter, position)
end
end
-# puts order.fetch_deep('dessert.itso.itso3.c')
+
+class Hash
+ def reshape(shape)
+ value_modifier(shape, self)
+ end
+end
+
+def value_modifier(shape, order)
+ shape.map do |k, v|
+ if v.is_a? Hash then value_modifier(v, order)
+ elsif v.is_a? Array then v.map { |index| value_modifier(index, order) }
+ else shape[k] = order.fetch_deep(v.to_s)
+ end
+ end
+ shape
+end
+
+class Array
+ def reshape(shape)
+ self.map do |index|
+ shape2 = shape.clone
+ value_modifier(shape2, index)
+ end
+ end
+end

Христо обнови решението на 22.10.2016 19:02 (преди около 8 години)

class Hash
def fetch_deep(string)
keys = string.split('.')
limiter = keys.count
fun(self, keys, limiter, 0)
end
end
class String
def integer?
self.to_i.to_s == self
end
end
def when_is_not_number(hash, keys_arr, limiter, position)
hash = Hash[hash.map { |( k, v)| [k.to_sym, v] }]
if hash[keys_arr[position].to_sym].class != NilClass
if position < limiter - 1 then position += 1
fun(hash[keys_arr[position - 1].to_sym], keys_arr, limiter, position)
else hash[keys_arr[position].to_sym]
end
else nil
end
end
def when_is_indeed_number(hash, keys_arr, limiter, position)
if hash[keys_arr[position].to_i].class != NilClass
if position < limiter - 1
position += 1
fun(hash[keys_arr[position - 1].to_i], keys_arr, limiter, position)
else hash[keys_arr[position].to_i]
end
else nil
end
end
def fun(hash, keys_arr, limiter, position)
if keys_arr[position].integer?
when_is_indeed_number(hash, keys_arr, limiter, position)
else
when_is_not_number(hash, keys_arr, limiter, position)
end
end
class Hash
def reshape(shape)
value_modifier(shape, self)
end
end
def value_modifier(shape, order)
shape.map do |k, v|
if v.is_a? Hash then value_modifier(v, order)
elsif v.is_a? Array then v.map { |index| value_modifier(index, order) }
else shape[k] = order.fetch_deep(v.to_s)
end
end
shape
end
class Array
def reshape(shape)
self.map do |index|
shape2 = shape.clone
value_modifier(shape2, index)
end
end
end
+

Причината за грозотията на 18 ред е, че стиловата проверка на сайта определя съответната функция като 9 реда дълга, но 2 от тези редове са всъщност редове само и единствено с end, което според http://fmi.ruby.bg/tasks/guide не би трябвало да се брои за такъв.

Ако оплакването ми е невалидно моля да бъда информиран (:

Да, rubocop брои и end за ред. Това в гайда е останало от миналата година, когато използвахме друг tool. Ръководството е в грешка тук.

Нашето решение има по максимум 4 реда в метод - помисли как можеш да реорганизираш нещата, така че да не се налагат толкова много случаи. Например, ако сложиш метод fetch_deep в Array можеш да си спестиш проверките дали нещото е Hash или Array и дали е число или стринг. :)

Христо обнови решението на 24.10.2016 00:24 (преди около 8 години)

-class Hash
- def fetch_deep(string)
- keys = string.split('.')
- limiter = keys.count
- fun(self, keys, limiter, 0)
+def when_is_array(hash, keys_arr, limiter, position)
+ if position < limiter - 1
+ position += 1
+ fun(hash[keys_arr[position - 1].to_i], keys_arr, limiter, position)
+ else hash[keys_arr[position].to_i]
end
end
-class String
- def integer?
- self.to_i.to_s == self
+def when_is_not_array(hash, keys_arr, limiter, position)
+ if position < limiter - 1
+ position += 1
+ fun(hash[keys_arr[position - 1].to_s], keys_arr, limiter, position)
+ else hash[keys_arr[position].to_s]
end
end
-def when_is_not_number(hash, keys_arr, limiter, position)
- hash = Hash[hash.map { |( k, v)| [k.to_sym, v] }]
+def when_is_indeed_number(hash, keys_arr, limiter, position)
+ if hash[keys_arr[position].to_i].class != NilClass
+ when_is_array(hash, keys_arr, limiter, position)
+ else
+ when_is_not_array(hash, keys_arr, limiter, position)
+ end
+end
+
+def when_is_symbol(hash, keys_arr, limiter, position)
if hash[keys_arr[position].to_sym].class != NilClass
if position < limiter - 1 then position += 1
fun(hash[keys_arr[position - 1].to_sym], keys_arr, limiter, position)
else hash[keys_arr[position].to_sym]
end
else nil
end
end
-def when_is_indeed_number(hash, keys_arr, limiter, position)
- if hash[keys_arr[position].to_i].class != NilClass
- if position < limiter - 1
- position += 1
- fun(hash[keys_arr[position - 1].to_i], keys_arr, limiter, position)
- else hash[keys_arr[position].to_i]
- end
- else nil
- end
+def when_is_string(hash, keys_arr, limiter, position)
+ hash = Hash[hash.map { |( k, v)| [k.to_sym, v] }]
+ when_is_symbol(hash, keys_arr, limiter, position)
end
+def when_is_not_number(hash, keys_arr, limiter, position)
+ when_is_string(hash, keys_arr, limiter, position)
+end
+
def fun(hash, keys_arr, limiter, position)
if keys_arr[position].integer?
when_is_indeed_number(hash, keys_arr, limiter, position)
else
when_is_not_number(hash, keys_arr, limiter, position)
end
end
class Hash
- def reshape(shape)
- value_modifier(shape, self)
+ def fetch_deep(string)
+ keys = string.split('.')
+ limiter = keys.count
+ fun(self, keys, limiter, 0)
end
end
+class String
+ def integer?
+ self.to_i.to_s == self
+ end
+end
+# #########################################
def value_modifier(shape, order)
shape.map do |k, v|
if v.is_a? Hash then value_modifier(v, order)
elsif v.is_a? Array then v.map { |index| value_modifier(index, order) }
else shape[k] = order.fetch_deep(v.to_s)
end
end
shape
end
+class Hash
+ def reshape(shape)
+ value_modifier(shape, self)
+ end
+end
+# #######################################
class Array
def reshape(shape)
self.map do |index|
shape2 = shape.clone
value_modifier(shape2, index)
end
end
-end
-
+end

Христо обнови решението на 24.10.2016 10:59 (преди около 8 години)

def when_is_array(hash, keys_arr, limiter, position)
if position < limiter - 1
position += 1
fun(hash[keys_arr[position - 1].to_i], keys_arr, limiter, position)
else hash[keys_arr[position].to_i]
end
end
def when_is_not_array(hash, keys_arr, limiter, position)
if position < limiter - 1
position += 1
fun(hash[keys_arr[position - 1].to_s], keys_arr, limiter, position)
else hash[keys_arr[position].to_s]
end
end
def when_is_indeed_number(hash, keys_arr, limiter, position)
if hash[keys_arr[position].to_i].class != NilClass
when_is_array(hash, keys_arr, limiter, position)
else
when_is_not_array(hash, keys_arr, limiter, position)
end
end
def when_is_symbol(hash, keys_arr, limiter, position)
if hash[keys_arr[position].to_sym].class != NilClass
if position < limiter - 1 then position += 1
fun(hash[keys_arr[position - 1].to_sym], keys_arr, limiter, position)
else hash[keys_arr[position].to_sym]
end
else nil
end
end
def when_is_string(hash, keys_arr, limiter, position)
hash = Hash[hash.map { |( k, v)| [k.to_sym, v] }]
when_is_symbol(hash, keys_arr, limiter, position)
end
-def when_is_not_number(hash, keys_arr, limiter, position)
- when_is_string(hash, keys_arr, limiter, position)
-end
-
def fun(hash, keys_arr, limiter, position)
if keys_arr[position].integer?
when_is_indeed_number(hash, keys_arr, limiter, position)
else
- when_is_not_number(hash, keys_arr, limiter, position)
+ when_is_string(hash, keys_arr, limiter, position)
end
end
class Hash
def fetch_deep(string)
keys = string.split('.')
limiter = keys.count
- fun(self, keys, limiter, 0)
+ begin fun(self, keys, limiter, 0)
+ end
end
end
class String
def integer?
self.to_i.to_s == self
end
end
+
# #########################################
+
def value_modifier(shape, order)
shape.map do |k, v|
if v.is_a? Hash then value_modifier(v, order)
elsif v.is_a? Array then v.map { |index| value_modifier(index, order) }
else shape[k] = order.fetch_deep(v.to_s)
end
end
shape
end
class Hash
def reshape(shape)
value_modifier(shape, self)
end
end
# #######################################
class Array
def reshape(shape)
self.map do |index|
shape2 = shape.clone
value_modifier(shape2, index)
end
end
-end
+end