How to find all the possible rows which are permutations of a given row in a matrix with Python

2 Answers

0 votes
def printPermutationRows(matrix, givenrow): 
    rows = len(matrix)
    cols = len(matrix[0])
    
    st = set() 
  
    for j in range(cols): 
        st.add(matrix[givenrow][j])     
  
    for i in range(rows): 
        if i == givenrow: 
            continue
  
        for j in range(cols): 
            if matrix[i][j] not in st: 
                j = j - 2
                break; 
        
        if j + 1 != cols: 
            continue
        
        print(i) 

  
givenrow = 1

matrix = [
    [7, 9, 4, 3, 1],
    [4, 7, 9, 1, 3],
    [4, 7, 8, 1, 2],
    [1, 6, 9, 7, 4],
    [9, 1, 3, 7, 4]]
  
printPermutationRows(matrix, givenrow) 



'''
run:

0
4

'''

 



answered Feb 22, 2024 by avibootz
0 votes
def printPermutationRows(matrix, givenrow): 
    rows = len(matrix)

    st = set(matrix[givenrow])
    for i in range(rows):
        if i == givenrow:
            continue
        if all(element in st for element in matrix[i]):
            print(i)

  
givenrow = 1

matrix = [
    [7, 9, 4, 3, 1, 5],
    [4, 7, 9, 1, 3, 5],
    [4, 7, 8, 1, 2, 5],
    [1, 6, 9, 7, 4, 5],
    [9, 1, 3, 7, 4, 5]]
  
printPermutationRows(matrix, givenrow) 




'''
run:

0
4

'''

 



answered Feb 22, 2024 by avibootz
...