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

第3章-1 大于身高的平均值

height = list(map(int,input().split(" ")))
sum = 0
for i in height:
    sum = sum + i
avg = sum / len(height)
for i in height:
    if i > avg:
        print(i,end = " ")

第3章-2 查验身份证

num = eval(input())
i = 1
ls = []
count = 0
weight = [7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2]
M = ['1','0','X','9','8','7','6','5','4','3','2']
add_weight = 0
error = []
while i <= num:
    str = input()
    ls.append(str)
    i = i + 1
for k in range(len(ls)):
    if ls[k][:-1].isdigit():
        for t in range(len(ls[k]) - 1):
            add_weight = add_weight + int(ls[k][t])*weight[t]
        res = add_weight % 11
        if M[res] == ls[k][-1]:
            continue
        else:
            error.append(ls[k])
    else:
        error.append(ls[k])
        count = count + 1
if count:
    for m in error:
        print(m)
else:
    print("All passed")

第3章-3 输出字母在字符串中位置索引

str = input()
a,b = input().split(" ")
index_list = []
char_list = []
for i in range(len(str)):
    if str[i] == a or str[i] == b:
        char_list.append(str[i])
        index_list.append(i)
list = list(zip(index_list[::-1],char_list[::-1]))
for j in range(len(list)):
    print(list[j][0],list[j][1])

第3章-4 查找指定字符

look = input()
str = input()
index = 0
flag = False
for i in range(len(str)):
    if str[i] == look:
        index = i
        flag = True
if not flag:
    print("Not Found")
else:
    print("index = ",index,sep = "")

第3章-5 字符转换

str = input()
res = ""
for i in range(len(str)):
    if str[i] <= '9' and str[i] >= '0':
        res = res + str[i]
print(int(res))

第3章-6 求整数序列中出现次数最多的数

values = list(map(int,input().split(" ")))
num = values[0]
dict = {}
for i in values[1::]:
    if i in dict.keys():
        dict[i] = dict[i] + 1
    else:
        dict[i] = 1
max_values = max(dict.values())
for key,value in dict.items():
    if value == max_values:
        print(key,value)

第3章-7 求最大值及其下标

num = eval(input())
count = 1
values = []
dict = {}
values = list(map(int,input().split(" ")))
for i in range(len(values)):
    if not (values[i] in dict.keys()):
        dict.update({values[i]:i})
max_keys = max(dict.keys())
for key,value in dict.items():
    if key == max_keys:
        print(key,value)

第3章-8 字符串逆序

str = input()
print(str[::-1])

第3章-9 字符串转换成十进制整数

while True:
    strs = input()
    if strs[-1] == "#":
        strs = strs[:-1]
        break
ls = []
flag = False
for str in strs:
    if str == "-":
        flag = True
    ls.append(str)
ls_select = []
strs_select = ""
for i in ls:
    if (i <= 'f' and i >= 'a') or (i <= 'F' and i >= 'A') or (i <= '9' and i >= '0'):
        ls_select.append(i)
for t in ls_select:
    strs_select = strs_select + t
if len(strs_select) != 0 and flag == True:
    print("-",int(strs_select,16),sep = '')
else:
    print(int(strs_select,16))

第3章-10 统计大写辅音字母

str = input()
count = 0
for i in str:
    if i != 'A' and i != 'E' and i != 'I' and i != 'O' and i != 'U' and i <= 'Z' and i >= 'A':
        count = count + 1
print(count)

第3章-11 字符串排序

strs = list(input().split(" "))
#冒泡排序算法实现
import operator
for i in range(len(strs)):
    for j in range(i+1,len(strs)):
        if operator.lt(strs[j],strs[i]):
            t = strs[j]
            strs[j] = strs[i]
            strs[i] = t
print("After sorted:")
for str in strs:
    print(str)

第3章-12 求整数的位数及各位数字之和

num = input()
sum = 0
len = len(num)
for i in num:
    sum = sum + int(i)
print(len,sum,sep = ' ')

第3章-13 字符串替换

strs = input()
ls = []
for str in strs:
    ls.append(str)
for i in range(len(ls)):
    if ls[i] >= 'A' and ls[i] <='Z':
        ls[i] = chr(155-ord(ls[i]))
for k in ls:
    print(k,end = '')

第3章-14 字符串字母大小写转换

while True:
    strs = input()
    if strs[-1] == "#":
        strs = strs[:-1]
        break
ls = []
for str in strs:
    ls.append(str)
for i in range(len(ls)):
    if ls[i] >= 'a' and ls[i] <= 'z':
        ls[i] = ls[i].upper()
    elif ls[i] >= 'A' and ls[i] <= 'Z':
        ls[i] = ls[i].lower()
for k in ls:
    print(k,end = '')

第3章-15 统计一行文本的单词个数

strs = input()
count = 1
i = 0
while i < len(strs):
    if strs[i] != ' ':
        i = i + 1
    else:
        if strs[i - 1] != ' ' and i != 0:
            count = count + 1
            i = i + 1
        else:
            i = i + 1
print(count)

第3章-16 删除重复字符

strs = input()
ls = []
ls_del = []
for str in strs:
    ls.append(str)
for i in range(len(ls)):
    if ls[i] not in ls_del:
        ls_del.append(ls[i])
ls_sort = sorted(ls_del,reverse = False)
for i in ls_sort:
    print(i,end = '')

第3章-17 删除字符

strs = input().strip()
ch = input().strip()
ls = []
i = 0
for str in strs:
    ls.append(str)
length = len(ls)
while i < length:
    if (ord(ls[i]) == ord(ch)) or ((ord(ls[i]) + 32) == ord(ch)) or ((ord(ls[i]) - 32) == ord(ch)):
        ls.pop(i)
        length = length - 1
    else:
        i = i + 1
res = ""
for k in ls:
    res = res + k
print("result:",res)

第3章-18 输出10个不重复的英文字母

strs = input()
ls = []
dict = {}
count = 0
num = 0
for str in strs:
    ls.append(str)
for i in ls:
    if dict:
        if i <= 'z' and i >= 'a':
            for j in list(dict.keys()):
                if ord(i) != ord(j) + 32 and ord(i) != ord(j):
                    num = num + 1
                    if num == len(list(dict.keys())):
                        dict.update({i:"true"})
                        count = count + 1
                        num = 0
                else:
                    num = 0
                    break
        elif i <= 'Z' and i >= 'A':
            for j in list(dict.keys()):
                if ord(i) != ord(j) and ord(i) != ord(j) -32:
                    num = num + 1
                    if num == len(list(dict.keys())):
                        dict.update({i:"true"})
                        count = count + 1
                        num = 0
                else:
                    num = 0
                    break
    else:
        dict.update({i:"true"})
        count = count + 1
if count < 10:
    print("not found")
else:
    for key in dict.keys():
        print(key,end = '')

第3章-19 找最长的字符串

num = eval(input())
count = 1
ls = []
while count <= num:
    str = input()
    ls.append(str)
    count = count + 1
max_len = len(ls[0])
for i in ls:
    if len(i) > max_len:
        max_len = len(i)
        max = i
print("The longest is",max)

第3章-20 逆序的三位数

strs = input()
ls = []
i = 0
for str in strs:
    ls.append(str)
while i < len(ls):
    if int(ls[-1]) != 0:
        for j in ls[::-1]:
            print(j,end = '')
        i = len(ls)
    else:
        if int(ls[-2]) == 0:
            print(ls[0])
            break
        else:
            print(ls[1],ls[0],sep = '')
            break

第3章-21 判断回文字符串

strs = input()
ls = []
count = 1
for str in strs:
    ls.append(str)
for i in range(len(ls) // 2):
    if ls[i] == ls[len(ls) - 1 -i]:
        count = count + 1
if count > len(ls) // 2:
    print("Yes")
else:
    print("No")

第3章-22 输出大写英文字母

strs = input()
ls = []
dict = {}
for str in strs:
    ls.append(str)
for i in range(len(ls)):
	if ls[i] <= 'Z' and ls[i] >= 'A' and i != 0:
		if not ls[i] in dict.keys():
			dict.update({ls[i]:"true"})
		else:
			continue

	elif ls[i] <= 'Z' and ls[i] >= 'A' and i == 0:
		dict.update({ls[i]:"true"})
	else:
		continue
if not len(dict):
	print("Not Found")
else:
	for key in dict.keys():
		print(key,end = '')
posted @ 2021-10-25 22:17  闲晚  阅读(328)  评论(0编辑  收藏  举报