lundi 8 juin 2020

Why control is going inside if condition while the condition is false

Function to find minimum number of eliminations such that sum of all adjacent elements is even

def min_elimination(n, arr): 
    countOdd = 0

    # Stores the new value 
    for i in range(n): 

        # Count odd numbers 
       ***if (arr[i] % 2): 
              countOdd += 1***

    # Return the minimum of even and 
    # odd count 
    return min(countOdd, n - countOdd) 

# Driver code 
if __name__ == '__main__': 
    arr = [1, 2, 3, 7, 9] 
    n = len(arr) 
    print(min_elimination(n, arr)) 

Please help me with the if condition. As the code works if(number%2) then control is going inside whereas the first element of list is odd number. So is there any difference between if(number%2) and if(number%2==0). Because when I tried if(number%2==0) control didn't go inside the if as the number was odd(check first element of the list).

Aucun commentaire:

Enregistrer un commentaire