''' DS2000 Fall 2024 Starter code for lecture on 10/22/24 Complete the code for the functions below (function signatures are in the code below, along with name/params/return type). The last one is optional and just for funsies. We should get through the first three all together! ''' import random def print_row_first(lst): ''' Parameter: 2d list of any values Returns: nothing Does: Prints the element at position 0 within each list ''' for row in lst: print(row[0]) def print_row_first_v2(lst): ''' (Another version -- the morning/afternoon sections approached it differently but both work!) Parameter: 2d list of any values Returns: nothing Does: Prints the element at position 0 within each list ''' for i in range(len(lst)): print(lst[i][0]) def multiply_2d(lst, val): ''' parameters: 2d list of ints/floats, int/float to multiply by returns: 2d list does: computes and returns val * values in the original ''' for i in range(len(lst)): for j in range(len(lst[i])): lst[i][j] = lst[i][j] * val return lst def weighted_avg(lst): ''' parameters: 2d list with one row of floats/weights, one row of floats/values returns: float does: computes the weighted average of the given list, assuming weights are in the first row and values in the second ''' avg = 0 for i in range(len(lst[0])): curr = (lst[0][i] * lst[1][i]) avg += curr return avg # This last one is a bonus, for funsies! Feel free to ignore def sum_rows(lst): ''' Parameters: 2d list of ints/floats Returns: 1d list of ints/floats Does: Sums each row of the given list, returns a list of sums ''' sums = [] for row in lst: curr_sum = sum(row) sums.append(curr_sum) return sums def main(): # Here in main, we're just calling the functions we've written to test them! # Mostly generating some random values to try out # Generate a 2d list with randomized values, and randomly-chosen number of # rows and columns. lst = [] num_rows = random.randint(1, 5) num_cols = random.randint(1, 8) for i in range(num_rows): row = [] for j in range(num_cols): row.append(random.randint(1, 200)) lst.append(row) # Here is my list for testing... print(lst) # Call our functions! First, print row[0] for every row print("Printing the first value in every row...") print_row_first(lst) # Second, double all the values in the list and print out doubled = multiply_2d(lst, 2) print("\n\nThis doubled the values in my list...") print(doubled) # Third, make up a new list to test weighted average weights = [.25, .3, .35, .1, .05] values = [89, 87, 98, 90, 81] scores = [weights, values] weighted = weighted_avg(scores) print("Weighted average is...", weighted, "\n\n") # Fourth, just for fun, get the sum of every row row_summed = sum_rows(lst) print("Here is my list of sums of every row...") print(row_summed) main()