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

PythonServer Side ProgrammingProgramming

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 :
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
raja
Published on 26-Sep-2018 15:13:08
Advertisements