Andreas' practical language comparison

Discussion in 'Python' started by Andreas Koch, Apr 25, 2004.

  1. Andreas Koch

    Andreas Koch Guest

    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!
    Andreas Koch, Apr 25, 2004
    #1
    1. Advertising

  2. Andreas Koch

    Peter Hansen Guest

    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
    Peter Hansen, Apr 25, 2004
    #2
    1. Advertising

  3. Andreas Koch

    Peter Hansen Guest

    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
    Peter Hansen, Apr 25, 2004
    #3
  4. Andreas Koch

    Andreas Koch Guest

    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!
    Andreas Koch, Apr 25, 2004
    #4
  5. Andreas Koch

    Andreas Koch Guest

    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!
    Andreas Koch, Apr 25, 2004
    #5
  6. Andreas Koch

    Georgy Guest

    "Andreas Koch" <> wrote in message news:c6glig$phm$07$-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!
    Georgy, Apr 25, 2004
    #6
  7. Andreas Koch

    Georgy Guest

    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" <> wrote in message news:c6glig$phm$07$-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!
    Georgy, Apr 25, 2004
    #7
  8. Andreas Koch

    GerritM Guest

    "Andreas Koch" <> schreef in bericht
    news:c6gu81$pk6$05$-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/
    GerritM, Apr 25, 2004
    #8
  9. Andreas Koch

    Peter Hansen Guest

    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
    Peter Hansen, Apr 25, 2004
    #9
  10. On Sun, 25 Apr 2004 22:46:48 +0200, "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.


    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
    Andrea Griffini, Apr 26, 2004
    #10
  11. On Sun, 25 Apr 2004 23:12:40 GMT, Andrea Griffini <>
    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.

    --
    > ============================================================== <
    > | Wulfraed Dennis Lee Bieber KD6MOG <
    > | Bestiaria Support Staff <
    > ============================================================== <
    > Home Page: <http://www.dm.net/~wulfraed/> <
    > Overflow Page: <http://wlfraed.home.netcom.com/> <
    Dennis Lee Bieber, Apr 26, 2004
    #11
  12. Andreas Koch

    Dan Bishop Guest

    Andreas Koch <> wrote in message news:<c6glig$phm$07$-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
    Dan Bishop, Apr 26, 2004
    #12
  13. 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" <> wrote in message
    news:c6glig$phm$07$-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!
    Elaine Jackson, Apr 26, 2004
    #13
  14. Andreas Koch

    Andreas Koch Guest

    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!
    Andreas Koch, Apr 26, 2004
    #14
  15. Andreas Koch

    Andreas Koch Guest

    Dan Bishop wrote:


    thanks, but all cases but "spread sheet" got
    solved in this nights mail rush :)

    --
    Andreas
    He screamed: THIS IS SIG!
    Andreas Koch, Apr 26, 2004
    #15
  16. Andreas Koch

    John J. Lee Guest

    Andrea Griffini <> writes:

    > On Sun, 25 Apr 2004 22:46:48 +0200, "GerritM" <>
    > 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
    John J. Lee, Apr 29, 2004
    #16
  17. Andreas Koch

    Terry Reedy Guest

    > 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
    Terry Reedy, Apr 30, 2004
    #17
  18. Andreas Koch

    Peter Hansen Guest

    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
    Peter Hansen, Apr 30, 2004
    #18
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul McGuire
    Replies:
    2
    Views:
    359
    Ville Vainio
    Oct 27, 2004
  2. Replies:
    0
    Views:
    243
  3. Georgy
    Replies:
    3
    Views:
    128
    gabriele renzi
    Jun 1, 2004
  4. A. Sinan Unur

    Practical Extraction Recording Language

    A. Sinan Unur, Oct 24, 2004, in forum: Perl Misc
    Replies:
    21
    Views:
    228
    Michele Dondi
    Oct 25, 2004
  5. Deepu
    Replies:
    1
    Views:
    238
    ccc31807
    Feb 7, 2011
Loading...

Share This Page