status

校对

lizzie

完成度100%

TableOfContents

1. 故事练习解答

1.1. CDays

1.1.1. CDays-5

  1. 计算今年是闰年嘛?

       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
  2. 利用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
  3. 找出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 好在哪里? :

   1 # coding : utf-8
   2 
   3 import os
   4 
   5 export = ""
   6 
   7 for root, dirs, files in os.walk('/media/cdrom0'):
   8 
   9   export+="\n %s;%s;%s" % (root,dirs,files)
  10 
  11 open('mycd2.cdc', 'w').write(export)

CDay-4-6.py又更加好在哪里? :

   1 # coding : utf-8
   2 
   3 import os
   4 
   5 export = []
   6 
   7 for root, dirs, files in os.walk('/media/cdrom0'):
   8 
   9     export.append("\n %s;%s;%s" % (root,dirs,files))
  10 
  11 open('mycd2.cdc', 'w').write(''.join(export))

(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. 

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个皇后,使得任两个皇后不在同行同列同正负对角线上。

1.1.4. CDays-2

(1) 把最后探索出的 cdcGrep()嵌入 pycdc-v0.5.py 实现完成版本的 PyCDC

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

1.1.5. CDays-1

1. 自动判定你自个儿/或是朋友的Blog 是什么编码的?

2. 如果是非utf-8 的,编写小程序自动将指定文章转换成utf-8 编码保存?

1.1.6. CDays0

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

1.1.7. CDays1

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

(2) 利用ConfigParser,将上述题目中产生的结果按照cdays+1-my.ini格式存储到文件cdays+1-result.txt中。

1.1.8. CDays2

(1) 如果在Karrigell 实例中,不复制 cdctools.py 到webapp 目录中,也可以令 index.ks 引用到? (2) 经过本章Karrigell的初步学习,实现一个简易的web留言系统。主要利用Karrigell_QuickForm实现提交留言并显示出来。

1.1.9. CDays3

(1) 熟悉线程相关知识后,利用Lock和RLock实现线程间的简单同步,使得10个线程对同一共享变量进行递增操作,使用加锁机制保证变量结果的正确。

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

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

1.2. KDays

1.2.1. KDays0

初次尝试使用Karrigell(下载地址为: http://tinyurl.com/6bpc3f).

如果出现意外,尝试根据屏幕提示进行排除,书后详细的参考解说...

1.2.2. KDays1

1.2.3. KDays2

1.2.4. KDays3

1.2.5. KDays4

1.2.6. KDays5

1.2.7. KDays6

1.2.8. KDaysN