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

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

Към профила на Иво Яков

Резултати

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

Код

# check value type before going deeper inside it
def type_check(value, rest)
value.go_deeper(*rest) if (value.is_a? Hash) || (value.is_a? Array)
end
class Hash
# gets the value with the current key and goes deeper inside it
def go_deeper(cur_key, *rest)
value = key?(cur_key) ? self[cur_key] : self[cur_key.to_sym]
rest.empty? ? value : value && type_check(value, rest)
end
def fetch_deep(keys)
go_deeper(*keys.split('.'))
end
def reshape(shape)
copy = shape.clone
copy.each do |key, val|
copy[key] =
(val.is_a? Hash) ? reshape(copy[key]) : fetch_deep(val)
end
copy
end
end
class Array
# Same as for go_deeper for hash, but with a few changes for arrays
def go_deeper(index, *rest)
index = index.to_i
value = self[index] if size > index
rest.empty? ? value : value && type_check(value, rest)
end
def fetch_deep(keys)
go_deeper(*keys.split('.'))
end
def reshape(shape)
copy = self.clone
self.each_with_index do |val, index|
copy[index] = val.reshape(shape)
end
copy
end
end

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

...............

Finished in 0.02322 seconds
15 examples, 0 failures

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

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

+# check value type before going deeper inside it
+def type_check(value, rest)
+ value.go_deeper(*rest) if (value.is_a? Hash) || (value.is_a? Array)
+end
+
+Hash.class_eval do
+ # gets the value with the current key and goes deeper inside it
+ def go_deeper(cur_key, *rest)
+ value = key?(cur_key) ? self[cur_key] : self[cur_key.to_sym]
+ rest.empty? ? value : value && type_check(value, rest)
+ end
+
+ def fetch_deep(keys)
+ go_deeper(*keys.split('.'))
+ end
+end
+
+Array.class_eval do
+ # same as for hash, but with mods for arrays
+ def go_deeper(index, *rest)
+ index = index.to_i
+ value = self[index] if size > index
+ rest.empty? ? value : value && type_check(value, rest)
+ end
+end

Това е само първата част от задачат, качвам я за коментар, не съм много сигурен дали методите трябва да ги сложа в class_eval, но взе че стана така и не съм ровил за друг начин

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

# check value type before going deeper inside it
def type_check(value, rest)
value.go_deeper(*rest) if (value.is_a? Hash) || (value.is_a? Array)
end
-
-Hash.class_eval do
+# Adding new methods to the Hash class
+class Hash
# gets the value with the current key and goes deeper inside it
def go_deeper(cur_key, *rest)
value = key?(cur_key) ? self[cur_key] : self[cur_key.to_sym]
rest.empty? ? value : value && type_check(value, rest)
end
def fetch_deep(keys)
go_deeper(*keys.split('.'))
end
+
+ def reshape(shape)
+ copy = shape.clone
+ copy.each do |key, val|
+ copy[key] =
+ val.is_a? Hash ? reshape(copy[key]) : fetch_deep(val)
+ end
+ copy
+ end
end
-Array.class_eval do
+# Adding new methods to the array class
+class Array
# same as for hash, but with mods for arrays
def go_deeper(index, *rest)
index = index.to_i
value = self[index] if size > index
rest.empty? ? value : value && type_check(value, rest)
+ end
+
+ def fetch_deep(keys)
+ go_deeper(*keys.split('.'))
+ end
+
+ def reshape(shape)
+ copy = clone
+ each_with_index do |val, index|
+ copy[index] = val.reshape(shape)
+ end
+ copy
end
end

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

+
# check value type before going deeper inside it
def type_check(value, rest)
value.go_deeper(*rest) if (value.is_a? Hash) || (value.is_a? Array)
end
-# Adding new methods to the Hash class
+
class Hash
# gets the value with the current key and goes deeper inside it
def go_deeper(cur_key, *rest)
value = key?(cur_key) ? self[cur_key] : self[cur_key.to_sym]
rest.empty? ? value : value && type_check(value, rest)
end
def fetch_deep(keys)
go_deeper(*keys.split('.'))
end
-
+
def reshape(shape)
copy = shape.clone
copy.each do |key, val|
copy[key] =
- val.is_a? Hash ? reshape(copy[key]) : fetch_deep(val)
+ (val.is_a? Hash) ? reshape(copy[key]) : fetch_deep(val)
end
copy
end
end
-# Adding new methods to the array class
class Array
- # same as for hash, but with mods for arrays
+ # Same as for go_deeper for hash, but with a few changes for arrays
def go_deeper(index, *rest)
index = index.to_i
value = self[index] if size > index
rest.empty? ? value : value && type_check(value, rest)
end
def fetch_deep(keys)
go_deeper(*keys.split('.'))
end
def reshape(shape)
- copy = clone
- each_with_index do |val, index|
+ copy = self.clone
+ self.each_with_index do |val, index|
copy[index] = val.reshape(shape)
end
copy
end
end