Validation of Equation Given as String | Function that returns true if the equation is valid ; If it is an integer then add it to another string array ; Evaluation of 1 st operator ; Evaluation of 2 nd operator ; Evaluation of 3 rd operator ; If the LHS result is equal to the RHS ; Driver code
def isValid ( string ) : NEW_LINE INDENT k = 0 ; NEW_LINE operands = [ " " ] * 5 ; NEW_LINE operators = [ " " ] * 4 ; NEW_LINE ans = 0 ; ans1 = 0 ; ans2 = 0 ; NEW_LINE for i in range ( len ( string ) ) : NEW_LINE INDENT if ( string [ i ] != ' + ' and string [ i ] != ' = ' and string [ i ] != ' - ' ) : NEW_LINE INDENT operands [ k ] += string [ i ] ; NEW_LINE DEDENT else : NEW_LINE INDENT operators [ k ] = string [ i ] ; NEW_LINE if ( k == 1 ) : NEW_LINE INDENT if ( operators [ k - 1 ] == ' + ' ) : NEW_LINE INDENT ans += int ( operands [ k - 1 ] ) + int ( operands [ k ] ) ; NEW_LINE DEDENT if ( operators [ k - 1 ] == ' - ' ) : NEW_LINE INDENT ans += int ( operands [ k - 1 ] ) - int ( operands [ k ] ) ; NEW_LINE DEDENT DEDENT if ( k == 2 ) : NEW_LINE INDENT if ( operators [ k - 1 ] == ' + ' ) : NEW_LINE INDENT ans1 += ans + int ( operands [ k ] ) ; NEW_LINE DEDENT if ( operators [ k - 1 ] == ' - ' ) : NEW_LINE INDENT ans1 -= ans - int ( operands [ k ] ) ; NEW_LINE DEDENT DEDENT if ( k == 3 ) : NEW_LINE INDENT if ( operators [ k - 1 ] == ' + ' ) : NEW_LINE INDENT ans2 += ans1 + int ( operands [ k ] ) ; NEW_LINE DEDENT if ( operators [ k - 1 ] == ' - ' ) : NEW_LINE INDENT ans2 -= ans1 - int ( operands [ k ] ) ; NEW_LINE DEDENT DEDENT k += 1 NEW_LINE DEDENT DEDENT if ( ans2 == int ( operands [ 4 ] ) ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT else : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = "2 ▁ + ▁ 5 ▁ + ▁ 3 ▁ + ▁ 1 ▁ = ▁ 11" ; NEW_LINE if ( isValid ( string ) ) : NEW_LINE INDENT print ( " Valid " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Invalid " ) ; NEW_LINE DEDENT DEDENT