Python(第十一周)

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

读取若干个点,每个点放入元组。并将所有点的点信息、点的类型、点与原点的距离打印出来。

函数接口定义:

readPoint() #从一行以,分隔的数中读取坐标,放入元组并返回
distance(point) #计算point与原点的距离并返回,要math库中的函数

裁判测试程序样例:

/* 请在这里填写答案 */
n = int(input())
for i in range(n):
    p = readPoint()
    print('Point = {}, type = {}, distance = {:.3f}'.format(p,type(p),distance(p)))

输入格式:
输入n,代表底下要输入n行点坐标。坐标全部为整数。
点坐标x,y,z以,分隔。坐标全部为整数。

注意:坐标以,分隔,相应位置可能无字符或者包含多个空格字符,读入时按照0进行处理。

输出格式:
见输出样例

输入样例:
5
1,1,1
,
2,1
3,1,3
5,
结尾无空行
输出样例:
Point = (1, 1, 1), type = <class ‘tuple’>, distance = 1.732
Point = (0, 0, 0), type = <class ‘tuple’>, distance = 0.000
Point = (2, 0, 1), type = <class ‘tuple’>, distance = 2.236
Point = (3, 1, 3), type = <class ‘tuple’>, distance = 4.359
Point = (5, 0, 0), type = <class ‘tuple’>, distance = 5.000
结尾无空行

import math


def readPoint():
    t = input().split(",")
    for i in range(len(t)):
        try:
            if eval(t[i])>0:
                t[i]=int(t[i])
        except:
            t[i]=0
    return tuple(t)
    return t


def distance(point):
    sum = 0
    for j in range(3):
        sum = sum + point[j] ** 2
    return math.sqrt(sum)

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

本题要求实现一个函数doubles(),带一个整数列表作为输入参数,输出列表中正好是前一个数的两倍的整数,每个数占一行。

函数接口定义:

doubles(lst)

lst是一个整数列表

裁判测试程序样例:

#  请在这里填写答案 

def main():
    mylist = [3, 0, 1, 2, 3, 6, 2, 4, 5, 6, 12]
    doubles(mylist)

main()

输入样例:
无输入数据

结尾无空行
输出样例:
在这里给出相应的输出。例如:

2
6
4
12

def doubles(lst):
    for i in range(len(lst)):
        if lst[i] == lst[i - 1] * 2:
            print(lst[i])

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

编写一个函数,该函数接受一个正整数作为参数,返回该整数的各位数字之和。

函数接口定义:

def digitSum(v)

v为输入整数(正整数); 函数返回一个整数,其值为v的各位数字之和。

裁判测试程序样例:

a = int(input())
print(digitSum(a))

输入样例:
291
结尾无空行
输出样例:
12
结尾无空行

拼尽全力还是不会?参考B站习题讲解

哔哩哔哩up主:海洋饼干叔叔 Python课程 Python习题 简洁的C和C++

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

4.特殊的数 (10 分)

一个特殊的正整数,它加上150后是一个完全平方数,再加上136又是一个完全平方数。从键盘输入 一个整数 n,求符合条件从n开始的最小的一个数。 用函数来实现判断一个数的是否是完全平方数。

函数接口定义:

在这里描述函数接口。例如:
def  Perfectsquare (x )

裁判测试程序样例:

在这里给出函数被调用进行测试的例子。例如:

/* 请在这里填写答案 */


n  = int(input())
while True:
    if Perfectsquare(n+150):        
        if Perfectsquare(n+136+150):                
            print(n)                        
            break                        
    n = n+1

输入样例:
在这里给出一组输入。例如:

10
结尾无空行
输出样例:
在这里给出相应的输出。例如:

75

def Perfectsquare(x):
    if int((x ** 0.5)) ** 2 == x:
        return True
    else:
        return False

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

现在需要统计若干段文字(英文)中的单词数量,并且还需统计每个单词出现的次数。

注1:单词之间以空格(1个或多个空格)为间隔。
注2:忽略空行或者空格行。

基本版:
统计时,区分字母大小写,且不删除指定标点符号。

进阶版:

统计前,需要从文字中删除指定标点符号!.,: *?。 注意:所谓的删除,就是用1个空格替换掉相应字符。
统计单词时需要忽略单词的大小写。
输入说明
若干行英文,最后以!!!为结束。

输出说明
单词数量
出现次数排名前10的单词(次数按照降序排序,如果次数相同,则按照键值的字母升序排序)及出现次数。

输入样例1
failure is probably the fortification in your pole

it is like a peek your wallet as the thief when you
are thinking how to spend several hard-won lepta

when you are wondering whether new money it has laid
background because of you then at the heart of the

most lax alert and most low awareness and left it

godsend failed
!!!
输出样例1
46
the=4
it=3
you=3
and=2
are=2
is=2
most=2
of=2
when=2
your=2
结尾无空行
输入样例2
Failure is probably The fortification in your pole!

It is like a peek your wallet as the thief when You
are thinking how to. spend several hard-won lepta.

when yoU are? wondering whether new money it has laid
background Because of: yOu?, then at the heart of the
Tom say: Who is the best? No one dare to say yes.
most lax alert and! most low awareness and* left it

godsend failed
!!!
结尾无空行
输出样例2
54
the=5
is=3
it=3
you=3
and=2
are=2
most=2
of=2
say=2
to=2

dict = dict()
while True:
    str = ''
    st = input()
    if st == '!!!!!':
        break
    for x in st:
        if x == '!' or x == '.' or x == ',' or x == ':' or x == '*' or x == '?':
            str += ' '
        else:
            str += x

    s = str.lower()
    for i in s.split():
        if i in dict.keys():
            dict[i] += 1
        else:
            dict[i] = 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))

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

CPU通过一个8位IO口读取了1个字节的内容,现在存储在一个bytes对象里,示例: b’\x45’;这8位分 别代表了车间里8个阀门的当前状态,1表示该阀门通,0表示该阀门断。请设计一个程序,从bytes对象解析出8个 阀门的当前状态,True表示通,False表示断。这8个状态应组织在一个列表中,其中,第i个元素对应输入字节的第i 位。

输出格式示例:[True, False, False, True, True,True,False,False]

输入格式:
形如 b’\x45’的单字节bytes。(注意是16进制)

输出格式:
包含8个布尔值的列表。其中,第i个元素代表输入字节的第i位(从低到高分别是0 ~ 7位)。

[True, False, True, False, False, False, True, False]

输入样例:
b’\x01’
结尾无空行
输出样例:
[True, False, False, False, False, False, False, False]
结尾无空行

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

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

编写一个程序,接受用户输入的一行英文句子(假设该句子仅由英文单词及空格构成,不包括逗号等符号),统计并输出该行句子包含的单词个数及单词的平均长度。

输入格式:
单词1 单词2 … 单词n

输出格式:
单词个数,单词平均长度(保留两位小数)

输入样例:
aaa bbb ccccccccccc
结尾无空行
输出样例:
3,5.67
结尾无空行

str = list(input().split(" "))
sum = 0
for x in str:
    sum += len(x)
print('{},{:.2f}'.format(len(str), sum / len(str)))

8.列表排序、逆序 (10 分)

已知列表元素为[12,3,48,6,79,63,89,7],对列表进行逆序输出、升序排序输出、逆序排序输出。

输入格式:

输出格式:
列表逆序结果为:[7, 89, 63, 79, 6, 48, 3, 12] 列表升序排序结果为:[3, 6, 7, 12, 48, 63, 79, 89] 列表降序排序结果为:[89, 79, 63, 48, 12, 7, 6, 3]

输入样例:
在这里给出一组输入。例如:

结尾无空行
输出样例:
在这里给出相应的输出。例如:

列表逆序结果为:[7, 89, 63, 79, 6, 48, 3, 12]
列表升序排序结果为:[3, 6, 7, 12, 48, 63, 79, 89]
列表降序排序结果为:[89, 79, 63, 48, 12, 7, 6, 3]
结尾无空行

l = [12, 3, 48, 6, 79, 63, 89, 7]
print('列表逆序结果为:{}'.format(l[::-1]))
print('列表升序排序结果为:{}'.format(sorted(l, reverse=False)))
print('列表降序排序结果为:{}'.format(sorted(l, reverse=True)))

9.列表插入 (10 分)

输入一个字符串 s 和一个非负整数 i, 列表 ls = [‘2’, ‘3’, ‘0’, ‘1’, ‘5’],在指定的位置 i 和 列表末尾分别插入用户输入的字符串 s。当 i >=5 时,相当于在列表末尾插入两次字符串 s 。

输入格式:
第一行输入一个字符串

第二行输入一个非负整数

输出格式:
插入新数据后的列表

输入样例:
在这里给出一组输入。例如:

a
2
结尾无空行
输出样例:
在这里给出相应的输出。例如:

[‘2’, ‘3’, ‘a’, ‘0’, ‘1’, ‘5’, ‘a’]
结尾无空行

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

猜你喜欢

转载自blog.csdn.net/Anemia_/article/details/121317824