Sort Rows by Prime, Even and Odd Count
Coding Problem Keys
Sort Rows by Prime, Even and Odd Count
Problem Statement
An integer matrix of size R*C is given as input. The program must sort the rows based on the number of prime numbers in descending order. If two rows contain the same number of prime numbers then sort the rows based on the number of even numbers(also non-prime). If two rows contain the same number of prime numbers and the same number of even numbers then sort the rows based on the number of odd numbers (also non-prime). If two rows contain the same number of prime numbers, even numbers and odd numbers then sort the rows based on the sum of elements of each row in descending order.Boundary Condition(s)
1 <= R, C <= 100Input Format
The first line contains the value of R and C separated by space(s).Next R lines contains C integers each separated by space(s).
Output Format
R lines containing C integers with the rows sorted based on given conditions.Example Input/Output 1
Input
5 54 6 4 3 1
3 1 1 5 3
1 7 2 3 3
3 2 6 9 8
1 4 3 7 4
Output
1 7 2 3 3
3 1 1 5 3
3 2 6 9 8
1 4 3 7 4
4 6 4 3 1
Example Input/Output 2
Input
4 42 1 2 3
3 4 1 2
9 7 9 3
7 4 2 3
Output
7 4 2 32 1 2 3
3 4 1 2
9 7 9 3
Max Execution Time Limit: 5000 millisecs
Solution
Programming Language: Python 3 Language
import math
def prime(ab):
f=0
if(ab<2):
return 0
for i in range(2,int(math.sqrt(ab))+1):
if(ab%i==0):
f=1
break
if(f==0):
return 1
else:
return 0
a,b=map(int,input().split())
c=[list(map(int,input().split())) for i in range(a)]
d=[]
iterator=0
for i in c:
pcount=0
ecount=0
ocount=0
for j in i:
if(prime(j)==1):
pcount+=1
if(j%2!=0):
ocount+=1
if(j%2==0):
ecount+=1
d.append([pcount,ecount,ocount,sum(i),i])
iterator+=1
d=sorted(d,reverse=True)
for i in d:
print(*i[4])
# Published By PKJCODERS
(Note: Incase If the code doesn't Pass the output kindly comment us with your feedback to help us improvise.)
Comments