"""File: sort_by_len_drv.py Purpose: Driver for sort_by_length function Run: python sort_by_len_drv.py Input: List of words, one per line. Carriage return at beginning of line to end input Output: The input list sorted from shortest to longest """ #---------------------------------------------------------------------- def sort_by_length(word_list): """Sort the words in word_list in order of increasing length This is a slight modification of a function in Section 12.7 of Allen Downey, Python for Software Design""" t_list = [] for word in word_list: t_list.append((len(word), word)) t_list.sort() new_list = [] for length, word in t_list: new_list.append(word) return new_list #---------------------------------------------------------------------- def enter_word_list(): """Read a list of words, one per line""" print "Enter a list of words, one per line" print "Hit enter at the beginning of a line to stop" wlist = [] word = raw_input() while word != '': wlist.append(word) word = raw_input() return wlist #---------------------------------------------------------------------- def print_word_list(wlist): """Print a list of words, one per line""" print "The words are:" for w in wlist: print " ", w #---------------------------------------------------------------------- if __name__ == "__main__": word_list = enter_word_list() new_list = sort_by_length(word_list) print_word_list(new_list)