Banana-Leopard/TASTY huggingface dataset:
''' Python program to arrange consonants and
vowels Nodes in a linked list '''
 
''' A linked list Node '''
class Node:
    def __init__(self, data):
        self.data = data;
        self.next = None;
     
''' Function to add new Node to the List '''
def append(headRef,  data):
    new_Node =  Node(data);
    last = headRef;
    if (headRef == None):
        headRef = new_Node;
        return headRef;
     
    while (last.next != None):
        last = last.next;
    last.next = new_Node;
    return headRef;
 
# utility function to print linked list
def printlist(head):
    if (head == None):
        print("Empty List");
        return;
     
    while (head != None):
        print(head.data, end="");
        if (head.next != None):
            print("-> ",end="");
        head = head.next;
     
    print();
 
 
'''
 * function to arrange consonants and vowels Nodes
 '''
def arrange(head):
    vowel = None;
    consonant = None;
    start = None;
    end = None;
    while (head != None):
        x = head.data;
 
        # Checking the current Node data is vowel or
        # not
        if (x == 'a' or x == 'e' or x == 'i' or x == 'o' or x == 'u'):
            if (vowel == None):
                vowel =  Node(x);
                start = vowel;
            else:
                vowel.next =  Node(x);
                vowel = vowel.next;
             
        else:
            if (consonant == None):
                consonant =  Node(x);
                end = consonant;
            else:
                consonant.next =  Node(x);
                consonant = consonant.next;
             
         
        head = head.next;
     
    # In case when there is no vowel in the incoming LL
    # then we have to return the head of the consonant LL
    if (start == None):
        return end;
 
    # Connecting the vowel and consonant LL
    vowel.next = end;
    return start;
 
# Driver code
if __name__ == '__main__':
    head = None;
    head = append(head, 'a');
    head = append(head, 'b');
    head = append(head, 'c');
    head = append(head, 'e');
    head = append(head, 'd');
    head = append(head, 'o');
    head = append(head, 'x');
    head = append(head, 'i');
 
    print("Linked list before :");
    printlist(head);
 
    head = arrange(head);
 
    print("Linked list after :");
    printlist(head);
 
# This code is contributed by Rajput-Ji

Time complexity: linear
Space complexity: linear
# Python3 program to partition a
# linked list around a given value.
import math
 
# Link list Node
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None
 
# A utility function to create a new node
def newNode(data):
    new_node = Node(data)
    new_node.data = data
    new_node.next = None
    return new_node
 
# Function to make a new list
# (using the existing nodes)
# and return head of new list.
def partition(head, x):
     
    # Let us initialize start and
    # tail nodes of new list
    tail = head
 
    # Now iterate original list
    # and connect nodes
    curr = head
    while (curr != None):
        next = curr.next
        if (curr.data < x):
             
            # Insert node at head.
            curr.next = head
            head = curr
         
        else:
             
            # Append to the list of greater values
            # Insert node at tail.
            tail.next = curr
            tail = curr
         
        curr = next
     
    tail.next = None
 
    # The head has changed, so we need
    # to return it to the user.
    return head
 
# Function to print linked list
def printList(head):
    temp = head
    while (temp != None):
        print(temp.data, end = " ")
        temp = temp.next
     
# Driver Code
if __name__=='__main__':
     
    # Start with the empty list
    head = newNode(3)
    head.next = newNode(5)
    head.next.next = newNode(8)
    head.next.next.next = newNode(2)
    head.next.next.next.next = newNode(10)
    head.next.next.next.next.next = newNode(2)
    head.next.next.next.next.next.next = newNode(1)
 
    x = 5
    head = partition(head, x)
    printList(head)
     
# This code is contributed by AbhiThakur

Time complexity: linear
Space complexity: constant
# Python3 implementation to modify the
# contents of the linked list
  
# Linked list node
class Node:
     
    def __init__(self):
         
        self.data = 0
        self.next = None
  
# Function to insert a node at the
# beginning of the linked list
def append(head_ref, new_data):
 
    # Allocate node
    new_node = Node()
     
    # Put in the data
    new_node.data = new_data
     
    # Link the old list at the end
    # of the new node
    new_node.next = head_ref
     
    # Move the head to point to the new node
    head_ref = new_node
     
    return head_ref
     
# Function to print the linked list
def printList(head):
 
    if (not head):
        return;
      
    while (head.next != None):
        print(head.data, end = ' -> ')
        head = head.next
     
    print(head.data)
     
# Function to middle node of list.
def find_mid(head):
 
    temp = head
    slow = head
    fast = head
      
    while (fast and fast.next):
         
        # Advance 'fast' two nodes, and
        # advance 'slow' one node
        slow = slow.next
        fast = fast.next.next
         
    # If number of nodes are odd then
    # update slow by slow.next;
    if (fast):
        slow = slow.next
  
    return slow
 
# Function to modify the contents of
# the linked list.
def modifyTheList(head, slow):
 
    # Create Stack.
    s = []
    temp = head
     
    while (slow):
        s.append(slow.data)
        slow = slow.next
 
    # Traverse the list by using
    # temp until stack is empty.
    while (len(s) != 0):
        temp.data = temp.data - s[-1]
        temp = temp.next
        s.pop()
     
# Driver code
if __name__=='__main__':
     
    head = None
      
    # creating the linked list
    head = append(head, 10)
    head = append(head, 7)
    head = append(head, 12)
    head = append(head, 8)
    head = append(head, 9)
    head = append(head, 2)
      
    # Call Function to Find the
    # starting point of second half of list.
    mid = find_mid(head)
      
    # Call function to modify the
    # contents of the linked list.
    modifyTheList( head, mid)
      
    # Print the modified linked list
    print("Modified List:")
    printList(head)
 
# This code is contributed by rutvik_56

Time complexity: linear
Space complexity: linear
# Python3 program to shift k
# elements in a matrix.
 
N = 4
# Function to shift first k
# elements of each row of
# matrix.
def shiftMatrixByK(mat, k):
    if (k > N) :
        print ("shifting is"
            " not possible")
        return
     
    j = 0
    while (j < N) :
         
        # Print elements from
        # index k
        for i in range(k, N):
            print ("{} " .
            format(mat[j][i]), end="")
             
        # Print elements before
        # index k
        for i in range(0, k):
            print ("{} " .
            format(mat[j][i]), end="")
             
        print ("")
        j = j + 1
 
# Driver code
mat = [[1, 2, 3, 4],
       [5, 6, 7, 8],
       [9, 10, 11, 12],
       [13, 14, 15, 16]]
k = 2
 
# Function call
shiftMatrixByK(mat, k)
 
# This code is contributed by
# Manish Shaw (manishshaw1)

Time complexity: quadratic
Space complexity: constant
# Python implementation to sort the rows
# of matrix in ascending order followed by
# sorting the columns in descending order
 
MAX_SIZE=10
  
# function to sort each row of the matrix
# according to the order specified by
# ascending.
def sortByRow(mat, n, ascending):
 
    for i in range(n):
        if (ascending):   
            mat[i].sort()
        else:
            mat[i].sort(reverse=True)
  
# function to find
# transpose of the matrix
def transpose(mat, n):
 
    for i in range(n):
        for j in range(i + 1, n):
         
            # swapping element at index (i, j)
            # by element at index (j, i)
            temp = mat[i][j]
            mat[i][j] = mat[j][i]
            mat[j][i] = temp
 
# function to sort
# the matrix row-wise
# and column-wise
def sortMatRowAndColWise(mat, n):
 
    # sort rows of mat[][]
    sortByRow(mat, n, True)
  
    # get transpose of mat[][]
    transpose(mat, n)
  
    # again sort rows of
    # mat[][] in descending
    # order.
    sortByRow(mat, n, False)
  
    # again get transpose of mat[][]
    transpose(mat, n)
  
# function to print the matrix
def printMat(mat, n):
 
    for i in range(n):
        for j in range(n):
            print(mat[i][j] , " ", end="")
        print()
 
#Driver code
n = 3
      
mat = [[3, 2, 1],
    [9, 8, 7],
    [6, 5, 4]]
  
print("Original Matrix:")
printMat(mat, n)
  
sortMatRowAndColWise(mat, n)
  
print("Matrix After Sorting:")
printMat(mat, n)
 
# This code is contributed
# by Anant Agarwal.

Time complexity: quadratic
Space complexity: constant
# Python program to find sum of
# middle row and column in matrix
 
  
def middlesum(mat,n):
  
    row_sum = 0
    col_sum = 0
      
    # loop for sum of row
    for i in range(n):
        row_sum += mat[n // 2][i]
      
    print("Sum of middle row = ",
                     row_sum)
      
    # loop for sum of column
    for i in range(n):
        col_sum += mat[i][n // 2]
      
    print("Sum of middle column = ",
                            col_sum)
 
# Driver code
mat= [[2, 5, 7],
     [3, 7, 2],
     [5, 6, 9]]
      
middlesum(mat, 3)
 
# This code is contributed
# by Anant Agarwal.

Time complexity: linear
Space complexity: constant
# Python3 program showing time difference
# in row major and column major access
 
# taking MAX 10000 so that time difference
# can be shown
from time import clock
MAX = 1000
 
arr = [[0 for i in range(MAX)] for i in range(MAX)]
 
 
def rowMajor():
    global arr
 
    # accessing element row wise
    for i in range(MAX):
        for j in range(MAX):
            arr[i][j] += 1
 
 
def colMajor():
    global arr
 
    # accessing element column wise
    for i in range(MAX):
        for j in range(MAX):
            arr[j][i] += 1
 
 
# Driver code
if __name__ == '__main__':
 
    # Time taken by row major order
    t = clock()
    rowMajor()
    t = clock() - t
    print("Row major access time :{:.2f} s".format(t))
 
    # Time taken by column major order
    t = clock()
    colMajor()
    t = clock() - t
    print("Column major access time :{:.2f} s".format(t))
 
# This code is contributed by mohit kumar 29

Time complexity: quadratic
Space complexity: quadratic
# Python program to rotate
# a matrix right by k times
 
# size of matrix
M = 3
N = 3
matrix = [[12, 23, 34],
          [45, 56, 67],
          [78, 89, 91]]
 
# function to rotate
# matrix by k times
def rotateMatrix(k) :
 
    global M, N, matrix
     
    # temporary array
    # of size M
    temp = [0] * M
     
    # within the size
    # of matrix
    k = k % M
     
    for i in range(0, N) :
     
        # copy first M-k elements
        # to temporary array
        for t in range(0, M - k) :
            temp[t] = matrix[i][t]
     
        # copy the elements from
        # k to end to starting
        for j in range(M - k, M) :
            matrix[i][j - M + k] = matrix[i][j]
     
        # copy elements from
        # temporary array to end
        for j in range(k, M) :
            matrix[i][j] = temp[j - k]
     
# function to display
# the matrix
def displayMatrix() :
 
    global M, N, matrix
    for i in range(0, N) :
     
        for j in range(0, M) :
            print ("{} " .
                   format(matrix[i][j]), end = "")
        print ()
 
# Driver code
k = 2
 
# rotate matrix by k
rotateMatrix(k)
 
# display rotated matrix
displayMatrix()
 
# This code is contributed by
# Manish Shaw(manishshaw1)

Time complexity: quadratic
Space complexity: linear
# Python Program to check given matrix
# is idempotent matrix or not.
import math
 
# Function for matrix multiplication.
def multiply(mat, res):
 
    N= len(mat)
    for i in range(0,N):
     
        for j in range(0,N):
         
            res[i][j] = 0
            for k in range(0,N):
                res[i][j] += mat[i][k] * mat[k][j]
 
# Function to check idempotent
# property of matrix.
def checkIdempotent(mat):
 
    N= len(mat)
    # Calculate multiplication of matrix
    # with itself and store it into res.
    res =[[0]*N for i in range(0,N)]
    multiply(mat, res)
 
    for i in range(0,N):
        for j in range(0,N):    
            if (mat[i][j] != res[i][j]):
                return False
    return True
 
# driver Function
mat = [ [2, -2, -4],
        [-1, 3, 4],
        [1, -2, -3] ]
     
# checkIdempotent function call.
if (checkIdempotent(mat)):
    print("Idempotent Matrix")
else:
    print("Not Idempotent Matrix.")
 
# This code is contributed by Gitanjali.

Time complexity: cubic
Space complexity: quadratic
# Program to implement involutory matrix.
N = 3;
 
# Function for matrix multiplication.
def multiply(mat, res):
 
    for i in range(N):
        for j in range(N):
            res[i][j] = 0;
            for k in range(N):
                res[i][j] += mat[i][k] * mat[k][j];
    return res;
 
# Function to check involutory matrix.
def InvolutoryMatrix(mat):
 
    res=[[0 for i in range(N)]
            for j in range(N)];
 
    # multiply function call.
    res = multiply(mat, res);
 
    for i in range(N):
        for j in range(N):
            if (i == j and res[i][j] != 1):
                return False;
            if (i != j and res[i][j] != 0):
                return False;
    return True;
 
# Driver Code
mat = [[1, 0, 0], [0, -1, 0], [0, 0, -1]];
 
# Function call. If function
# return true then if part
# will execute otherwise
# else part will execute.
if (InvolutoryMatrix(mat)):
    print("Involutory Matrix");
else:
    print("Not Involutory Matrix");
 
# This code is contributed by mits

Time complexity: cubic
Space complexity: quadratic
# Python3 code to swap the element
# of first and last row and display
# the result
 
 
def interchangeFirstLast(mat, n, m):
    rows = n
 
    # swapping of element between
    # first and last rows
    for i in range(n):
        t = mat[0][i]
        mat[0][i] = mat[rows-1][i]
        mat[rows-1][i] = t
 
 
# Driver code
if __name__ == "__main__":
    mat = [[8, 9, 7, 6],
           [4, 7, 6, 5],
           [3, 2, 1, 8],
           [9, 9, 7, 7]]
 
    n = 4
    m = 4
 
    # Function call
    interchangeFirstLast(mat, n, m)
 
    # printing the interchanged matrix
    for i in range(n):
        for j in range(m):
            print(mat[i][j], end=" ")
        print("\n")
 
# This code is contributed by Shrikant13.

Time complexity: linear
Space complexity: constant
# Python3 code to sort 2D matrix row-wise
def sortRowWise(m):
     
    # loop for rows of matrix
    for i in range(len(m)):
         
        # loop for column of matrix
        for j in range(len(m[i])):
             
            # loop for comparison and swapping
            for k in range(len(m[i]) - j - 1):
                 
                if (m[i][k] > m[i][k + 1]):
                     
                    # swapping of elements
                    t = m[i][k]
                    m[i][k] = m[i][k + 1]
                    m[i][k + 1] = t
                     
    # printing the sorted matrix
    for i in range(len(m)):
        for j in range(len(m[i])):
            print(m[i][j], end=" ")
        print()
 
# Driver code
m = [[9, 8, 7, 1 ],[7, 3, 0, 2],[9, 5, 3, 2],[ 6, 3, 1, 2 ]]
sortRowWise(m)
 
# This code is contributed by shubhamsingh10

Time complexity: quadratic
Space complexity: constant
# Python3 code to sort 2D matrix row-wise
def sortRowWise(m):
     
    # One by one sort individual rows.
    for i in range(len(m)):
        m[i].sort()
         
    # printing the sorted matrix
    for i in range(len(m)):
        for j in range(len(m[i])):
            print(m[i][j], end=" ")
        print()
         
    return 0
 
# Driver code
m = [[9, 8, 7, 1 ],[7, 3, 0, 2],[9, 5, 3, 2 ],[ 6, 3, 1, 2]]
 
sortRowWise(m)
 
# This code is contributed by shubhamsingh10

Time complexity: quadratic
Space complexity: constant
# Python 3 code to check Markov Matrix
 
def checkMarkov(m) :
     
    # Outer loop to access rows
    # and inner to access columns
    for i in range(0, len(m)) :
         
        # Find sum of current row
        sm = 0
        for j in range(0, len(m[i])) :
            sm = sm + m[i][j]
 
        if (sm != 1) :
            return False
             
    return True
     
# Matrix to check
m = [ [ 0, 0, 1 ],
      [ 0.5, 0, 0.5 ],
      [ 1, 0, 0 ]      ]
 
# Calls the function check()
if (checkMarkov(m)) :
    print(" yes ")
else :
    print(" no ")
     
     
# This code is contributed by Nikita Tiwari.

Time complexity: quadratic
Space complexity: constant
# Python3 Program to check if matrix
# is diagonal matrix or not.
 
N = 4
 
# Function to check matrix
# is diagonal matrix
# or not.  
def isDiagonalMatrix(mat) :
    for i in range(0, N):
        for j in range(0, N) :
             
            # condition to check
            # other elements
            # except main diagonal
            # are zero or not.
            if ((i != j) and
             (mat[i][j] != 0)) :
                return False
 
    return True
 
 
# Driver function
mat = [[ 4, 0, 0, 0 ],
       [ 0, 7, 0, 0 ],
       [ 0, 0, 5, 0 ],
       [ 0, 0, 0, 1 ]]
 
if (isDiagonalMatrix(mat)) :
    print("Yes")
else :
    print("No")
     
     
# This code is contributed by Nikita Tiwari.

Time complexity: quadratic
Space complexity: constant
# Program to check matrix
# is scalar matrix or not.
 
 
N = 4
 
# Function to check matrix is
# scalar matrix or not.
def isScalarMatrix(mat) :
     
    # Check all elements
    # except main diagonal are
    # zero or not.
    for i in range(0,N) :
        for j in range(0,N) :
            if ((i != j)
               and (mat[i][j] != 0)) :
                return False
  
    # Check all diagonal
    # elements are same or not.
    for i in range(0,N-1) :
        if (mat[i][i] !=
           mat[i + 1][i + 1]) :
            return False
 
    return True
 
 
# Driver function
mat = [[ 2, 0, 0, 0 ],
       [ 0, 2, 0, 0 ],
       [ 0, 0, 2, 0 ],
       [ 0, 0, 0, 2 ]]
 
# Function call
if (isScalarMatrix(mat)):
    print("Yes")
else :
    print("No")
     
     
# This code is contributed by
# Nikita tiwari.

Time complexity: quadratic
Space complexity: constant
# Python 3 implementation to
# sort the matrix row-wise
# and column-wise
MAX_SIZE = 10
 
# function to sort each
# row of the matrix
def sortByRow(mat, n):
    for i in range (n):
         
        # sorting row number 'i'
        for j in range(n-1):
            if mat[i][j] > mat[i][j + 1]:
                temp = mat[i][j]
                mat[i][j] = mat[i][j + 1]
                mat[i][j + 1] = temp
 
# function to find
# transpose of the matrix
def transpose(mat, n):
    for i in range (n):
        for j in range(i + 1, n):
 
            # swapping element at
            # index (i, j) by element
            # at index (j, i)
            t = mat[i][j]
            mat[i][j] = mat[j][i]
            mat[j][i] = t
 
# function to sort
# the matrix row-wise
# and column-wise
def sortMatRowAndColWise(mat, n):
     
    # sort rows of mat[][]
    sortByRow(mat, n)
 
    # get transpose of mat[][]
    transpose(mat, n)
 
    # again sort rows of mat[][]
    sortByRow(mat, n)
 
    # again get transpose of mat[][]
    transpose(mat, n)
 
# function to print the matrix
def printMat(mat, n):
    for i in range(n):
        for j in range(n):
            print(str(mat[i][j] ), end = " ")
        print();
         
# Driver Code
mat = [[ 4, 1, 3 ],
       [ 9, 6, 8 ],
       [ 5, 2, 7 ]]
n = 3
 
print("Original Matrix:")
printMat(mat, n)
 
sortMatRowAndColWise(mat, n)
 
print("\nMatrix After Sorting:")
printMat(mat, n)
 
# This code is contributed
# by ChitraNayal

Time complexity: quadratic
Space complexity: constant
# Python program to print magic square of double order
 
def DoublyEven(n):
     
    # 2-D matrix with all entries as 0
    arr = [[(n*y)+x+1 for x in range(n)]for y in range(n)]
 
    # Change value of array elements at fix location
    # as per the rule (n*n+1)-arr[i][[j]
     
    # Corners of order (n/4)*(n/4)
    # Top left corner
    for i in range(0,n//4):
        for j in range(0,n//4):
            arr[i][j] = (n*n + 1) - arr[i][j];
     
    # Top right corner
    for i in range(0,n//4):
        for j in range(3 * (n//4),n):
            arr[i][j] = (n*n + 1) - arr[i][j];
 
    # Bottom Left corner
    for i in range(3 * (n//4),n):
        for j in range(0,n//4):
            arr[i][j] = (n*n + 1) - arr[i][j];
     
    # Bottom Right corner
    for i in range(3 * (n//4),n):
        for j in range(3 * (n//4),n):
            arr[i][j] = (n*n + 1) - arr[i][j];
             
    # Centre of matrix,order (n/2)*(n/2)
    for i in range(n//4,3 * (n//4)):
        for j in range(n//4,3 * (n//4)):
            arr[i][j] = (n*n + 1) - arr[i][j];
     
    # Printing the square
    for i in range(n):
        for j in range(n):
            print ('%2d ' %(arr[i][j]),end=" ")
        print()
         
# Driver Program
n = 8
DoublyEven(n)
 
# Contributed by Harshit Agrawal           

Time complexity: quadratic
Space complexity: quadratic
# Python program to generate
# odd sized magic squares
# A function to generate odd
# sized magic squares
 
 
def generateSquare(n):
 
    # 2-D array with all
    # slots set to 0
    magicSquare = [[0 for x in range(n)]
                   for y in range(n)]
 
    # initialize position of 1
    i = n // 2
    j = n - 1
 
    # Fill the magic square
    # by placing values
    num = 1
    while num <= (n * n):
        if i == -1 and j == n:  # 3rd condition
            j = n - 2
            i = 0
        else:
 
            # next number goes out of
            # right side of square
            if j == n:
                j = 0
 
            # next number goes
            # out of upper side
            if i < 0:
                i = n - 1
 
        if magicSquare[int(i)][int(j)]:  # 2nd condition
            j = j - 2
            i = i + 1
            continue
        else:
            magicSquare[int(i)][int(j)] = num
            num = num + 1
 
        j = j + 1
        i = i - 1  # 1st condition
 
    # Printing magic square
    print("Magic Square for n =", n)
    print("Sum of each row or column",
          n * (n * n + 1) // 2, "\n")
 
    for i in range(0, n):
        for j in range(0, n):
            print('%2d ' % (magicSquare[i][j]),
                  end='')
 
            # To display output
            # in matrix form
            if j == n - 1:
                print()
 
# Driver Code
 
 
# Works only when n is odd
n = 7
generateSquare(n)
 
# This code is contributed
# by Harshit Agrawal

Time complexity: quadratic
Space complexity: quadratic
# Python3 program to check whether a given
# matrix is magic matrix or not
# Returns true if mat[][] is magic
# square, else returns false.
def isMagicSquare( mat) :
  n = len(mat)
  # sumd1 and sumd2 are the sum of the two diagonals
  sumd1=0
  sumd2=0
  for i in range(n):
    # (i, i) is the diagonal from top-left -> bottom-right
    # (i, n - i - 1) is the diagonal from top-right -> bottom-left
    sumd1+=mat[i][i]
    sumd2+=mat[i][n-i-1]
    # if the two diagonal sums are unequal then it is not a magic square
  if not(sumd1==sumd2):
    return False
  for i in range(n):
    #sumr is rowsum and sumc is colsum
    sumr=0
    sumc=0
    for j in range(n):
      sumr+=mat[i][j]
      sumc+=mat[j][i]
    if not(sumr==sumc==sumd1):
      return False
    #if all the conditions are satisfied then it is a magic square
  return True
     
       
           
     
     
# Driver Code
mat = [ [ 2, 7, 6 ],
        [ 9, 5, 1 ],
        [ 4, 3, 8 ] ]
     
if (isMagicSquare(mat)) :
    print( "Magic Square")
else :
    print( "Not a magic Square")
    

Time complexity: quadratic
Space complexity: constant