Solving the Einstein's Riddle in Python

Juha Haataja juha.haataja at csc.no-spam.fi
Mon May 24 07:12:38 EDT 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



More information about the Python-list mailing list