Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The function sum_rows should add all the values in each row of data and return a

ID: 3778928 • Letter: T

Question

The function sum_rows should add all the values in each row of data and return a new list containing those sums. The data parameter is a 2D list, or more specifically, a list of lists. You must use the len() function to determine how many rows there are, and for each row, how many columns there are. Look at the following example. This list represents the data parameter. sum_rows should return the following list of row sums: Complete your implementation of sum_rows below. Use loops. You may not use recursion or the built-in sum function. def sum_rows(data):

Explanation / Answer

Here goes the required code for the above program using Python

Code:

def sum_rows(data):
    n=len(data)
  
    result=[]
    for i in range(n):
        m=len(data[i])
        sum=0
        for j in range(m):
            sum=sum+data[i][j]
        result.append(sum)
    print result
  
  
sum_rows([[3,-8,2,34],[9,-19],[-27,30,45],[-2,5]])

Explanation:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote