Click here to Skip to main content
15,039,149 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Now out of this list of lis how can I find a maximum number???
NOTE: max() is showing a NoneType Error

What I have tried:

Python
def adjacentElementsProduct(inputArray):
    for i in range(0, len(inputArray)-1):
        lis=(inputArray[i]*inputArray[i+1])
        print(lis)
Posted
Updated 13hrs ago
v2
Comments
Patrice T 14-Oct-17 14:39pm
   
Where is the max function in your code ?

What you are looking for is the largest product of two numbers in a list: which means multiplying each pair and comparing each against the previous maximum, not just printing each product.
So try this:
1) Create a variable outside the loop, call it maxSoFar and set it to the product of the first pair.
2) Loop through each element (except the final one)
2.1) Multiply each element by the one after it.
2.2) Compare that value with maxSoFar
2.2.1) If it's larger, set maxSoFar to the new product.
3) Continue with the loop
4) After the loop, print maxSoFar

Simple! But ... this is your homework, so I'll give you no code! Give it a try - this really isn't a difficult task.
   
Python
def adjacentElementsProduct(inputArray):
    lst1 = 0
    for i in range(0, len(inputArray)-1):
        lst2 = inputArray[i] * inputArray[i+1]
        if lst2 > lst1:
            lst1 = lst2<
    return lst1
   
v3
Comments
Richard MacCutchan 23-Jun-20 5:04am
   
"try to provide proper indentation"
Which you forgot to do. Also, this question is nearly three years old, so it is unlikely that the OP is still waiting for an answer.
function adjacentElementsProduct(inputArray) {
let max = -Infinity;
for (let i = 1; i < inputArray.length; i++) {
max = Math.max(inputArray[i] * inputArray[i - 1], max);
}

return max;
}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900