status |
校对 |
lizzie |
完成度100% |
1. 故事练习解答
1.1. CDays
1.1.1. CDays-5
计算今年是闰年嘛?
1 #coding:utf-8 2 '''cdays-5-exercise-1.py 判断今年是否是闰年 3 @note: 使用了import, time模块, 逻辑分支, 字串格式化等 4 ''' 5 6 import time #导入time模块 7 8 thisyear = time.localtime()[0] #获取当前年份 9 10 if thisyear % 400 == 0 or thisyear % 4 ==0 and thisyear % 100 <> 0: #判断闰年条件, 满足模400为0, 或者模4为0但模100不为0 11 print 'this year %s is a leap year' % thisyear 12 else: 13 print 'this year %s is not a leap year' % thisyear
- 运行结果 attachment:Bcdays-5-exercise-1.png
利用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)。
1 #coding:utf-8 2 '''cdays-5-exercise-2.py 求表达式的值 3 @note: 基本表达式运算, 格式化输出, math模块 4 @see: math模块使用可参考http://docs.python.org/lib/module-math.html 5 ''' 6 7 x = 12*34+78-132/6 #表达式计算 8 y = (12*(34+78)-132)/6 9 z = (8.6/4)**5 10 11 print '12*34+78-132/6 = %d' % x 12 print '(12*(34+78)-132)/6 = %d' % y 13 print '(8.6/4)**5 = %f' % z 14 15 import math #导入数学计算模块 16 17 a = math.fmod(145, 23) #求余函数 18 b = math.sin(0.5) #正弦函数 19 c = math.cos(0.5) #余弦函数 20 21 print '145/23的余数 = %d' % a 22 print 'sin(0.5) = %f' %b 23 print 'cos(0.5) = %f' %c
- 运行结果 attachment:Bcdays-5-exercise-2.png
找出0~100之间的所有素数。
#coding:utf-8 '''cdays-5-exercise-3.py 求0~100之间的所有素数 @note: for循环, 列表类型 @see: math模块使用可参考http://docs.python.org/lib/module-math.html ''' from math import sqrt N = 100 #基本的方法 result1 = [] for num in range(2, N): f = True for snu in range(2, int(sqrt(num))+1): if num % snu == 0: f = False break if f: result1.append(num) print result1 #更好的方法 result2 = [ p for p in range(2, N) if 0 not in [ p% d for d in range(2, int(sqrt(p))+1)] ] print result2
- 运行结果 attachment:Bcdays-5-exercise-3.png
1.1.2. CDays-4
(1) os 模块中还有哪些功能可以使用? -- 提示使用 dir()和help()
(2) open() 还有哪些模式可以使用?
(3) 尝试for .. in ..循环可以对哪些数据类型进行操作?
(4) 格式化声明,还有哪些格式可以进行约定?
(5) 现在的写入文件模式好嘛? 有改进的余地?
CDay-4-5.py 好在哪里? :
CDay-4-6.py又更加好在哪里? :
(6) 读取文件cdays-4-test.txt内容,去除空行和注释行后,以行为单位进行排序,并将结果输出为cdays-4-result.txt。 cdays-4-test.txt~
#some words Sometimes in life, You find a special friend; Someone who changes your life just by being part of it. Someone who makes you laugh until you can't stop; Someone who makes you believe that there really is good in the world. Someone who convinces you that there really is an unlocked door just waiting for you to open it. This is Forever Friendship. when you're down, and the world seems dark and empty, Your forever friend lifts you up in spirits and makes that dark and empty world suddenly seem bright and full. Your forever friend gets you through the hard times,the sad times,and the confused times. If you turn and walk away, Your forever friend follows, If you lose you way, Your forever friend guides you and cheers you on. Your forever friend holds your hand and tells you that everything is going to be okay.
- 运行结果 attachment:cdays-4-exercise-6.png
1.1.3. CDays-3
(1) 根据DiPy 10.6. 处理命令行参数(http://www.woodpecker.org.cn/diveintopython/scripts_and_streams/command_line_arguments.html )使用getopt.getopt()优化当前功能函式。
(2) 读取某一简单索引文件cdays-3-test.txt,其每行格式为文档序号 关键词,现需根据这些信息转化为倒排索引,即统计关键词在哪些文档中,格式如下:包含该关键词的文档数 关键词 => 文档序号。其中,原索引文件作为命令行参数传入主程序,并设计一个collect函式统计 "关键字<->序号" 结果对,最后在主程序中输出结果至屏幕。
cdays-3-test.txt 内容:
1 key1 2 key2 3 key1 7 key3 8 key2 10 key1 14 key2 19 key4 20 key1 30 key3
(3) 八皇后问题。在8*8的棋盘上,放置8个皇后,使得任两个皇后不在同行同列同正负对角线上。
- 运行结果 attachment:cdays-3-exercise-123.png
1.1.4. CDays-2
(1) 把最后探索出的 cdcGrep()嵌入 pycdc-v0.5.py 实现完成版本的 PyCDC
(2) 编写一个类,实现简单的栈。数据的操作按照先进后出(FILO)的顺序。主要成员函数为put(item),实现数据item插入栈中;get(),实现从栈中取一个数据。
- 运行结果 attachment:cdays+3-exercise-12.png attachment:cdays+3-exercise-3.png
1.1.5. CDays-1
1. 自动判定你自个儿/或是朋友的Blog 是什么编码的?
2. 如果是非utf-8 的,编写小程序自动将指定文章转换成utf-8 编码保存?
- 运行结果 attachment:cdays+3-exercise-12.png
1.1.6. CDays0
(1) 请根据软件发布的流程和软件开发的编码规范,将读者之前章节写的程序修改并发布出去。另外,可以查找下除了epydoc外还有哪些较好的py文档生成器?
- 运行结果 attachment:cdays+3-exercise-12.png
1.1.7. CDays1
(1) 编程实现以下功能并进行最大化的优化:遍历指定目录下的所有文件,找出其中占用空间最大的前3个文件。
(2) 利用ConfigParser,将上述题目中产生的结果按照cdays+1-my.ini格式存储到文件cdays+1-result.txt中。
- 运行结果 attachment:cdays+3-exercise-12.png
1.1.8. CDays2
(1) 如果在Karrigell 实例中,不复制 cdctools.py 到webapp 目录中,也可以令 index.ks 引用到? (2) 经过本章Karrigell的初步学习,实现一个简易的web留言系统。主要利用Karrigell_QuickForm实现提交留言并显示出来。
- 运行结果 attachment:cdays+3-exercise-12.png
1.1.9. CDays3
(1) 熟悉线程相关知识后,利用Lock和RLock实现线程间的简单同步,使得10个线程对同一共享变量进行递增操作,使用加锁机制保证变量结果的正确。
(2) 使用Queue实现多线程间的同步。比如说,十个输入线程从终端输入字符串,另十个输出线程依次获取字符串并输出到屏幕。
(3) Python中的Event是用于线程间的相互通信,主要利用信号量机制。修改题一的程序,利用信号量重新实现多线程对同一共享变量进行递增操作。
- 运行结果 attachment:cdays+3-exercise-12.png
1.2. KDays
1.2.1. KDays0
初次尝试使用Karrigell(下载地址为: http://tinyurl.com/6bpc3f).
- 下载Karrigell解压
- 在终端中进入Karrigell所在目录
- 运行python Karrigell.py, 看看出现什么?
- 再在浏览器中输入localhost, 再看看出现什么?
如果出现意外,尝试根据屏幕提示进行排除,书后详细的参考解说...
- 运行结果 attachment:cdays+3-exercise-12.png
1.2.2. KDays1
- 经过这一节的学习, 熟悉Karrigell.ini基本设置和基本环境部署后(可参考README.txt中), 创建一个自己的站点(mysite), 并依次实现以下功能:
- (0) 将Karrigell 站配置成发布到 localhost:8081 端口来
- (1) 在首页(index.pih)中输出Hello, Karrigell's world! 即在地址栏中输入localhost:8081/mysite出现Hello, Karrigell's world!
- (2) 添加一个登录页(login.pih), 用户输入姓名, 密码后进入首页, 并在首页中输出Hello, 姓名's world!即当输入localhost:8081/mysite后, 首先进行登录, 我们输入姓名和密码后, 转入首页, 其显示Hello, 姓名's world! 具体可参见png/中截图所示.
- 运行结果 attachment:cdays+3-exercise-12.png
1.2.3. KDays2
1.2.4. KDays3
1.2.5. KDays4
1.2.6. KDays5
1.2.7. KDays6
1.2.8. KDaysN