Differences between revisions 6 and 32 (spanning 26 versions)
Revision 6 as of 2008-04-26 14:07:18
Size: 5949
Editor: lizzie
Comment:
Revision 32 as of 2008-07-02 06:38:02
Size: 790
Editor: ZoomQuiet
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
## page was renamed from ObpLovelyPython/LpyAttach-answer
## page was renamed from ObpLovelyPython/Answer
Line 4: Line 6:
||'''status'''|| 校对  || ShengYan || 完成度70%||


[[TableOfContents]]
||'''status''' || 校对 || lizzie || 完成度100% ||
[[TableOfContents(2)]]
Line 10: Line 10:
== CDays ==
=== CDays-5 ===
## 编辑正文收集在:
### http://openbookproject.googlecode.com/svn/trunk/LovelyPython/exercise/part2-KDays/LpyAttach3answer-KDays.moin
### http://openbookproject.googlecode.com/svn/trunk/LovelyPython/exercise/part1-CDays/LpyAttach3answer-CDays.moin
Line 13: Line 14:
 * 计算今年是闰年嘛?
    相关代码:
{{{#!python
#coding:utf-8
'''cdays-5-exercise-1.py this year is a leap year or not
    @author: U{shengyan<mailto:[email protected]>}
    @version:$Id$
'''
#导入time模块
import time
#获取当前年份
thisyear = time.localtime()[0]
#判断闰年条件,满足模400为0,或者模4为0但模100不为0
if thisyear % 400 == 0 or thisyear % 4 ==0 and thisyear % 100 <> 0:
 print 'this year %s is a leap year' % thisyear
else:
 print 'this year %s is not a leap year' % thisyear
}}}
== CDays 故事解答 ==
[[Include(/CDays,,from="^##startInc",to="^##endInc")]]
Line 32: Line 17:
 * 利用python作为科学计算器。熟悉Python中的常用运算符,并分别求出表达式12*34+78-132/6、(12*(34+78)-132)/6、(8.6/4)**5的值。并利用`math`模块进行数学计算,分别求出145/23的余数,0.5的sin和cos值等等,详细可参考help(math)。
    相关代码:
{{{#!python
#coding:utf-8
'''cdays-5-exercise-2.py basic operation and math library
    @author: U{shengyan<mailto:[email protected]>}
    @version:$Id$
'''
#表达式计算
x = 12*34+78-132/6
y = (12*(34+78)-132)/6
z = (8.6/4)**5

print '12*34+78-132/6 = %d' % x
print '(12*(34+78)-132)/6 = %d' % y
print '(8.6/4)**5 = %f' % z

#导入数学计算模块
import math
#求余函数
a = math.fmod(145, 23)
#正弦函数
b = math.sin(0.5)
#余弦函数
c = math.cos(0.5)

print '145/23的余数 = %d' % a
print 'sin(0.5) = %f' %b
print 'cos(0.5) = %f' %c
}}}
 * 编写程序,在屏幕上打印出如下图案:
attachment:cdays-5-exercise-3.png
    相关代码:
{{{#!python
#coding:utf-8
'''cdays-5-exercise-3.py print out and for expression
    @author: U{shengyan<mailto:[email protected]>}
    @version:$Id$
'''

for index in range(1, 6):
 if index > 3:
  #调整index
  index = 2*3 -index
 #输出每行空格个数
 print ' '*(3-index),
 #输出每行*的个数
 print '*'*(2*index - 1)
}}}

截图


=== CDays-4 ===
 * os 模块中还有哪些功能可以使用? -- 提示使用 `dir()` `help()`

 * `open()` 还有哪些模式可以使用?

 * 尝试`for .. in ..` 循环可以对哪些数据类型进行操作?

 * 格式化声明,还有哪些格式可以进行约定?

 * 现在的写入文件模式好嘛? 有改进的余地?
'''CDay-4-5.py''' 好在哪里? :
{{{#!python
# coding : utf-8
import os
export = ""
for root, dirs, files in os.walk('/media/cdrom0'):
  export+="\n %s;%s;%s" % (root,dirs,files)
open('mycd2.cdc', 'w').write(export)
}}}

'''CDay-4-6.py''' 又更加好在哪里? :
{{{#!python
# coding : utf-8
import os
export = []
for root, dirs, files in os.walk('/media/cdrom0'):
    export.append("\n %s;%s;%s" % (root,dirs,files))
open('mycd2.cdc', 'w').write(''.join(export))
}}}

 * 读取文件cdays-4-test.txt内容,去除空行和注释行后,以行为单位进行排序,并将结果输出为cdays-4-result.txt。

=== CDays-3 ===
 * 根据 [http://www.woodpecker.org.cn/diveintopython/scripts_and_streams/command_line_arguments.html DiPy 10.6. 处理命令行参数] 使用`getopt.getopt()` 优化当前功能函式。

 * 读取某一简单索引文件cdays-3-test.txt,其每行格式为文档序号 关键词,现需根据这些信息转化为倒排索引,即统计关键词在哪些文档中,格式如下:包含该关键词的文档数 关键词 => 文档序号。其中,原索引文件作为命令行参数传入主程序,并设计一个collect函数统计关键字-序号结果对,最后在主程序中输出结果至屏幕。

 * 八皇后问题。在8*8的棋盘上,放置8个皇后,使得任两个皇后不在同行同列同正负对角线上。
=== CDays-2 ===
 * 把最后探索出的 `cdcGrep()`嵌入 pycdc-v0.5.py 实现完成版本的 PyCDC

 * 编写一个类,实现简单的栈。数据的操作按照先进后出(FILO)的顺序。主要成员函数为put(item),实现数据item插入栈中;get(),实现从栈中取一个数据。

=== CDays-1 ===
 * 自动判定你自个儿的Blog 是什么编码的?

 * 在题一基础上,如果blog的编码不是utf-8,编写小程序自动将其转换成utf-8 编码保存到本地?
=== CDays-0 ===

 * 请根据软件发布的流程和软件开发的编码规范,将读者之前章节写的程序修改并发布出去。另外,可以查找下除了epydoc外还有哪些较好的py文档生成器?

=== CDays+1 ===
 * 编程实现以下功能并进行最大化的优化:遍历指定目录下的所有文件,找出其中占用空间最大的前3个文件。

 * 利用ConfigParser,将上述题目中产生的结果按照cdays+1-my.ini格式存储到文件cdays+1-result.txt中。
=== CDays+2 ===
 * 如果在Karrigell 实例中,不复制 cdctools.py 到webapp 目录中,也可以令 index.ks 引用到?

 * 经过本章Karrigell的初步学习,实现一个简易的web留言系统。主要利用Karrigell_quick_form实现提交留言并显示出来。
=== CDays+3 ===
 * 熟悉线程相关知识后,利用Lock和RLock实现线程间的简单同步,使得10个线程对同一共享变量进行递增操作,使用加锁机制保证变量结果的正确。

 * 使用Queue实现多线程间的同步。比如说,十个输入线程从终端输入字符串,另十个输出线程依次获取字符串并输出到屏幕。

 * Python中的Event是用于线程间的相互通信,主要利用信号量机制。修改题一的程序,利用信号量重新实现多线程对同一共享变量进行递增操作。

== KDays ==

== 小结 ==
## 总体语法等等叙述,注意给出相关知识的阅读指导
== KDays 故事解答 ==
[[Include(/KDays,,from="^##startInc",to="^##endInc")]]
Line 159: Line 22:
::-- ZoomQuiet [[[DateTime(2008-04-26T07:41:41Z)]]]
[[PageComment2]]
 . ::-- ZoomQuiet [[[DateTime(2008-04-26T07:41:41Z)]]] [[PageComment2]]

status

校对

lizzie

完成度100%

TableOfContents(2)

1. 故事练习解答

1.1. CDays 故事解答

Include(/CDays,,from="^##startInc",to="^##endInc")

1.2. KDays 故事解答

Include(/KDays,,from="^##startInc",to="^##endInc")


ObpLovelyPython/LpyAttach3answer (last edited 2009-12-25 07:14:24 by localhost)