赞
踩
第1关 列表的属性与方法
- def main(n):
- ls = []
- for i in range(n):
- lsinput = input().split()
- operate = lsinput[0]
- if operate == 'insert':
- ls.insert(int(lsinput[1]),int(lsinput[2]))
- elif operate == 'remove':
- ls.remove(int(lsinput[1]))
- elif operate == 'append':
- ls.append(int(lsinput[1]))
- elif operate == 'sort':
- ls.sort()
- elif operate == 'pop':
- ls.pop()
- elif operate == 'reverse':
- ls.reverse()
- elif operate == 'print':
- print(ls)
- if __name__ == '__main__':
- N = int(input())
- main(N)
第2关 推导式与生成器
- ls = ['the lord of the rings','anaconda','legally blonde','gone with the wind']
- s = input() # 输入一个字符
- # 当输入为"1"时,输出元素为0-9的3次方的列表 [0, 1, 8, 27, 64, 125, 216, 343, 512, 729]
- if s == '1':
- print([x ** 3 for x in range(10)])
- # 当输入为"2"时,输出元素为0-9中偶数的3次方的列表 [0, 8, 64, 216, 512]
- elif s == '2':
- print([x ** 3 for x in range(10) if x % 2 == 0])
- # 当输入为"3"时,输出元素为元组的列表,元组中元素依次是0-9中的奇数和该数的3次方[(1, 1), (3, 27), (5, 125), (7, 343), (9, 729)]
- elif s == '3':
- print([(x,x ** 3) for x in range(10) if x % 2 == 1])
- # 当输入为"4"时,将ls中每个元素单词首字母大写输出['The lord of the rings', 'Anaconda', 'Legally blonde', 'Gone with the wind']
- elif s == '4':
- print([s.capitalize() for s in ls])
- # 当输入为其他字符时,执行以下语句
- else:
- print("结束程序")
第3关 列表的合并与排序
- lst_a = list(map(int,input().split()))
- lst_b = list(map(int,input().split()))
- lst_ab = lst_a + lst_b
- lst_ab.sort(reverse=True)
- print(lst_ab)
第4关 二维列表排序
- m = int(input())
- n = int(input())
- myList = [('dungeon',7),('winterfell',4),('bran',9),('meelo',6)]
- if m > len(myList):
- m = len(myList)
- print(sorted(myList,key=lambda x:x[1])[:m])
- score = [[ 'Angle', '0121701100106',99],[ 'Jack', '0121701100107',86],[ 'Tom', '0121701100109',65],[ 'Smith', '0121701100111',100],['Bob','0121701100115',77],['Lily','0121701100117',59]]
- if n > len(score):
- n = len(score)
- print(sorted(score,key=lambda x:x[0])[:n])
- print(sorted(score,key=lambda x:x[2])[:n])
第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关 身份证号升位
- n = input()
- list1 = list(n)
- Wi=[7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2]
- if int(list1[6])+int(list1[7]) >= 5:
- list1.insert(6,9)
- list1.insert(6,1)
- else:
- list1.insert(6,0)
- list1.insert(6,2)
- s = 0
- for i in range(17):
- s += int(list1[i]) * int(Wi[i])
- m = s % 11
- list2 = [1,0,'X',9,8,7,6,5,4,3,2]
- list1.append(list2[m])
- for x in list1:
- print(*str(x),end = '')
第7关 完美立方数
- N = int(input())
- for a in range(2,N+1):
- for b in range(2,a):
- for c in range(b,a):
- for d in range(c,a):
- if a**3 == b**3+c**3+d**3:
- print(f'Cube = {a},Triple = ({b},{c},{d})')
第8关 约瑟夫环问题
- def Josephus(n,k):
- ls_n=list(range(1,n+1))
- while len(ls_n)>k-1:
- ls_n=ls_n[k:]+ls_n[:k-1]
- return ls_n
- n,k=map(int,input().split())
- if k>=2 and n>=k:
- print(Josephus(n,k))
- else:
- print('Data Error!')
第9关 统计英文文件中的单词数
- def read_file(file):
- """接收文件名为参数,读取文件中的数据到字符串中,返回这个字符串"""
- with open(file, 'r', encoding='utf-8') as text: # 创建文件对象
- txt =text.read() # 读文件为字符串
- return txt # 返回字符串
-
-
- def word_list(txt):
- """接收字符串为参数,用空格替换字符串中所有标点符号,根据空格将字符串切分为列表
- 返回值为元素为单词的列表"""
- for i in ",.!\'":
- txt = txt.replace(i, ' ')
- return txt.split()
-
-
- def number_of_words(ls):
- """接收一个以单词为元素的列表为参数,返回列表中单词数量,返回值为整型"""
- return len(ls)
-
-
- if __name__ == '__main__':
- filename = input() # 读入文件名
- text = read_file('step10/'+filename) # 读取'step10/'文件夹中用户输入的文件名得到文件内容,存入text
- words_list = word_list(text) # 处理text,得到单词的列表
- words_counts = number_of_words(words_list) #统计单词列表word_list里的单词数
- print(f'共有{words_counts}个单词')
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。