Important Updates: Code line 12 changed from "universalTracker 2" to "universalTracker = 2"
I'm doing the adjacentElementsProduct challenge on CodeSignal, and it's asking me to create a function that shows the highest product from each adjacent pair of numbers in the list. I wrote this, and I can't seem to find what's wrong with it. Once I press run in VSCode, it just hangs. I have to force quit the program.
def adjacentElementsProduct(inputArray):
highestProduct = 0
universalTracker = 0
firstNum = inputArray[universalTracker]
secondNum = inputArray[universalTracker 1]
arrayLength = len(inputArray)
while universalTracker != (arrayLength - 1):
currentProduct = firstNum * secondNum
if currentProduct > highestProduct:
highestProduct = currentProduct
universalTracker = 2
adjacentElementsProduct([3, 6, -2, -5, 7, 3])
If anyone could give me an explanation as to what I did wrong, a working solution, and why it works, I would greatly appreciate it.
CodePudding user response:
If you notice, you are doing the following inside the while loop.
universalTracker 2
This isn't really updating the variable that you intended to update.
To make it work, you should do
universalTracker = universalTracker 2
Or equivalently
universalTracker = 2
CodePudding user response:
It's simple Just change while loop condition to
while universalTracker >= (arrayLength - 1):
Because, let's say your array length is 5, and universalTracker is updating by 2, it won't be equal to 5 It will be 2,4,6,8,10,12 ... and so on.
Let me know if this makes sense..