Python program to find the redundancy rates for each row of a matrix
Given a Matrix, the task is to write a python program that can compute redundancy rates of each row, i.e rate of total number of repeated characters.
Redundancy Rate : Rate of repetition of elements.
Formula:
Examples:
Input : test_list = [[4, 5, 2, 4, 3], [5, 5, 5, 5, 5], [8, 7, 8, 8, 7], [1, 2, 3, 4, 5]]
Output : [0.19999999999999996, 0.8, 0.6, 0.0]
Explanation : 1 – [2/5] = 0.6 for 3rd row.
Input : test_list = [[5, 5, 5, 5, 5], [8, 7, 8, 8, 7], [1, 2, 3, 4, 5]]
Output : [0.8, 0.6, 0.0]
Explanation : 1 – [2/5] = 0.6 for 2nd row.
Method 1 : Using loop and set()
In this, we perform task of computing rate using fraction of unique elements computed using length of set, to all the elements in list, subtracted from 1.
Example:
Python3
# initializing list test_list = [[ 4 , 5 , 2 , 4 , 3 ], [ 5 , 5 , 5 , 5 , 5 ], [ 8 , 7 , 8 , 8 , 7 ], [ 1 , 2 , 3 , 4 , 5 ]] # printing original list print ( "The original list is : " + str (test_list)) res = [] for sub in test_list: # getting Redundancy res.append( 1 - len ( set (sub)) / len (sub)) # printing result print ( "Matrix Redundancy ? : " + str (res)) |
Output:
The original list is : [[4, 5, 2, 4, 3], [5, 5, 5, 5, 5], [8, 7, 8, 8, 7], [1, 2, 3, 4, 5]]
Matrix Redundancy ? : [0.19999999999999996, 0.8, 0.6, 0.0]
Time Complexity: O(n*m)
Auxiliary Space: O(n)
Method 2 : Using list comprehension
Uses similar functionality as above method, only difference is that its one liner solution computed using list comprehension.
Example:
Python3
# initializing list test_list = [[ 4 , 5 , 2 , 4 , 3 ], [ 5 , 5 , 5 , 5 , 5 ], [ 8 , 7 , 8 , 8 , 7 ], [ 1 , 2 , 3 , 4 , 5 ]] # printing original list print ( "The original list is : " + str (test_list)) # list comprehension for one liner res = [ 1 - len ( set (sub)) / len (sub) for sub in test_list] # printing result print ( "Matrix Redundancy ? : " + str (res)) |
Output:
The original list is : [[4, 5, 2, 4, 3], [5, 5, 5, 5, 5], [8, 7, 8, 8, 7], [1, 2, 3, 4, 5]]
Matrix Redundancy ? : [0.19999999999999996, 0.8, 0.6, 0.0]
Time Complexity: O(n) where n is the number of elements in the list “test_list”. The list comprehension is used to perform the task and it takes O(n) time.
Auxiliary Space: O(n), new list of size O(n) is created where n is the number of elements in the list
Method #3:Using Counter() function
Python3
from collections import Counter # initializing list test_list = [[ 4 , 5 , 2 , 4 , 3 ], [ 5 , 5 , 5 , 5 , 5 ], [ 8 , 7 , 8 , 8 , 7 ], [ 1 , 2 , 3 , 4 , 5 ]] # printing original list print ( "The original list is : " + str (test_list)) res = [] for sub in test_list: # getting Redundancy freq = Counter(sub) res.append( 1 - len (freq) / len (sub)) # printing result print ( "Matrix Redundancy ? : " + str (res)) |
The original list is : [[4, 5, 2, 4, 3], [5, 5, 5, 5, 5], [8, 7, 8, 8, 7], [1, 2, 3, 4, 5]] Matrix Redundancy ? : [0.19999999999999996, 0.8, 0.6, 0.0]
Time Complexity: O(N*N)
Auxiliary Space: O(N)
Please Login to comment...