Create a program that reads words.txt (link near top of our home page) in order to: determine the length of the longest word(s) and output that length plus the first word of this length found. determine the length of the shortest word(s) and output that length plus the first word of this length found determine and output the average length of all the words in the list.

Respuesta :

# In Python 3
# https://pastebin.com/fPSmmE5w

longest = [""]
shortest = None


with open("words.txt") as words:
   for line in words.readlines():
       for word in line.split():
           if len(word) > len(longest[0]):
               longest = [word]
           elif len(word) == len(longest[0]):
               longest.append(word)
           elif shortest is None or len(word) < len(shortest[0]):
               shortest = [word]
           elif len(word) == len(shortest[0]):
               shortest.append(word)


def format_printable(words):
   return ["{}: {}".format(len(word), word) for word in words]


print(
   "Longest:\n",
   "\n".join(format_printable(longest)),
   "\nShortest:\n",
   "\n".join(format_printable(shortest)),
)