Question

Design a function in python that sums all the numbers in a multi-dimensional list of numbers....

Design a function in python that sums all the numbers in a multi-dimensional list of numbers. The function should accept the list as an argument. For example, give the following list [[3,5,6,8,9], [0,1,5,8,7], [2,5,9,2,4], [9,5,4,2,1]] The function should add all the numbers in the list and print the result. Analyze your solution and determine the time complexity of the function. Write the time complexity in the form of T(n) = ….

0 0
Add a comment Improve this question Transcribed image text
Answer #1
Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change. 

If you are satisfied with the solution, please rate the answer. Let me know for any help with any other questions.

Thank You !
===========================================================================

def sum_list(lst):
    total = 0
    for row in lst:
        if isinstance(row, list):
            total += sum_list(row)
        else:
            total += row
    return total


print(sum_list([[3, 5, 6, 8, 9], [0, 1, 5, 8, 7, [1, [1, 1, 1, 1, 1], 2, 3, 4]], [2, 5, 9, 2, 4], [9, 5, 4, 2, 1]]))

===========================================================

Time Complexity will be of order O(n) where n is the number of values in each list

Add a comment
Know the answer?
Add Answer to:
Design a function in python that sums all the numbers in a multi-dimensional list of numbers....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a python program (recursive.py) that contains a main() function. The main() should test the following...

    Write a python program (recursive.py) that contains a main() function. The main() should test the following functions by calling each one with several different values. Here are the functions: 1) rprint - Recursive Printing: Design a recursive function that accepts an integer argument, n, and prints the numbers 1 up through n. 2) rmult - Recursive Multiplication: Design a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times...

  • ****python**** Q1. Write a recursive function that returns the sum of all numbers up to and...

    ****python**** Q1. Write a recursive function that returns the sum of all numbers up to and including the given value. This function has to be recursive; you may not use loops! For example: Test Result print('%d : %d' % (5, sum_up_to(5))) 5 : 15 Q2, Write a recursive function that counts the number of odd integers in a given list. This function has to be recursive; you may not use loops! For example: Test Result print('%s : %d' % ([2,...

  • Question 1 100 pts Design a function that accepts a list of numbers as an argument...

    Question 1 100 pts Design a function that accepts a list of numbers as an argument (this function only has one parameter, which is a list). The function should recursively calculate the sum of all the numbers in the list and return that value. Hint: refer to the Summing a range of list elements with recursion" example in slides.

  • PLEASE DO THIS WITH PYTHON! Please do this with PYTHON! Assume the example of a name...

    PLEASE DO THIS WITH PYTHON! Please do this with PYTHON! Assume the example of a name list for all problems below (use it as a line in your code) e.g. nameist -ll'Julia Truong'.'Chen Wu'vJeb Castro,'Ron Kennedy', 'X YI 1. Write a recursive function convertNames0 to accept the initial nameList and return a new list containing only first names. Provide function call and print the resulting list after the function had returned the results. (solution similar to problem on the slide...

  • The problem is this Design a function that accepts a list as an argument and returns...

    The problem is this Design a function that accepts a list as an argument and returns the largest value in the list. The function should use recursion to find the largest item. My coding so far is this def main(): # Prints the largest value in list user_input = input('Enter a list of numbers seperated by a space: ') user_list = user_input.split() print('Largest number is ', max_Number(user_list), '.') # Recursion function that finds the maximum number in a sequence of...

  • Use Python 3.7 to write the following program: Given a list of n numbers, write a...

    Use Python 3.7 to write the following program: Given a list of n numbers, write a function to shift the numbers circularly by some integer k (k < n). The function should take a list, the amount to shift k, and an argument specifying the direction to shift (left or right). Suggestion: use the % operation and also you need a number between 0 inclusive and the length of the list.

  • Define a function that creates a list of all the numbers that are factors of the...

    Define a function that creates a list of all the numbers that are factors of the user's number. For example, if the function is called factor, factor(36) should return [1, 2, 3, 4, 6, 9, 12, 18, 36]. The numbers in your list should be sorted from least to greatest, and 1 and the original number should be included. Remember to consider negative numbers as well as 0. Bonus: Have the program print the factors of the users number in...

  • Python Language Only!!!! Python Language Only!!!! Python Language Only!!!! Python 3 is used. A. Write a...

    Python Language Only!!!! Python Language Only!!!! Python Language Only!!!! Python 3 is used. A. Write a function named smaller that accepts two arguments: a list, and a number n. Assume that the list contains numbers. The function should display all numbers in the list that are smaller than the number n. Write a code that declares and initializes a list of numbers and a variable number and pass them to the function to test it. Please see the outcome below:...

  • FOR PYTHON: Write a function findMinRow() that takes a two-dimensional list of numbers as a parameter....

    FOR PYTHON: Write a function findMinRow() that takes a two-dimensional list of numbers as a parameter. It returns the index of the minimum row in the list. The minimum row is the row with the smallest sum of elements. If the list has multiple rows that achieve the minimum value, the last such row should be returned. If the list is empty the function should return -1. The following shows several sample runs of the function: Python 3.4.1 Shell File...

  • Please solve this problem using Python Write a quicksort method that accept a list of numbers...

    Please solve this problem using Python Write a quicksort method that accept a list of numbers and use list comprehension to construct the method. Note that you need to sort the numbers in descending order . You MUST use List Comprehension to do the sorting Your program should contain the function with format shown as below: def quicksort(a): # Your codes here. Use List Comprehension and return the result.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT