On Fri, 2006-06-16 at 06:05 +0900, James Edward Gray II wrote:
> On Jun 15, 2006, at 1:34 PM, Logesh Pillay wrote:
> 
> > I'd appreciate suggestions how to improve it.
> 
> Well, I've tried to Rubify the code a bit.  

If I could just make a couple of small extra suggestions,

> 
> Here's the code.
> 
> class NQueens

     class << self
       def choose(k)
         new.choose(k)
       end
     end

>    def initialize
>      @n = 8
>      @board = [nil] * (@n + 1)
>    end
> 
>    def choose(k)
>      if k > @n
>        puts @board.join
>      else
>        1.upto(@n) do |i|
>          @board[k] = i
>          choose(k + 1) if k == 1 or still_good?(k)
>        end
>      end
>    end
 
     # explicit returns are a pet peeve of mine :)
     def still_good(k)
       !(1...k).any? do |i|
         @board[k] == @board[i] or k-i == (@board[k] - @board[i]).abs
      end
    end
> end
> 
> if __FILE__ == $PROGRAM_NAME
    NQueens.choose(1)
> end
> 
> Hope that helps.
> 
> James Edward Gray II

-- 
Ross Bamford - rosco / roscopeco.REMOVE.co.uk