Question

The sum of the elements in a tuple can be recusively calculated as follows: The sum...

The sum of the elements in a tuple can be recusively calculated as follows:

  • The sum of the elements in a tuple of size 0 is 0
  • Otherwise, the sum is the value of the first element added to the sum of the rest of the elements

Write a function named sum that accepts a tuple as an argument and returns the sum of the elements in the tuple.

please write code in Python and provide output.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is code:

def sumTuple(tup):
if len(tup) == 0:
return 0
return tup[0] + sumTuple(tup[1: len(tup)])

Sample code to test:

def sumTuple(tup):
if len(tup) == 0:
return 0
return tup[0] + sumTuple(tup[1: len(tup)])


tup1 = (1, 2, 3, 4, 5)
print(sumTuple(tup1))

tup2 = ()
print(sumTuple(tup2))

Output:

Add a comment
Know the answer?
Add Answer to:
The sum of the elements in a tuple can be recusively calculated as follows: The sum...
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
  • For Python for myprogramming lab. please use simple code The elements of a tuple can be...

    For Python for myprogramming lab. please use simple code The elements of a tuple can be set to 0 recursively as follows: If the tuple has a length of zero, nothing should be done to it. Otherwise, set the first element of the tuple to 0, and then append it to the rest of the tuple. Write a function named clearthat accepts one argument, a tuple of any length, and returns a tuple of the same length with every element...

  • Written in python programming for MyProLab The elements of a tuple can be set to 0...

    Written in python programming for MyProLab The elements of a tuple can be set to 0 recursively as follows: If the tuple has a length of zero, nothing should be done to it. Otherwise, set the first element of the tuple to 0, and then append it to the rest of the tuple. Write a function named clearthat accepts one argument, a tuple of any length, and returns a tuple of the same length with every element set to 0.

  • write in java 1. Assume the availability of a method  named  makeLine that can be passed a non-negative...

    write in java 1. Assume the availability of a method  named  makeLine that can be passed a non-negative integer  n and a character  c and return a String consisting of n identical characters that are all equal to c. Write a method  named  printTriangle that receives two integer  parameters  n and k. If n is negative the method does nothing. If n happens to be an even number, itsvalue is raised to the next odd number (e.g. 4-->5). Then, when k has the value zero, the method prints...

  • 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...

  • Q.1. Create a C++ class template called ArrayCalc, to perform a series of mathematical operations on...

    Q.1. Create a C++ class template called ArrayCalc, to perform a series of mathematical operations on an array. These mathematical operations are calculating the minimum, maximum, sum and average values of the array. The ArrayCalc class template accepts one type parameter, which can be defined as: template <class T>. This allows ArrayCalc to carry out operations for different array data types. ArrayCalc has the following public methods, described here in pseudo code: i. void FindMin(inArrayData[], inArraySize, SoutMinVal); (This function accepts...

  • The integral integral_3^9 x^2 dx can be calculated approximately by the left sum method (finding the...

    The integral integral_3^9 x^2 dx can be calculated approximately by the left sum method (finding the area of multiple rectangles whose top left corners lie on the curve). Figure 1 shows such an approximation with 4 rectangles. Write a MATLAB function to calculate the truncation error caused by the left sum method for integral_3^9 x^2 dx. Your function should meet the following requirements: It should be named NumericalInt It should have the number of rectangles as the sole input argument....

  • Please Write in Java An array is sorted (in ascending order) if each element of the...

    Please Write in Java An array is sorted (in ascending order) if each element of the array is less than or equal to the next element . An array of size 0 or 1 is sorted Compare the first two elements of the array ; if they are out of order, the array is not sorted; otherwise, check the if the rest of the array is sorted. Write a boolean -valued  method  named  isSorted that accepts an integer  array , and the number of...

  • python 2..fundamentals of python 1.Package Newton’s method for approximating square roots (Case Study 3.6) in a...

    python 2..fundamentals of python 1.Package Newton’s method for approximating square roots (Case Study 3.6) in a function named newton. This function expects the input number as an argument and returns the estimate of its square root. The script should also include a main function that allows the user to compute square roots of inputs until she presses the enter/return key. 2.Convert Newton’s method for approximating square roots in Project 1 to a recursive function named newton. (Hint: The estimate of...

  • Write a C program as follows: Single source code file Calls a function with an arbitrary...

    Write a C program as follows: Single source code file Calls a function with an arbitrary name (i.e. you name it) that accepts two arrays of the same size The function should add each element in the arrays together and place the values in a third array Each array element, each array address, and the sum are printed to the screen in tabulated format with headersI also would like to request that LOTS of comments be included, as I need...

  • Can someone help me understand this step by step (C++) Array Shifter and Array Reversal Write...

    Can someone help me understand this step by step (C++) Array Shifter and Array Reversal Write a function that accepts an array of doubles and the array's size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to element 1 of the new array, element 1 of the argument array...

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