""" CS101 Example uses of dictionaries """ import string # List review def find_max(L): """ Finds and returns the max element in list L """ m = L[0] # declare the first element to be our starting point for item in L: # for every item in the list if item > m: # if the item is greater than our current maximum m = item # make the item the NEW current maximum return m # give back the max after checking every element # Packing and unpacking def find_min_max(L): """ Returns an implicitly packed tuple containing the min and max values in L. """ minimum = L[0] # set max and min to be the first element in L maximum = L[0] for item in L: # for every item in L if item > maximum: # if it is greater than max maximum = item # make it the new max if item < minimum: # if it is less than the min minimum = item # make it the new min return minimum, maximum # return a tuple with min, max #----------------------------------------------------------------- def unpack_min_max(L): """ Shows how we can unpack a function call that returns multiple elements. """ m1, m2 = find_min_max(L) # make a call to find_min_max() and unpack # the 2 item tuple it returns into m1 and m2 print(m1) print(m2) #----------------------------------------------------------------- # Dictionaries def german_dict(): """ Creates and returns a small German-to-English dictionary. """ german = dict() # create empty dictionary german['cat'] = 'die Katze' german['dog'] = 'der Hund' german['to see'] = 'sehen' german['to drive'] = 'fahren' german['sun'] = 'die Sonne' return german def print_iteration(d): """ Iterates through a dictionary and prints the iterator (keys) """ for key in d: print(key) def print_keys(d): """ Explicitly iterates through the keys of a dictionary """ for k in d.keys(): print(k) def print_values(d): """ Iterates through the values of a dictionary (does not show the keys). """ for v in d.values(): print(v) def print_items(d): """ Uses unpacking to print key, value pairs """ for k, v in d.items(): print("Key is: " + k + " -- " + "Value is: " + v) #----------------------------------------------------------------- # Files def print_lines(filename): """ Prints out each line of a file. """ f = open(filename, encoding="utf-8") # open the file as unicode document for line in f: # for each line in the file print(line) # print the line def process_file(filename): """ Opens a text file and processes each line, printing each clean word """ f = open(filename, encoding="utf-8") # open the file as unicode document for line in f: # for every line in the file words = line.strip().split() # make a list of words for w in words: # for every word in the list of words no_punc_w = clean(w) # clean the word print(no_punc_w) # and print it out def clean(s): """ Removes all punctuation (including internal) from a string s. """ for char in string.punctuation: # for every character in the # string.punctuation constant s = s.replace(char,'') # replace the char in s with nothing # (i.e. "erase it") return s # give back the clean version of s