Solving the Einstein's Riddle in Python

J

Juha Haataja

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
 
M

moma

Juha said:
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.
 
C

Christos TZOTZIOY Georgiou

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

Erik Max Francis

Juha said:
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.
 
C

Christos TZOTZIOY Georgiou

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

Carl Banks

Juha Haataja said:
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"
 
J

Juha Haataja

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...[/QUOTE]

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
 
A

A. Lloyd Flanagan

Erik Max Francis said:
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.
 
C

Christos TZOTZIOY Georgiou

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 :)
 
C

Christos TZOTZIOY Georgiou

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,190
Latest member
ClayE7480

Latest Threads

Top