Velocity Reviews > Solving the Einstein's Riddle in Python

# Solving the Einstein's Riddle in Python

Juha Haataja
Guest
Posts: n/a

 05-24-2004
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

moma
Guest
Posts: n/a

 05-24-2004
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

Aika hyvä !
norja = kala.

Christos TZOTZIOY Georgiou
Guest
Posts: n/a

 05-24-2004
On Mon, 24 May 2004 14:12:38 +0300, rumours say that Juha Haataja
<(E-Mail Removed)-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

Erik Max Francis
Guest
Posts: n/a

 05-24-2004
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.velocityreviews.com/forums/(E-Mail Removed) && 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

Christos TZOTZIOY Georgiou
Guest
Posts: n/a

 05-24-2004
On Mon, 24 May 2004 10:06:14 -0700, rumours say that Erik Max Francis
<(E-Mail Removed)> 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
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

Carl Banks
Guest
Posts: n/a

 05-25-2004
Juha Haataja <(E-Mail Removed)-spam.fi> wrote in message news:<(E-Mail Removed)>...
> 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

Erik Max Francis
Guest
Posts: n/a

 05-25-2004
Christos TZOTZIOY Georgiou wrote:

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

Thanks .

--
__ Erik Max Francis && (E-Mail Removed) && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
\__/ Something to remember you by.
-- Leonard Shelby

Juha Haataja
Guest
Posts: n/a

 05-25-2004
In article <(E-Mail Removed)>,
Christos "TZOTZIOY" Georgiou <(E-Mail Removed)> 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
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

A. Lloyd Flanagan
Guest
Posts: n/a

 05-25-2004
Erik Max Francis <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> 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

Christos TZOTZIOY Georgiou
Guest
Posts: n/a

 05-25-2004
On Tue, 25 May 2004 09:05:53 +0300, rumours say that Juha Haataja
<(E-Mail Removed)-spam.fi> might have written:

>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
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,
--
TZOTZIOY, I speak England very best,
"I have a cunning plan, m'lord" --Sean Bean as Odysseus/Ulysses