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
# 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()
Comments
Leave a comment