**Not a member of Pastebin yet?**

**, it unlocks many cool features!**

__Sign Up__- # This is the solution to the Final Test of part 1 of the DIY Guide for Learning to Code.
- # We'll start by defining some functions we're going to use. If you want, you
- # can skip to the end to see the main program, which is a loop that just calls our
- # function evaluateEquation.
- # The following does all the hard work of deciding whether an equation is True or False.
- def evaluateEquation(equation):
- # Remove spaces from the equation entirely
- equation = equation.replace(' ', '')
- # Split the equation into two halves, one on each side of the equals sign
- indexOfEqualSign = equation.find('=')
- firstHalf = equation[0:indexOfEqualSign]
- secondHalf = equation[(indexOfEqualSign + 1):] # We add one to the first index so that we don't include the sign itself. When we don't include a second index, that means "go to the end"
- # For each of these halves, simplify it into a single number.
- # We're actually going to use a "helper function" here, because this is a lot of work that we're actually going to use twice.
- # You can see what the function looks like down below.
- firstHalf = simplifyHalf(firstHalf)
- # Great! When that function finally completes, we'll be left with just a number. Now we need to do the exact same thing for the second half:
- secondHalf = simplifyHalf(secondHalf)
- # So now, we have firstHalf and secondHalf, which are now both just numbers. All we need to do is compare them!
- return (firstHalf == secondHalf)
- # The following is a "helper function" that we use a couple times in the evaluateEquation function above.
- def simplifyHalf(half):
- # The goal here is to start from the first number, the first operator,
- # and the second number (For example, "3 * 5"). Evaluate that, and replace
- # those three items with the result ("21"). Repeat until you only have a number left.
- while(('+' in half) or ('-' in half) or ('*' in half) or ('/' in half)):
- # We need to find the first operator (of any type) in the string. We do this in the "findIndexOfFirstOperator" function, which you can find defined below.
- firstOperatorIndex = findIndexOfFirstOperator(half)
- # Now to find the second one. Why do we need to know where the second operator is?
- # This will tell us where to stop looking for numbers, so we'll just have two numbers with an operator in between.
- # The operator we're looking for is really the first operator *after* the one we already found.
- secondOperatorIndex = findIndexOfFirstOperator(half[(firstOperatorIndex + 1):])
- # It's possible the second operator does not exist - Let's set it to the end of the string if that's the case
- if(secondOperatorIndex == -1):
- secondOperatorIndex = len(half)
- else:
- secondOperatorIndex = secondOperatorIndex + firstOperatorIndex + 1 # Otherwise, get the position in the entire string
- # Let's figure out what those two numbers are, that are separated by an operator.
- firstNumber = half[0:firstOperatorIndex]
- secondNumber = half[(firstOperatorIndex + 1):secondOperatorIndex]
- print(firstNumber)
- print(secondNumber)
- # So now, we have firstNumber, firstOperator, and secondNumber. Now we can simplify it!
- if(half[firstOperatorIndex] == "+"):
- val = int(firstNumber) + int(secondNumber)
- elif(half[firstOperatorIndex] == "-"):
- val = int(firstNumber) - int(secondNumber)
- elif(half[firstOperatorIndex] == '*'):
- val = int(firstNumber) * int(secondNumber)
- elif(half[firstOperatorIndex] == '/'):
- val = int(firstNumber) / int(secondNumber)
- # We now have a simplified value, so let's plug it in.
- half = str(val) + half[secondOperatorIndex:]
- # When this loop completes, we'll have just a number!
- return half
- # The following is a "helper function" that we use a couple times in the simplifyHalf function above.
- def findIndexOfFirstOperator(expression):
- # Find the first of each type of operator
- firstPlus = expression.find('+')
- firstMinus = expression.find('-')
- firstTimes = expression.find('*')
- firstDiv = expression.find('/')
- # Now to find which of these actually comes first. We're trying to find the lowest of these numbers that is NOT -1
- firstOp = firstPlus
- if(firstOp == -1 or (firstMinus != -1 and firstMinus < firstOp)):
- firstOp = firstMinus
- if(firstOp == -1 or (firstTimes != -1 and firstTimes < firstOp)):
- firstOp = firstTimes
- if(firstOp == -1 or (firstDiv != -1 and firstDiv < firstOp)):
- firstOp = firstDiv
- return firstOp
- # So now that all our functions are defined, here's all that the main program takes:
- equationFromConsole = input("Enter equation: ")
- while(equationFromConsole != "exit"): # This is how we get it to quit out
- print(evaluateEquation(equationFromConsole))
- equationFromConsole = input("Enter equation: ")
- # All done! The real trick to all of this was being able to take work that
- # you have to do multiple times, and make it a function. We did this three times:
- # 1. For finding the first operator in a string
- # 2. For simplifying one half of the expression to a single number
- # 3. For figuring out whether an equation is true or false
- # All of these functions build on each other, and that's the key to solving a really complex problem
- # with code - Break it down into parts, and treat each part as a simpler problem.

RAW Paste Data

We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.