Andreas' practical language comparison

Georgy no.mail at available.net
Sun Apr 25 14:33:24 EDT 2004


Quick'n'dirty translation of Algol-68 solution:


# N queens task * translation from Algol-68 (C) 2004 Georgy
# Algol-68; http://www.kochandreas.com/home/language/cases/8QUEENS_A68G.HTM
# All: http://www.kochandreas.com/home/language/matrix.htm

def solve( n, all_solutions=False ): # solve n-queens problem

  class Done(Exception): pass

  n1 = n-1 # some optimization :-)

  column = [True]*n      # available columns
  diaga  = [True]*(n+n1) # available tr-bl diags (c+r)
  diagb  = [True]*(n+n1) # available tl-br diags (c-r) + n-1
  position = [0] * n

  def print_row( q ): # print a row
    stars = ['*']*n
    stars[q] = 'Q'
    for s in stars: print s,
    print

  def try_row(r):
    """try_row(r) -- the first r-1 queens have been placed in the top rows,
    #  column/diaga/diagb record which columns and diagonals are still available.
    """
    for c in range(n):
      if r >= n:
        print_row( position[c] )
        if c == n1:
          if all_solutions:
            print '-'*(n+n1)
            return
          else:
            raise Done
      elif column[c] and diaga[c+r] and diagb[c-r + n-1]:
        column[c] = diaga[c+r] = diagb[c-r + n1] = False
        position[r] = c
        try_row( r+1 )
        column[c] = diaga[c+r] = diagb[c-r + n1] = True

  try:
    try_row(0)
  except Done:
    pass

solve( 8 ) # find one solution; for all solutions: solve( 8, True )


"Andreas Koch" <nospam at kochandreas.com> wrote in message news:c6glig$phm$07$4 at news.t-online.com...
| Hi all,
|
| i started a little "practical language comparison" - practical
| in the sense that i compare how actual distributions and versions
| and their libraries (not abstract language specifications) solve small
| test cases like the 8 queens problem.
|
| Currently there are contributions for 17 different languages, and
| none for Phyton (and Lisp. And Forth. And many others ).
| If someone is interested in contributing a few implementations,
| please have a look at:
|
| http://www.kochandreas.com/home/language/lang.htm
|
| and mail me your code snippets (or critics) or post them here.
|
|                         thanks a lot,
| -- 
|                                    Andreas
| He screamed: THIS IS SIG!





More information about the Python-list mailing list