473,396 Members | 1,786 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Andreas' practical language comparison

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
17 2022
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
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
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
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

"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
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
"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
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
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
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
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
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


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
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
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
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
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: A.M. Kuchling | last post by:
python.org has a page of "Python vs. X" language comparisons at <http://www.python.org/doc/Comparisons.html>. They're all pretty outdated, and often unfair because they're written by a person who...
134
by: Joseph Garvin | last post by:
As someone who learned C first, when I came to Python everytime I read about a new feature it was like, "Whoa! I can do that?!" Slicing, dir(), getattr/setattr, the % operator, all of this was very...
24
by: Xah Lee | last post by:
What is Expresiveness in a Computer Language 20050207, Xah Lee. In languages human or computer, there's a notion of expressiveness. English for example, is very expressive in manifestation,...
26
by: nospam | last post by:
Just wondering, What do you think the difference in performance would be between (1.) Compiled C# (2.) Compiled C++ (3.) and Assembly Language And how would the mix be if some if any of...
19
by: Roberto Dias | last post by:
Why is C++ a powerful language? Does it fit for engineering purpose? I mean, for doing matrices manipulation, numerical computing, solving equations, and, eventually, for file streaming. Should I...
93
by: Matt | last post by:
Hi folks. Can you help with some questions? I gather that some types supported by g++ are nonstandard but have been proposed as standards. Are the long long and unsigned long long types still...
134
by: evolnet.regular | last post by:
I've been utilising C for lots of small and a few medium-sized personal projects over the course of the past decade, and I've realised lately just how little progress it's made since then. I've...
28
by: robert | last post by:
In very rare cases a program crashes (hard to reproduce) : * several threads work on an object tree with dict's etc. in it. Items are added, deleted, iteration over .keys() ... ). The threads are...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.