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
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.
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]...
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.
def sum_gt_avg(num_list): Implement a function that returns the sum of the numbers in num_list that have...
def sum_gt_avg(num_list): Implement a function that returns the sum of the numbers in num_list that have a value greater than the average value in the list. • Parameters: num_list is a list of numbers (mixed integers and floats) • Examples: sum_gt_avg([1,2,3,4,5]) → 9 # 4+5 sum_gt_avg([1,2,3,-4,5]) → 10 # 2+3+5 sum_gt_avg([-1,-2,-3,-4,-5]) → -3 # -1-2 in python
- Design and implement a function with no input parameters. The function keeps receiving a number...
- Design and implement a function with no input parameters. The function keeps receiving a number from input (user) and adds the numbers together. The application keeps doing it until the user enter 0. Then the application will stop and print the total sum and average of the numbers the user had entered.
in python You will be writing a program that can be used to sum up and...
in python You will be writing a program that can be used to sum up and report lab scores. Your program must allow a user to enter points values for the four parts of the problem solving process (0-5 points for each step), the code (0-20 points), and 3 partner ratings (0-10) points each. It should sum the points for each problem-solving step, the code, and the average of the three partner ratings and print out a string reporting the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT