用Python解决爱因斯坦的谜语 [英] Solving the Einstein's Riddle in Python

查看:68
本文介绍了用Python解决爱因斯坦的谜语的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从四月开始学习Python,并希望专家对列表处理的评论很少。


我设法实现了Python用于解决所谓的爱因斯坦谜语的代码,请参阅例如:

http://www.manbottle.com/trivia/Einstein_s_Riddle.htm


我解决了这个问题两种方式,首先是通过使用列表和列表推导直截了当的方式。然后我使用Python对象模型对
进行了重构,以便从Python列表对象中获取新的数据类型。这里有两个版本的

版本:

http://www.csc.fi/oppaat/f95/python/talot.py
http://www.csc.fi/oppaat/f95/python/talot_oo.py


我为没有评论代码而道歉。


(我对数据进行了本地化,因此现在的国籍主要是北欧的b $ b。第一个版本的输入数据是芬兰语。

另外,我不喜欢吸烟,所以我将

数据中的那部分更改为不同类型的音乐。 )这是OO版本的最终输出:


选择:1

1 2 3 4 5

黄色蓝色红色绿色白色

挪威丹麦芬兰德国瑞典

水茶牛奶咖啡啤酒

蓝调歌剧摇滚流行金属

猫马鸟鱼狗


因此德国人拥有4只鱼。


OO版本有点慢(1.6秒对1.2),但

更容易理解。


我想知道为什么代码为50 % 慢点。原则上,

唯一的区别应该是在OO版本中我正在使用

__init__方法,并且在原始版本中我正在创建

列出使用[]。


请随时评论编程风格。

我刚刚开始了解语言,以及我在Fortran 90/95中的
背景可能是学习上的障碍

Python。


谢谢,


- JuHa

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

推荐答案

Juha Haataja写道:
Juha Haataja wrote:
我一直在学习Python自4月份以来,我希望专家对列表处理的评论很少。

我设法实现了一个Python代码来解决所谓的爱因斯坦的谜语,参见例如:

http:// www。 manbottle.com/trivia/Einstein_s_Riddle.htm

我解决了他有两个方面的问题,首先是使用列表和列表推导的直截了当的方式。然后我通过从Python列表对象派生新的数据类型,使用Python对象模型重构代码。这里有两个版本:

http://www.csc.fi/oppaat/f95/python/talot.py
http://www.csc.fi/oppaat/f95/python/talot_oo.py

我为没有评论代码而道歉。

(我对数据进行了本地化,因此各民族现在主要是北欧人。第一版的输入数据是芬兰语。
另外,我不喜欢吸烟,所以我将那部分数据改为不同类型的音乐。)

以下是OO版本的最终输出:
<选择:1
1 2 3 4 5
黄色蓝色红色绿色白色芬兰丹麦芬兰德国瑞典
水茶牛奶咖啡啤酒蓝调歌剧摇滚流行金属
猫马比尔d鱼狗

因此德国人拥有4号鱼。

OO版本有点慢(1.6秒对1.2),但
更容易理解。

我想知道为什么代码慢了50%。原则上,唯一的区别应该是在OO中我正在使用
__init__方法,并且在原始版本中我使用[]创建
列表。

请随时评论编程风格。
我刚刚开始了解语言,我在Fortran 90/95中的背景可能是学习上的障碍
Python。

谢谢,

- JuHa
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。



Not bad,
Aika hyv? !
norja = kala.


2004年5月24日星期一14:12:38 +0300,谣言说Juha Haataja
< ju ********** @ csc.no-spam.fi>可能写得:
On Mon, 24 May 2004 14:12:38 +0300, rumours say that Juha Haataja
<ju**********@csc.no-spam.fi> might have written:
我从四月开始学习Python,并希望专家对列表处理的评论很少。

我设法实现一个Python代码来解决所谓的爱因斯坦的谜语,参见例如:

http://www.manbottle.com/trivia/Einstein_s_Riddle.htm
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




也许你想看看我的看法
http://www.sil-tec.gr/~tzot/python/ (检查logic_puzzle.py)。它似乎比你的版本慢,但是它是通用的(即你可以用一种简单的自定义语言来描述类似的组合拼图)。我

在该页面中有几个文本文件链接...

-

TZOTZIOY,我说英格兰最好,

Ils sont fous ces Redmontains! --Harddix



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


Juha Haataja写道:
Juha Haataja wrote:
我设法实现了一个Python代码来解决
所以 - 被称为爱因斯坦的谜语......
I managed to implement a Python code for solving the
so-called Einstein''s Riddle, ...




请注意,这个谜语实际上与爱因斯坦无关。


-

__ Erik Max Francis&& ma*@alcyone.com && http://www.alcyone.com/max/

/ \美国加利福尼亚州圣何塞市&& 37 20 N 121 53 W&& AIM erikmaxfrancis

\ __ /心灵不是要装满的船只,而是要点火。

- 普鲁塔克



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


这篇关于用Python解决爱因斯坦的谜语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆