《Python核心编程》第二版第八章练习题答案 第一部分

这几天看C++的东西,python这边又落下了,后面会接着补上

8-1 (a)A,C将会被执行;

      (b)A,D,E将会被执行(pass不会影戏E的执行)

      (c)A,B将会被执行

8–2. 循环. 编写一个程序, 让用户输入三个数字: (f)rom, (t)o, 和 (i)ncrement . 以 i
为步长, 从 f 计数到 t , 包括 f 和 t . 例如, 如果输入的是 f == 2, t == 26, i == 4 , 程序
将输出 2, 6, 10, 14, 18, 22, 26.

解答:

 

#!/usr/bin/python
# -*- coding:utf-8 -*- 
#Filename:8-2.py
'''
Created on 2012-8-9

@author: wanglei
'''
import sys
def loopoutput(f,t,i):
    if(f<=t):
        if i<0:
            print "f小于t时,步长i值不能为负数"
            sys.exit()
        while(f<=t):
            print f,
            f +=i
    else:
        if i>0:
            print "f大于t时,步长i值不能为正数"
            sys.exit()
        while(f>=t):
            print f,
            f +=i
f=input("input the from: ")
t=input("input the to: ")
i=input("input the increment: ")
loopoutput(f,t,i)

 

输出:

input the from: 2
input the to: 26
input the increment: 4
2 6 10 14 18 22 26


input the from: 5
input the to: 10
input the increment: -2
f小于t时,步长i值不能为负数

input the from: 100
input the to: 50
input the increment: -5
100 95 90 85 80 75 70 65 60 55 50

input the from: 100
input the to: 50
input the increment: 5
f大于t时,步长i值不能为正数

程序在题目要求的基础上加入了步长可以为负值的从大到小的输出

8–3. range() . 如果我们需要生成下面的这些列表, 分别需要在 range() 内建函数中提
供那些参数?
(a) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
(b) [3, 6, 9, 12, 15, 18]
(c) [-20, 200, 420, 640, 860]

解答:

(a)range(10)
(b)range(0,21,3)
(c)range(-20,880,220)

8–4. 素数. 我们在本章已经给出了一些代码来确定一个数字的最大约数或者它是否是一个
素数. 请把相关代码转换为一个返回值为布尔值的函数,函数名为 isprime() . 如果输入的是一个
素数, 那么返回 True , 否则返回 False .

解答:

#!/usr/bin/python
# -*- coding:utf-8 -*- 
#Filename:8-4.py
'''
Created on 2012-8-10

@author: wanglei
'''
def isprime(number):
    tmp=number/2
    #print tmp
    isprime=True
    while tmp>1:
        if number%tmp==0:
            isprime=False
            print tmp
        tmp -=1
        #print tmp
    return isprime
print isprime(237)

输出:

79
3
False

程序同时输出了它的因子

 

8–5. 约数. 完成一个名为 getfactors() 的函数. 它接受一个整数作为参数, 返回它所有
约数的列表, 包括 1 和它本身

解答:

#!/usr/bin/python
# -*- coding:utf-8 -*- 
#Filename:8-5.py
'''
Created on 2012-8-10

@author: wanglei
'''
def getfactors(number):
    loopnum=1
    alist=[]
    while loopnum<=number:
        if number%loopnum==0:
            #print loopnum
            alist.append(loopnum)
        loopnum +=1
    return alist
alist=getfactors(8)
print alist

输出:

[1,2,4,8]

 

8–6. 素因子分解. 以刚才练习中的 isprime() 和 getfactors() 函数为基础编写一个函
数, 它接受一个整数作为参数, 返回该整数所有素数因子的列表. 这个过程叫做求素因子分解, 它
输出的所有因子之积应该是原来的数字. 注意列表里可能有重复的元素. 例如输入 20 , 返回结果
应该是 [2, 2, 5] .

 

 解答:

#!/usr/bin/python
# -*- coding:utf-8 -*- 
#Filename:8_6_2.py
'''
Created on 2012-8-10

@author: wanglei
'''
import test8_5,test8_4
def getprimefactors(number):#函数返回number的素因子列表
     factorslist=test8_5.getfactors(number)#保存number的所有因子
     #print "flist: ",factorslist
     primefaclist=[]
     for i in factorslist:
         if test8_4.isprime(i):#筛选素因子
             primefaclist.append(i)
     #print 'plist: ',primefaclist
     return primefaclist
def primefactors(number):#函数返回number的素因子分解列表
     result=[]
     tmp=number
     while tmp!=1:
         result.append(getprimefactors(tmp)[0])
         tmp=tmp/getprimefactors(tmp)[0]
         #print 'tmp: ',tmp
         #print 'get...',getprimefactors(tmp)[0]
     return result
print primefactors(20)
print primefactors(120)    

程序输出:

[2, 2, 5]
[2, 2, 2, 3, 5]

程序引入了8-4,8-5中定义的getfactors()方法和isprime()方法,由于import语句对于模块名的限制,此处将这两个文件名分别改为

test8_5,test8_4

 

 

 

 

 

posted on 2012-08-10 16:20  温柔的暴力  阅读(3056)  评论(1编辑  收藏  举报

导航