Question

In: Computer Science

(Python) Implement a function to compute a sum that can compute sum for an arbitrary number...

(Python)

Implement a function to compute a sum that can compute sum for an arbitrary number of input integers or float numbers. In other words, calls like this should all work:

flexible_sum(x1, x2) # sum of two integer or float numbers or strings x1 and x2

flexible_sum(x1, x2, x3) # sum of 3 input parameters

and can also handle a single input parameter, returning it unchanged and with the same type, i.e.:

  flexible_sum(1)

returns 1

and can accept an arbitrary number of strings as input parameters and return their concatenation. In other words, the following call

  flexible_sum('one ', 'two ', 'three ')

should give result string

  'one two three '

And the function can also accept a single input parameter in the form of a list of an arbitrary length and return a sum for it.

If you can, try to implement your function without calls to any other external functions, including Python built-in functions.

Solutions

Expert Solution

def flexible_sum(*args):

    s = ''
    c = 0
    l =[]
    if len(args) == 1:       #it the args has length 1 it is either a list or just a single value
        for i in args:
            l = i
        if isinstance(l,list):     #isinstance is used to check if the parameter is list
            for i in l:
                c += i
            return c
        else:
            return l
    elif  isinstance(args[1], str):    #isinstace is used to check if the parameter is string
        for i in args:
            s+=i
        return s
    else:
        for i in args:
            c+=i
        return c


print(flexible_sum(1))                  #taking one parameter
print(flexible_sum(2,3,4))              #taking three int as parameter
print(flexible_sum(1.2,4.6,7.9))        #taking three float as parameter
print(flexible_sum([1,2,3,4,5]))        #taking a list as parameter
print(flexible_sum('one ','two ','three '))         #taking strings as parameter

output:


Related Solutions

Python program. Write a python program that can convert any radix-d (arbitrary base) number to the...
Python program. Write a python program that can convert any radix-d (arbitrary base) number to the equivalent radix-e (another arbitrary base) number. Where e and d are members in [2, 16]. Remember, base 16 needs to be calculated as hexadecimal. So, if radix-d is input as a hexadecimal number, it needs to convert and output to desired base. Conversely, if base 16 is the desired output, then the output needs to show a hexadecimal number. Hints: The easiest approach is...
Python: Implement edit_diff using recursion, which is a diff function that returns the minimum number of...
Python: Implement edit_diff using recursion, which is a diff function that returns the minimum number of edit operations needed to transform the start word into the goal word. There are three kinds of edit operations: Add a letter to start, Remove a letter from start, Substitute a letter in start for another. Each edit operation contributes 1 to the difference between two words. >>> big_limit = 10 >>> edit_diff("roses", "arose", big_limit) # roses -> aroses -> arose 2 >>> edit_diff("tesng",...
using python: Given an arbitrary whole number, please write an if statement to output if it...
using python: Given an arbitrary whole number, please write an if statement to output if it is an even number or an odd number
Write a function printTwoLargest() that inputs an arbitrary number of positive numbers from the user. The...
Write a function printTwoLargest() that inputs an arbitrary number of positive numbers from the user. The input of numbers stops when the first negative or zero value is entered by the user. The function then prints the two largest values entered by the user. If no positive numbers are entered a message to that effect is printed instead of printing any numbers. If only one number is inputted, only the largest is printed out (see 2nd example below). Sample output:...
Provide the Java code to compute the sum, average, maximum number and minimum number if I...
Provide the Java code to compute the sum, average, maximum number and minimum number if I have a string sentence of double numbers. Assume that the length of the string sentence is not known. It can be of any length. To split a string based on the comma character use the following. String sentence = "A,B,C,D,E"; String[] stringsArray = receivedSentence.split(","); Then stringsArray is an array of five elements such that: stringsArray[0] = 'A' stringsArray[1] = 'B' stringsArray[2] = 'C' stringsArray[3]...
Can someone do this python program? Write a function that takes a number as a parameter...
Can someone do this python program? Write a function that takes a number as a parameter and then prints out all of the factors for that number.
I need to implement a code in python to guess a random number. The number must...
I need to implement a code in python to guess a random number. The number must be an integer between 1 and 50 inclusive, using the module random function randint. The user will have four options to guess the number. For each one of the failed attempts, the program it should let the user know whether is with a lower or higher number and how many tries they have left. If the user guess the number, the program must congratulate...
Write a function in Python to compute the sample mean of a list of numbers. The...
Write a function in Python to compute the sample mean of a list of numbers. The return value should be the sample mean and the input value should be the list of numbers. Do not use a built-in function for the mean. Show an example of your function in use.    Thank You
Write a Python program that calls a function to sum all the numbers in a list...
Write a Python program that calls a function to sum all the numbers in a list and returns the result to the caller. The main program creates a list (with hard-coded or user input) and passes the list as an argument to the function. You may not use the built-in function, sum. The program calls a second function to multiply all the numbers in a list passed to it by main and returns the product back to the caller. List...
show that every function can be expressed as the sum of an even function and an...
show that every function can be expressed as the sum of an even function and an odd function and that there is only one way to do this.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT