473,387 Members | 1,582 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Solving the Einstein's Riddle in Python

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
Jul 18 '05 #1
10 6486
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.
Jul 18 '05 #2
On Mon, 24 May 2004 14:12:38 +0300, rumours say that Juha Haataja
<ju**********@csc.no-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
Jul 18 '05 #3
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 && ma*@alcyone.com && 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
Jul 18 '05 #4
On Mon, 24 May 2004 10:06:14 -0700, rumours say that Erik Max Francis
<ma*@alcyone.com> 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
Jul 18 '05 #5
Juha Haataja <ju**********@csc.no-spam.fi> wrote in message news:<ju********************************@newsread. funet.fi>...
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
Jul 18 '05 #6
Christos TZOTZIOY Georgiou wrote:
I saw your name and said,
"this
name is strangely familiar" :) Great site.


Thanks :-).

--
__ Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
\__/ Something to remember you by.
-- Leonard Shelby
Jul 18 '05 #7
In article <e9********************************@4ax.com>,
Christos "TZOTZIOY" Georgiou <tz**@sil-tec.gr> 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
Jul 18 '05 #8
Erik Max Francis <ma*@alcyone.com> wrote in message news:<40***************@alcyone.com>...
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.
Jul 18 '05 #9
On Tue, 25 May 2004 09:05:53 +0300, rumours say that Juha Haataja
<ju**********@csc.no-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
Jul 18 '05 #10
On 25 May 2004 09:58:20 -0700, rumours say that
al************@comcast.net (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
Jul 18 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

63
by: pythonchallenge | last post by:
For the riddles' lovers among you, you are most invited to take part in the Python Challenge, the first python programming riddle on the net. You are invited to take part in it at:...
0
by: pythonchallenge | last post by:
For the riddles' lovers among you, you are most invited to take part in the Python Challenge, the first python programming riddle on the net. You are invited to take part in it at:...
1
by: Sara Khalatbari | last post by:
Hey guys! Thanks for helping me find time&date. Have you seen this? This is the first programming riddle on the net with 20 levels! http://www.pythonchallenge.com/ ...
41
by: puzzlecracker | last post by:
Given an array of size n and populated with consecutive integers from 1 to n i.e. in random order. Two integers are removed, meaning zero is placed in their places. Give O (n) efficient algorithm...
6
by: SPE - Stani's Python Editor | last post by:
I know that this code is nonsense, but why does this print 'Why?' a = 1 if a >2: try: 5/0 except: raise else: print 'why?'
6
by: snaidis | last post by:
I'm new here and I have a quite smart riddle for you people: Try to write a program which prints itself. I have a pretty interesting article about this programming problem with the answer, but...
1
by: ImortalSorrow | last post by:
Please I need urgent help with this program! Everything is working well except a part where i want to repeat the riddle if the answer isnt correct. here's the code..hope someone can help me! ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.