Author name: Vikram Chiluka

Program to Print Series 1,2,8,16,32...n

Python Program to Print Series 1, 2, 4, 8, 16, 32…n

In the previous article, we have discussed Python Program to Swap Upper Diagonal Elements with Lower Diagonal Elements of Matrix.
Given a number N and the task is to print the series (1,2,4,8,16,32…N) till the given number N in Python.

Examples:

Example1:

Input:

Given Number = 200

Output:

The above series till the given number{ 200 } is : 
1 2 4 8 16 32 64 128

Explanation:

1, 1*2, 2*2, 4*2, 8*2, 16*2, 32*2, 64*2
we cannot have 128*2=256 because it exceeds the given limit number 200.

Example2:

Input:

Given Number = 50

Output:

The above series till the given number{ 50 } is :
1 2 4 8 16 32

Explanation:

1, 1*2, 2*2, 4*2, 8*2, 16*2
we cannot have 32*2= 64 because it exceeds the given limit number 50.

Program to Print Series 1,2,8,16,32…n in Python

Below are the ways to print the series (1,2,8,16,32…N) till the given number N in Python:

Method #1: Using While Loop (Static Input)

Approach:

  • Give the number N as static input and store it in a variable.
  • Take a variable to say itr and initialize its value to 1.
  • Loop until the above-declared variable itr value is less than or equal to the given number using the while loop.
  • If it is true then print the value of itr separated by spaces.
  • Multiply the itr value with 2 and store it in the same variable itr.
  • The Exit of the Program.

Below is the implementation:

# Give the number N as static input and store it in a variable.
gvn_numb = 200
# Take a variable to say itr and initialize its value to 1.
itr = 1
# Loop until the above-declared variable itr value is less than or equal to the
# given number using the while loop.
print("The above series till the given number{", gvn_numb, "} is :")
while itr <= gvn_numb:
  # If it is true then print the value of itr separated by spaces.
    print(itr, end=" ")
    # Multiply the itr value with 2 and store it in the same variable itr.
    itr *= 2

Output:

The above series till the given number{ 200 } is :
1 2 4 8 16 32 64 128

Method #2: Using While loop (User Input)

Approach:

  • Give the number N as user input using the int(input()) function and store it in a variable.
  • Take a variable to say itr and initialize its value to 1.
  • Loop until the above-declared variable itr value is less than or equal to the given number using the while loop.
  • If it is true then print the value of itr separated by spaces.
  • Multiply the itr value with 2 and store it in the same variable itr.
  • The Exit of the Program.

Below is the implementation:

# Give the number N as user input using the int(input()) function and store it in a variable.
gvn_numb = int(input("Enter some Random Number = "))
# Take a variable to say itr and initialize its value to 1.
itr = 1
# Loop until the above-declared variable itr value is less than or equal to the
# given number using the while loop.
print("The above series till the given number{", gvn_numb, "} is :")
while itr <= gvn_numb:
  # If it is true then print the value of itr separated by spaces.
    print(itr, end=" ")
    # Multiply the itr value with 2 and store it in the same variable itr.
    itr *= 2

Output:

Enter some Random Number = 50
The above series till the given number{ 50 } is :
1 2 4 8 16 32

Find a comprehensive collection of Examples of Python Programs ranging from simple ones to complex ones to guide you throughout your coding journey.

Python Program to Print Series 1, 2, 4, 8, 16, 32…n Read More »

Program to Find Unique Elements in a Matrix

Python Program to Find Unique Elements in a Matrix

In the previous article, we have discussed Python Program to Find Maximum Element of each Row in a Matrix

Given a matrix, the task is to find the unique matrix elements in the given matrix 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 2 2
3 4 2 
5 1 5

Output:

The unique matrix elements in the given matrix are :
1 2 3 4 5

Example2:

Input:

Given Matrix : 
3 4 4
1 6 5
88 5 3

Output:

The unique matrix elements in the given matrix are :
3 4 1 6 5 88

Program to Find Unique Elements in a Matrix in Python

Below are the ways to find the unique matrix elements in the given matrix in python:

Method #1: Using Counter() function (Hashing, Static Input)

Approach:

  • Import the Counter Function from collections using the import keyword.
  • 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 list and initialize it to empty using [] or list() function.
  • Loop till the given number of rows using the For loop.
  • Inside the For loop, Iterate till the given number of columns using another Nested For loop(Inner For loop).
  • Add the gvnmatrix[n][m] value to the list using the append() function where n is the iterator value of the parent For loop and m is the iterator value of the inner For loop.
  • Apply the Counter function to the matrix elements list which stores the matrix element as key and its frequency as value
  • Print all the keys of the mtrxfreq(using keys() function) which are the uniques values in the given matrix
  • The Exit of the Program.

Below is the implementation:

# import the Counter Function from collections using the import keyword
from collections import Counter
# Give the matrix as static input and store it in a variable.
mtrx = [[1, 2, 2], [3, 4, 2], [5, 1, 5]]
# 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 list and initialize it to empty using [] or list() function
mtrxlist = []
# Loop till the given number of rows using the For loop.
for n in range(mtrxrows):
    # Inside the For loop, Iterate till the given number of columns using another
    # Nested For loop(Inner For loop).
    for m in range(mtrxcols):
        # add the gvnmatrix[n][m] value to the list using the append() function
        # where n is the iterator value of the parent For loop and m is the iterator
        # value of the inner For loop.
        mtrxlist.append(mtrx[n][m])
# Apply the Counter function to the matrix elements list which stores the matrix element
# as key and its frequency as value
mtrxfreq = Counter(mtrxlist)
# Print all the keys of the mtrxfreq(using keys() function)
# which are the uniques values in the given matrix
print('The unique matrix elements in the given matrix are :')
print(*mtrxfreq.keys())

Output:

The unique matrix elements in the given matrix are :
1 2 3 4 5

Method #2: Using Counter() function (Hashing, User Input)

Approach:

  • Import the Counter Function from collections using the import keyword.
  • 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 list and initialize it to empty using [] or list() function.
  • Loop till the given number of rows using the For loop.
  • Inside the For loop, Iterate till the given number of columns using another Nested For loop(Inner For loop).
  • Add the gvnmatrix[n][m] value to the list using the append() function where n is the iterator value of the parent For loop and m is the iterator value of the inner For loop.
  • Apply the Counter function to the matrix elements list which stores the matrix element as key and its frequency as value
  • Print all the keys of the mtrxfreq(using keys() function) which are the uniques values in the given matrix
  • The Exit of the Program.

Below is the implementation:

# import the Counter Function from collections using the import keyword
from collections import Counter
# 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 list and initialize it to empty using [] or list() function
mtrxlist = []
# Loop till the given number of rows using the For loop.
for n in range(mtrxrows):
    # Inside the For loop, Iterate till the given number of columns using another
    # Nested For loop(Inner For loop).
    for m in range(mtrxcols):
        # add the gvnmatrix[n][m] value to the list using the append() function
        # where n is the iterator value of the parent For loop and m is the iterator
        # value of the inner For loop.
        mtrxlist.append(mtrx[n][m])
# Apply the Counter function to the matrix elements list which stores the matrix element
# as key and its frequency as value
mtrxfreq = Counter(mtrxlist)
# Print all the keys of the mtrxfreq(using keys() function)
# which are the uniques values in the given matrix
print('The unique matrix elements in the given matrix are :')
print(*mtrxfreq.keys())

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 = 3 4 4
Enter {3} elements of row {2} separated by spaces = 1 6 5
Enter {3} elements of row {3} separated by spaces = 88 5 3
The unique matrix elements in the given matrix are :
3 4 1 6 5 88

If you wanna write simple python programs as a part of your coding practice refer to numerous Simple Python Program Examples existing and learn the approach used.

Python Program to Find Unique Elements in a Matrix Read More »

Program to Find the Row with Maximum Number of 1s in Matrix

Python Program to Find the Row with Maximum Number of 1s in Matrix

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.

Python Program to Find the Row with Maximum Number of 1s in Matrix Read More »

Program to Calculate Sum of all Maximum Occurring Elements in Matrix

Python Program to Calculate Sum of all Maximum Occurring Elements in Matrix

In the previous article, we have discussed Python Program to Calculate Sum of all Minimum Frequency Elements in Matrix

Given a matrix, the task is to find the sum of all Maximum occurring elements of the given Matrix 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 2 2
3 4 2
5 1 5

Output:

The sum of all the maximum frequency elements in the given matrix is = { 2 }

Explanation:

Here the element 2 have maximum frequency i.e 3 so sum=2

Example2:

Input:

Given Matrix : 
5 5 5
7 7 7
1 2 3

Output:

The sum of all the maximum frequency elements in the given matrix is = { 12 }

Program to Calculate Sum of all Maximum Occurring Elements in Matrix in Python

Below are the ways to find the sum of all maximum Occurring elements of the given Matrix in Python.

Method #1: Using Counter() function (Hashing, Static Input)

Approach:

  • Import the Counter Function from collections using the import keyword.
  • 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 list and initialize it to empty using [] or list() function.
  • Loop till the given number of rows using the For loop.
  • Inside the For loop, Iterate till the given number of columns using another Nested For loop(Inner For loop).
  • Add the gvnmatrix[n][m] value to the list using the append() function where n is the iterator value of the parent For loop and m is the iterator value of the inner For loop.
  • Apply the Counter function to the matrix elements list which stores the matrix element as key and its frequency as value.
  • Calculate the maximum frequency using the values() and max() function(Here values function convert the frequency dictionary values to list and we find the maximum value from it using the max() function) and store it in a variable say maxFreq.
  • Take a variable(say maxFreqSum) that stores the sum of all the maximum frequency elements of the matrix and initialize its value to 0.
  • Loop in this frequency dictionary using the For loop.
  • Check if any key has a frequency equal to maxFreq using the if conditional statement.
  • If it is true then add the key(Matrix Element) to the maxFreqSum.
  • Print the maxFreqSum.
  • The Exit of the Program.

Below is the implementation:

# import the Counter Function from collections using the import keyword
from collections import Counter
# Give the matrix as static input and store it in a variable.
mtrx = [[1, 2, 2], [3, 4, 2], [5, 1, 5]]
# 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 list and initialize it to empty using [] or list() function
mtrxlist = []
# Loop till the given number of rows using the For loop.
for n in range(mtrxrows):
    # Inside the For loop, Iterate till the given number of columns using another
    # Nested For loop(Inner For loop).
    for m in range(mtrxcols):
        # add the gvnmatrix[n][m] value to the list using the append() function
        # where n is the iterator value of the parent For loop and m is the iterator
        # value of the inner For loop.
        mtrxlist.append(mtrx[n][m])
# Apply the Counter function to the matrix elements list which stores the matrix element
# as key and its frequency as value
mtrxfreq = Counter(mtrxlist)
# Calculate the maximum frequency using the values() and max() function
# (Here values function convert the frequency dictionary values to list
# and we find the maximum value from it using the max() function)
# and store it in a variable say maxFreq.
maxFreq = max(mtrxfreq.values())
# Take a variable(say maxFreqSum) that stores the sum of all the maximum frequency elements of the matrix
# and initialize its value to 0.
maxFreqSum = 0
# Loop in this frequency dictionary using the For loop.
for key in mtrxfreq:
    # Check if any key has a frequency equal to maxFreq using the if conditional statement.
    if(mtrxfreq[key] == maxFreq):
        # If it is true then add the key(Matrix Element) to the maxFreqSum.
        maxFreqSum = maxFreqSum+key
# Print the maxFreqSum.
print(
    'The sum of all the maximum frequency elements in the given matrix is = {', maxFreqSum, '}')

Output:

The sum of all the maximum frequency elements in the given matrix is = { 2 }

Method #2: Using Counter() function (Hashing, User Input)

Approach:

  • Import the Counter Function from collections using the import keyword.
  • 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 list and initialize it to empty using [] or list() function.
  • Loop till the given number of rows using the For loop.
  • Inside the For loop, Iterate till the given number of columns using another Nested For loop(Inner For loop).
  • Add the gvnmatrix[n][m] value to the list using the append() function where n is the iterator value of the parent For loop and m is the iterator value of the inner For loop.
  • Apply the Counter function to the matrix elements list which stores the matrix element as key and its frequency as value
  • Calculate the maximum frequency using the values() and max() function(Here values function convert the frequency dictionary values to list and we find the maximum value from it using the max() function) and store it in a variable say maxFreq.
  • Take a variable(say maxFreqSum) that stores the sum of all the maximum frequency elements of the matrix and initialize its value to 0.
  • Loop in this frequency dictionary using the For loop.
  • Check if any key has a frequency equal to maxFreq using the if conditional statement.
  • If it is true then add the key(Matrix Element) to the maxFreqSum.
  • Print the maxFreqSum.
  • The Exit of the Program.

Below is the implementation:

# import the Counter Function from collections using the import keyword
from collections import Counter
# 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 list and initialize it to empty using [] or list() function
mtrxlist = []
# Loop till the given number of rows using the For loop.
for n in range(mtrxrows):
    # Inside the For loop, Iterate till the given number of columns using another
    # Nested For loop(Inner For loop).
    for m in range(mtrxcols):
        # add the gvnmatrix[n][m] value to the list using the append() function
        # where n is the iterator value of the parent For loop and m is the iterator
        # value of the inner For loop.
        mtrxlist.append(mtrx[n][m])
# Apply the Counter function to the matrix elements list which stores the matrix element
# as key and its frequency as value
mtrxfreq = Counter(mtrxlist)
# Calculate the maximum frequency using the values() and max() function
# (Here values function convert the frequency dictionary values to list
# and we find the maximum value from it using the max() function)
# and store it in a variable say maxFreq.
maxFreq = max(mtrxfreq.values())
# Take a variable(say maxFreqSum) that stores the sum of all the maximum frequency elements of the matrix
# and initialize its value to 0.
maxFreqSum = 0
# Loop in this frequency dictionary using the For loop.
for key in mtrxfreq:
    # Check if any key has a frequency equal to maxFreq using the if conditional statement.
    if(mtrxfreq[key] == maxFreq):
        # If it is true then add the key(Matrix Element) to the maxFreqSum.
        maxFreqSum = maxFreqSum+key
# Print the maxFreqSum.
print(
    'The sum of all the maximum frequency elements in the given matrix is = {', maxFreqSum, '}')

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 = 5 5 5
Enter {3} elements of row {2} separated by spaces = 7 7 7
Enter {3} elements of row {3} separated by spaces = 1 2 3
The sum of all the maximum frequency elements in the given matrix is = { 12 }

If you are new to the Python Programming Language then practice using our Python Programming Examples for Beginners as our expert team has designed them from scratch.

Python Program to Calculate Sum of all Maximum Occurring Elements in Matrix Read More »

Program to Calculate Sum of all Minimum Frequency Elements in Matrix

Python Program to Calculate Sum of all Minimum Frequency Elements in Matrix

In the previous article, we have discussed Python Program to Find Sum of all Odd Frequency Elements in Matrix

Given a matrix, the task is to find the sum of all minimum Frequency elements of the given Matrix 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 2 2
3 4 2
5 1 5

Output:

The sum of all the minimum frequency elements in the given matrix is = { 7 }

Explanation:

Here the elements 4,3 have minimum frequency i.e 1 so sum=4+3=7

Example2:

Input:

Given Matrix : 
1 1 1
2 2 3
3 4 4

Output:

The sum of all the minimum frequency elements in the given matrix is = { 9 }

Program to Calculate Sum of all Minimum Frequency Elements in Matrix in Python

Below are the ways to find the sum of all minimum Frequency elements of the given Matrix in Python.

Method #1: Using Counter() function (Hashing, Static Input)

Approach:

  • Import the Counter Function from collections using the import keyword.
  • 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 list and initialize it to empty using [] or list() function.
  • Loop till the given number of rows using the For loop.
  • Inside the For loop, Iterate till the given number of columns using another Nested For loop(Inner For loop).
  • Add the gvnmatrix[n][m] value to the list using the append() function where n is the iterator value of the parent For loop and m is the iterator value of the inner For loop.
  • Apply the Counter function to the matrix elements list which stores the matrix element as key and its frequency as value.
  • Calculate the minimum frequency using the values() and min() function(Here values function convert the frequency dictionary values to list and we find the minimum value from it using the min() function) and store it in a variable say minFreq.
  • Take a variable(say minFreqSum) that stores the sum of all the minimum frequency elements of the matrix and initialize its value to 0.
  • Loop in this frequency dictionary using the For loop.
  • Check if any key has a frequency equal to minFreq using the if conditional statement.
  • If it is true then add the key(Matrix Element) to the minFreqSum.
  • Print the minFreqSum.
  • The Exit of the Program.

Below is the implementation:

# import the Counter Function from collections using the import keyword
from collections import Counter
# Give the matrix as static input and store it in a variable.
mtrx = [[1, 2, 2], [3, 4, 2], [5, 1, 5]]
# 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 list and initialize it to empty using [] or list() function
mtrxlist = []
# Loop till the given number of rows using the For loop.
for n in range(mtrxrows):
    # Inside the For loop, Iterate till the given number of columns using another
    # Nested For loop(Inner For loop).
    for m in range(mtrxcols):
        # add the gvnmatrix[n][m] value to the list using the append() function
        # where n is the iterator value of the parent For loop and m is the iterator
        # value of the inner For loop.
        mtrxlist.append(mtrx[n][m])
# Apply the Counter function to the matrix elements list which stores the matrix element
# as key and its frequency as value
mtrxfreq = Counter(mtrxlist)
# Calculate the minimum frequency using the values() and min() function
# (Here values function convert the frequency dictionary values to list
# and we find the minimum value from it using the min() function)
# and store it in a variable say minFreq.
minFreq = min(mtrxfreq.values())
# Take a variable(say minFreqSum) that stores the sum of all the minimum frequency elements of the matrix
# and initialize its value to 0.
minFreqSum = 0
# Loop in this frequency dictionary using the For loop.
for key in mtrxfreq:
    # Check if any key has a frequency equal to minFreq using the if conditional statement.
    if(mtrxfreq[key] == minFreq):
        # If it is true then add the key(Matrix Element) to the minFreqSum.
        minFreqSum = minFreqSum+key
# Print the minFreqSum.
print(
    'The sum of all the minimum frequency elements in the given matrix is = {', minFreqSum, '}')

Output:

The sum of all the minimum frequency elements in the given matrix is = { 7 }

Method #2: Using Counter() function (Hashing, User Input)

Approach:

  • Import the Counter Function from collections using the import keyword.
  • 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 list and initialize it to empty using [] or list() function.
  • Loop till the given number of rows using the For loop.
  • Inside the For loop, Iterate till the given number of columns using another Nested For loop(Inner For loop).
  • Add the gvnmatrix[n][m] value to the list using the append() function where n is the iterator value of the parent For loop and m is the iterator value of the inner For loop.
  • Apply the Counter function to the matrix elements list which stores the matrix element as key and its frequency as value
  • Calculate the minimum frequency using the values() and min() function(Here values function convert the frequency dictionary values to list and we find the minimum value from it using the min() function) and store it in a variable say minFreq.
  • Take a variable(say minFreqSum) that stores the sum of all the minimum frequency elements of the matrix and initialize its value to 0.
  • Loop in this frequency dictionary using the For loop.
  • Check if any key has a frequency equal to minFreq using the if conditional statement.
  • If it is true then add the key(Matrix Element) to the minFreqSum.
  • Print the minFreqSum.
  • The Exit of the Program.

Below is the implementation:

# import the Counter Function from collections using the import keyword
from collections import Counter
# 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 list and initialize it to empty using [] or list() function
mtrxlist = []
# Loop till the given number of rows using the For loop.
for n in range(mtrxrows):
    # Inside the For loop, Iterate till the given number of columns using another
    # Nested For loop(Inner For loop).
    for m in range(mtrxcols):
        # add the gvnmatrix[n][m] value to the list using the append() function
        # where n is the iterator value of the parent For loop and m is the iterator
        # value of the inner For loop.
        mtrxlist.append(mtrx[n][m])
# Apply the Counter function to the matrix elements list which stores the matrix element
# as key and its frequency as value
mtrxfreq = Counter(mtrxlist)
# Calculate the minimum frequency using the values() and min() function
# (Here values function convert the frequency dictionary values to list
# and we find the minimum value from it using the min() function)
# and store it in a variable say minFreq.
minFreq = min(mtrxfreq.values())
# Take a variable(say minFreqSum) that stores the sum of all the minimum frequency elements of the matrix
# and initialize its value to 0.
minFreqSum = 0
# Loop in this frequency dictionary using the For loop.
for key in mtrxfreq:
    # Check if any key has a frequency equal to minFreq using the if conditional statement.
    if(mtrxfreq[key] == minFreq):
        # If it is true then add the key(Matrix Element) to the minFreqSum.
        minFreqSum = minFreqSum+key
# Print the minFreqSum.
print(
    'The sum of all the minimum frequency elements in the given matrix is = {', minFreqSum, '}')

 

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 1
Enter {3} elements of row {2} separated by spaces = 2 2 3
Enter {3} elements of row {3} separated by spaces = 3 4 4
The sum of all the minimum frequency elements in the given matrix is = { 9 }

Remediate your knowledge gap by attempting the Python Code Examples regularly and understand the areas of need and work on them.

Python Program to Calculate Sum of all Minimum Frequency Elements in Matrix Read More »

Program to Find Sum of all Odd Frequency Elements in Matrix

Python Program to Find Sum of all Odd Frequency Elements in Matrix

In the previous article, we have discussed Python Program to Find Sum of all Even Frequency Elements in Matrix

Given a matrix, the task is to find the sum of all odd Frequency elements of the given Matrix 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 2 2
3 4 2
5 1 5

Output:

The sum of all the odd frequency elements in the given matrix is = { 9 }

Explanation:

Here the odd Frequency elements in the given matrix are 2,4,3 so sum=2+4+3=9

Example2:

Input:

Given Matrix : 
7 2
2 1

Output:

The sum of all the odd frequency elements in the given matrix is = { 8 }

Program to Find Sum of all Odd Frequency Elements in Matrix

Below are the ways to find the sum of all the odd Frequency elements of the given Matrix in Python.

Method #1: Using Counter() function (Hashing, Static Input)

Approach:

  • Import the Counter Function from collections using the import keyword.
  • 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 list and initialize it to empty using [] or list() function.
  • Loop till the given number of rows using the For loop.
  • Inside the For loop, Iterate till the given number of columns using another Nested For loop(Inner For loop).
  • Add the gvnmatrix[n][m] value to the list using the append() function where n is the iterator value of the parent For loop and m is the iterator value of the inner For loop.
  • Apply the Counter function to the matrix elements list which stores the matrix element as key and its frequency as value
  • Take a variable(say oddFreqSum) that stores the sum of all the odd frequency elements of the matrix and initialize its value to 0.
  • Loop in this frequency dictionary using the For loop.
  • Check if any key has frequency odd using the if conditional statement.
  • If it is true then add the key(Matrix Element) to the oddFreqSum.
  • Print the oddFreqSum.
  • The Exit of the Program.

Below is the implementation:

# import the Counter Function from collections using the import keyword
from collections import Counter
# Give the matrix as static input and store it in a variable.
mtrx = [[1, 2, 2], [3, 4, 2], [5, 1, 5]]
# 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 list and initialize it to empty using [] or list() function
mtrxlist = []
# Loop till the given number of rows using the For loop.
for n in range(mtrxrows):
    # Inside the For loop, Iterate till the given number of columns using another
    # Nested For loop(Inner For loop).
    for m in range(mtrxcols):
        # add the gvnmatrix[n][m] value to the list using the append() function
        # where n is the iterator value of the parent For loop and m is the iterator
        # value of the inner For loop.
        mtrxlist.append(mtrx[n][m])
# Apply the Counter function to the matrix elements list which stores the matrix element
# as key and its frequency as value
mtrxfreq = Counter(mtrxlist)
# Take a variable(say oddFreqSum) that stores the sum of all the odd frequency elements
# of the matrix and initialize its value to 0.
oddFreqSum = 0
# Loop in this frequency dictionary using the For loop.
for key in mtrxfreq:
    # Check if any key has frequency odd using the if conditional statement.
    if(mtrxfreq[key] % 2 != 0):
        # If it is true then add the key(Matrix Element) to the oddFreqSum.
        oddFreqSum = oddFreqSum+key
# Print the oddFreqSum.
print(
    'The sum of all the odd frequency elements in the given matrix is = {', oddFreqSum, '}')

Output:

The sum of all the odd frequency elements in the given matrix is = { 9 }

Method #2: Using Counter() function (Hashing, User Input)

Approach:

  • Import the Counter Function from collections using the import keyword.
  • 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 list and initialize it to empty using [] or list() function.
  • Loop till the given number of rows using the For loop.
  • Inside the For loop, Iterate till the given number of columns using another Nested For loop(Inner For loop).
  • Add the gvnmatrix[n][m] value to the list using the append() function where n is the iterator value of the parent For loop and m is the iterator value of the inner For loop.
  • Apply the Counter function to the matrix elements list which stores the matrix element as key and its frequency as value
  • Take a variable(say oddFreqSum) that stores the sum of all the odd frequency elements of the matrix and initialize its value to 0.
  • Loop in this frequency dictionary using the For loop.
  • Check if any key has frequency odd using the if conditional statement.
  • If it is true then add the key(Matrix Element) to the oddFreqSum.
  • Print the oddFreqSum.
  • The Exit of the Program.

Below is the implementation:

# import the Counter Function from collections using the import keyword
from collections import Counter
# 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 list and initialize it to empty using [] or list() function
mtrxlist = []
# Loop till the given number of rows using the For loop.
for n in range(mtrxrows):
    # Inside the For loop, Iterate till the given number of columns using another
    # Nested For loop(Inner For loop).
    for m in range(mtrxcols):
        # add the gvnmatrix[n][m] value to the list using the append() function
        # where n is the iterator value of the parent For loop and m is the iterator
        # value of the inner For loop.
        mtrxlist.append(mtrx[n][m])
# Apply the Counter function to the matrix elements list which stores the matrix element
# as key and its frequency as value
mtrxfreq = Counter(mtrxlist)
# Take a variable(say oddFreqSum) that stores the sum of all the odd frequency elements
# of the matrix and initialize its value to 0.
oddFreqSum = 0
# Loop in this frequency dictionary using the For loop.
for key in mtrxfreq:
    # Check if any key has frequency odd using the if conditional statement.
    if(mtrxfreq[key] % 2 != 0):
        # If it is true then add the key(Matrix Element) to the oddFreqSum.
        oddFreqSum = oddFreqSum+key
# Print the oddFreqSum.
print(
    'The sum of all the odd frequency elements in the given matrix is = {', oddFreqSum, '}')

Output:

Enter some random number of rows of the matrix = 2
Enter some random number of columns of the matrix = 2
Enter {2} elements of row {1} separated by spaces = 7 2
Enter {2} elements of row {2} separated by spaces = 2 1
The sum of all the odd frequency elements in the given matrix is = { 8 }

Explore more Example Python Programs with output and explanation and practice them for your interviews, assignments and stand out from the rest of the crowd.

Python Program to Find Sum of all Odd Frequency Elements in Matrix Read More »

Program to Find Sum of all Even Frequency Elements in Matrix

Python Program to Find Sum of all Even Frequency Elements in Matrix

In the previous article, we have discussed Python Program to Find Elements Larger than Half of the Elements in an Array/List

Given a matrix, the task is to find the sum of all r Frequency elements of the given Matrix 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 2 2
3 4 2
5 1 5

Output:

The sum of all the even frequency elements in the given matrix is = { 6 }

Explanation:

Here the even Frequency elements in the given matrix are 1,5 so sum=1+5=6

Example2:

Input:

Given Matrix : 
2 2 3 3
1 7 8 3
8 9 1 7
5 6 7 8

Output:

The sum of all the even frequency elements in the given matrix is = { 3 }

Program to Find Sum of all Even Frequency Elements in Matrix in Python

Below are the ways to find the sum of all the even Frequency elements of the given Matrix in Python.

Method #1: Using Counter() function (Hashing, Static Input)

Approach:

  • Import the Counter Function from collections using the import keyword.
  • 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 list and initialize it to empty using [] or list() function.
  • Loop till the given number of rows using the For loop.
  • Inside the For loop, Iterate till the given number of columns using another Nested For loop(Inner For loop).
  • Add the gvnmatrix[n][m] value to the list using the append() function where n is the iterator value of the parent For loop and m is the iterator value of the inner For loop.
  • Apply the Counter function to the matrix elements list which stores the matrix element as key and its frequency as value
  • Take a variable(say evenFreqSum) that stores the sum of all the even frequency elements of the matrix and initialize its value to 0.
  • Loop in this frequency dictionary using the For loop.
  • Check if any key has frequency even using the if conditional statement.
  • If it is true then add the key(Matrix Element) to the evenFreqSum.
  • Print the evenFreqSum.
  • The Exit of the Program.

Below is the implementation:

# import the Counter Function from collections using the import keyword
from collections import Counter
# Give the matrix as static input and store it in a variable.
mtrx = [[1, 2, 2], [3, 4, 2], [5, 1, 5]]
# 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 list and initialize it to empty using [] or list() function
mtrxlist = []
# Loop till the given number of rows using the For loop.
for n in range(mtrxrows):
    # Inside the For loop, Iterate till the given number of columns using another
    # Nested For loop(Inner For loop).
    for m in range(mtrxcols):
        # add the gvnmatrix[n][m] value to the list using the append() function
        # where n is the iterator value of the parent For loop and m is the iterator
        # value of the inner For loop.
        mtrxlist.append(mtrx[n][m])
# Apply the Counter function to the matrix elements list which stores the matrix element
# as key and its frequency as value
mtrxfreq = Counter(mtrxlist)
# Take a variable(say evenFreqSum) that stores the sum of all the even frequency elements
# of the matrix and initialize its value to 0.
evenFreqSum = 0
# Loop in this frequency dictionary using the For loop.
for key in mtrxfreq:
    # Check if any key has frequency even using the if conditional statement.
    if(mtrxfreq[key] % 2 == 0):
        # If it is true then add the key(Matrix Element) to the evenFreqSum.
        evenFreqSum = evenFreqSum+key
# Print the evenFreqSum.
print(
    'The sum of all the even frequency elements in the given matrix is = {', evenFreqSum, '}')

Output:

The sum of all the even frequency elements in the given matrix is = { 6 }

Method #2: Using Counter() function (Hashing, User Input)

Approach:

  • Import the Counter Function from collections using the import keyword.
  • 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 list and initialize it to empty using [] or list() function.
  • Loop till the given number of rows using the For loop.
  • Inside the For loop, Iterate till the given number of columns using another Nested For loop(Inner For loop).
  • Add the gvnmatrix[n][m] value to the list using the append() function where n is the iterator value of the parent For loop and m is the iterator value of the inner For loop.
  • Apply the Counter function to the matrix elements list which stores the matrix element as key and its frequency as value
  • Take a variable(say evenFreqSum) that stores the sum of all the even frequency elements of the matrix and initialize its value to 0.
  • Loop in this frequency dictionary using the For loop.
  • Check if any key has frequency even using the if conditional statement.
  • If it is true then add the key(Matrix Element) to the evenFreqSum.
  • Print the evenFreqSum.
  • The Exit of the Program.

Below is the implementation:

# import the Counter Function from collections using the import keyword
from collections import Counter
# 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 list and initialize it to empty using [] or list() function
mtrxlist = []
# Loop till the given number of rows using the For loop.
for n in range(mtrxrows):
    # Inside the For loop, Iterate till the given number of columns using another
    # Nested For loop(Inner For loop).
    for m in range(mtrxcols):
        # add the gvnmatrix[n][m] value to the list using the append() function
        # where n is the iterator value of the parent For loop and m is the iterator
        # value of the inner For loop.
        mtrxlist.append(mtrx[n][m])
# Apply the Counter function to the matrix elements list which stores the matrix element
# as key and its frequency as value
mtrxfreq = Counter(mtrxlist)
# Take a variable(say evenFreqSum) that stores the sum of all the even frequency elements
# of the matrix and initialize its value to 0.
evenFreqSum = 0
# Loop in this frequency dictionary using the For loop.
for key in mtrxfreq:
    # Check if any key has frequency even using the if conditional statement.
    if(mtrxfreq[key] % 2 == 0):
        # If it is true then add the key(Matrix Element) to the evenFreqSum.
        evenFreqSum = evenFreqSum+key
# Print the evenFreqSum.
print(
    'The sum of all the even frequency elements in the given matrix is = {', evenFreqSum, '}')

Output:

Enter some random number of rows of the matrix = 4
Enter some random number of columns of the matrix = 4
Enter {4} elements of row {1} separated by spaces = 2 2 3 3
Enter {4} elements of row {2} separated by spaces = 1 7 8 3
Enter {4} elements of row {3} separated by spaces = 8 9 1 7
Enter {4} elements of row {4} separated by spaces = 5 6 7 8
The sum of all the even frequency elements in the given matrix is = { 3 }

If you are learning Python then the Python Programming Example is for you and gives you a thorough description of concepts for beginners, experienced programmers.

Python Program to Find Sum of all Even Frequency Elements in Matrix Read More »

Program to Find Elements Larger than Half of the Elements in an ArrayList

Python Program to Find Elements Larger than Half of the Elements in an Array/List

In the previous article, we have discussed Python Program to Sort the Given Matrix

Given a list and the task is to find the elements in a list that are greater than half of the total number of elements in the given list.

In the case of odd elements, we must print elements greater than floor(n/2) where n is the total number of elements in the given list.

Examples:

Example1:

Input:

Given List = [4, 6, 3, 1, 8, 9]

Output:

The elements in a list that are greater than half of the total number of elements in the given list
6
8
9

Explanation:

Here the elements which are greater than 4 are 6,8,9

Example2:

Input:

Given List =  [1, 0, 6, 9, 3, 4, 2, 6, 3]

Output:

The elements in a list that are greater than half of the total number of elements in the given list
3
4
6
6
9

Program to Find Elements Larger than Half of the Elements in an Array/List in Python

Below are the ways to find the elements in a list that are greater than half of the total number of elements in the given list:

Method #1: Using For Loop (Static Input)

Approach:

  • Give the list as static input and store it in a variable.
  • Calculate the length of the given list using the len() function and store it in another variable.
  • Pass the given list and length of the given list as the arguments to the getElementsLarger() function.
  • Create a function to say getElementsLarger() which takes the given list and length of the given list as the arguments and prints the elements in a list that are greater than half of the total number of elements in the given list.
  • Inside the function, sort the given list using the sorted() function and store it in a variable.
  • Loop from half the length of the given list to the length of the given list-1 using the for loop.
  • Inside the loop, print the element present at the iterator value of the above-sorted list.
  • The Exit of the Program.

Below is the implementation:

# Create a function to say getElementsLarger() which takes the given list and length of
# the given list as the arguments and prints the elements in a list that are greater than
# half of the total number of elements in the given list.


def getElementsLarger(gvn_lst, lengt_lst):
        # Inside the function, sort the given list using the sorted() function and store
        # it in a variable.
    k = sorted(gvn_lst)
    # Loop from half the length of the given list to the length of the given list-1
    # using the for loop.
    for itr in range(lengt_lst//2, lengt_lst):
     # Inside the loop, print the element present at the iterator value of the
     # above-sorted list.

        print(k[itr])


# Give the list as static input and store it in a variable.
gvn_lst = [4, 6, 3, 1, 8, 9]
# Calculate the length of the given list using the len() function and store it in
# another variable.
lengt_lst = len(gvn_lst)
print("The elements in a list that are greater than half of the total number of elements in the given list")
# Pass the given list and length of the given list as the arguments to the
# getElementsLarger() function.
getElementsLarger(gvn_lst, lengt_lst)

Output:

The elements in a list that are greater than half of the total number of elements in the given list
6
8
9

Method #2: Using For loop (User Input)

Approach:

  • Give the list as user input using the list(),map(),split(),int functions and store it in a variable.
  • Calculate the length of the given list using the len() function and store it in another variable.
  • Pass the given list and length of the given list as the arguments to the getElementsLarger() function.
  • Create a function to say getElementsLarger() which takes the given list and length of the given list as the arguments and prints the elements in a list that are greater than half of the total number of elements in the given list.
  • Inside the function, sort the given list using the sorted() function and store it in a variable.
  • Loop from half the length of the given list to the length of the given list-1 using the for loop.
  • Inside the loop, print the element present at the iterator value of the above-sorted list.
  • The Exit of the Program.

Below is the implementation:

# Create a function to say getElementsLarger() which takes the given list and length of
# the given list as the arguments and prints the elements in a list that are greater than
# half of the total number of elements in the given list.


def getElementsLarger(gvn_lst, lengt_lst):
        # Inside the function, sort the given list using the sorted() function and store
        # it in a variable.
    k = sorted(gvn_lst)
    # Loop from half the length of the given list to the length of the given list-1
    # using the for loop.
    for itr in range(lengt_lst//2, lengt_lst):
     # Inside the loop, print the element present at the iterator value of the
     # above-sorted list.

        print(k[itr])


# Give the list as user input using the list(),map(),split(),int functions and 
# store it in a variable.
gvn_lst = list( map(int, input('Enter some random list element separated by spaces = ').split()))
# Calculate the length of the given list using the len() function and store it in
# another variable.
lengt_lst = len(gvn_lst)
print("The elements in a list that are greater than half of the total number of elements in the given list")
# Pass the given list and length of the given list as the arguments to the
# getElementsLarger() function.
getElementsLarger(gvn_lst, lengt_lst)

Output:

Enter some random list element separated by spaces = 10 20 45 23 45 10
The elements in a list that are greater than half of the total number of elements in the given list
23
45
45

Grab the opportunity and utilize the Python Program Code Examples over here to prepare basic and advanced topics too with ease and clear all your doubts.

Python Program to Find Elements Larger than Half of the Elements in an Array/List Read More »

Program to Sort the Given Matrix

Python Program to Sort the Given Matrix

In the previous article, we have discussed Python Program to Find Unique Elements in a Matrix

Given a matrix and the task is to sort the given matrix 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 5 3
9 11 2
6 2 3

Output:

The given matrix after sorting: 
1 2 2 
3 3 5 
6 9 11

Example2:

Input:

Given Matrix :
6 2 5
2 3 1
4 2 0

Output:

The given matrix after sorting: 
0 1 2 
2 2 3 
4 5 6

Program to Sort the Given Matrix in Python

Below are the ways to sort the given matrix 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 list and initialize it to empty using [] or list() function.
  • Loop till the given number of rows using the For loop.
  • Inside the For loop, Iterate till the given number of columns using another Nested For loop(Inner For loop).
  • Add the gvnmatrix[n][m] value to the list using the append() function where n is the iterator value of the parent For loop and m is the iterator value of the inner For loop.
  • Sort the matrix list using the sort() function.
  • Take a variable to store the pointer to index of matrix list and initialize its value to 0.
  • Loop till the given number of rows using the For loop.
  • Inside the For loop, Iterate till the given number of columns using another Nested For loop(Inner For loop).
  • Assign the gvnmatrix[n][m] value with the mtrxlist[p] value where n is the iterator value of the parent For loop and m is the iterator value of the inner For loop.
  • Print the mtrx[n][m] value.
  • Increment the pointer(p) with 1.
  • The Exit of the Program.
Below is the implementation:
# Give the matrix as static input and store it in a variable.
mtrx = [[1, 5, 3], [9, 11, 2], [6, 2, 3]]
# 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 list and initialize it to empty using [] or list() function
mtrxlist = []
# Loop till the given number of rows using the For loop.
for n in range(mtrxrows):
    # Inside the For loop, Iterate till the given number of columns using another
    # Nested For loop(Inner For loop).
    for m in range(mtrxcols):
        # add the gvnmatrix[n][m] value to the list using the append() function
        # where n is the iterator value of the parent For loop and m is the iterator
        # value of the inner For loop.
        mtrxlist.append(mtrx[n][m])
# sort the matrix list using the  sort() function
mtrxlist.sort()
# Take a variable to store the pointer to index of matrix list and initialize its value to 0
p = 0
print("The given matrix after sorting: ")
# Loop till the given number of rows using the For loop.
for n in range(mtrxrows):
    # Inside the For loop, Iterate till the given number of columns using another
    # Nested For loop(Inner For loop).
    for m in range(mtrxcols):
        # Assign the gvnmatrix[n][m] value with the mtrxlist[p] value
        # where n is the iterator value of the parent For loop and m is the iterator
        # value of the inner For loop.
        mtrx[n][m] = mtrxlist[p]
        # print the mtrx[n][m] value
        print(mtrx[n][m], end=' ')
        # increment the pointer(p) with 1
        p = p+1
    print()

Output:

The given matrix after sorting: 
1 2 2 
3 3 5 
6 9 11

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 list and initialize it to empty using [] or list() function.
  • Loop till the given number of rows using the For loop.
  • Inside the For loop, Iterate till the given number of columns using another Nested For loop(Inner For loop).
  • Add the gvnmatrix[n][m] value to the list using the append() function where n is the iterator value of the parent For loop and m is the iterator value of the inner For loop.
  • Sort the matrix list using the sort() function.
  • Take a variable to store the pointer to index of matrix list and initialize its value to 0.
  • Loop till the given number of rows using the For loop.
  • Inside the For loop, Iterate till the given number of columns using another Nested For loop(Inner For loop).
  • Assign the gvnmatrix[n][m] value with the mtrxlist[p] value where n is the iterator value of the parent For loop and m is the iterator value of the inner For loop.
  • Print the mtrx[n][m] value.
  • Increment the pointer(p) with 1.
  • The Exit of the Program.

Below is the implementation:

# import the Counter Function from collections using the import keyword
from collections import Counter
# 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 list and initialize it to empty using [] or list() function
mtrxlist = []
# Loop till the given number of rows using the For loop.
for n in range(mtrxrows):
    # Inside the For loop, Iterate till the given number of columns using another
    # Nested For loop(Inner For loop).
    for m in range(mtrxcols):
        # add the gvnmatrix[n][m] value to the list using the append() function
        # where n is the iterator value of the parent For loop and m is the iterator
        # value of the inner For loop.
        mtrxlist.append(mtrx[n][m])
# sort the matrix list using the  sort() function
mtrxlist.sort()
# Take a variable to store the pointer to index of matrix list and initialize its value to 0
p = 0
print("The given matrix after sorting: ")
# Loop till the given number of rows using the For loop.
for n in range(mtrxrows):
    # Inside the For loop, Iterate till the given number of columns using another
    # Nested For loop(Inner For loop).
    for m in range(mtrxcols):
        # Assign the gvnmatrix[n][m] value with the mtrxlist[p] value
        # where n is the iterator value of the parent For loop and m is the iterator
        # value of the inner For loop.
        mtrx[n][m] = mtrxlist[p]
        # print the mtrx[n][m] value
        print(mtrx[n][m], end=' ')
        # increment the pointer(p) with 1
        p = p+1
    print()

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 = 6 2 5
Enter {3} elements of row {2} separated by spaces = 2 3 1
Enter {3} elements of row {3} separated by spaces = 4 2 0
The given matrix after sorting: 
0 1 2 
2 2 3 
4 5 6

Practice Python Program Examples to master coding skills and learn the fundamental concepts in the dynamic programming language Python.

Python Program to Sort the Given Matrix Read More »

Program to Find Maximum Element of each Row in a Matrix

Python Program to Find Maximum Element of each Row in a Matrix

In the previous article, we have discussed Python Program to Find the Row with Maximum Number of 1s in Matrix

Given a matrix, the task is to find the maximum element of each row for the given matrix 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 5 6
11 5 4
9 34 5

Output:

The maximum element of the row { 1 } is : 6
The maximum element of the row { 2 } is : 11
The maximum element of the row { 3 } is : 34

Example2:

Input:

Given Matrix : 
1 6 2
10 14 1
6 9 11

Output:

The maximum element of the row { 1 } is : 6
The maximum element of the row { 2 } is : 14
The maximum element of the row { 3 } is : 11

Program to Find Maximum Element of each Row in a Matrix in Python

Below are the ways to find the maximum element of each row for the given matrix 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.
  • Calculate the maximum element of the row using the max() function and store it in a variable.
  • Print the maximum element of the given row.
  • The Exit of the Program.

Below is the implementation:

# Give the matrix as static input and store it in a variable.
mtrx = [[1, 5, 6], [11, 5, 4], [9, 34, 5]]
# 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 maximum element of the row using the max()
    # function and store it in a variable
    maxRowEle = max(mtrx[p])
    # print the maximum element of the given row
    print('The maximum element of the row {', p+1, '} is :', maxRowEle)

Output:

The maximum element of the row { 1 } is : 6
The maximum element of the row { 2 } is : 11
The maximum element of the row { 3 } is : 34

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.
  • Calculate the maximum element of the row using the max() function and store it in a variable.
  • Print the maximum element of the given row.
  • 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 maximum element of the row using the max()
    # function and store it in a variable
    maxRowEle = max(mtrx[p])
    # print the maximum element of the given row
    print('The maximum element of the row {', p+1, '} is :', maxRowEle)

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 6 2
Enter {3} elements of row {2} separated by spaces = 10 14 1
Enter {3} elements of row {3} separated by spaces = 6 9 11
The maximum element of the row { 1 } is : 6
The maximum element of the row { 2 } is : 14
The maximum element of the row { 3 } is : 11

Find a comprehensive collection of Examples of Python Programs ranging from simple ones to complex ones to guide you throughout your coding journey.

Python Program to Find Maximum Element of each Row in a Matrix Read More »