全国计算机二级Python-第11套-基本操作-43

考生文件夹下存在一个文件PY103.py,请写代码替换横线,不修改其他代码,实现以下功能:
在一组单词中,查找出所有长度最长的单词,如果给定的一组单词
是: “cad”, “VB”,”Python”,”MATLAB” “hello”, “world”则输出结果为:
the longest words are:
Python
MATLAB

 

参考答案

  1.  
  2. def proc(strings):
  3.  
  4. m = 0
  5.  
  6. lst = []
  7.  
  8. for i in range(len(strings)):
  9.  
  10. if len(strings[i]) > m:
  11.  
  12. m = len(strings[i])
  13.  
  14. for i in range(len(strings)):
  15.  
  16. if len(strings[i]) == m:
  17.  
  18. lst.append(strings[i])
  19.  
  20. return lst
  21.  
  22.  
  23.  
  24. strings = ['cad' ,'VB', 'Python', 'MATLAB', 'hello', 'world']
  25.  
  26. result = proc(strings)
  27.  
  28. print("the longest words are:")
  29.  
  30. for item in result:
  31.  
  32. print("{: >25}".format(item))
  33.  
历年真题

全国计算机二级Python-第11套-基本操作-42

2024-4-19 14:53:27

历年真题

全国计算机二级Python第11套-简单应用-44

2024-4-22 8:59:32

个人中心
购物车
优惠劵
今日签到
搜索