首页 > Python资料 博客日记

【头歌-Python】Python第六章作业

2024-06-01 18:00:06Python资料围观125

文章【头歌-Python】Python第六章作业分享给大家,欢迎收藏Python资料网,专注分享技术知识

第1关:列表的属性与方法

a = int(input())
ls = []
for i in range(a):
    t = input().split(" ")
    if t[0] == 'insert':
        ls.insert(int(t[1]), int(t[2]))
    elif t[0] == 'print':
        print(ls)
    elif t[0] == 'remove':
        ls.remove(int(t[1]))  
    elif t[0] == 'append':
        ls.append(int(t[1]))
    elif t[0] == 'sort':
        ls.sort()
    elif t[0] == 'pop':
        ls.pop()
    elif t[0] == 'reverse':
        ls.reverse()

第2关:推导式与生成器

ls = ['the lord of the rings', 'anaconda', 'legally blonde', 'gone with the wind']
s = input()  

if s == '1':
    print([x ** 3 for x in range(10)])

elif s == '2':
    print([x ** 3 for x in [0, 2, 4, 6, 8]])

elif s == '3':
    print([(x, x ** 3) for x in [1, 3, 5, 7, 9]])

elif s == '4':
    for i in range(4):
        ls[i] = ls[i].capitalize()
    print(ls)

else:
    print('结束程序')

第3关:列表的合并与排序

list1 = list(map(int,input().split()))
list2 = list(map(int,input().split()))

for i in list2:
    list1.append(i)

list1.sort(reverse=True)

print(list1)

第4关:二维列表排序

a = int(input())
b = int(input())

list1 = [('dungeon',7),('winterfell',4),('bran',9),('meelo',6)]
list2 = [[ 'Angle', '0121701100106',99], [ 'Jack', '0121701100107',86], [ 'Tom', '0121701100109',65], [ 'Smith', '0121701100111', 100], ['Bob', '0121701100115',77], ['Lily', '0121701100117', 59]]

if a>=len(list1):
    print(sorted(list1,key = lambda x:x[1]))
else:
    print(sorted(list1,key = lambda x:x[1])[0:a])

if b>=len(list2):
    print(sorted(list2,key = lambda x:x[0]))
    print(sorted(list2,key = lambda x:x[2]))
else:
    print(sorted(list2,key = lambda x:x[0])[0:b])
    print(sorted(list2,key = lambda x:x[2])[0:b])




第5关:动物重量排序

ls = []
while True:
    temp = input() 
    if temp == '': 
        break
    ls.append(temp.split()) 
print(sorted(ls,key=lambda x:float(x[1][:-1]) * 1000 if x[1][-1] == 't' else float(x[1][:-2])))

第6关:身份证号升位

a = input()

if (int(a[6]) == 0 and int(a[7]) >= 5) or int(a[6]) > 0:
    str = a[:6] + '19' + a[6:]
else:
    str = a[:6] + '20' + a[6:]

sum = (int(str[0]) * 7 + int(str[1]) * 9 + int(str[2]) * 10 + int(str[3]) * 5 + int(str[4]) * 8 + int(str[5]) * 4 + int(str[6]) * 2 + int(str[7]) * 1 + int(str[8]) * 6 + int(str[9]) * 3 + int(str[10]) * 7 + int(str[11]) * 9 + int(str[12]) * 10 + int(str[13]) * 5 + int(str[14]) * 8 + int(str[15]) * 4 + int(str[16]) * 2) % 11

if sum == 0:
    str = str + '1'
if sum == 1:
    str = str + '10'
if sum == 2:
    str = str + 'X'
if sum == 3:
    str = str + '9'
if sum == 4:
    str = str + '8'
if sum == 5:
    str = str + '7'
if sum == 6:
    str = str + '6'
if sum == 7:
    str = str + '5'
if sum == 8:
    str = str + '4'
if sum == 9:
    str = str + '3'
if sum == 10:
    str = str + '2'
print(str)

第7关:完美立方数

Cube = int(input())
for i in range(2,Cube+1):
    for a in range(2,i+1):
        for b in range(a,i+1):
            for c in range(b,i+1):
                if pow(i,3)==pow(a,3)+pow(b,3)+pow(c,3):
                    print('Cube = {},Triple = ({},{},{})'.format(i,a,b,c))
           

第8关:约瑟夫环问题

n, k = map(int, input().split(' '))

ls = []
if k < 2 or n < k or n<0 or k<0:
    print('Data Error!')
else:
    for i in range(1, n + 1):
        ls.append(i)
    while len(ls) > k - 1:
        num = 1
        for i in ls:
            if num == k:
                ls.pop(k-1)
                ls = ls[k-1:] + ls[:k-1]
                break
            num = num + 1
    ls.sort()
    print(ls)



第9关:统计英文文件中的单词数

import string
def read_file(file):
    """接收文件名为参数,读取文件中的数据到字符串中,返回这个字符串"""
    with open(file, 'r', encoding='utf-8') as f:
        data = f.read()
    return data
 

def word_list(txt):
    """接收字符串为参数,用空格替换字符串中所有标点符号,根据空格将字符串切分为列表
    返回值为元素为单词的列表"""
    for punctuation in string.punctuation:
        txt = txt.replace(punctuation, ' ')
    return txt.split()



def number_of_words(ls):
    """接收一个以单词为元素的列表为参数,返回列表中单词数量,返回值为整型"""
    return len(ls)


if __name__ == '__main__':
    filename = input()                         
    text = read_file('step10/'+filename)        
    words_list = word_list(text)               
    words_counts = number_of_words(words_list)  
    print('共有{}个单词'.format(words_counts))


版权声明:本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!

标签:

相关文章

本站推荐