Skip to content

Instantly share code, notes, and snippets.

@lbvf50mobile
Last active July 29, 2021 11:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lbvf50mobile/94a5522bb54c6545555c7058a78b63ed to your computer and use it in GitHub Desktop.
Save lbvf50mobile/94a5522bb54c6545555c7058a78b63ed to your computer and use it in GitHub Desktop.
Question by @Nakilon from 29.07.2021 Thursday.
# Question by @Nakilon from 29.07.2021 Thursday.
puts "Make an array four times bigger!"
def make_four(array)
array.map!{|x| x + x}
(0...array.size).each do |i|
array.push(array[i])
end
end
a = [[1]]
make_four(a)
p a
make_four(a)
p a
make_four(a)
p a
puts "Convert an array from a 4 dimensional to a 2 dimensional!"
def four2two(x)
x.each_with_object(Array.new){ |row,obj|
obj.push(row.map(&:first).flatten)
obj.push(row.map(&:last).flatten)
}
end
four = [
[
[[1,2],[3,4]], # element
[[5,6],[7,8]] # element
] # row
]
two = four2two(four)
p four
p two
puts "One more example of convertion."
input = [
[[[1,2],[3,4]],[[5,6],[7,8]]],
[[[9,10],[11,12]],[[13,14],[15,16]]]
]
output = four2two(input)
p input
p output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment