Coverage for e21_longest_word.py : 100%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1#!/usr/bin/env python3
2"""Solution to chapter 5, exercise 21: longest_word"""
5import os
8def find_longest_word(filename):
9 """Given a filename, return the longest word in the file."""
10 longest_word = ''
11 for one_line in open(filename):
12 for one_word in one_line.split():
13 if len(one_word) > len(longest_word):
14 longest_word = one_word
15 return longest_word
18def find_all_longest_words(dirname):
19 """Given a directory name, return a dict in which the keys
20are filenames in the directory and the values are
21the strings -- the longest word in each file."""
22 return {filename: find_longest_word(os.path.join(dirname, filename))
23 for filename in os.listdir(dirname)
24 if os.path.isfile(os.path.join(dirname, filename))}