''' DS2500 Spring 2025 Sample code from lecture - 1/10/25 In class today, we are writing a function to: - take in a dictionary as its only parameter - find the max value in the dictionary - return the key, value for the max (as a tuple!) ''' def dct_max(dct): ''' parameters: one dictionary with numeric values returns: tuple with two items, key and value does: finds the max value in the given dictionary, returns the value and its key ''' max_value = float("-inf") max_key = "" for key, value in dct.items(): if value > max_value: max_value = value max_key = key return (max_key, max_value) def sum_lst(lst): ''' parameters: list of strings that could be floats returns: a float does: converts elements to float, skipping empty strings, and sums the values ''' total = 0 for num in lst: if num != "": total += float(num) return total def main(): # Call our dct_max function to make sure it works # it should dct = {"a" : 1, "b" : 13, "c" : 8} key, val = dct_max(dct) print(f"The max in our dictionary is {key}, {val}") # Call our sum_lst function to make sure it works! # it should return 7.7 lst = ["1.4", "2.8", "", "3.5"] total = sum_lst(lst) print(f"The sum of our list, once coverted is {total}") # One more example from the slides # Turn a lst [x, y, z, w] into a key, value # pair of a dictionary {x : [y, z, w]} dct = {} lst = [4, 6, 8, 10, 12] dct[lst[0]] = lst[1:] print(f"This should print 4 : [6, 8, 10, 12]... {dct}") if __name__ == "__main__": main()