Answer to Question #308485 in Python for Michael

Question #308485

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 ,define a function Find_Max(n) 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

Example-2

Example-3

Example-4

Input:

01231

 

Output:

10

Input:

891

 

Output:

73Input:

0000

 

Output:


Input:

45f

 

Output:

Invalid Number



1
Expert's answer
2022-03-09T10:24:29-0500
# MaxValue.py


def Find_Max(S):
    D = list(S)
    for d in D:
        if d not in "0123456789":
            return 'Invalid Number'    
    return max_val(D)

def max_val(D):
    if len(D) == 1:
        return int(D[0])
    
    d = int(D[-1])
    D1 = D[:-1]
    m = max_val(D1)
    m1 = d + m
    m2 = d * m

    return m1 if m1 > m2 else m2


def main():
    S = input()
    m = Find_Max(S)
    print(m)

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