# CS146 class example: files, lists def sentence_stats(sentence): """ Prints out various statistics about a sentence Args: sentence: string to be analyzed """ words = sentence.split() print_stats(words) def print_stats(words): """ Print out various statistics about the input collection of strings Args: words: Collection of strings """ print("Number of words:", len(words)) if len(words) > 0: print("Longest word:", longest_word(words)) print("Shortest word: ", shortest_word(words)) print("Avg. word length: ", average_word_length(words)) # NOTE: if you want to try the examples from class you'll need to # download the file of English words, "english.txt" def file_stats(filename): """ Prints out various statistics about the words in the file, assuming one word per line. Args: filename: Path to file """ words = read_words(filename) print_stats(words) def read_words(filename): """ Read file into list of words assuming one word per line Args: filename: Path to file Returns: list of words """ with open(filename, "r") as word_file: words = [] for line in word_file: # Assuming one word per line (we strip the newline from # the end of the line) words.append(line.strip()) return words def average_word_length(words): """ Compute the average length in characters of a collection of strings Args: words: Collection of strings Returns: float of average word length """ length_sum = 0 for word in words: length_sum += len(word) return length_sum / len(words) def longest_word(words): """ Find longest string in collection of strings Args: words: Non-empty collection of strings Returns: longest string """ longest = words[0] # To avoid comparing words[0] twice, you could use words[1:] for word in words: if len(word) > len(longest): longest = word return longest # An alternate implementation that takes of advantage of Python features we haven't learned about is # return max(words, key=len) # Here `key` is an optional keyword parameter that expects a function as its argument. That function is # applied to each element of the list before performing the comparison, i.e., comparing key(words[0]) to # key(words[1]). Effectively it is performing a similar computation to our implementation above. Although # we haven't discussed it yet in class, we can assigned functions to variables, pass them as arguments, etc.abs # termed "functions as first class objects". Doing so allows us to provide the computation to perform as an argument. def shortest_word(words): """ Find shortest string in collection of strings Args: words: Non-empty collection of strings Returns: shortest string """ shortest = words[0] for word in words: if len(word) < len(shortest): shortest = word return shortest