In the previous article, we have discussed Python Program for Markov Matrix
Given a matrix with just 0 and 1 elements, the task is to find the row in the matrix with the greatest number of 1s in that row in Python.
What is a matrix:
A matrix is a rectangular sequence of numbers divided into columns and rows. A matrix element or entry is a number that appears in a matrix.
Example:
Above is the matrix which contains 5 rows and 4 columns and having elements from 1 to 20.
In this order, the dimensions of a matrix indicate the number of rows and columns.
Here as there are 5 rows and 4 columns it is called a 5*4 matrix.
Examples:
Example1:
Input:
Given Matrix : 1 0 0 1 1 1 1 0 1
Output:
The row which is containing maximum number of 1 is : 2
Example2:
Input:
Given Matrix : 1 1 0 0 0 1 0 1 1
Output:
The row which is containing maximum number of 1 is : 3
Note: If both the rows are having the same number of 1’s then it prints the last row.
Program to Find the Row with Maximum Number of 1s in Matrix in Python
Below are the ways to find the row in the matrix with the greatest number of 1s in that row in Python:
Method #1: Using For Loop (Static Input)
Approach:
- Give the matrix as static input and store it in a variable.
- Calculate the number of rows of the given matrix by calculating the length of the nested list using the len() function and store it in a variable mtrxrows.
- Calculate the number of columns of the given matrix by calculating the length of the first list in the nested list using the len() function and store it in a variable mtrxcolums.
- Take a variable that stores the number of 1’s in the row and initialize it with 0.
- Loop till the given number of rows using the For loop.
- Inside the loop, calculate the number of ones in that row using the count() function.
- Check if the number of ones in that row is greater or equal to than maxOnes using the if conditional statement.
- If it is true then initialize maxOnes with numbOfOnes.
- Take a variable and store this row number in this row.
- Print the row that is containing the maximum number of 1’s by printing maxOnesRow.
- The Exit of the Program.
Below is the implementation:
# Give the matrix as static input and store it in a variable. mtrx = [[1, 0, 0], [1, 1, 1], [1, 0, 1]] # Calculate the number of rows of the given matrix by # calculating the length of the nested list using the len() function # and store it in a variable mtrxrows. mtrxrows = len(mtrx) # Calculate the number of columns of the given matrix by # calculating the length of the first list in the nested list # using the len() function and store it in a variable mtrxcols. mtrxcols = len(mtrx[0]) # take a variable which stores the number of 1's in the row and initialize it with 0 maxOnes = 0 # Loop till the given number of rows using the For loop. for p in range(mtrxrows): # calculate the number of ones in that row using the count() function numbOfOnes = mtrx[p].count(1) # check if the number of ones in that row is greater or equal to # than maxOnes using the if conditional statement if(numbOfOnes >= maxOnes): # if it is true then initializee maxOnes with numbOfOnes maxOnes = numbOfOnes # take a variable and store this row number in this row maxOnesRow = p+1 # print the row that is containing maximum number of 1's by printing maxOnesRow print('The row which is containing maximum number of 1 is :', maxOnesRow)
Output:
The row which is containing maximum number of 1 is : 2
Method #2: Using For loop (User Input)
Approach:
- Give the number of rows of the matrix as user input using the int(input()) function and store it in a variable.
- Give the number of columns of the matrix as user input using the int(input()) function and store it in another variable.
- Take a list and initialize it with an empty value using [] or list() to say gvnmatrix.
- Loop till the given number of rows using the For loop
- Inside the For loop, Give all the row elements of the given Matrix as a list using the list(),map(),int(),split() functions and store it in a variable.
- Add the above row elements list to gvnmatrix using the append() function.
- Take a variable that stores the number of 1’s in the row and initialize it with 0.
- Loop till the given number of rows using the For loop.
- Inside the loop, calculate the number of ones in that row using the count() function.
- Check if the number of ones in that row is greater or equal to than maxOnes using the if conditional statement.
- If it is true then initialize maxOnes with numbOfOnes.
- Take a variable and store this row number in this row.
- Print the row that is containing the maximum number of 1’s by printing maxOnesRow.
- The Exit of the Program.
Below is the implementation:
# Give the number of rows of the matrix as user input using the int(input()) function # and store it in a variable. mtrxrows = int(input('Enter some random number of rows of the matrix = ')) # Give the number of columns of the matrix as user input using the int(input()) function # and store it in another variable. mtrxcols = int(input('Enter some random number of columns of the matrix = ')) # Take a list and initialize it with an empty value using [] or list() to say gvnmatrix. mtrx = [] # Loop till the given number of rows using the For loop for n in range(mtrxrows): # Inside the For loop, Give all the row elements of the given Matrix as a list using # the list(),map(),int(),split() functions and store it in a variable. l = list(map(int, input( 'Enter {'+str(mtrxcols)+'} elements of row {'+str(n+1)+'} separated by spaces = ').split())) # Add the above row elements list to gvnmatrix using the append() function. mtrx.append(l) # take a variable which stores the number of 1's in the row and initialize it with 0 maxOnes = 0 # Loop till the given number of rows using the For loop. for p in range(mtrxrows): # calculate the number of ones in that row using the count() function numbOfOnes = mtrx[p].count(1) # check if the number of ones in that row is greater or equal to # than maxOnes using the if conditional statement if(numbOfOnes >= maxOnes): # if it is true then initializee maxOnes with numbOfOnes maxOnes = numbOfOnes # take a variable and store this row number in this row maxOnesRow = p+1 # print the row that is containing maximum number of 1's by printing maxOnesRow print('The row which is containing maximum number of 1 is :', maxOnesRow)
Output:
Enter some random number of rows of the matrix = 3 Enter some random number of columns of the matrix = 3 Enter {3} elements of row {1} separated by spaces = 1 1 0 Enter {3} elements of row {2} separated by spaces = 0 0 1 Enter {3} elements of row {3} separated by spaces = 0 1 1 The row which is containing maximum number of 1 is : 3
Find the best practical and ready-to-use Python Programming Examples that you can simply run on a variety of platforms and never stop learning.