Question

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
//if you have any query then comment below.please rate the answer?

def rec_sum (list) if len (list)0: return0 else: return list [0] + rec_sum(list [1:]) n-int (input (Enter the number of elemEnter the number of elements for list: 3 Enter element:4 Enter element:5 Enter element:6 Sum of all elements: 15

def rec_sum(list):
    if len(list)==0:
        return 0
    else:
        return list[0] + rec_sum(list[1:])

n=int(input("Enter the number of elements for list:"))
list=[]
for i in range(0,n):
    element=int(input("Enter element:"))
    list.append(element)
print("Sum of all elements: ",rec_sum(list))
Add a comment
Know the answer?
Add Answer to:
Question 1 100 pts Design a function that accepts a list of numbers as an argument...
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...

  • Sum of Numbers Problem: Write a method that accepts an integer argument and returns the sum...

    Sum of Numbers Problem: Write a method that accepts an integer argument and returns the sum of all the integers from 1 up to the number passed as an argument. For example, if 50 is passed as an argument, the method will return the sum of 1, 2, 3, 4, . . . 50. Use recursion to calculate the sum. Demonstrate the method in a program. The program should be called SumOfNumbers.java.

  • Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters...

    Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times y. Remember, multiplication can be performed as repeated addition as follows: 5×6=6+6+6+6+6 2. Recursive findings Write a recursive boolean method named reFinding. The method should search an array for a specified value, and return true if the value is found in the array, or false if the value is not found in...

  • 1. Write a function called ordinal_sum that accepts a string argument and returns the sum of...

    1. Write a function called ordinal_sum that accepts a string argument and returns the sum of the ordinal values of each character in the string. The ordinal value of a character is its numeric Unicode code point in decimal. 2. Write code that creates a Python set containing each unique character in a string named my_string. For example, if the string is 'hello', the set would be {'h', 'e', 'l', 'o'} (in any order). Assign the set to a variable...

  • 2. Write a scheme function get-even-nums that accepts a list of numbers and returns a new...

    2. Write a scheme function get-even-nums that accepts a list of numbers and returns a new list that contains only the even numbers. (Hint: use reminder function) Sample runs: (get-even-nums ‘(1 2 3 4 5 6 7 8 9)) should return (8 6 4 2). (get-even-nums ‘(1 3 5 7 9)) should return ’( ).

  • c++ Write a function that uses recursion to raise a number to a power. The function...

    c++ Write a function that uses recursion to raise a number to a power. The function should accept two arguments: the number to be raised and the exponent. Assume that the exponent is a nonnegative integer. Demonstrate the function in a program. Write a function that accepts an integer argument and returns the sum of all the integers from 1 up to the number passed as an argument. For example, if 50 is passed as an argument, the function will...

  • HW7.26. Return a CSV string from a list of numbers The function below takes a single...

    HW7.26. Return a CSV string from a list of numbers The function below takes a single parameter, a list of numbers called number_list. Complete the function to return a string of the provided numbers as a series of comma separate values (CSV). For example, if the function was provided the argument [22, 33, 44], the function should return '22,33,44'. Hint: in order to use the join function you need to first convert the numbers into strings, which you can do...

  • Function name: triangular Parameters: integer Returns: a list Description: Write a function called triangular that accepts...

    Function name: triangular Parameters: integer Returns: a list Description: Write a function called triangular that accepts one integer parameter that returns a list with the same number of terms as the value of the parameter. If the parameter is zero or a negative number, you should return an empty list A triangular number is the sum of a positive integer and all the integers before it. The series is as follows: 1, 3, 6, 10, 15, 21,

  • Please help, using Python Write a Python function sum_of_4th_powers, which accepts one argument. You can assume...

    Please help, using Python Write a Python function sum_of_4th_powers, which accepts one argument. You can assume that the function is only called with an integer n as argument, i.e., no need to worry about invalid inputs. If the argument n can be written as a sum n=a4+b4+c4 with all of a,b,c being non-zero, your function should return such a decomposition. Otherwise your function must return False. For example: sum_of_4th_powers(5) must return False, sum_of_4th_powers(18) could return (1,1,2). *Please go step by...

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
ADVERTISEMENT