The three rules of Ruby Q.:
-
Please do not post any solutions or spoiler discussion for this quiz
until
48 hours have passed from the time on this message. -
Support Ruby Q. by submitting ideas as often as you can:
- Enjoy!
Suggestion: A [QUIZ] in the subject of emails about the problem helps
everyone
on Ruby T. follow the discussion.
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
by Matthew M.
You have a square sheet of grid paper, 16 cells across by 16 down. A
number is
written in each cell, starting at 1 in the upper-left corner, going
across each
line then down the paper, ending with 256 in the lower-right corner.
Your task is write a Ruby function to fold the paper in half repeatedly
(along
the horizontal or vertical middle), until you are left with a single
cell, 256
layers thick, and report back on the order of those layers, top to
bottom.
The input to the function is a string containing the characters “T” (the
top
edge folded down to the bottom edge), “B” (bottom up to top), “R” (right
to
left) and “L” (left to right). Not every combination of those will be
valid, so
make sure you verify your input. Output will be an Array of Integers
from 1 to
256, arranged in the top-to-bottom layer order after all folds are done.
To help clarify, here’s a 2x2 example.
+-+-+
|1|2|
+-+-+
|3|4|
+-+-+
fold("RB") => [3, 4, 2, 1]
fold("TL") => [3, 1, 2, 4]
fold("LR") => raise exception for invalid input
Extra credit: Make your fold function take an additional input
parameter(s),
the dimensions of the paper; dimensions should be power-of-2 in order to
fold
down to one cell.
Extra extra credit: Write a method check_fold() that takes the
resulting Array
of Integers from the quiz and returns the transformation String argument
(i.e.
fold(check_fold(final_order)) == final_order).