Solving the Einstein's Riddle in Python

Discussion in 'Python' started by Juha Haataja, May 24, 2004.

  1. Juha Haataja

    Juha Haataja Guest

    I have been learning Python since April, and would like a
    few comments from the experts on list processing.

    I managed to implement a Python code for solving the
    so-called Einstein's Riddle, see e.g.:

    http://www.manbottle.com/trivia/Einstein_s_Riddle.htm

    I solved the problem in two ways, first the straightforward
    way by using lists and list comprehensions. Then I
    refactored to code using the Python object model by
    deriving new datatypes from the Python list object. The
    two versions are available here:

    http://www.csc.fi/oppaat/f95/python/talot.py
    http://www.csc.fi/oppaat/f95/python/talot_oo.py

    I apologize for not commenting the code.

    (I localized the data, so the nationalities are now mainly
    Nordic. The input data of the first version is in Finnish.
    Also, I don't like smoking, so I changed that part of the
    data to different types of music.)

    Here is the final output of the OO version:

    Choices: 1
    1 2 3 4 5
    yellow blue red green white
    norway denmark finland germany sweden
    water tea milk coffee beer
    blues opera rock pop metal
    cat horse bird fish dog

    Thus the German owns the fish in house 4.

    The OO version is somewhat slower (1.6 seconds vs. 1.2), but
    easier to understand.

    I'm wondering why the code is 50% slower. In principle, the
    only difference should be that in the OO versio I'm using an
    __init__ method, and in the original version I was creating
    lists using [].

    Please feel free to comment on the programming style.
    I'm just starting to get to know the language, and my
    background in Fortran 90/95 may be a handicap in learning
    Python.

    Thanks,

    - JuHa
     
    Juha Haataja, May 24, 2004
    #1
    1. Advertising

  2. Juha Haataja

    moma Guest

    Juha Haataja wrote:
    > I have been learning Python since April, and would like a
    > few comments from the experts on list processing.
    >
    > I managed to implement a Python code for solving the
    > so-called Einstein's Riddle, see e.g.:
    >
    > http://www.manbottle.com/trivia/Einstein_s_Riddle.htm
    >
    > I solved the problem in two ways, first the straightforward
    > way by using lists and list comprehensions. Then I
    > refactored to code using the Python object model by
    > deriving new datatypes from the Python list object. The
    > two versions are available here:
    >
    > http://www.csc.fi/oppaat/f95/python/talot.py
    > http://www.csc.fi/oppaat/f95/python/talot_oo.py
    >
    > I apologize for not commenting the code.
    >
    > (I localized the data, so the nationalities are now mainly
    > Nordic. The input data of the first version is in Finnish.
    > Also, I don't like smoking, so I changed that part of the
    > data to different types of music.)
    >
    > Here is the final output of the OO version:
    >
    > Choices: 1
    > 1 2 3 4 5
    > yellow blue red green white
    > norway denmark finland germany sweden
    > water tea milk coffee beer
    > blues opera rock pop metal
    > cat horse bird fish dog
    >
    > Thus the German owns the fish in house 4.
    >
    > The OO version is somewhat slower (1.6 seconds vs. 1.2), but
    > easier to understand.
    >
    > I'm wondering why the code is 50% slower. In principle, the
    > only difference should be that in the OO versio I'm using an
    > __init__ method, and in the original version I was creating
    > lists using [].
    >
    > Please feel free to comment on the programming style.
    > I'm just starting to get to know the language, and my
    > background in Fortran 90/95 may be a handicap in learning
    > Python.
    >
    > Thanks,
    >
    > - JuHa


    Not bad,
    Aika hyvä !
    norja = kala.
     
    moma, May 24, 2004
    #2
    1. Advertising

  3. On Mon, 24 May 2004 14:12:38 +0300, rumours say that Juha Haataja
    <-spam.fi> might have written:

    >I have been learning Python since April, and would like a
    >few comments from the experts on list processing.
    >
    >I managed to implement a Python code for solving the
    >so-called Einstein's Riddle, see e.g.:
    >
    > http://www.manbottle.com/trivia/Einstein_s_Riddle.htm


    Perhaps you would like to take a look at my take on that at
    http://www.sil-tec.gr/~tzot/python/ (check for the logic_puzzle.py). It
    seems that it's slower than your version, but it's generic (ie you can
    describe similar combinatorial puzzles with an easy custom language). I
    have a couple of links in that page for the text files...
    --
    TZOTZIOY, I speak England very best,
    Ils sont fous ces Redmontains! --Harddix
     
    Christos TZOTZIOY Georgiou, May 24, 2004
    #3
  4. Juha Haataja wrote:

    > I managed to implement a Python code for solving the
    > so-called Einstein's Riddle, ...


    Note that this riddle actually has nothing to do with Einstein.

    --
    __ Erik Max Francis && && http://www.alcyone.com/max/
    / \ San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
    \__/ The mind is not a vessel to be filled but a fire to be kindled.
    -- Plutarch
     
    Erik Max Francis, May 24, 2004
    #4
  5. On Mon, 24 May 2004 10:06:14 -0700, rumours say that Erik Max Francis
    <> might have written:

    >Juha Haataja wrote:
    >
    >> I managed to implement a Python code for solving the
    >> so-called Einstein's Riddle, ...

    >
    >Note that this riddle actually has nothing to do with Einstein.


    Oh, yeah, forgot to mention that. And, either most of the people I know
    are in the 2% of the population, or the puzzle is not exactly an IQ
    test...

    PS Erik, the other day (on the matter of _Troy_ the movie) I was looking
    for an English translation of the _Iliad_ on the web for a friend, and
    Google had your site somewhere in the first results page (don't remember
    the exact search terms I used, though); I saw your name and said, "this
    name is strangely familiar" :) Great site.
    --
    TZOTZIOY, I speak England very best,
    "I have a cunning plan, m'lord" --Sean Bean as Odysseus/Ulysses
     
    Christos TZOTZIOY Georgiou, May 24, 2004
    #5
  6. Juha Haataja

    Carl Banks Guest

    Juha Haataja <-spam.fi> wrote in message news:<>...
    > I have been learning Python since April, and would like a
    > few comments from the experts on list processing.
    >
    > I managed to implement a Python code for solving the
    > so-called Einstein's Riddle, see e.g.:
    >
    > http://www.manbottle.com/trivia/Einstein_s_Riddle.htm
    >
    > I solved the problem in two ways, first the straightforward
    > way by using lists and list comprehensions. Then I
    > refactored to code using the Python object model by
    > deriving new datatypes from the Python list object. The
    > two versions are available here:
    >
    > http://www.csc.fi/oppaat/f95/python/talot.py
    > http://www.csc.fi/oppaat/f95/python/talot_oo.py


    Wow, you wrote all that code? Here's my solution:

    print "I don't know"


    --
    CARL BANKS
     
    Carl Banks, May 25, 2004
    #6
  7. Christos TZOTZIOY Georgiou wrote:

    > I saw your name and said,
    > "this
    > name is strangely familiar" :) Great site.


    Thanks :).

    --
    __ Erik Max Francis && && http://www.alcyone.com/max/
    / \ San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
    \__/ Something to remember you by.
    -- Leonard Shelby
     
    Erik Max Francis, May 25, 2004
    #7
  8. Juha Haataja

    Juha Haataja Guest

    In article <>,
    Christos "TZOTZIOY" Georgiou <> wrote:

    >> Perhaps you would like to take a look at my take on that at

    > http://www.sil-tec.gr/~tzot/python/ (check for the logic_puzzle.py). It
    > seems that it's slower than your version, but it's generic (ie you can
    > describe similar combinatorial puzzles with an easy custom language). I
    > have a couple of links in that page for the text files...


    Thanks for this pointer. I did try googling for Python
    code for solving this problem, but for some reason
    missed your site. Your solution is really nice,
    a general way of coding the problem in Python.

    - JuHa

    PS. I added some documentation and little tweaks
    to my Python code at

    http://www.csc.fi/oppaat/f95/python/talot_oo.py
     
    Juha Haataja, May 25, 2004
    #8
  9. Erik Max Francis <> wrote in message news:<>...
    > Juha Haataja wrote:
    >
    > > I managed to implement a Python code for solving the
    > > so-called Einstein's Riddle, ...

    >
    > Note that this riddle actually has nothing to do with Einstein.


    Actually you're just confusing your Einsteins. Note that the problem
    page states it was published early in the 19th century. This is
    because it had nothing to do with Albert Einstein, the disoverer of
    relativity.

    Actually it was created by Al Einstein, and he was really quite famous
    for it, until that Albert guy came along a century later. So then
    this new guy has a little theory or two, and suddenly everybody
    forgets about Al. Kind of a sad story, actually.
     
    A. Lloyd Flanagan, May 25, 2004
    #9
  10. On Tue, 25 May 2004 09:05:53 +0300, rumours say that Juha Haataja
    <-spam.fi> might have written:

    >Your solution is really nice,
    >a general way of coding the problem in Python.


    Thanks for the kind words; I just pointed my solution out of a tendency
    to produce as generic code as I can (that is why my code is also so much
    slower than yours...). I saw your code, and I believe you have already
    a good grasp of 'pythonness'; I also enjoyed your algorithm's gradual
    elimination of combinations --its speed too. My code was also mostly
    written in my python learning period too (an improvement over my older
    awk version :).

    If you ever make something more general (I mean, solving different
    puzzles without code changes) than your solution and faster than mine,
    I'd be glad to know :)
    --
    TZOTZIOY, I speak England very best,
    "I have a cunning plan, m'lord" --Sean Bean as Odysseus/Ulysses
     
    Christos TZOTZIOY Georgiou, May 25, 2004
    #10
  11. On 25 May 2004 09:58:20 -0700, rumours say that
    (A. Lloyd Flanagan) might have written:

    >Actually it was created by Al Einstein, and he was really quite famous
    >for it, until that Albert guy came along a century later. So then
    >this new guy has a little theory or two, and suddenly everybody
    >forgets about Al. Kind of a sad story, actually.


    Right. Not only so, but Albert also used the same blackboard Al used,
    as you can clearly see in the link provided by the OP:

    http://www.manbottle.com/trivia/Einstein_s_Riddle.htm

    I think Albert Einstein was a fraud, and the true hero was another guy
    with the same name.
    --
    TZOTZIOY, I speak England very best,
    "I have a cunning plan, m'lord" --Sean Bean as Odysseus/Ulysses
     
    Christos TZOTZIOY Georgiou, May 25, 2004
    #11
    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. pythonchallenge
    Replies:
    76
    Views:
    1,581
    Tim Peters
    May 27, 2005
  2. pythonchallenge
    Replies:
    0
    Views:
    242
    pythonchallenge
    May 8, 2005
  3. Sara Khalatbari
    Replies:
    1
    Views:
    305
    Peter Hansen
    May 10, 2005
  4. SPE - Stani's Python Editor

    Python riddle

    SPE - Stani's Python Editor, Dec 6, 2005, in forum: Python
    Replies:
    6
    Views:
    394
    Steven D'Aprano
    Dec 7, 2005
  5. Lionel
    Replies:
    14
    Views:
    1,204
Loading...

Share This Page