《Python核心编程》答案 第8章

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了《Python核心编程》答案 第8章相关的知识,希望对你有一定的参考价值。

8-1:

(a):C, E

(b):D, E

(c):B, E

8-2:

#!/usr/bin/env python

print Input three numbers to f, t, i:
f = int(raw_input(f: ))
t = int(raw_input(t: ))
i = int(raw_input(i: ))
for item in range(f, t+1, i):
    print item,

8-3:

(a): range(10)

(b): range(3, 19, 3)

(c): range(-20, 861, 220)

8-4:

def isprime(num):
    count = num / 2
    while count > 1:
        if num % count == 0:
            return False
        count -= 1
    if num == 1:
        return False
    return True

8-5:

def getfactors(num):
    ans = []
    for i in range(1, num+1):
        if num % i == 0:
            ans.append(i)
    return ans

8-6:

def isprime(num):
    count = num / 2
    while count > 1:
        if num % count == 0:
            return False
        count -= 1
    if num == 1:
        return False
    return True

def getfactors(num):
    ans = []
    for i in range(1, num+1):
        if num % i == 0:
            ans.append(i)
    return ans

def primeFactorization(num):
    factors = getfactors(num)
    primefactor = [item for item in factors if isprime(item)]
    ans = []
    for i in primefactor:
        while num % i == 0:
            ans.append(i)
            num = num / i
    return ans

8-7:

def getfactors(num):
    ans = []
    for i in range(1, num+1):
        if num % i == 0:
            ans.append(i)
    return ans

def isperfect(num):
    factorsum = sum(getfactors(num)[:-1])
    if factorsum == num:
        return 1
    else:
        return 0

8-8:

def factorial(n):
    ans = 1
    for item in range(1, n+1):
        ans *= item
    return ans

8-9:

def fibonacci(n):    #递归方法
    if n == 1 or n == 2:
        return 1
    return fibonacci(n-2) + fibonacci(n-1)

def fibonacci2(n):    #非递归方法
    a, b = 1, 1
    if n == 1 or n == 2:
        return 1
    for x in range(n-2):
        a, b = b, a + b
    return b

8-10:稍后更新

8-11:

num = int(raw_input(Enter total number of names: ))
print
names = {}
wrongnum = 0
for item in range(num):
    name = raw_input(Please enter name %d:  % item)
    if , not in name:
        wrongnum += 1
        print "Wrong format... should be Last, First"
        print "You have done this %d time(s) already. Fixing input..." % wrongnum
        ln, fn = name.split()
        names[fn] = ln
    else:
        ln, fn = name.split(,)
        fn = fn.strip()
        names[ln] = fn
print
print "The sorted list (by last name) is:"
for lastname in sorted(names):
    print %s, %s % (lastname, names[lastname])

8-12:

 1 def mybin(num):
 2     ans = ‘‘
 3     while num > 0:
 4         b = str(num % 2)
 5         ans = b + ans
 6         num = num / 2
 7     if len(ans) < 5:
 8         ans = 0*(5-len(ans)) + ans
 9     return ans
10 
11 def myoct(num):
12     ans = ‘‘
13     while num > 0:
14         b = str(num % 8)
15         ans = b + ans
16         num = num / 8
17     return ans
18 
19 def myhex(num):
20     ans = ‘‘
21     adict = {0: 0, 1:1, 2:2, 3:3, 4:4, 5:5, 6:6, 7:7, 8:8, 9:9, 10:a, 11:b, 12:c, 13:d, 14:e, 15:f}
22     while num > 0:
23         b = adict[num % 16]
24         ans = b + ans
25         num = num / 16
26     return ans
27 
28 begin = int(raw_input(Enter begin value: ))
29 end = int(raw_input(Enter end value: ))
30 if 31 < end < 127:
31     print "%5s\t%5s\t%5s\t%5s\t%5s" % ("DEC", "BIN", "OCT", "HEX", "ACSII")
32     print - * 40
33     for i in range(begin, (end+1)):
34         if i < 32:
35             c = ‘‘
36         else:
37             c = chr(i)
38         print "%5d\t%5s\t%5s\t%5s\t%5s" % (i, mybin(i), myoct(i), myhex(i), c)
39 else:
40     print "%5s\t%5s\t%5s\t%5s" % ("DEC", "BIN", "OCT", "HEX")
41     print - * 32
42     for i in range(begin, (end+1)):
43         print "%5d\t%5s\t%5s\t%5s" % (i, mybin(i), myoct(i), myhex(i))

8-13:稍后更新

以上是关于《Python核心编程》答案 第8章的主要内容,如果未能解决你的问题,请参考以下文章

《Python核心编程》答案 第9章

Python核心编程第二版 第十二章课后答案

python编程快速上手之第8章实践项目参考答案

Python核心编程第二版 第六章课后练习

《简明Python编程》核心笔记(1~5章)

python编程快速上手之第8章实践项目参考答案(8.9.2)