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

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

Към профила на Лазар Дилов

Резултати

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

Код

def integer?(x)
x.to_i.to_s == x
end
def parse(current, x)
if integer?(x)
x = x.to_i
elsif current[x] == nil
x = x.to_sym
end
x
end
def fetch_deep_iter(iter, addr_array)
return if iter.nil?
return iter unless addr_array.any?
x = addr_array[0]
current = iter[parse(iter, x)]
addr_array.shift
fetch_deep_iter(current, addr_array)
end
class Hash
def fetch_deep(address)
address_arr = address.split(".")
fetch_deep_iter(self, address_arr)
end
end
class Hash
def reshape(shape)
current = self
shape.each do |key, value|
if value.is_a? Hash
reshape(value)
else
shape[key] = current.fetch_deep(value)
end
end
end
end
class Array
def reshape(shape)
arr = []
current = self
temp = shape.clone
current.map do |x|
arr.push(x.reshape(temp))
temp = shape.clone
end
arr
end
end

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

.......F.......

Failures:

  1) 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-1klfxoz/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)>'

Finished in 0.00808 seconds
15 examples, 1 failure

Failed examples:

rspec /tmp/d20161024-13689-1klfxoz/spec.rb:108 # Task 2 Hash#fetch_deep can fetch integer-like keys from hashes

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

Лазар обнови решението на 18.10.2016 21:59 (преди над 7 години)

+def integer?(x)
+ x.to_i.to_s == x
+end
+
+def parse(current, x)
+ if integer?(x)
+ x = x.to_i
+ elsif current[x] == nil
+ x = x.to_sym
+ end
+ x
+end
+
+class Hash
+ def fetch_deep(address)
+ address_arr = address.split(".")
+ current = self
+ address_arr.each do |x|
+ current = current[parse(self, x)]
+ end
+ current
+ end
+end
+
+class Hash
+ def reshape(shape)
+ current = self
+ shape.each do |key, value|
+ if value.is_a? Hash
+ reshape(value)
+ else
+ shape[key] = current.fetch_deep(value)
+ end
+ end
+ end
+end
+
+class Array
+ def reshape(shape)
+ arr = []
+ current = self
+ temp = shape.clone
+ current.map do |x|
+ arr.push(x.reshape(temp))
+ temp = shape.clone
+ end
+ arr
+ end
+end

Лазар обнови решението на 19.10.2016 15:58 (преди над 7 години)

def integer?(x)
x.to_i.to_s == x
end
def parse(current, x)
if integer?(x)
x = x.to_i
elsif current[x] == nil
x = x.to_sym
end
x
end
+def fetch_deep_iter(iter, addr_array)
+ return iter unless addr_array.any?
+ x = addr_array[0]
+ current = iter[parse(iter, x)]
+ addr_array.shift
+ fetch_deep_iter(current, addr_array)
+end
+
class Hash
def fetch_deep(address)
address_arr = address.split(".")
- current = self
- address_arr.each do |x|
- current = current[parse(self, x)]
- end
- current
+ fetch_deep_iter(self, address_arr)
end
end
class Hash
def reshape(shape)
current = self
shape.each do |key, value|
if value.is_a? Hash
reshape(value)
else
shape[key] = current.fetch_deep(value)
end
end
end
end
class Array
def reshape(shape)
arr = []
current = self
temp = shape.clone
current.map do |x|
arr.push(x.reshape(temp))
temp = shape.clone
end
arr
end
-end
+end

Лазар обнови решението на 23.10.2016 14:49 (преди над 7 години)

def integer?(x)
x.to_i.to_s == x
end
def parse(current, x)
if integer?(x)
x = x.to_i
elsif current[x] == nil
x = x.to_sym
end
x
end
def fetch_deep_iter(iter, addr_array)
+ return if iter.nil?
return iter unless addr_array.any?
x = addr_array[0]
current = iter[parse(iter, x)]
addr_array.shift
fetch_deep_iter(current, addr_array)
end
class Hash
def fetch_deep(address)
address_arr = address.split(".")
fetch_deep_iter(self, address_arr)
end
end
class Hash
def reshape(shape)
current = self
shape.each do |key, value|
if value.is_a? Hash
reshape(value)
else
shape[key] = current.fetch_deep(value)
end
end
end
end
class Array
def reshape(shape)
arr = []
current = self
temp = shape.clone
current.map do |x|
arr.push(x.reshape(temp))
temp = shape.clone
end
arr
end
end