浙大版《Python程序设计》题目集(第五章)

第5章-1 输出星期名缩写

num = eval(input())
abbr = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"]
print(abbr[num - 1])

第5章-2 图的字典表示

num = eval(input())
count = 1
ls = []
vertex = []
len_sum = 0
vertex_sum = 0
edge = 0
while count <= num:
    strs = input()
    ls.append(strs)
    count = count + 1
for str in ls:
    for t in range(len(str)):
        if str[t].isdigit(): 
            if str[t + 1].isdigit():
                s = str[t] + str[t + 1]
                len_sum = len_sum + int(s)
                edge = edge + 1
                continue
            else:
                len_sum = len_sum + int(str[t])
                if not str[t - 1].isdigit():
                    edge = edge + 1
                    continue
                else:
                    continue
        elif str[t].isalpha() and str[t] not in vertex:
            vertex.append(str[t])
            continue
        else:
            continue
vertex_sum = vertex_sum + len(vertex)
print(vertex_sum,edge,len_sum,sep = ' ',end = '')

第5章-3 四则运算(用字典实现)

num1 = eval(input())
operation = input()
num2 = eval(input())
dict = {"+":format(num1 + num2,'.2f'),"-":format(num1 - num2,'.2f'),"*":format(num1 * num2,'.2f'),"/":format(num1 / num2,'.2f') if num2 != 0 else "divided by zero"}
for key in dict.keys():
    if key == operation:
        print(dict[key])

第5章-4 分析活动投票情况

scores = set(map(int,input().split(",")))
for i in range(6,11):
    if i not in scores:
        print(i)
    else:
        continue

第5章-5 统计字符出现次数

strs = input()
look = input()
count = 0
for str in strs:
    if look == str:
        count = count + 1
print(count)

第5章-6 统计工龄

num = eval(input())
ages = list(map(int,input().split(" ")))
dict = {}
for age in ages:
    count = 0
    if age not in dict.keys():
        count = 1
        dict.update({age:count})
        continue
    else:
        count = dict[age] + 1
        dict.update({age:count})
        continue
ls = zip(dict.keys(),dict.values())
ages = sorted(ls,key = lambda ls:ls[0])
for age in ages:
    print(age[0],":",age[1],sep = '')

第5章-7 列表去重

strs = input()
ls = strs[1:-1].split(",")
ls_single = list(set(ls))
ls_single.sort(key = ls.index)
for t in ls_single:
    print(int(t),end = ' ')

第5章-8 能被3,5和7整除的数的个数(用集合实现)

#题目要求方法
a,b = map(int,input().split(" "))
ls = []
for i in range(a,b + 1):
    if i % (3 * 5 * 7) == 0:
        ls.append(i)
res = set(ls)
print(len(res))
#常规方法
a,b = map(int,input().split(" "))
ls = []
for i in range(a,b + 1):
    if i % 3 == 0 and i % 5 == 0 and i % 7 == 0:
        ls.append(i)
print(len(ls))

第5章-9 求矩阵鞍点的个数

import numpy as np
dimension = eval(input())
count = 1
matrix = []
cnt = 0
while count <= dimension:
    rows = list(map(int,input().split(" ")))
    matrix.append(rows)
    count = count + 1
for row in range(len(matrix)):
    row_max = max(matrix[row])
    for col in range(len(matrix[row])):
        if matrix[row][col] == row_max:
            col_min = min(np.array(matrix)[:,col])
            if col_min == matrix[row][col]:
                cnt = cnt + 1
            else:
                break
print(cnt)

第5章-10 两数之和

#方法一(常规)
nums = list(map(int,input().split(",")))
num = eval(input())
dict = {}
count = 0
for i in range(len(nums)):
    for j in range(i + 1,len(nums)):
        if nums[i] + nums[j] == num:
            dict.update({i:j})
            count = count + 1
        else:
            continue
if not count:
    print("no answer")
else:
    for key,value in dict.items():
        print(key,value)
#方法二(一重循环+字典实现)
nums = list(map(int,input().split((","))))
num = eval(input())
i = 0
j = 1
dict = {}
count = 0
while i < len(nums) and j < len(nums):
    if nums[i] + nums [j] == num:
        dict.update({i:j})
        count = count + 1
        break
    else:
        j = j + 1
        if j == len(nums):
            j = 0
            i = i + 1
        else:
            continue
if not count:
    print("no answer")
else:
    for key,value in dict.items():
        print(key,value)

第5章-11 字典合并

str1 = eval(input())
str2 = eval(input())
dict = {}
values = {}
for key,value in str1.items():
    if key not in dict.keys():
        dict[key] = value
        values[key] = value
for key,value in str2.items():
    if key not in dict.keys():
        dict[key] = value
    else:
        dict[key] = value + values[key]
ls = sorted(dict.items(),key = lambda dict:dict[0] if type(dict[0]) == int else ord(dict[0]))
print("{",end = '')
for t in ls:
    if type(t[0]) == int:
        print('{}:{}'.format(*t),end = '')
    elif type(t[0]) == str:
        print('"{}":{}'.format(*t),end = '')
    if t != ls[-1]:
        print(",",end = '')
print("}")
posted @ 2021-10-27 20:46  闲晚  阅读(282)  评论(0)    收藏  举报