Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Solving the Einstein's Riddle in Python

Reply
Thread Tools

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
 
Reply With Quote
 
 
 
 
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


Not bad,
Aika hyvä !
norja = kala.
 
Reply With Quote
 
 
 
 
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
 
Reply With Quote
 
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
 
Reply With Quote
 
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
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
 
Reply With Quote
 
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
 
Reply With Quote
 
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
 
Reply With Quote
 
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
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
 
Reply With Quote
 
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
forgets about Al. Kind of a sad story, actually.
 
Reply With Quote
 
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:

>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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Solving simple mathematical equation solving speed Lionel Java 14 06-23-2008 08:01 AM
Python riddle SPE - Stani's Python Editor Python 6 12-07-2005 11:12 AM
Python Challenge ahead [NEW] for riddle lovers pythonchallenge Python 76 05-27-2005 05:22 AM
The first programming riddle on the net. Python-challenge Sara Khalatbari Python 1 05-10-2005 01:16 PM
Python Challenge ahead [NEW] - for riddle lovers pythonchallenge Python 0 05-08-2005 07:09 PM



Advertisments