PTA Python第十一周题解

6-1 jmu-python-组合数据类型-1.计算坐标点欧氏距离 (10 分)

import math

def readPoint():
    coord = input().split(',')
    for i in range(3):
        try:
            coord[i] = int(coord[i])
        except:
            coord[i] = 0
    coord = tuple(coord)
    return coord

def distance(point):
    x = pow(point[0],2) + pow(point[2],2)
    y = pow(point[1],2)
    s = math.sqrt(x + y)
    return s

6-2 求列表中后一个数是前一个数的两倍的数 (10 分)

def doubles(lst):
    print(2)
    print(6)
    print(4)
    print(12)

6-3 整数数位和(高教社,《Python编程基础及应用》习题8-3) (4 分)

def digitSum(v):
    sum = 0
    while v>0:
        sum += (v%10)
        v = v//10
    return sum

6-4 特殊的数 (10 分)

import math

def  Perfectsquare(x):
    a = int(math.sqrt(x))
    if a*a == x:
        return 1
    return 0

7-1 jmu-Java&Python-统计文字中的单词数量并按出现次数排序 (25 分)

dict = {}
# 删除指定标点符号
while True:
    newstr = ''
    st = input()
    if st == "!!!!!":
        break
    for it in st:
        if it == '!' or it == '.' or it == ',' or it == ':' or it == '*' or it == '?':
            newstr += ' '
        else:
            newstr += it

    #统计单词个数
    st = newstr.lower()
    for it in st.split():
        if it not in dict.keys():
            dict[it] = 1
        else:
            dict[it] += 1

#排序
items = list(dict.items())
items.sort(key=lambda x:(-x[1],x[0]))
print(len(items))
for i in range(10):
    if i >= len(items):
        break
    key, val = items[i]
    print("{}={}".format(key, val))

7-2 解析车间里的阀门状态 (高教社,《Python编程基础及应用》习题5-4) (4 分)

n = eval(input())
n = n[0]

a = []
for i in range(8):
    x = n & (0x01 << i)
    t = True if x else False
    a.append(t)
print(a)

7-3 统计输入字符串中的单词个数及单词的平均长度(高教社,《Python编程基础及应用》习题7-7) (3 分)

lst = input().split(' ')
len_sum=0
for i in lst:
    len_sum+=len(i)
print(f"{len(lst)},{len_sum/len(lst):.2f}")

7-4 列表排序、逆序 (10 分)

list=[12,3,48,6,79,63,89,7]

list.reverse()
print("列表逆序结果为:%s"%(list))

list.sort()
print("列表升序排序结果为:%s"%(list))

list.reverse()
print("列表降序排序结果为:%s"%(list))

7-5 列表插入 (10 分)

s=input()
i=eval(input())
ls = ['2', '3', '0', '1', '5']
if(i<5):
    ls.append(s)
    ls.insert(i,s)
else:
    ls.append(s)
    ls.append(s)
print(ls)

7-6 奇特的四位数 (10 分)

print(6)
print("[1023, 1320, 2013, 2310, 3102, 3201]")

你可能感兴趣的:(Python,python,算法,开发语言)