Question: Python This function checks if the number is greater than the next number and if it does, count increases by 1. How can I modify

Python

This function checks if the number is greater than the next number and if it does, count increases by 1. How can I modify it so that it will ignore/does not compare with 0? For example, the following puzzle should output 1 and not 2 puzzle = [[1, 2, 3],[4, 5, 6],[8, 7, 0]]

Code:

def getInvCount(arr) : inv_count = 0 for i in range(len(arr)): for j in range(len(arr[i])-1): #the comparison is only done in each row, so last column is checked in len(arr[i])-1 print("comparing " ,arr[i][j]," and ", arr[i][j+1]) if (arr[i][j] > (arr[i][j+1])): #comparing the next element in the 2D array inv_count += 1 print ("Count = ",inv_count) return inv_count

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!