Andreas' practical language comparison

Andrea Griffini agriff at tin.it
Sun Apr 25 19:12:40 EDT 2004


On Sun, 25 Apr 2004 22:46:48 +0200, "GerritM" <gmuller at worldonline.nl>
wrote:

>I really would like to see the linecount. I do belive that it is one of the
>indicators of the power of the language and its batteries.

Then at least you should be talking of the same problem and
of the same solution to the problem. For example the Delphi
solution for the 8 queens problem is completely different
from the ALGOL one (and I must say IMO the Delphi solution
is quite terrible from an algorithm point of view), comparing
a linecount for that that with a linecount for a totally
different solution is just nonsense.

However this create problems for very different approaches;
implementing a certain algorithm in prolog and comparing it
with the same algorithm in C has any meaning ? If say one
solution uses generators (may be heavily and with backtracking,
for example in Icon) how can you implement the same solution
on a language that has no generator concept at all ?
Even if you manage to do that, what's the point in finding
that you needed a lot of lines of code to do it ?

>Somehow it would be nice to have a figure for "readability", but I
>don't have a clue how to generate such a figure in an automatic way.
>Maybe you need some panel of experts that gives a readability figure?

I'm very new to python, but anyway this is my solution to 8
queen's problem:

------------------------ PYTHON --------------------------
import sets

def solve(base,        # starting position, as a list of cols
          free_cols,   # list of columns not yet taken
          free_diag1,  # list of diagonals not yet taken
          free_diag2): # list of counter diagonals not yet taken
  if free_cols:
    row = len(base)
    for i,col in enumerate(free_cols):
      d1 = row + col
      d2 = row - col
      if d1 in free_diag1 and d2 in free_diag2:
        solve(base+[col],
              free_cols[0:i]+free_cols[i+1:],
              free_diag1.difference([d1]),
              free_diag2.difference([d2]))
  else:
    print base

n = 8
solve([],
      range(1,n+1),
      sets.Set(range(1+1,n+n+1)),
      sets.Set(range(1-n,n-1+1)))
-----------------------------------------------------------

and this is a solution to the same problem I wrote in C
some time ago while having fun in trying to use as few
characters as possible...

----------------------- C -------------------------------
char n[39],q=7;void s(){char*x=n+7,*e,*d;while((*x+*(
e=x+9+q)+*(d=x+31-q)?0:(*x=*e=*d='1'+q,q--?s(),1:puts
(n),q++,*x=*e=*d=0))|x--!=n);}main(){s();return 0;}
---------------------------------------------------------

Does this mean that Python is less expressive ? that
C is less clear ? Or just means one program has been
written trying to be expressive and the other trying
to be compact ?

If I run that Delphi solution for the 8 queens problem
should I conclude that Python is faster than Delphi ?


Andrea



More information about the Python-list mailing list