Home » Swap Grid Diagonals

Swap Grid Diagonals

Swap the diagonals in 10×10 grid as shown.

📌 Challenge Details and Links
ExcelBI Excel Challenge Number: 701
Challenge Difficulty: ⭐️
📥Download Sample File
📥Link to the solutions on LinkedIn

Solving the challenge of Swap Grid Diagonals with Power Query

Power Query solution 1 for Swap Grid Diagonals, proposed by Meganathan Elumalai:
let
  Source = Excel.CurrentWorkbook(){[Name = "Table1"]}[Content], 
  Result = [
    Lst = Table.ToRows(Source), 
    n = List.Count(Lst) - 1, 
    L = List.Transform, 
    P = List.Positions, 
    fin = Table.FromRows(
      L(
        P(Lst), 
        (f) =>
          L(
            P(Lst), 
            (x) => if f = x then Lst{f}{n - x} else if x = n - f then Lst{f}{f} else Lst{f}{x}
          )
      )
    )
  ][fin]
in
  Result

Solving the challenge of Swap Grid Diagonals with Excel

Excel solution 1 for Swap Grid Diagonals, proposed by Bo Rydobon 🇹🇭:
=LET(x,A2:J11,c,-COLUMN(x),m,MUNIT(ROWS(x)),IF(m+SORTBY(m,c),SORTBY(x,c),x))
Excel solution 2 for Swap Grid Diagonals, proposed by Rick Rothstein:
=LET(
    a,
    A2:J11,
    n,
    ROWS(
        a
    ),
    MAKEARRAY(
        n,
        n,
        LAMBDA(
            r,
            c,
            IF(
                r=c,
                INDEX(
                    a,
                    r,
                    n-c+1
                ),
                IF(
                    c=n-r+1,
                    INDEX(
                    a,
                    r,
                    n-c+1
                ),
                    INDEX(
                        a,
                        r,
                        c
                    )
                )
            )
        )
    )
)
Excel solution 3 for Swap Grid Diagonals, proposed by Kris Jaganah:
=LET(a,A2:J11,b,ROWS(a),c,INDEX,MAKEARRAY(b,b,LAMBDA(x,y,IFS(x=y,c(a,x,b+1-x),x+y=b+1,c(a,x,x),1,c(a,x,y)))))
Excel solution 4 for Swap Grid Diagonals, proposed by Julian Poeltl:
=LET(
    A,
    A2:J11,
    R,
    ROWS(
        A
    ),
    MAKEARRAY(
        R,
        R,
        LAMBDA(
            B,
            C,
            INDEX(
                A,
                B,
                LET(
                    O,
                    R-B+1,
                    IF(
                        B=C,
                        O,
                        IF(
                            C=O,
                            B,
                            C
                        )
                    )
                )
            )
        )
    )
)
Excel solution 5 for Swap Grid Diagonals, proposed by Aditya Kumar Darak 🇮🇳:
=LET(
 _a, A2:J11,
 _n, ROWS(_a),
 _r, SEQUENCE(_n),
 _c, SEQUENCE(1, _n),
 INDEX(_a, _r, IFS(_r = _c, _n + 1 - _r, _c = _n + 1 - _r, _r, 1, _c))
)
Excel solution 6 for Swap Grid Diagonals, proposed by Timothée BLIOT:
=LET(
    A,
    A2:J11,
    I,
    INDEX,
    MAKEARRAY(
        10,
        10,
        LAMBDA(
            x,
            y,
            IF(
                x=y,
                I(
                    A,
                    x,
                    11-y
                ),
                IF(
                    11-x=y,
                    I(
                    A,
                    x,
                    11-y
                ),
                    I(
                        A,
                        x,
                        y
                    )
                )
            )
        )
    )
)
Excel solution 7 for Swap Grid Diagonals, proposed by Hussein SATOUR:
=LET(
    s,
    SEQUENCE(
        10
    ),
    m,
    SEQUENCE(
        10,
        10
    ),
    a,
    11*s-10,
    b,
    9*s+1,
    INDEX(
        TOCOL(
            A2:J11
        ),
        IF(
            m=a,
            b,
            IF(
                m=b,
                a,
                m
            )
        )
    )
)
Excel solution 8 for Swap Grid Diagonals, proposed by Duy Tùng:
=LET(a,A2:J11,b,ROWS(a),IFNA(MAKEARRAY(b,b,LAMBDA(x,v,IFS(v=x,INDEX(a,v,b+1-x),v+x-b=1,INDEX(a,x,x)))),a))
Excel solution 9 for Swap Grid Diagonals, proposed by Sunny Baggu:
=LET(
    
     _r1,
     SEQUENCE(
         ROWS(
             A2:J11
         )
     ),
    
     _r2,
     SORT(
         _r1,
          ,
          -1
     ),
    
     _c1,
     TOROW(
         _r1
     ),
    
     _c2,
     TOROW(
         _r2
     ),
    
     _a,
     INDEX(
         A2:J11,
          _c2,
          _r1
     ),
    
     _b,
     INDEX(
         A2:J11,
          _c1,
          _r2
     ),
    
     IF(
         _r2 = _c1,
          _a,
          IF(
              _r1 = _c1,
               _b,
               A2:J11
          )
     )
    
)
Excel solution 10 for Swap Grid Diagonals, proposed by LEONARD OCHEA 🇷🇴:
=LET(
    S,
    SEQUENCE,
    v,
    S(
        10
    ),
    h,
    S(
        ,
        10
    ),
    INDEX(
        A2:J11,
        v,
        IFS(
            v=h,
            11-h,
            v+h=11,
            11-h,
            1,
            h
        )
    )
)
Excel solution 11 for Swap Grid Diagonals, proposed by Anshu Bantra:
=MAKEARRAY( 10,
     10,
    
 LAMBDA( r,
     c,
    
 LET(
 data_,
     A2:J11,
    
 IFS(
 r = c,
      INDEX(data_,
     r,
     10 - (c - 1)),
    
 10 - (c - 1) = r,
     INDEX(
         data_,
          r,
          r
     ),
    
 TRUE,
     INDEX(
         data_,
          r,
          c
     )
 )
 )
 )
)
Excel solution 12 for Swap Grid Diagonals, proposed by Md. Zohurul Islam:
=LET(z,A2:J11,n,COLUMNS(z),
a,SEQUENCE(n)+SEQUENCE(,n),
v,TOCOL(IF(a=n+1,z,1/x),3),
w,TOCOL(IF(MUNIT(n),z,1/x),3),
j,IFS(z=v,w,MUNIT(n),v,1,z),
j)
Excel solution 13 for Swap Grid Diagonals, proposed by ferhat CK:
=LET(
    r,
    A2:J11,
    q,
    SEQUENCE(
        10,
        ,
        10,
        -1
    ),
    w,
    SEQUENCE(
        ,
        10,
        10,
        -1
    ),
    a,
    IF(
        IF(
            COLUMN(
                r
            )=q,
            r
        ),
        INDEX(
            IF(
                ROW(
                r
            )-1=COLUMN(
                r
            ),
                r
            ),
            SEQUENCE(
                10
            ),
            w
        ),
        ""
    ),
    b,
    INDEX(
        IF(
            COLUMN(
                r
            )=q,
            r,
            ""
        ),
        SEQUENCE(
                10
            ),
        w
    ),
    z,
    MAKEARRAY(
        10,
        10,
        LAMBDA(
            x,
            y,
            IFS(
                x=y,
                INDEX(
                    b,
                    x,
                    y
                ),
                y=10-x+1,
                INDEX(
                    a,
                    x,
                    y
                ),
                1,
                INDEX(
                    r,
                    x,
                    y
                )
            )
        )
    ),
    z
)
Excel solution 14 for Swap Grid Diagonals, proposed by Charles Roldan:
=LET(A, A2:J11, n, ROWS(A), i, SEQUENCE(n), j, TOROW(i), k, n + 1 - j, INDEX(A, i, IF((i = j) + (i = k), k, j)))
Excel solution 15 for Swap Grid Diagonals, proposed by Jaroslaw Kujawa:
=LET(
    x;
    A2:J11;
    i;
    INDEX;
    MAKEARRAY(
        ROWS(
            x
        );
        COLUMNS(
            x
        );
        LAMBDA(
            r;
            c;
            IF(
                r=c;
                i(
                    x;
                    r;
                    11-r
                );
                IF(
                    r+c=11;
                    i(
                        x;
                        r;
                        r
                    );
                    i(
                        x;
                        r;
                        c
                    )
                )
            )
        )
    )
)
Excel solution 16 for Swap Grid Diagonals, proposed by Meganathan Elumalai:
=LET(
    a,
    A2:J11,
    n,
    ROWS(
        a
    ),
    r,
    SEQUENCE(
        n
    ),
    c,
    TOROW(
        r
    ),
    IFS(
        r=c,
        INDEX(
            a,
            r,
            n-r+1
        ),
        n-c+1=r,
        INDEX(
            a,
            r,
            r
        ),
        1,
        a
    )
)
Excel solution 17 for Swap Grid Diagonals, proposed by Guillermo Arroyo:
=LET(
    m,
    A2:J11,
    MAKEARRAY(
        10,
        10,
        LAMBDA(
            i,
            j,
            IF(
                OR(
                    i=j,
                    i+j=11
                ),
                INDEX(
                    m,
                    i,
                    11-j
                ),
                INDEX(
                    m,
                    i,
                    j
                )
            )
        )
    )
)
Excel solution 18 for Swap Grid Diagonals, proposed by Fredson Alves Pinho:
=MAKEARRAY(10,10,LAMBDA(r,c,INDEX(A2:J11,r,((r=c)-(r+c=11))*(11-2*r)+c)))
Excel solution 19 for Swap Grid Diagonals, proposed by Ernesto Vega Castillo:
=LET(a,A2:J11,b,COLUMNS(a),MAKEARRAY(ROWS(a),b,LAMBDA(r,c,SWITCH(TRUE,r=c,INDEX(a,r,b-r+1),SWITCH(TRUE,c=b-r+1,INDEX(a,r,r),INDEX(a,r,c))))))
Excel solution 20 for Swap Grid Diagonals, proposed by Craig Runciman:
=LET(a,CHOOSECOLS(SEQUENCE(10,10)*(MUNIT(10)+CHOOSECOLS(MUNIT(10),SEQUENCE(10,,10,-1))),SEQUENCE(10,,10,-1)),b,IF(a,a,SEQUENCE(10,10)),c,TOCOL(A2:J11),MAP(b,LAMBDA(e,INDEX(c,e))))
Excel solution 21 for Swap Grid Diagonals, proposed by Ben Warshaw:
=LET(
    
     _,
     $A$2:$J$11,
    
     _Step1,
     MAKEARRAY(
         10,
          10,
          LAMBDA(
              r,
              c,
               IF(
                   r = c,
                    1,
                    0
               )
          )
     ),
    
     _Step2,
     MAKEARRAY(
         
          10,
         
          10,
         
          LAMBDA(
              r,
              c,
               INDEX(
                   _Step1,
                    r,
                    COLUMNS(
                        _Step1
                    ) + 1 - c
               )
          )
          
     ),
    
     _Step3,
     _Step1 + _Step2,
    
     _Result,
     IF(
         
          _Step3,
         
          MAKEARRAY(
              10,
               10,
               LAMBDA(
                   r,
                   c,
                    INDEX(
                        _,
                         r,
                         COLUMNS(
                             _
                         ) + 1 - c
                    )
               )
          ),
         
          _
          
     ),
    
     _Result
    
)
Excel solution 22 for Swap Grid Diagonals, proposed by Miguel Angel Franco García:
=LET(
    a;
    SECUENCIA(
        10;
        10;
        10;
        -1
    );
    INDICE(
        A2:J11;
        SECUENCIA(
            10
        );
        SI(
            a<=0;
            TOMAR(
                a;
                1
            );
            a
        )
    )
)
Excel solution 23 for Swap Grid Diagonals, proposed by 🍀 Nacho Cardenal:
=LET(_d;A2:J11;_i;INDICE;ARCHIVOMAKEARRAY(10;10;LAMBDA(_r;_c;SI.CONJUNTO(_r=_c;_i(_d;_r;11-_c);_c+_r=11;_i(_d;_r;_r);VERDADERO;_i(_r;_c)))))

Solving the challenge of Swap Grid Diagonals with Python

Python solution 1 for Swap Grid Diagonals, proposed by Konrad Gryczan, PhD:
import pandas as pd
import numpy as np
path = "701 Swap Diagonals.xlsx"
input_matrix = pd.read_excel(path, sheet_name=0, usecols="A:J", skiprows=1, nrows=10, header=None).to_numpy()
test_matrix = pd.read_excel(path, sheet_name=0, usecols="L:U", skiprows=1, nrows=10, header=None).to_numpy()
def swap_diagonals(matrix):
 for i in range(len(matrix)):
 matrix[i][i], matrix[i][~i] = matrix[i][~i], matrix[i][i]
 return matrix
res_matrix = swap_diagonals(input_matrix.copy())
print(np.array_equal(res_matrix, test_matrix)) # True
                    
                  
Python solution 2 for Swap Grid Diagonals, proposed by Abdallah Ally:
df = pd.read_excel(io=file_path, usecols='A:J', skiprows=1, header=None)
for i in df.index
 df.iat[i, i], df.iat[i, -(i + 1)] = df.iat[i, -(i + 1)],  df.iat[i, i]
                    
                  

Solving the challenge of Swap Grid Diagonals with Python in Excel

Python in Excel solution 1 &for Swap Grid Diagonals, proposed by Alejandro Campos:
grid = xl("A2:J11").values
n = grid.shape[0]
for i in range(n): grid[i,i], grid[i,n-i-1] = grid[i,n-i-1], grid[i,i]
grid
                    
                  
Python in Excel solution 2 for Swap Grid Diagonals, proposed by Aditya Kumar Darak 🇮🇳:
arr = xl("A2:J11").values
n = arr.shape[0]
i = np.arange(n)
arr[i, i], arr[i, n - 1 - i] = arr[i, n - 1 - i], arr[i, i].copy()
arr
                    
                  

Solving the challenge of Swap Grid Diagonals with R

R solution 1 for Swap Grid Diagonals, proposed by Konrad Gryczan, PhD:
library(tidyverse)
library(readxl)
path = "Excel/701 Swap Diagonals.xlsx"
input = read_excel(path, range = "A2:J11", col_names = FALSE) %>% as.matrix()
test = read_excel(path, range = "L2:U11", col_names = FALSE) %>% as.matrix()
d1 = diag(input)
d2 = diag(input[, ncol(input):1])
diag(input) <- d2
diag(input[, ncol(input):1]) <- d1
all(input == test) # [1] TRUE
                    
                  

&&

Leave a Reply