From: James Edward Gray II Date: 2007-01-19T22:16:49+09:00 Subject: Fwd: Please Forward: Ruby Quiz Submission --Apple-Mail-1--220638515 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Begin forwarded message: > From: "Nicolas Modrzyk" > Date: January 19, 2007 1:12:26 AM CST > To: submission@rubyquiz.com > Subject: Please Forward: Ruby Quiz Submission > > Hi there, > > This was my first quiz, and it took me more than an hour to solve > it (><) > But it was really fun so decided to send it over. > > This is a submission for the number spiral, quiz 109 > > Can't wait for the next one ... > > Niko, --Apple-Mail-1--220638515 Content-Transfer-Encoding: 7bit Content-Type: application/octet-stream; x-unix-mode=0666; name=spiral.rb Content-Disposition: attachment; filename=spiral.rb ## Nicolas Modrzyk: hellonico at gmail.com # January 19, 2007 # def spiral(n) # handle case for 0 puts 0 if n==0 1.upto(n) { |row| spiral_line(n, row) } end def spiral_line(n, row) line = Array.new 1.upto(n) { |col| line[col]="\t"+spiral_row_col(n, row, col).to_s } puts line.to_s end def spiral_row_col(n, row, col) return 0 if(n==0) if(n%2==0 and row==1) return n*(n-1) + (col-1) end if(n%2==0 and col==1) return n*(n-1) - (row-1) end if(n%2==1 and row==n) return (n*n) - (col) end if(n%2==1 and col==n) return n*(n-1) - (n-row) end return spiral_row_col(n-1, row, col) if n%2==1 return spiral_row_col(n-1, row-1, col-1) if n%2==0 end spiral(8) --Apple-Mail-1--220638515 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; format=flowed --Apple-Mail-1--220638515--