# 2.给定由若干单词组成的字符串,单词前后用一些空格字符隔开,返回字符串中最长单词的长度# s = "fly me to the moon"# 结果为:4deflength_longest_word(s):
words = s.split()# 以空格切分
max_length =0for word in words:iflen(word)> max_length:
max_length =len(word)return max_length
s_test =" fly me to the moon "print(length_longest_word(s_test))
"""
3.给一个文件,名称为a.txt,文件有2行内容:
bb cc dd aa
zz ee ff yy
要求读取该文件,并输出zz yy ff ee dd cc bb aa
"""# 打开文件withopen('a.txt','r')asfile:
lines =file.readlines()
list_words =[]for line in lines:
words = line.strip().split()
list_words += words
list_words.sort()print(' '.join(list_words[::-1]))