human
Minimum and Maximum values of an expression with * and + | Utility method to check whether a character is operator or not.
method prints minimum and maximum value obtainable from an expression.
store operator and numbers in different vectors.
storing last number in vector.
initializing minval and maxval 2D array.
initializing main diagonal by num values.
looping similar to matrix chain multiplication and updating both 2D arrays.
if current operator is ' + ' , updating tmp variable by addition.
if current operator is ' * ' , updating tmp variable by multiplication.
updating array values by tmp variables.
last element of first row will store the result.
Driver code"
How can the above be solved in Python?
assistant
#Here is code to minimum and Maximum values of an expression with * and + and the other steps required.
 def isOperator ( op ) :
	return ( op == ' + ' or op == ' * ' )
def printMinAndMaxValueOfExp ( exp ) :
	num = [ ]
	opr = [ ]
	tmp = " "
	for i in range ( len ( exp ) ) :
		if ( isOperator ( exp [ i ] ) ) :
			opr . append ( exp [ i ] )
			num . append ( int ( tmp ) )
			tmp = " "
		else :
			tmp += exp [ i ]
	num . append ( int ( tmp ) )
	llen = len ( num )
	minVal = [ [ 0 for i in range ( llen ) ] for i in range ( llen ) ]
	maxVal = [ [ 0 for i in range ( llen ) ] for i in range ( llen ) ]
	for i in range ( llen ) :
		for j in range ( llen ) :
			minVal [ i ] [ j ] = 10 ** 9
			maxVal [ i ] [ j ] = 0
			if ( i == j ) :
				minVal [ i ] [ j ] = maxVal [ i ] [ j ] = num [ i ]
	for L in range ( 2 , llen + 1 ) :
		for i in range ( llen - L + 1 ) :
			j = i + L - 1
			for k in range ( i , j ) :
				minTmp = 0
				maxTmp = 0
				if ( opr [ k ] == ' + ' ) :
					minTmp = minVal [ i ] [ k ] + minVal [ k + 1 ] [ j ]
					maxTmp = maxVal [ i ] [ k ] + maxVal [ k + 1 ] [ j ]
				elif ( opr [ k ] == ' * ' ) :
					minTmp = minVal [ i ] [ k ] * minVal [ k + 1 ] [ j ]
					maxTmp = maxVal [ i ] [ k ] * maxVal [ k + 1 ] [ j ]
				if ( minTmp < minVal [ i ] [ j ] ) :
					minVal [ i ] [ j ] = minTmp
				if ( maxTmp > maxVal [ i ] [ j ] ) :
					maxVal [ i ] [ j ] = maxTmp
	print ( " Minimum ▁ value ▁ : ▁ " , minVal [ 0 ] [ llen - 1 ] , " , ▁ \ ▁ Maximum ▁ value ▁ : ▁ " , maxVal [ 0 ] [ llen - 1 ] )
expression = "1 + 2*3 + 4*5"
printMinAndMaxValueOfExp ( expression )