浙大版《Python 程序设计》题目集第六章答案(自用)

第6章-1 输入列表,求列表元素和(eval输入应用) (10分)
list1 = eval(input())
print(sum(list1))
第6章-2 一帮一 (15分)
n = int(input())
ans = []
for i in range(n):
    list1 = input().split()
    tmp = list1[0] + list1[1]
    ans.append(tmp)


while ans != []:
    for i in range(len(ans)-1,-1,-1):
       
        if ans[0][0] != ans[i][0]:
            print(ans[0][1:],ans[i][1:])
            ans.pop(i)
            ans.pop(0)
            break
第6章-3 列表或元组的数字元素求和 (20分)
def help1(list1):
    list2 = []
    for i in list1:
        if type(i) == int:
            list2.append(i)
        elif type(i) == list or type(i) == tuple:
            for j in help1(i):
                list2.append(j)
        else:
            pass
    return list2
            
            
    
list1 = eval(input())
print(sum(help1(list1)))
第6章-4 列表数字元素加权和(1) (40分)
def help1(list1, n):
    summ = 0
    for i in list1:
        if type(i) == int or type(i) == float:
            summ += i*n
        if type(i) == list:

            summ += help1(i, n+1)
    return summ

list1 = eval(input())
print(help1(list1,1))

第6章-5 列表元素个数的加权和(1) (40分)
def help1(list1, n):
    summ = 0
    for i in list1:
        if type(i) == int or type(i) == float:
            summ += 1*n
        if type(i) == list:

            summ += help1(i, n+1)
    return summ

list1 = eval(input())
print(help1(list1,1))

第6章-6 求指定层的元素个数 (40分)
def help1(list1, n,m):
    summ = 0

    for i in list1:
        if type(i) == int or type(i) == float :
            if n == m:
                summ += 1
        if type(i) == list:

            summ += help1(i, n+1,m)
    return summ

list1 = eval(input())
m = int(input())
print(help1(list1,1,m))

第6章-7 找出总分最高的学生 (15分)
n = int(input())
list1 = []

maxx_point = 0
maxx_name = ""
maxx_number = ""

for i in range(n):
    tmp1 = input().split()
    tmp1_point = eval(tmp1[2]) + eval(tmp1[3]) + eval(tmp1[4])
    if tmp1_point > maxx_point:
        maxx_point = tmp1_point
        maxx_name = tmp1[1]
        maxx_number = tmp1[0]
        
print(maxx_name, maxx_number,maxx_point)
第6章-8 *6-7 输出全排列 (20分)
def help1(list1,n,list2):
    for i in list1:
        if len(i) == n:
            print(i)
        else:
            tmp = []
            for j in list2:

                if j not in i:
                    str1 = i + j
                    tmp.append(str1)
            
            help1(tmp,n,list2)

list1 = []
list2 = []
n = int(input())
for i in range(1,n+1):
    list1.append(str(i))
    list2.append(str(i))
help1(list1,n,list2)
发布了21 篇原创文章 · 获赞 0 · 访问量 481

猜你喜欢

转载自blog.csdn.net/qq_39901722/article/details/104718717