Computer >> Computer tutorials >  >> Programming >> Python

Python program using map function to find row with maximum number of 1's


2D array is given and the elements of the arrays are 0 and 1. All rows are sorted. We have to find row with maximum number of 1's. Here we use map ().The map function is the simplest one among Python built-ins used for functional programming. These tools apply functions to sequences and other iterables.

Example

Input
Array is [[0, 1, 1, 1, 1],[0, 0, 1, 1, 1],[1, 1, 1, 1, 1],[0, 0, 0, 0, 1]]
The maximum number of 1's = 2

Algorithm

Step 1: sum of on each row of the matrix using map function.
Step 2: it will return a list of sum of all one's in each row.
Step 3: then print index of maximum sum in a list.

Example Code

# Python program to find the row with maximum number of 1's
def maximumofones(n):
   max1 = list(map(sum,n))
   print ("MAXIMUM NUMBER OF 1's ::>",max1.index(max(max1)))
   # Driver program
   if __name__ == "__main__":
   n = [[0, 1, 1, 1, 1],[0, 0, 1, 1, 1],[1, 1, 1, 1, 1],[0, 0, 0, 0, 1]]
maximumofones(n)

Output

MAXIMUM NUMBER OF 1's ::> 2