考生文件夹下存在一个文件PY103.py,请写代码替换横线,不修改其他代码,实现以下功能:
在一组单词中,查找出所有长度最长的单词,如果给定的一组单词
是: “cad”, “VB”,”Python”,”MATLAB” “hello”, “world”则输出结果为:
the longest words are:
Python
MATLAB
参考答案
- def proc(strings):
- m = 0
- lst = []
- for i in range(len(strings)):
- if len(strings[i]) > m:
- m = len(strings[i])
- for i in range(len(strings)):
- if len(strings[i]) == m:
- lst.append(strings[i])
- return lst
- strings = ['cad' ,'VB', 'Python', 'MATLAB', 'hello', 'world']
- result = proc(strings)
- print("the longest words are:")
- for item in result:
- print("{: >25}".format(item))