SimplePrograms挑战 [英] SimplePrograms challenge

查看:62
本文介绍了SimplePrograms挑战的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我提出了一个挑战,通过一个很好的例子扩展以下

页面:

现在页面以15个例子开始,这些例子很多,但是他们仍然在表面上挣扎。 (还有两个八个
皇后区的实现,但我想填写代码行中的

差距,而且它们有点长现在。)


我正在寻找一个好的16行代码示例,其中包含

以下品质:


1)它介绍了一些重要的Python概念

前15个程序没有涵盖。


2)它不是太多深奥。 Python新手是

的观众(但你可以假设他们对于一般的编程来说并不是新手。)


3)它在Python 2.4上运行。


4)它不只是展示一个概念;它解决了面值问题。 (它可以解决一个异想天开的问题,比如计算兔子,但程序本身

应该是完整的和适当简单的

手头有问题。)


5)你愿意用

质量来审查你的代码。


6)PEP 8没有重大偏离。


任何接受者?


- 史蒂夫



__________________________________________________ __________________________________

正在寻找交易?使用Yahoo!查找航班和酒店的优惠价格FareChase。
http://farechase.yahoo.com/

Hi, I''m offering a challenge to extend the following
page by one good example:

http://wiki.python.org/moin/SimplePrograms

Right now the page starts off with 15 examples that
cover lots of ground in Python, but they''re still
scratching the surface. (There are also two Eight
Queens implementations, but I''m looking to fill the
gap in lines-of-code, and they''re a little long now.)

I''m looking for a good 16-line code example with the
following qualities:

1) It introduces some important Python concept that
the first 15 programs don''t cover.

2) It''s not too esoteric. Python newbies are the
audience (but you can assume they''re not new to
programming in general).

3) It runs on Python 2.4.

4) It doesn''t just demonstrate a concept; it solves
a problem at face value. (It can solve a whimsical
problem, like counting rabbits, but the program itself
should be "complete" and "suitably simple" for the
problem at hand.)

5) You''re willing to have your code reviewed by the
masses.

6) No major departures from PEP 8.

Any takers?

-- Steve



__________________________________________________ __________________________________
Looking for a deal? Find great prices on flights and hotels with Yahoo! FareChase.
http://farechase.yahoo.com/

推荐答案

#阅读CSV文件,元组拆包

import csv


#pacific .csv包含:

#1,CA,California

#2,AK,Alaska

#3,OR,Oregon

#4,WA,Washington

#5,HI,Hawaii


reader = csv.reader(open(''pacific.csv') '))

为id,abbr,读者姓名:

print''%s缩写为:"%s"''%(name,abbr)

# reading CSV files, tuple-unpacking
import csv

#pacific.csv contains:
#1,CA,California
#2,AK,Alaska
#3,OR,Oregon
#4,WA,Washington
#5,Hawaii

reader = csv.reader(open(''pacific.csv''))
for id, abbr, name in reader:
print ''%s is abbreviated: "%s"'' % (name, abbr)


6月11日下午6:56,Steve Howell< showel ... @ yahoo.comwrote:
On Jun 11, 6:56 pm, Steve Howell <showel...@yahoo.comwrote:

我提出了一个挑战,通过一个很好的例子扩展以下

页面:

http://wiki.python.org/moin/SimplePrograms


现在这个页面以15个例子开始,这些例子在Python中覆盖了很多,但是他们仍然在表面上挣扎。 (还有两个八个
皇后区的实现,但我想填写代码行中的

差距,而且它们有点长现在。)


我正在寻找一个好的16行代码示例,其中包含

以下品质:


1)它介绍了一些重要的Python概念

前15个程序没有涵盖。


2)它不是太多深奥。 Python新手是

的观众(但你可以假设他们对于一般的编程来说并不是新手。)


3)它在Python 2.4上运行。


4)它不只是展示一个概念;它解决了面值问题。 (它可以解决一个异想天开的问题,比如计算兔子,但程序本身

应该是完整的和适当简单的

手头有问题。)


5)你愿意用

质量来审查你的代码。


6)PEP 8没有重大偏离。


任何接受者?
Hi, I''m offering a challenge to extend the following
page by one good example:

http://wiki.python.org/moin/SimplePrograms

Right now the page starts off with 15 examples that
cover lots of ground in Python, but they''re still
scratching the surface. (There are also two Eight
Queens implementations, but I''m looking to fill the
gap in lines-of-code, and they''re a little long now.)

I''m looking for a good 16-line code example with the
following qualities:

1) It introduces some important Python concept that
the first 15 programs don''t cover.

2) It''s not too esoteric. Python newbies are the
audience (but you can assume they''re not new to
programming in general).

3) It runs on Python 2.4.

4) It doesn''t just demonstrate a concept; it solves
a problem at face value. (It can solve a whimsical
problem, like counting rabbits, but the program itself
should be "complete" and "suitably simple" for the
problem at hand.)

5) You''re willing to have your code reviewed by the
masses.

6) No major departures from PEP 8.

Any takers?



好​​的,添加了基于doctest的单元测试示例版本;更多

Pythonic ;-)
$ b $bAndré


P.S.恭喜你开始吧!

Ok, doctest-based version of the Unit test example added; so much more
Pythonic ;-)

André

P.S. Congrats for starting this!


>

- 史蒂夫


__________________________________________________ __________________________________

正在寻找交易?使用Yahoo!查找航班和酒店的优惠价格FareChase。 http://farechase.yahoo.com/


6月12日上午8:51,infidel< saint.infi ... @ gmail.comwrote:
On Jun 12, 8:51 am, infidel <saint.infi...@gmail.comwrote:

#读取CSV文件,元组拆包

import csv

#pacific.csv包含:

#1,CA,加州

#2,AK,Alaska

#3,OR,Oregon

#4,WA,Washington

#5,HI,夏威夷


reader = csv.reader(open(''pacific.csv''))
# reading CSV files, tuple-unpacking
import csv

#pacific.csv contains:
#1,CA,California
#2,AK,Alaska
#3,OR,Oregon
#4,WA,Washington
#5,Hawaii

reader = csv.reader(open(''pacific.csv''))



为了通用性和可移植性,这应该是:

reader = csv.reader(open(''pacific.csv'',''rb''))

For generality and portability, this should be:
reader = csv.reader(open(''pacific.csv'', ''rb''))


for id,abbr,读者姓名:

print''%s缩写为:"%s"''%(name,abbr)
for id, abbr, name in reader:
print ''%s is abbreviated: "%s"'' % (name, abbr)



这个例子并没有说明为什么要使用csv模块

而不是:

for open(''pacific.csv''):

id,abbr,name = line.rstrip()。split('', '')

#etc

这对于简单的示例文件来说已经足够了。

and this example doesn''t demonstrate why one should use the csv module
instead of:
for line in open(''pacific.csv''):
id, abbr, name = line.rstrip().split('','')
# etc
which is quite adequate for the simplistic example file.


这篇关于SimplePrograms挑战的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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