Banana-Leopard/TASTY huggingface dataset:
# Python3 program to find the difference
# between the sum of diagonal.
def difference(arr, n):
 
    # Initialize sums of diagonals
    d1 = 0
    d2 = 0
 
    for i in range(0, n):
        d1 = d1 + arr[i][i]
        d2 = d2 + arr[i][n - i - 1]
         
    # Absolute difference of the sums
    # across the diagonals
    return abs(d1 - d2)
 
# Driver Code
n = 3
 
arr = [[11, 2, 4],
       [4 , 5, 6],
       [10, 8, -12]]
 
print(difference(arr, n))
     
# This code is contributed
# by ihritik

Time complexity: linear
Space complexity: constant
# Python program to fill a matrix with
# values from 1 to n*n in spiral fashion.
 
# Fills a[m][n] with values
# from 1 to m*n in spiral fashion.
def spiralFill(m, n, a):
 
    # Initialize value to be filled in matrix.
    val = 1
 
    # k - starting row index
    # m - ending row index
    # l - starting column index
    # n - ending column index
    k, l = 0, 0
    while (k < m and l < n):
 
        # Print the first row from the remaining rows.
        for i in range(l, n):
            a[k][i] = val
            val += 1
        k += 1
 
        # Print the last column from the remaining columns.
        for i in range(k, m):
            a[i][n - 1] = val
            val += 1
        n -= 1
 
        # Print the last row from the remaining rows.
        if (k < m):
            for i in range(n - 1, l - 1, -1):
                a[m - 1][i] = val
                val += 1
            m -= 1
 
        # Print the first column from the remaining columns.
        if (l < n):
            for i in range(m - 1, k - 1, -1):
                a[i][l] = val
                val += 1
            l += 1
 
# Driver program
if __name__ == '__main__':
    m, n = 4, 4
    a = [[0 for j in range(n)] for i in range(m)]
    spiralFill(m, n, a)
    for i in range(m):
        for j in range(n):
            print(a[i][j], end=' ')
        print('')
 
# This code is contributed by Parin Shah

Time complexity: quadratic
Space complexity: quadratic
# A simple Python program to implement Game of Life
 
# driver program
 
# Function to print next generation
def nextGeneration(grid, M, N):
    future = [[0 for i in range(N)] for j in range(M)]
 
    # Loop through every cell
    for l in range(M):
        for m in range(N):
             
            # finding no Of Neighbours that are alive
            aliveNeighbours = 0
            for i in range(-1,2):
                for j in range(-1,2):
                    if ((l+i>=0 and l+i<M) and (m+j>=0 and m+j<N)):
                        aliveNeighbours += grid[l + i][m + j]
 
            # The cell needs to be subtracted from
            # its neighbours as it was counted before
            aliveNeighbours -= grid[l][m]
 
            # Implementing the Rules of Life
 
            # Cell is lonely and dies
            if ((grid[l][m] == 1) and (aliveNeighbours < 2)):
                future[l][m] = 0
 
            # Cell dies due to over population
            elif ((grid[l][m] == 1) and (aliveNeighbours > 3)):
                future[l][m] = 0
 
            # A new cell is born
            elif ((grid[l][m] == 0) and (aliveNeighbours == 3)):
                future[l][m] = 1
 
            # Remains the same
            else:
                future[l][m] = grid[l][m]
 
 
    print("Next Generation")
    for i in range(M):
        for j in range(N):
            if (future[i][j] == 0):
                print(".",end="")
            else:
                print("*",end="")
        print()
 
M,N = 10,10
 
# Initializing the grid.
grid = [ [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
    [ 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 ],
    [ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 ],
    [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
    [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
    [ 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 ],
    [ 0, 0, 1, 1, 0, 0, 0, 0, 0, 0 ],
    [ 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 ],
    [ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 ],
    [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]
]
 
# Displaying the grid
print("Original Generation")
for i in range(M):
    for j in range(N):
        
        if(grid[i][j] == 0):
            print(".",end = "")
        else:
            print("*",end = "")
    print()
print()
nextGeneration(grid, M, N)
 
# This code is contributed by shinjanpatra

Time complexity: quadratic
Space complexity: quadratic
# Python 3 program to find the maximum
# sum of hour glass in a Matrix
 
# Fixing the size of the Matrix.
# Here it is of order 6 x 6
R = 5
C = 5
 
# Function to find the maximum sum of hour glass
def MaxSum(arr):
 
    # Considering the matrix also contains
    max_sum = -50000 
     
    # Negative values , so initialized with
    # -50000. It can be any value but very
    # smaller.
    # max_sum=0 -> Initialize with 0 only if your
    # matrix elements are positive
 
    if(R < 3 or C < 3):
        print("Not possible")
        exit()
 
    # Here loop runs (R-2)*(C-2) times considering
    # different top left cells of hour glasses.
    for i in range(0, R-2):
        for j in range(0, C-2):
                     
            # Considering arr[i][j] as top
            # left cell of hour glass.
            SUM = (arr[i][j] + arr[i][j + 1] + arr[i][j + 2]) + (arr[i + 1][j + 1]) +       (arr[i + 2][j] +
                    arr[i + 2][j + 1] + arr[i + 2][j + 2])
 
            # If previous sum is less
            # then current sum then
            # update new sum in max_sum
            if(SUM > max_sum):
                max_sum = SUM
            else:
                continue
 
    return max_sum
 
 
# Driver Code
arr = [[1, 2, 3, 0, 0],
       [0, 0, 0, 0, 0],
       [2, 1, 4, 0, 0],
       [0, 0, 0, 0, 0],
       [1, 1, 0, 1, 0]]
res = MaxSum(arr)
 
print(f"Maximum sum of hour glass = {res}")
 
# This code is written by Akshay Prakash
# Code is modified by Susobhan Akhuli

Time complexity: quadratic
Space complexity: constant
# Python3 program for finding
# MAXimum and MINimum in a matrix.
MAX = 100
 
# Finds MAXimum and MINimum in arr[0..n-1][0..n-1]
# using pair wise comparisons
def MAXMIN(arr, n):
 
    MIN = 10**9
    MAX = -10**9
 
    # Traverses rows one by one
    for i in range(n):
        for j in range(n // 2 + 1):
             
        # Compare elements from beginning
        # and end of current row
            if (arr[i][j] > arr[i][n - j - 1]):
                if (MIN > arr[i][n - j - 1]):
                    MIN = arr[i][n - j - 1]
                if (MAX< arr[i][j]):
                    MAX = arr[i][j]
            else:
                if (MIN > arr[i][j]):
                    MIN = arr[i][j]
                if (MAX< arr[i][n - j - 1]):
                    MAX = arr[i][n - j - 1]
 
    print("MAXimum =", MAX, ", MINimum =", MIN)
 
# Driver Code
arr = [[5, 9, 11],
       [25, 0, 14],
       [21, 6, 4]]
 
MAXMIN(arr, 3)
 
# This code is contributed by Mohit Kumar

Time complexity: quadratic
Space complexity: constant
# Python 3 program to print
# matrix in anti-spiral form
R = 4
C = 5
 
def antiSpiralTraversal(m, n, a):
    k = 0
    l = 0
 
    # k - starting row index
    # m - ending row index
    # l - starting column index
    # n - ending column index
    # i - iterator
    stk = []
 
    while (k <= m and l <= n):
         
        # Print the first row
        # from the remaining rows
        for i in range(l, n + 1):
            stk.append(a[k][i])
        k += 1
 
        # Print the last column
        # from the remaining columns
        for i in range(k, m + 1):
            stk.append(a[i][n])
        n -= 1
 
        # Print the last row
        # from the remaining rows
        if ( k <= m):
            for i in range(n, l - 1, -1):
                stk.append(a[m][i])
            m -= 1
 
        # Print the first column
        # from the remaining columns
        if (l <= n):
            for i in range(m, k - 1, -1):
                stk.append(a[i][l])
            l += 1
         
    while len(stk) != 0:
        print(str(stk[-1]), end = " ")
        stk.pop()
 
# Driver Code
mat = [[1, 2, 3, 4, 5],
       [6, 7, 8, 9, 10],
       [11, 12, 13, 14, 15],
       [16, 17, 18, 19, 20]];
 
antiSpiralTraversal(R - 1, C - 1, mat)
 
# This code is contributed
# by ChitraNayal

Time complexity: quadratic
Space complexity: quadratic
# Python3 program to find trace and
# normal of given matrix
import math
 
# Size of given matrix
MAX = 100;
 
# Returns Normal of a matrix
# of size n x n
def findNormal(mat, n):
 
    sum = 0;
    for i in range(n):
        for j in range(n):
            sum += mat[i][j] * mat[i][j];
    return math.floor(math.sqrt(sum));
 
# Returns trace of a matrix of
# size n x n
def findTrace(mat, n):
 
    sum = 0;
    for i in range(n):
        sum += mat[i][i];
    return sum;
 
# Driver Code
mat = [[1, 1, 1, 1, 1],
       [2, 2, 2, 2, 2],
       [3, 3, 3, 3, 3],
       [4, 4, 4, 4, 4],
       [5, 5, 5, 5, 5]];
 
print("Trace of Matrix =", findTrace(mat, 5));
 
print("Normal of Matrix =", findNormal(mat, 5));
 
# This code is contributed by mits

Time complexity: quadratic
Space complexity: constant
# Python 3 program to find
# minimum operations required
# to set all the element of
# binary matrix
 
# Return minimum operation
# required to make all 1s.
def minOperation(arr):
    ans = 0
    for i in range(N - 1, -1, -1):
        for j in range(M - 1, -1, -1):
             
            # check if this
            # cell equals 0
            if(arr[i][j] == 0):
     
                # increase the
                # number of moves
                ans += 1
 
                # flip from this cell
                # to the start point
                for k in range(i + 1):
                    for h in range(j + 1):
                     
                        # flip the cell
                        if (arr[k][h] == 1):
                            arr[k][h] = 0
                        else:
                            arr[k][h] = 1
                     
    return ans
 
# Driver Code
mat = [[ 0, 0, 1, 1, 1],
       [0, 0, 0, 1, 1],
       [0, 0, 0, 1, 1],
       [1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1]]
M = 5
N = 5
 
print(minOperation(mat))
 
# This code is contributed
# by ChitraNayal

Time complexity: quadratic
Space complexity: quadratic
# Python3 Code to Print a given 
# matrix in reverse spiral form
 
# This is a modified code of
# https:#www.geeksforgeeks.org/print-a-given-matrix-in-spiral-form/
R, C = 3, 6
 
def ReversespiralPrint(m, n, a):
 
    # Large array to initialize it
    # with elements of matrix
    b = [0 for i in range(100)]
 
    #/* k - starting row index
    #l - starting column index*/
    i, k, l = 0, 0, 0
 
    # Counter for single dimension array
    # in which elements will be stored
    z = 0
 
    # Total elements in matrix
    size = m * n
 
    while (k < m and l < n):
         
        # Variable to store value of matrix.
        val = 0
 
        # Print the first row
        # from the remaining rows
        for i in range(l, n):
             
            # printf("%d ", a[k][i])
            val = a[k][i]
            b[z] = val
            z += 1
        k += 1
 
        # Print the last column
        # from the remaining columns
        for i in range(k, m):
 
            # printf("%d ", a[i][n-1])
            val = a[i][n - 1]
            b[z] = val
            z += 1
 
        n -= 1
 
        # Print the last row
        # from the remaining rows
        if (k < m):
            for i in range(n - 1, l - 1, -1):
                 
                # printf("%d ", a[m-1][i])
                val = a[m - 1][i]
                b[z] = val
                z += 1
 
        m -= 1
 
        # Print the first column
        # from the remaining columns
        if (l < n):
            for i in range(m - 1, k - 1, -1):
                 
                # printf("%d ", a[i][l])
                val = a[i][l]
                b[z] = val
                z += 1
            l += 1
 
    for i in range(size - 1, -1, -1):
        print(b[i], end = " ")
 
# Driver Code
a = [[1, 2, 3, 4, 5, 6],
     [7, 8, 9, 10, 11, 12],
     [13, 14, 15, 16, 17, 18]]
 
ReversespiralPrint(R, C, a)
 
# This code is contributed by mohit kumar

Time complexity: quadratic
Space complexity: constant
# Python 3 program to check
# whether given matrix
# is skew-symmetric or not
ROW=3
COL=3
 
# Utility function to
# create transpose matrix
def transpose(transpose_matrix,matrix):
    for i in range (ROW):
        for j in range(COL):
            transpose_matrix[j][i] = matrix[i][j]
             
# Utility function to
# check skew - symmetric
# matrix condition
def check(transpose_matrix,matrix):
    for i in range(ROW):
        for j in range(COL):
            if (matrix[i][j] != -transpose_matrix[i][j]):
                return False
    return True
     
# Utility function to print a matrix
def printMatrix(matrix):
    for i in range (ROW):
        for j in range(COL):
            print(matrix[i][j]," ",end="")
        print()
         
# Driver program to test above functions
matrix= [
            [0, 5, -4],
            [-5, 0, 1],
            [4, -1, 0],
        ]
transpose_matrix=[[0 for i in range(3)] for j in range(3)]
 
# Function create transpose matrix
transpose(transpose_matrix, matrix)
print("Transpose matrix:")
printMatrix(transpose_matrix)
 
# Check whether matrix is
# skew-symmetric or not
if (check(transpose_matrix, matrix)):
    print("Skew Symmetric Matrix")
else:
    print("Not Skew Symmetric Matrix")
 
# This code is contributed
# by Azkia Anam.

Time complexity: quadratic
Space complexity: quadratic
# Python 3 program to find sum of
# matrix element where each element
# is integer division of row and column.
 
# Return sum of matrix element
# where each element is division
# of its corresponding row and column.
def findSum(N):
    ans = 0
    for i in range(1, N + 1):
        for j in range(1, N + 1):
            ans += i // j
    return ans
 
# Driver code
N = 2
print(findSum(N))
 
# This code is contributed
# by Shrikant13

Time complexity: quadratic
Space complexity: constant
# Program to find sum of matrix element
# where each element is integer division 
# of row and column.
 
# Return sum of matrix element where each
# element is division of its corresponding
# row and column.
def findSum(n):
    ans = 0; temp = 0;
 
    for i in range(1, n + 1):
 
        # count the number of elements of
        # each column. Initialize to i -1
        # because number of zeroes are i - 1.
        if temp < n:
            temp = i - 1
 
            # For multiply
            num = 1
            while temp < n:
                if temp + i <= n:
                    ans += i * num
                else:
                    ans += (n - temp) * num
                temp += i
                num += 1
    return ans
 
# Driver Code
N = 2
print(findSum(N))
 
# This code is contributed by Shrikant13

Time complexity: quadratic
Space complexity: constant
# Python3 program to find number of
# countOpsation to make two matrix
# equals
def countOps(A, B, m, n):
 
    # Update matrix A[][] so that only
    # A[][] has to be countOpsed
    for i in range(n):
        for j in range(m):
            A[i][j] -= B[i][j];
 
    # Check necessary condition for
    # condition for existence of full
    # countOpsation
    for i in range(1, n):
        for j in range(1, n):
            if (A[i][j] - A[i][0] -
                A[0][j] + A[0][0] != 0):
                return -1;
 
    # If countOpsation is possible
    # calculate total countOpsation
    result = 0;
 
    for i in range(n):
        result += abs(A[i][0]);
 
    for j in range(m):
        result += abs(A[0][j] - A[0][0]);
 
    return (result);
 
# Driver code
if __name__ == '__main__':
    A = [[1, 1, 1],
         [1, 1, 1],
         [1, 1, 1]];
 
    B = [[1, 2, 3],
         [4, 5, 6],
         [7, 8, 9]];
          
    print(countOps(A, B, 3, 3));
 
# This code is contributed by Rajput-Ji

Time complexity: quadratic
Space complexity: constant
# Python3 program to print 2 coils of a
# 4n x 4n matrix.
 
# Print coils in a matrix of size 4n x 4n
def printCoils(n):
     
    # Number of elements in each coil
    m = 8*n*n
     
    # Let us fill elements in coil 1.
    coil1 = [0]*m
     
    # First element of coil1
    # 4*n*2*n + 2*n
    coil1[0] = 8*n*n + 2*n
     
    curr = coil1[0]
     
    nflg = 1
    step = 2
     
    # Fill remaining m-1 elements in coil1[]
    index = 1
    while (index < m):
         
        # Fill elements of current step from
        # down to up
        for i in range(step):
             
            # Next element from current element
            curr = coil1[index] = (curr - 4*n*nflg)
            index += 1
            if (index >= m):
                break
        if (index >= m):
            break
         
        # Fill elements of current step from
        # up to down.
        for i in range(step):
             
            curr = coil1[index] = curr + nflg
            index += 1
            if (index >= m):
                break
        nflg = nflg*(-1)
        step += 2
     
    #get coil2 from coil1 */
     
    coil2 = [0]*m
    i = 0
    while(i < 8*n*n):
        coil2[i] = 16*n*n + 1 -coil1[i]
        i += 1
    # Print both coils
    print("Coil 1 :", end = " ")
    i = 0
    while(i < 8*n*n):
        print(coil1[i], end = " ")
        i += 1
    print("\nCoil 2 :", end = " ")
    i = 0
    while(i < 8*n*n):
        print(coil2[i], end = " ")
        i += 1
 
# Driver code
 
n = 1
printCoils(n)
 
# This code is contributed by shubhamsingh10

Time complexity: quadratic
Space complexity: quadratic
# Python3 program to find sum of matrix
# in which each element is absolute
# difference of its corresponding
# row and column number row.
 
# Return the sum of matrix in which each
# element is absolute difference of its
# corresponding row and column number row
def findSum(n):
 
    # Generate matrix
    arr = [[0 for x in range(n)]
              for y in range (n)]
    for i in range (n):
        for j in range (n):
            arr[i][j] = abs(i - j)
 
    # Compute sum
    sum = 0
    for i in range (n):
        for j in range(n):
            sum += arr[i][j]
 
    return sum
 
# Driver Code
if __name__ == "__main__":
 
    n = 3
    print (findSum(n))
     
# This code is contributed by ita_c

Time complexity: quadratic
Space complexity: quadratic
# Python 3 program to find sum
# of matrix in which each element
# is absolute difference of its
# corresponding row and column
# number row.
 
# Return the sum of matrix in
# which each element is absolute
# difference of its corresponding
# row and column number row
def findSum(n):
    sum = 0
    for i in range(n):
        sum += i * (n - i)
    return 2 * sum
 
# Driver code
n = 3
print(findSum(n))
 
# This code is contributed by Shrikant13

Time complexity: linear
Space complexity: constant
# Python 3 program to find sum of matrix
# in which each element is absolute
# difference of its corresponding row
# and column number row.
 
# Return the sum of matrix in which
# each element is absolute difference
# of its corresponding row and column
# number row
def findSum(n):
    n -= 1
    sum = 0
    sum += (n * (n + 1)) / 2
    sum += (n * (n + 1) * (2 * n + 1)) / 6
    return int(sum)
 
# Driver Code
n = 3
print(findSum(n))
 
# This code contributed by Rajput-Ji

Time complexity: constant
Space complexity: constant
# Python3 program to find if a matrix is symmetric.
MAX = 1000
 
 
def checkHV(arr, N, M):
 
    # Initializing as both horizontal and vertical
    # symmetric.
    horizontal = True
    vertical = True
 
    # Checking for Horizontal Symmetry. We compare
    # first row with last row, second row with second
    # last row and so on.
    i = 0
    k = N - 1
    while(i < N // 2):
 
        # Checking each cell of a column.
        for j in range(M):
 
            # check if every cell is identical
            if (arr[i][j] != arr[k][j]):
                horizontal = False
                break
        i += 1
        k -= 1
 
    # Checking for Vertical Symmetry. We compare
    # first column with last column, second column
    # with second last column and so on.
    i = 0
    k = M - 1
    while(j < M // 2):
 
        # Checking each cell of a row.
        for i in range(N):
 
            # check if every cell is identical
            if (arr[i][j] != arr[i][k]):
                vertical = False
                break
        j += 1
        k -= 1
 
    if (not horizontal and not vertical):
        print("NO")
    elif (horizontal and not vertical):
        print("HORIZONTAL")
    elif (vertical and not horizontal):
        print("VERTICAL")
    else:
        print("BOTH")
 
 
# Driver code
mat = [[1, 0, 1], [0, 0, 0], [1, 0, 1]]
 
checkHV(mat, 3, 3)
 
# This code is contributed by shubhamsingh10

Time complexity: quadratic
Space complexity: constant
# Python 3 program to find maximum
# possible determinant of 0/n matrix.
# Function for maximum determinant
def maxDet(n):
    return 2 * n * n * n
 
# Function to print resultant matrix
def resMatrix(n):
    for i in range(3):
        for j in range(3):
 
            # three position where 0 appears
            if i == 0 and j == 2:
                print("0", end = " ")
            else if i == 1 and j == 0:
                print("0", end = " ")
            else if i == 2 and j == 1:
                print("0", end = " ")
 
            # position where n appears
            else:
                print(n, end = " ")
        print("\n")
         
# Driver code
n = 15
print("Maximum Detrminat=", maxDet(n))
print("Resultant Matrix:")
resMatrix(n)
 
# This code is contributed by Shrikant13

Time complexity: constant
Space complexity: constant
# Python3 program to find sum of
# diagonals of spiral matrix
 
# function returns sum of diagonals
def spiralDiaSum(n):
     
    if n == 1:
        return 1
 
    # as order should be only odd
    # we should pass only odd
    # integers
    return (4 * n*n - 6 * n + 6 +
               spiralDiaSum(n-2))
     
# Driver program
n = 7;
print(spiralDiaSum(n))
 
# This code is contributed by Anant Agarwal.

Time complexity: linear
Space complexity: linear
# Python3 program to find perimeter of area
# covered by 1 in 2D matrix consists of 0's and 1's.
 
R = 3
C = 5
 
# Find the number of covered side for mat[i][j].
def numofneighbour(mat, i, j):
 
    count = 0;
 
    # UP
    if (i > 0 and mat[i - 1][j]):
        count+= 1;
 
    # LEFT
    if (j > 0 and mat[i][j - 1]):
        count+= 1;
 
    # DOWN
    if (i < R-1 and mat[i + 1][j]):
        count+= 1
 
    # RIGHT
    if (j < C-1 and mat[i][j + 1]):
        count+= 1;
 
    return count;
 
# Returns sum of perimeter of shapes formed with 1s
def findperimeter(mat):
 
    perimeter = 0;
 
    # Traversing the matrix and finding ones to
    # calculate their contribution.
    for i in range(0, R):
        for j in range(0, C):
            if (mat[i][j]):
                perimeter += (4 - numofneighbour(mat, i, j));
 
    return perimeter;
 
# Driver Code
mat = [ [0, 1, 0, 0, 0],
        [1, 1, 1, 0, 0],
        [1, 0, 0, 0, 0] ]
 
print(findperimeter(mat), end="\n");
 
# This code is contributed by Akanksha Rai

Time complexity: quadratic
Space complexity: constant