def s( d ) : ''' Returns the sum of the values in dataset d. ''' # s(d) calculates the sum of all of the elements in the entire dataset # Recall d is a dataset, a list of lists. # sum( list ) can only be called on a SINGLE list of numbers like sum( [1,2,3] ) = 6 result = 0 # Accumulator for our sum for row in d: # Loop through each list in our dataset (each row in our dataset) for number in row: # Loop through each number in each row result = result + number # Add each number from each inner list to our result sum return result # Alternate Way result = 0 for row in d: sum_row = sum( row ) # Call the sum function on each inner list in our dataset result = result + sum_row # Add the sum of each row to our result accumulator for the total sum return result # Anytime we ask you to look at a dataset, if we want you to look at each cell (smaller parts of rows) # you want to use a nested loop: # for row in dataset: # for cell in row: # [[1,2,3], [4,5,6]] # ^ Cell value (each value in each row) # row row ''' DO NOT MODIFY THE BELOW CODE ''' if ( __name__ == '__main__' ) : import sigma d1 = [ [ 0 ], [ 1, 2 ], [ 1, 2, 3 ], [ 0 ] ] d2 = [ [ 1, 0, 1, 2, 2 ], [ 3, 0, 1, 1, 1, 0 ], [ 2 ], [ 0, 0, 1 ] ] d3 = [ [ 3, 0, 3], [ 3, 0, 3, 0, 1], [ 1, 0, 2 ] ] d4 = [ ] s1 = sigma.s( d1 ); print( 's( d1 ) =', s1 ) s2 = sigma.s( d2 ); print( 's( d2 ) =', s2 ) s3 = sigma.s( d3 ); print( 's( d3 ) =', s3 ) s4 = sigma.s( d4 ); print( 's( d4 ) =', s4 )