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 RollNo_W12B_1.py. In RollNo_W12B_1.py, 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:
73
Input:
0000
Output:
Input:
45f
Output:
Invalid Number
# MaxValue.py
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
# RollNo_W12B_1.py
from MaxValue import max_val
def Find_Max(S):
L = [d for d in S]
try:
res = max_val(L)
except:
res = "Invalid Number"
return res
def main():
S = input()
res = Find_Max(S)
print(res)
if __name__ == '__main__':
main()
Comments
Leave a comment