R
Ruby Quiz
The three rules of Ruby Quiz:
1. Please do not post any solutions or spoiler discussion for this quiz until
48 hours have passed from the time on this message.
2. Support Ruby Quiz by submitting ideas as often as you can:
http://www.rubyquiz.com/
3. Enjoy!
Suggestion: A [QUIZ] in the subject of emails about the problem helps everyone
on Ruby Talk follow the discussion. Please reply to the original quiz message,
if you can.
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
by Bob Showalter
(Taken from the puzzle by William Wu at
http://www.ocf.berkeley.edu/~wwu/riddles/cs.shtml)
[Editor's Note: This was also a code golf problem a few months back:
http://codegolf.com/oblongular-number-spirals --JEG2]
Write a Ruby program to print out a "spiral" of numbers that fill a NxN square.
Your program will take a single argument to specify the dimensions of the square
(1 or higher). The number zero represents the center of the spiral, and the
succeeding integers spiral out in a clockwise (or counterclockwise; your choice)
direction from the center until the square is filled.
Your program should write the output line by line, without using an array to
build up the data first.
Here's the output for an 8x8 spiral:
56 57 58 59 60 61 62 63
55 30 31 32 33 34 35 36
54 29 12 13 14 15 16 37
53 28 11 2 3 4 17 38
52 27 10 1 0 5 18 39
51 26 9 8 7 6 19 40
50 25 24 23 22 21 20 41
49 48 47 46 45 44 43 42
1. Please do not post any solutions or spoiler discussion for this quiz until
48 hours have passed from the time on this message.
2. Support Ruby Quiz by submitting ideas as often as you can:
http://www.rubyquiz.com/
3. Enjoy!
Suggestion: A [QUIZ] in the subject of emails about the problem helps everyone
on Ruby Talk follow the discussion. Please reply to the original quiz message,
if you can.
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
by Bob Showalter
(Taken from the puzzle by William Wu at
http://www.ocf.berkeley.edu/~wwu/riddles/cs.shtml)
[Editor's Note: This was also a code golf problem a few months back:
http://codegolf.com/oblongular-number-spirals --JEG2]
Write a Ruby program to print out a "spiral" of numbers that fill a NxN square.
Your program will take a single argument to specify the dimensions of the square
(1 or higher). The number zero represents the center of the spiral, and the
succeeding integers spiral out in a clockwise (or counterclockwise; your choice)
direction from the center until the square is filled.
Your program should write the output line by line, without using an array to
build up the data first.
Here's the output for an 8x8 spiral:
56 57 58 59 60 61 62 63
55 30 31 32 33 34 35 36
54 29 12 13 14 15 16 37
53 28 11 2 3 4 17 38
52 27 10 1 0 5 18 39
51 26 9 8 7 6 19 40
50 25 24 23 22 21 20 41
49 48 47 46 45 44 43 42