By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,985 Members | 1,574 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,985 IT Pros & Developers. It's quick & easy.

Andreas' practical language comparison

P: n/a
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!
Jul 18 '05 #1
Share this Question
Share on Google+
17 Replies


P: n/a
Andreas Koch wrote:
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.

http://www.kochandreas.com/home/language/lang.htm

and mail me your code snippets (or critics) or post them here.


The Java implementation of Bubble Sort doesn't follow the
specification for the algorithm. It fails to use a "swapped"
flag to determine when to terminate the loop.

-Peter
Jul 18 '05 #2

P: n/a
Andreas Koch wrote:
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.


You might want to put a little more thought into the way you
present the information there. As it stands, it appears likely
to heavily influence people to produce the shortest possible
programs, rather than the most readable or even most typical for
a given language.

Also, even if you leave the line counts in, different languages
(and people) typically count lines differently. For example,
some line-counting programs for C count semicolons and commas
rather than newlines, as these are closer to representative of
the number of *statements*, and that's what matters most of the
time when you are thinking "lines of code".

I sent bubble sort, based on the algorithm, but I'm not going
to bother with more if this is simply a fewest-lines competition,
since the first person to post some Perl will win (although K
appears to be ready to give tight competition in that area,
if the current list is any indication).

-Peter
Jul 18 '05 #3

P: n/a
Peter Hansen wrote:

The Java implementation of Bubble Sort doesn't follow the
specification for the algorithm. It fails to use a "swapped"
flag to determine when to terminate the loop.


Thanks Peter - for a quick solution i corrected the problem
myself (and corrected sort order, too), and added your
python examples.
--
Andreas
He screamed: THIS IS SIG!
Jul 18 '05 #4

P: n/a
Peter Hansen wrote:

You might want to put a little more thought into the way you
present the information there. As it stands, it appears likely
to heavily influence people to produce the shortest possible
programs, rather than the most readable or even most typical for
a given language.
A valid point. We currently have a discussion about this top
on c.l.misc, where i made my first proposals for my site. Feel
free to join in.

I sent bubble sort, based on the algorithm, but I'm not going
to bother with more if this is simply a fewest-lines competition,
since the first person to post some Perl will win (although K
appears to be ready to give tight competition in that area,
if the current list is any indication).


It is on no way a fewest-lines competiton, especially considering
that my favourite language (Delphi) had the most lines in nearly
every test case :-)

I removed the line counts from the overview matrix until we find
a better solution.
--
Andreas
He screamed: THIS IS SIG!
Jul 18 '05 #5

P: n/a

"Andreas Koch" <no****@kochandreas.com> wrote in message news:c6*************@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!
Jul 18 '05 #6

P: n/a
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/lang...UEENS_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" <no****@kochandreas.com> wrote in message news:c6*************@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!
Jul 18 '05 #7

P: n/a
"Andreas Koch" <no****@kochandreas.com> schreef in bericht
news:c6*************@news.t-online.com...
<..snip...>
I sent bubble sort, based on the algorithm, but I'm not going
to bother with more if this is simply a fewest-lines competition,
since the first person to post some Perl will win (although K
appears to be ready to give tight competition in that area,
if the current list is any indication).


It is on no way a fewest-lines competiton, especially considering
that my favourite language (Delphi) had the most lines in nearly
every test case :-)

I removed the line counts from the overview matrix until we find
a better solution.

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. 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?

kind regards, Gerrit
--
www.extra.research.philips.com/natlab/sysarch/

Jul 18 '05 #8

P: n/a
GerritM 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. 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'd reply to this, but as there is already a discussion on
comp.lang.misc (as Andreas said) it would probably be
silly to continue one in parallel here...

-Peter
Jul 18 '05 #9

P: n/a
On Sun, 25 Apr 2004 22:46:48 +0200, "GerritM" <gm*****@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
Jul 18 '05 #10

P: n/a
On Sun, 25 Apr 2004 23:12:40 GMT, Andrea Griffini <ag****@tin.it>
declaimed the following in comp.lang.python:

for example in Icon) how can you implement the same solution
on a language that has no generator concept at all ?
Heh... I think even FORTRAN-IV had notation that would allow you
to create the equivalent of a "generator"... At least, some had an
extension that allowed for calling into the midpoint of a subprogram
body.

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 18 '05 #11

P: n/a
Andreas Koch <no****@kochandreas.com> wrote in message news:<c6*************@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.


# ************ Sort 2 ************

import sys

lines = file(sys.argv[1]).readlines()
lines.sort()
file('sorted.txt', 'w').writelines(lines)

# ************ Type "file" ************

import sys

for line in file(sys.argv[1]):
print line
Jul 18 '05 #12

P: n/a
At my website (SeanMcIlroy.nexuswebs.net) I keep a compressed file of all the
modules I've written so far in the process of teaching myself python. One of
them has to do with prime numbers, primitive roots, etc, which I see forms part
of your language comparison. There are also python implementations of some
standard graph algorithms (Kruskal, Dijkstra) and assorted other mathematical
tidbits, as well as some toy games (tic-tac-toe, nim, mastermind). Help yourself
to whatever you want.
"Andreas Koch" <no****@kochandreas.com> wrote in message
news:c6*************@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!
Jul 18 '05 #13

P: n/a


Andrea Griffini wrote:

Hi Andrea!
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)


I didn't really get the ALGOL one. My first Delphi version
was using bitmaps and marking endangered fields, but the
code was pretty horrible because i found no elegant way
to fill the diagonals but using 2 loops (~ 20 lines of
not very intuitive additional code)

I find the current version to be quite nicely readable,
while the bitmap solution would probably be faster.
comparing
a linecount for that that with a linecount for a totally
different solution is just nonsense.
Depends. The solution often reflects how certain problems
are handled in a language.
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 ?
I have some tests that require to solve an problem, and some
that require to use an certain algorith. I think you can
encounter both situations in real life. Of course, saying
"A needs 20 lines so its better than B which needs 22 lines"
is idiotic.
I'm very new to python, but anyway this is my solution to 8
queen's problem:
Thanks, i allready had lots of submissions by mail this
night. Lots more feedback than expected :-D
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 ?


Good questions. Any ideas for solutions?

--
Andreas
He screamed: THIS IS SIG!
Jul 18 '05 #14

P: n/a
Dan Bishop wrote:
thanks, but all cases but "spread sheet" got
solved in this nights mail rush :-)

--
Andreas
He screamed: THIS IS SIG!
Jul 18 '05 #15

P: n/a
Andrea Griffini <ag****@tin.it> writes:
On Sun, 25 Apr 2004 22:46:48 +0200, "GerritM" <gm*****@worldonline.nl>
wrote: [...] I'm very new to python, but anyway this is my solution to 8
queen's problem:

[...]

I think there's an n-queens solution by Tim Peters somewhere, written
as an example of Python simple generators (which is mostly 'his'
language feature, IIRC).
John
Jul 18 '05 #16

P: n/a
John J. Lee wrote:
I think there's an n-queens solution by Tim Peters somewhere, written
as an example of Python simple generators (which is mostly 'his'
language feature, IIRC).


Google found a page which says:
Two other examples in Lib/test/test_generators.py produce solutions
for the N-Queens problem (placing queens on an chess board so that
no queen threatens another) and the Knight's Tour (a route that takes
a knight to every square of an chessboard without visiting any square
twice).

So I guess the answer would be "use the source".

-Peter
Jul 18 '05 #17

P: n/a
> I think there's an n-queens solution by Tim Peters somewhere, written
as an example of Python simple generators (which is mostly 'his'
language feature, IIRC).


It is in Lib/test/test_generators.py. It also has other examples of fancy
footwork with generators . Anyone interested in the topic should read it.

Terry J. Reedy


Jul 18 '05 #18

This discussion thread is closed

Replies have been disabled for this discussion.