python核心编程v2.0 第7章习题答案(下)

8.

# encoding=utf-8

if __name__ == '__main__':
    i = 0
    db = {}
    while i<2:
        name = raw_input('name:\n')
        id = raw_input('id:\n')
        db[id]=name
        i= i+1

    # #sort with key
    for eachkey in sorted(db):
        print 'id: %s' % eachkey
        print 'name:%s' % db[eachkey]

    #sort with value
    #return tuple type 
    for eachkey in sorted(db.items(),key=lambda d:d[1]):
        print 'name:%s' % eachkey[1]
        print 'id:%s' % eachkey[0]


9.
a)理解成了字符串adc-mno的那种翻译,如果是a-m,b-n使用zip建立字典替换即可

# encoding=utf-8
def tr(srcstr,dststr,string):
    if srcstr in string:
        string = string.replace(srcstr,dststr)
    else:
        print 'string dont have the srcstr'
    return string

if __name__ == '__main__':
    srcstr = 'abc'
    dststr = 'mno'
    string = 'abcdef'
    print tr(srcstr,dststr,string)

b)在a的基础上修改

# encoding=utf-8
def tr(srcstr,dststr,string,index):
    if index == '1':
        srcstr_u = srcstr.upper()
        string_u = string.upper()
        if srcstr_u in string_u:
            #找到目标字符串位置
            t = string_u.find(srcstr_u)
            #剪切字符串
            string = string[:t]+dststr+string[t+len(srcstr):]


    else:
        if srcstr in string:
            string = string.replace(srcstr,dststr)
        else:
            print 'string dont have the srcstr'
    return string

if __name__ == '__main__':
    srcstr = 'cde'
    dststr = 'mno'
    string = 'AbcdEf'

    #1 大小写不区分
    index = raw_input('1/0')
    print tr(srcstr,dststr,string,index)

c)做a-m,b-n类型的映射

# encoding=utf-8
def tr(srcstr,dststr,string,index):
    if index == '1':
        srcstr_u = srcstr.upper()
        string_u = string.upper()
        dic = dict(zip(srcstr_u,dststr))
        for i in range(len(string_u)):
            for j in dic:
                if j == string_u[i]:
                    string = string[:i] + dic[j] + string[i + 1:]


    else:
        dic = dict(zip(srcstr,dststr))
        for i in range(len(string)):
            for j in dic:
                if j == string[i]:
                    string = string[:i]+dic[j]+string[i+1:]
    return string

if __name__ == '__main__':
    srcstr = 'cdef'
    dststr = 'mno'
    string = 'AbcdEf'

    #1 大小写不区分
    index = raw_input('1/0')
    print tr(srcstr,dststr,string,index)

10.
a)

import string
alpha = string.letters
alpha_x = alpha[:26]
alpha_d = alpha[26:]
#避免后面部分下标超出范围
alpha_x = alpha_x*2
alpha_d = alpha_d*2

while True:
    st = raw_input('input a string')
    st_m = ''

    for i in range(len(st)):
        if st[i] in alpha_x:
            index = alpha_x.find(st[i])
            st_m = st_m+alpha_x[index+13]
        else:
            index = alpha_d.find(st[i])
            st_m = st_m+alpha_d[index+13]
    print st_m

b)加上了对句子中空格的处理

import string
alpha = string.letters
alpha_x = alpha[:26]
alpha_d = alpha[26:]
alpha_x = alpha_x*2
alpha_d = alpha_d*2

while True:
    st = raw_input('Enter string to rot13 ')
    print 'your string to en/decrypt was: %s' % st
    st_m = ''

    for i in range(len(st)):
        if st[i] in alpha_x:
            index = alpha_x.find(st[i])
            st_m = st_m+alpha_x[index+13]
        elif st[i]==' ':
            st_m = st_m + ' '
        else:
            index = alpha_d.find(st[i])
            st_m = st_m+alpha_d[index+13]
    print 'The rot13 string is %s' % st_m

结果:

Enter string to rot13 
This is a short sentence
your string to en/decrypt was:     This is a short sentence
The rot13 string is :     Guvf vf n fubeg fragrapr
Enter string to rot13 
Guvf vf n fubeg fragrapr
your string to en/decrypt was:     Guvf vf n fubeg fragrapr
The rot13 string is :     This is a short sentence
Enter string to rot13 

11.可哈希的对象才能作为字典的键,字符串,数字可作为键,list,dic可变类型不可作为键。当元组中只有不可变参数时,可作为键

12.
a)数学上,set 为一个或多个确定的元素所构成的整体。
b)python中,集合对象是一组无序排列的可哈希的值

13.

import random

A = set('')
B = set('')

i = 1
while i<=10:
    n = random.randint(0,9)
    A.add(n)
    m = random.randint(0,9)
    B.add(m)
    i = i+1

print A,B
print A|B,A&B

14.

import random

A = set('')
B = set('')

i = 1
while i<=10:
    n = random.randint(0,9)
    A.add(n)
    m = random.randint(0,9)
    B.add(m)
    i = i+1

print A,B
# print A|B,A&B
i = 0
while i <3:
    an = list(raw_input('A&B = '))
    for j in range(len(an)):
        an[j] = int(an[j])
    an = set(an)
    print an
    print A&B
    if an == A&B :
        print 'right'
        break
    else:
        print 'wrong'
    i = i+1
else:
    print 'right answer is ',A&B

附加题

import random

while True:
    A = set('')
    B = set('')
    i = 1
    while i<10:
        n = random.randint(0,9)
        A.add(n)
        i = i+1

    i = 1
    while i<5:
        m = random.randint(0,9)
        B.add(m)
        i = i+1

    print A,B
    user = raw_input('is a subset?Y/N')
    index = A.issuperset(B)
    if (index and user == 'Y') or (not index and user == 'N'):
        print 'right'
    else:
        print 'wrong'

15.in not in 是测试元素是否属于集合的。只写了两个,其余类似。要实现自定义的运算,只需要更改if从句中的语句

import random

while True:

    an = list(raw_input('A '))
    bn = list(raw_input('B'))

    for j in range(len(an)):
        an[j] = int(an[j])
    an = set(an)

    for i in range(len(bn)):
        bn[i] = int(bn[i])
    bn = set(bn)


    index = raw_input('operator:')

    if index == '!=':
        ans = an != bn
        print ans
    elif index == '<':
        ans = an < bn
        print ans
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值