Answer to Question #310304 in Python for void

Question #310304

Given a string of numbers S, the task is to find the maximum value from the string S, you can add a ‘+’ or ‘*’ sign between any two numbers.

For example, S=891. As 8*9*1 = 72 and 8*9+1 = 73. So, 73 is maximum.

Write a python module MaxValue.py for solve the above task. You need to import this module in your file which takes a string and return maximum value or error message as shown in example by calling the appropriate function implemented in MaxValue.py module. Also handle the possible exceptions and display the exception message in form of string.

Example-1

Input:

01231

 

Output:

10

example 2

Input:

891

 

Output:

73

example 3

Input:

0000

 

Output:


example 4

Input:

45f

 

Output:

Invalid Number


1
Expert's answer
2022-03-12T12:46:29-0500
# MaxValue.py

def max_value(L):
    if len(L) == 1:
        return L[0]
    
    rest = max_value(L[:-1])
    x1 = L[-1] + rest
    x2 = L[-1] * rest
    if x1 > x2:
        return x1
    else:
        return x2
    
def MaxValue(s):
    L = [int(ch) for ch in s]
    return max_value(L)


# RollNo_W12B_1.py
from MaxValue import MaxValue

def main():
    S = input()
    try:
        res = MaxValue(S)
        print(res)
    except:
        print('Invalid Number')

if __name__ == '__main__':
    main()

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS