当前位置:   article > 正文

【头歌-Python】Python第六章作业_头歌python第六章答案

头歌python第六章答案

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

  1. a = int(input())
  2. ls = []
  3. for i in range(a):
  4. t = input().split(" ")
  5. if t[0] == 'insert':
  6. ls.insert(int(t[1]), int(t[2]))
  7. elif t[0] == 'print':
  8. print(ls)
  9. elif t[0] == 'remove':
  10. ls.remove(int(t[1]))
  11. elif t[0] == 'append':
  12. ls.append(int(t[1]))
  13. elif t[0] == 'sort':
  14. ls.sort()
  15. elif t[0] == 'pop':
  16. ls.pop()
  17. elif t[0] == 'reverse':
  18. ls.reverse()

第2关:推导式与生成器

  1. ls = ['the lord of the rings', 'anaconda', 'legally blonde', 'gone with the wind']
  2. s = input()
  3. if s == '1':
  4. print([x ** 3 for x in range(10)])
  5. elif s == '2':
  6. print([x ** 3 for x in [0, 2, 4, 6, 8]])
  7. elif s == '3':
  8. print([(x, x ** 3) for x in [1, 3, 5, 7, 9]])
  9. elif s == '4':
  10. for i in range(4):
  11. ls[i] = ls[i].capitalize()
  12. print(ls)
  13. else:
  14. print('结束程序')

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

  1. list1 = list(map(int,input().split()))
  2. list2 = list(map(int,input().split()))
  3. for i in list2:
  4. list1.append(i)
  5. list1.sort(reverse=True)
  6. print(list1)

第4关:二维列表排序

  1. a = int(input())
  2. b = int(input())
  3. list1 = [('dungeon',7),('winterfell',4),('bran',9),('meelo',6)]
  4. list2 = [[ 'Angle', '0121701100106',99], [ 'Jack', '0121701100107',86], [ 'Tom', '0121701100109',65], [ 'Smith', '0121701100111', 100], ['Bob', '0121701100115',77], ['Lily', '0121701100117', 59]]
  5. if a>=len(list1):
  6. print(sorted(list1,key = lambda x:x[1]))
  7. else:
  8. print(sorted(list1,key = lambda x:x[1])[0:a])
  9. if b>=len(list2):
  10. print(sorted(list2,key = lambda x:x[0]))
  11. print(sorted(list2,key = lambda x:x[2]))
  12. else:
  13. print(sorted(list2,key = lambda x:x[0])[0:b])
  14. print(sorted(list2,key = lambda x:x[2])[0:b])

第5关:动物重量排序

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

第6关:身份证号升位

  1. a = input()
  2. if (int(a[6]) == 0 and int(a[7]) >= 5) or int(a[6]) > 0:
  3. str = a[:6] + '19' + a[6:]
  4. else:
  5. str = a[:6] + '20' + a[6:]
  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
  7. if sum == 0:
  8. str = str + '1'
  9. if sum == 1:
  10. str = str + '10'
  11. if sum == 2:
  12. str = str + 'X'
  13. if sum == 3:
  14. str = str + '9'
  15. if sum == 4:
  16. str = str + '8'
  17. if sum == 5:
  18. str = str + '7'
  19. if sum == 6:
  20. str = str + '6'
  21. if sum == 7:
  22. str = str + '5'
  23. if sum == 8:
  24. str = str + '4'
  25. if sum == 9:
  26. str = str + '3'
  27. if sum == 10:
  28. str = str + '2'
  29. print(str)

第7关:完美立方数

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

第8关:约瑟夫环问题

  1. n, k = map(int, input().split(' '))
  2. ls = []
  3. if k < 2 or n < k or n<0 or k<0:
  4. print('Data Error!')
  5. else:
  6. for i in range(1, n + 1):
  7. ls.append(i)
  8. while len(ls) > k - 1:
  9. num = 1
  10. for i in ls:
  11. if num == k:
  12. ls.pop(k-1)
  13. ls = ls[k-1:] + ls[:k-1]
  14. break
  15. num = num + 1
  16. ls.sort()
  17. print(ls)

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

  1. import string
  2. def read_file(file):
  3. """接收文件名为参数,读取文件中的数据到字符串中,返回这个字符串"""
  4. with open(file, 'r', encoding='utf-8') as f:
  5. data = f.read()
  6. return data
  7. def word_list(txt):
  8. """接收字符串为参数,用空格替换字符串中所有标点符号,根据空格将字符串切分为列表
  9. 返回值为元素为单词的列表"""
  10. for punctuation in string.punctuation:
  11. txt = txt.replace(punctuation, ' ')
  12. return txt.split()
  13. def number_of_words(ls):
  14. """接收一个以单词为元素的列表为参数,返回列表中单词数量,返回值为整型"""
  15. return len(ls)
  16. if __name__ == '__main__':
  17. filename = input()
  18. text = read_file('step10/'+filename)
  19. words_list = word_list(text)
  20. words_counts = number_of_words(words_list)
  21. print('共有{}个单词'.format(words_counts))

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/614371
推荐阅读
相关标签
  

闽ICP备14008679号