现在的位置: 首页 > 综合 > 正文

《Python核心编程》第二版第209页第八章练习 续三 -Python核心编程答案-自己做的-

2012年12月11日 ⁄ 综合 ⁄ 共 3224字 ⁄ 字号 评论关闭

本博客列出的答案不是来自官方资源,是我自己做的练习,可能有误。

8-11.文本处理。要求输入一个姓名列表,输入格式是“Last Name, First Name”即姓逗号名。编写程序处理输入,如果用户输入错误,比如“Last Name First Name,”,请纠正这些错误,并通知用户。同时你还需要记录输入错误次数。当用户输入结束后,给列表排序,然后以“姓,名”的顺序显示。
输入输出示例(你不需要完全按照这里的例子完成):
% nametrack.py
Enter total number of names: 5

Please enter name 0: Smith, Joe
Please enter name 1: Marry Wong
>> Wrong format ... should be Last, First.
>> You have done this 1 time(s) already, Fixing input ...
Please enter name 2: Hamilton, Gerald
Please enter name 3: Royce, Linda
Please enter name 4: Winston Salem
>> Wrong format ... should be Last, First.
>> You have done this 2 time(s) already, Fixing input ...

The sorted list(by last name) is:
    Hamilton, Gerald
    Royce, Linda
    Salem, Winston
    Smith, Joe
    Wong, Marry
【答案】
代码如下:

def fixing(name):
    name_temp = name.split()
    return name_temp[-1] + ", " + name_temp[-2]

def verify(name):
    if "," in name: return True
    else: return False
     
total_number = int(raw_input("Enter total number of names: "))
name_list = []
error_times = 0
notice1 = "Please enter name "
noticea = "Wrong format ... should be Last, First."
notice2 = "You have done this "
notice3 = " time(s) already, Fixing input ..."
for i in range(total_number):
    name = raw_input(notice1 + str(i) + ": ")
    if verify(name):
        name_list.append(name)
    else:
        error_times += 1
        print noticea
        print notice2 + str(error_times) + notice3
        name_list.append(fixing(name))

print "\n The sorted list(by last name) is:"
name_list.sort()
for i in range(len(name_list)): print name_list[i]

 

8-12.(整型)位操作。编写一个程序,用户给出起始和结束数字后给出一个下面这样的表格,分别显示出两个数字间所有整型的十进制,二进制,八进制和十六进制表示。如果字符是可打印的ASCII字符,也要把它打印出来,如果没有一个是可打印字符,就省略掉ASCII那一栏的表头。请参考下面的输出格式:
输出示例1
------------
输入起始值:9
输入结束值:18

DEC    BIN    OCT    HEX
----------------------------
9    01001    11    9
10    01010    12    a
11    01011    13    b
12    01100    14    c
13    01101    15    d
14    01110    16    e
15    01111    17    f
16    10000    20    10
17    10001    21    11
18    10010    22    12

输出示例2
------------
输入起始值:26
输入结束值:41

DEC    BIN    OCT    HEX    ASCII
-------------------------------------
26    011010    32    1a
27    011011    33    1b
28    011100    34    1c
29    011101    35    1d
30    011110    36    1e
31    011111    37    1f
32    100000    40    20
34    100010    42    22    "
35    100011    43    23    #
36    100100    44    24    $
37    100101    45    25    %
38    100110    46    26    &
39    100111    47    27    '
40    101000    50    28    (
41    101001    51    29    )

【答案】
一个不完善的代码如下:

def to_bin(i):
    a = str(bin(i))
    return a[0] + a[2:]
    
def to_oct(i):
    a = str(oct(i))
    return a[1:]

def to_hex(i):
    a = str(hex(i))
    return a[2:]

begin_number = int(raw_input("Please input the begin number: ... "))
end_number = int(raw_input("Please input the ending number ... "))
print "The beginning number is: ", begin_number
print "The ending number is: ", end_number

print "DEC", "\tBIN", "\tOCT", "\tHEX"
print "--------------------------------"
i = begin_number
while i <= end_number:
    print i, "\t", to_bin(i), "\t", to_oct(i), "\t", to_hex(i)
    i += 1

【执行结果】

Please input the begin number: ... 9
Please input the ending number ... 18
The beginning number is:  9
The ending number is:  18
DEC     BIN     OCT     HEX
--------------------------------
9      01001     11     9
10     01010     12     a
11     01011     13     b
12     01100     14     c
13     01101     15     d
14     01110     16     e
15     01111     17     f
16     010000     20     10
17     010001     21     11
18     010010     22     12

【未完】
这里仅仅部分实现了程序的要求。相比原题,还有一定差距,目前感觉有点难度,暂时押后。

【参考】
python 常用转换函数
http://blog.chinaunix.net/space.php?uid=16362696&do=blog&id=2746671

 

8-13.程序执行性能。在8.5.2节里,我们介绍了两种基本的迭代序列方法:(1)通过序列项,以及(2)通过序列索引遍历。该小节的末尾我们指出后一种方法在序列很长的时候性能不佳(在我的系统下,性能相差了将近两倍[83%])你认为它的原因是什么?
【注】这里的“在8.5.2节里”,应该是指8.6.2节,书的第195页。
【未完】感觉是这样,但具体怎么科学的解释,目前感觉有点难度,暂时押后。

抱歉!评论已关闭.