SimplePrograms挑战

编程入门 行业动态 更新时间:2024-10-07 10:23:23
本文介绍了SimplePrograms挑战的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我提出了一个挑战,通过一个很好的例子扩展以下 页面: 现在页面以15个例子开始,这些例子很多,但是他们仍然在表面上挣扎。 (还有两个八个皇后区的实现,但我想填写代码行中的 差距,而且它们有点长现在。) 我正在寻找一个好的16行代码示例,其中包含 以下品质: 1)它介绍了一些重要的Python概念 前15个程序没有涵盖。 2)它不是太多深奥。 Python新手是 的观众(但你可以假设他们对于一般的编程来说并不是新手。) 3)它在Python 2.4上运行。 4)它不只是展示一个概念;它解决了面值问题。 (它可以解决一个异想天开的问题,比如计算兔子,但程序本身 应该是完整的和适当简单的 手头有问题。) 5)你愿意用 质量来审查你的代码。 6)PEP 8没有重大偏离。 任何接受者? - 史蒂夫 __________________________________________________ __________________________________ 正在寻找交易?使用Yahoo!查找航班和酒店的优惠价格FareChase。 farechase.yahoo/

Hi, I''m offering a challenge to extend the following page by one good example: wiki.python/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. farechase.yahoo/

推荐答案

#阅读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 ... @ yahoowrote: On Jun 11, 6:56 pm, Steve Howell <showel...@yahoowrote: 我提出了一个挑战,通过一个很好的例子扩展以下 页面: wiki.python/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: wiki.python/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。 farechase.yahoo/

6月12日上午8:51,infidel< saint.infi ... @ gmailwrote: On Jun 12, 8:51 am, infidel <saint.infi...@gmailwrote: #读取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挑战

本文发布于:2023-11-30 18:42:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1651105.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:SimplePrograms

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!