codeparrot/xlcost-text-to-code huggingface dataset:
Construct a special tree from given preorder traversal | Utility function to create a new Binary Tree node ; A recursive function to create a Binary Tree from given pre [ ] preLN [ ] arrays . The function returns root of tree . index_ptr is used to update index values in recursive calls . index must be initially passed as 0 ; store the current value ; of index in pre [ ] Base Case : All nodes are constructed ; Allocate memory for this node and increment index for subsequent recursive calls ; If this is an internal node , construct left and right subtrees and link the subtrees ; A wrapper over constructTreeUtil ( ) ; Initialize index as 0. Value of index is used in recursion to maintain the current index in pre [ ] and preLN [ ] arrays . ; This function is used only for testing ; first recur on left child ; then print the data of node ; now recur on right child ; Driver Code
class newNode : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def constructTreeUtil ( pre , preLN , index_ptr , n ) : NEW_LINE INDENT index = index_ptr [ 0 ] NEW_LINE if index == n : NEW_LINE INDENT return None NEW_LINE DEDENT temp = newNode ( pre [ index ] ) NEW_LINE index_ptr [ 0 ] += 1 NEW_LINE if preLN [ index ] == ' N ' : NEW_LINE INDENT temp . left = constructTreeUtil ( pre , preLN , index_ptr , n ) NEW_LINE temp . right = constructTreeUtil ( pre , preLN , index_ptr , n ) NEW_LINE DEDENT return temp NEW_LINE DEDENT def constructTree ( pre , preLN , n ) : NEW_LINE INDENT index = [ 0 ] NEW_LINE return constructTreeUtil ( pre , preLN , index , n ) NEW_LINE DEDENT def printInorder ( node ) : NEW_LINE INDENT if node == None : NEW_LINE INDENT return NEW_LINE DEDENT printInorder ( node . left ) NEW_LINE print ( node . data , end = " ▁ " ) NEW_LINE printInorder ( node . right ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = None NEW_LINE pre = [ 10 , 30 , 20 , 5 , 15 ] NEW_LINE preLN = [ ' N ' , ' N ' , ' L ' , ' L ' , ' L ' ] NEW_LINE n = len ( pre ) NEW_LINE root = constructTree ( pre , preLN , n ) NEW_LINE print ( " Following ▁ is ▁ Inorder ▁ Traversal ▁ of " , " the ▁ Constructed ▁ Binary ▁ Tree : " ) NEW_LINE printInorder ( root ) NEW_LINE DEDENT
Minimum Possible value of | ai + aj | function for finding pairs and min value ; initialize smallest and count ; iterate over all pairs ; is abs value is smaller than smallest update smallest and reset count to 1 ; if abs value is equal to smallest increment count value ; print result ; Driver Code
def pairs ( arr , n , k ) : NEW_LINE INDENT smallest = 999999999999 NEW_LINE count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if abs ( arr [ i ] + arr [ j ] - k ) < smallest : NEW_LINE INDENT smallest = abs ( arr [ i ] + arr [ j ] - k ) NEW_LINE count = 1 NEW_LINE DEDENT elif abs ( arr [ i ] + arr [ j ] - k ) == smallest : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT print ( " Minimal ▁ Value ▁ = ▁ " , smallest ) NEW_LINE print ( " Total ▁ Pairs ▁ = ▁ " , count ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 5 , 7 , 5 , 1 , 9 , 9 ] NEW_LINE k = 12 NEW_LINE n = len ( arr ) NEW_LINE pairs ( arr , n , k ) NEW_LINE DEDENT
Rank of an element in a stream | Python3 program to find rank of an element in a stream . ; Inserting a new Node . ; Updating size of left subtree . ; Function to get Rank of a Node x . ; Step 1. ; Step 2. ; Step 3. ; Driver code
class newNode : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = self . right = None NEW_LINE self . leftSize = 0 NEW_LINE DEDENT DEDENT def insert ( root , data ) : NEW_LINE INDENT if root is None : NEW_LINE INDENT return newNode ( data ) NEW_LINE DEDENT if data <= root . data : NEW_LINE INDENT root . left = insert ( root . left , data ) NEW_LINE root . leftSize += 1 NEW_LINE DEDENT else : NEW_LINE INDENT root . right = insert ( root . right , data ) NEW_LINE DEDENT return root NEW_LINE DEDENT def getRank ( root , x ) : NEW_LINE INDENT if root . data == x : NEW_LINE INDENT return root . leftSize NEW_LINE DEDENT if x < root . data : NEW_LINE INDENT if root . left is None : NEW_LINE INDENT return - 1 NEW_LINE DEDENT else : NEW_LINE INDENT return getRank ( root . left , x ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if root . right is None : NEW_LINE INDENT return - 1 NEW_LINE DEDENT else : NEW_LINE INDENT rightSize = getRank ( root . right , x ) NEW_LINE if rightSize == - 1 : NEW_LINE INDENT return - 1 NEW_LINE DEDENT else : NEW_LINE INDENT return root . leftSize + 1 + rightSize NEW_LINE DEDENT DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 5 , 1 , 4 , 4 , 5 , 9 , 7 , 13 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE x = 4 NEW_LINE root = None NEW_LINE for i in range ( n ) : NEW_LINE INDENT root = insert ( root , arr [ i ] ) NEW_LINE DEDENT print ( " Rank ▁ of " , x , " in ▁ stream ▁ is : " , getRank ( root , x ) ) NEW_LINE x = 13 NEW_LINE print ( " Rank ▁ of " , x , " in ▁ stream ▁ is : " , getRank ( root , x ) ) NEW_LINE x = 8 NEW_LINE print ( " Rank ▁ of " , x , " in ▁ stream ▁ is : " , getRank ( root , x ) ) NEW_LINE DEDENT
Rank of an element in a stream | Driver code
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = [ 5 , 1 , 14 , 4 , 15 , 9 , 7 , 20 , 11 ] NEW_LINE key = 20 NEW_LINE arraySize = len ( a ) NEW_LINE count = 0 NEW_LINE for i in range ( arraySize ) : NEW_LINE INDENT if a [ i ] <= key : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT print ( " Rank ▁ of " , key , " in ▁ stream ▁ is : " , count - 1 ) NEW_LINE DEDENT
Special two digit numbers in a Binary Search Tree | A Tree node ; Function to create a new node ; If the tree is empty , return a new node ; If key is smaller than root 's key,  go to left subtree and set successor  as current node ; return the ( unchanged ) node pointer ; Function to find if number is special or not ; sum_of_digits , prod_of_digits Check if number is two digit or not ; Function to count number of special two digit number ; Driver code ; Function call , to check each node for special two digit number
class Node : NEW_LINE INDENT def __init__ ( self , x ) : NEW_LINE INDENT self . data = x NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def insert ( node , data ) : NEW_LINE INDENT global succ NEW_LINE root = node NEW_LINE if ( node == None ) : NEW_LINE INDENT return Node ( data ) NEW_LINE DEDENT if ( data < node . data ) : NEW_LINE INDENT root . left = insert ( node . left , data ) NEW_LINE DEDENT elif ( data > node . data ) : NEW_LINE INDENT root . right = insert ( node . right , data ) NEW_LINE DEDENT return root NEW_LINE DEDENT def check ( num ) : NEW_LINE INDENT sum = 0 NEW_LINE i = num NEW_LINE if ( num < 10 or num > 99 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT sum_of_digits = ( i % 10 ) + ( i // 10 ) NEW_LINE prod_of_digits = ( i % 10 ) * ( i // 10 ) NEW_LINE sum = sum_of_digits + prod_of_digits NEW_LINE DEDENT if ( sum == num ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT def countSpecialDigit ( rt ) : NEW_LINE INDENT global c NEW_LINE if ( rt == None ) : NEW_LINE INDENT return NEW_LINE DEDENT else : NEW_LINE INDENT x = check ( rt . data ) NEW_LINE if ( x == 1 ) : NEW_LINE INDENT c += 1 NEW_LINE DEDENT countSpecialDigit ( rt . left ) NEW_LINE countSpecialDigit ( rt . right ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = None NEW_LINE c = 0 NEW_LINE root = insert ( root , 50 ) NEW_LINE root = insert ( root , 29 ) NEW_LINE root = insert ( root , 59 ) NEW_LINE root = insert ( root , 19 ) NEW_LINE root = insert ( root , 53 ) NEW_LINE root = insert ( root , 556 ) NEW_LINE root = insert ( root , 56 ) NEW_LINE root = insert ( root , 94 ) NEW_LINE root = insert ( root , 13 ) NEW_LINE countSpecialDigit ( root ) NEW_LINE print ( c ) NEW_LINE DEDENT
Construct Special Binary Tree from given Inorder traversal | Recursive function to construct binary of size len from Inorder traversal inorder [ ] . Initial values of start and end should be 0 and len - 1. ; Find index of the maximum element from Binary Tree ; Pick the maximum value and make it root ; If this is the only element in inorder [ start . . end ] , then return it ; Using index in Inorder traversal , construct left and right subtress ; Function to find index of the maximum value in arr [ start ... end ] ; Helper class that allocates a new node with the given data and None left and right pointers . ; This funtcion is here just to test buildTree ( ) ; first recur on left child ; then print the data of node ; now recur on right child ; Driver Code ; Assume that inorder traversal of following tree is given 40 / \ 10 30 / \ 5 28 ; Let us test the built tree by printing Insorder traversal
def buildTree ( inorder , start , end ) : NEW_LINE INDENT if start > end : NEW_LINE INDENT return None NEW_LINE DEDENT i = Max ( inorder , start , end ) NEW_LINE root = newNode ( inorder [ i ] ) NEW_LINE if start == end : NEW_LINE INDENT return root NEW_LINE DEDENT root . left = buildTree ( inorder , start , i - 1 ) NEW_LINE root . right = buildTree ( inorder , i + 1 , end ) NEW_LINE return root NEW_LINE DEDENT def Max ( arr , strt , end ) : NEW_LINE INDENT i , Max = 0 , arr [ strt ] NEW_LINE maxind = strt NEW_LINE for i in range ( strt + 1 , end + 1 ) : NEW_LINE INDENT if arr [ i ] > Max : NEW_LINE INDENT Max = arr [ i ] NEW_LINE maxind = i NEW_LINE DEDENT DEDENT return maxind NEW_LINE DEDENT class newNode : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def printInorder ( node ) : NEW_LINE INDENT if node == None : NEW_LINE INDENT return NEW_LINE DEDENT printInorder ( node . left ) NEW_LINE print ( node . data , end = " ▁ " ) NEW_LINE printInorder ( node . right ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT inorder = [ 5 , 10 , 40 , 30 , 28 ] NEW_LINE Len = len ( inorder ) NEW_LINE root = buildTree ( inorder , 0 , Len - 1 ) NEW_LINE print ( " Inorder ▁ traversal ▁ of ▁ the " , " constructed ▁ tree ▁ is ▁ " ) NEW_LINE printInorder ( root ) NEW_LINE DEDENT
Sum of middle row and column in Matrix | Python program to find sum of middle row and column in matrix ; loop for sum of row ; loop for sum of column ; Driver code
def middlesum ( mat , n ) : NEW_LINE INDENT row_sum = 0 NEW_LINE col_sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT row_sum += mat [ n // 2 ] [ i ] NEW_LINE DEDENT print ( " Sum ▁ of ▁ middle ▁ row ▁ = ▁ " , row_sum ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT col_sum += mat [ i ] [ n // 2 ] NEW_LINE DEDENT print ( " Sum ▁ of ▁ middle ▁ column ▁ = ▁ " , col_sum ) NEW_LINE DEDENT mat = [ [ 2 , 5 , 7 ] , [ 3 , 7 , 2 ] , [ 5 , 6 , 9 ] ] NEW_LINE middlesum ( mat , 3 ) NEW_LINE
Row | taking MAX 10000 so that time difference can be shown ; accessing element row wise ; accessing element column wise ; Driver code ; Time taken by row major order ; Time taken by column major order
MAX = 1000 NEW_LINE from time import clock NEW_LINE arr = [ [ 0 for i in range ( MAX ) ] for i in range ( MAX ) ] NEW_LINE def rowMajor ( ) : NEW_LINE INDENT global arr NEW_LINE for i in range ( MAX ) : NEW_LINE INDENT for j in range ( MAX ) : NEW_LINE INDENT arr [ i ] [ j ] += 1 NEW_LINE DEDENT DEDENT DEDENT def colMajor ( ) : NEW_LINE INDENT global arr NEW_LINE for i in range ( MAX ) : NEW_LINE INDENT for j in range ( MAX ) : NEW_LINE INDENT arr [ j ] [ i ] += 1 NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT t = clock ( ) NEW_LINE rowMajor ( ) ; NEW_LINE t = clock ( ) - t NEW_LINE print ( " Row ▁ major ▁ access ▁ time ▁ : { : .2f } ▁ s " . format ( t ) ) NEW_LINE t = clock ( ) NEW_LINE colMajor ( ) NEW_LINE t = clock ( ) - t NEW_LINE print ( " Column ▁ major ▁ access ▁ time ▁ : { : .2f } ▁ s " . format ( t ) ) NEW_LINE DEDENT
Rotate the matrix right by K times | size of matrix ; function to rotate matrix by k times ; temporary array of size M ; within the size of matrix ; copy first M - k elements to temporary array ; copy the elements from k to end to starting ; copy elements from temporary array to end ; function to display the matrix ; Driver code ; rotate matrix by k ; display rotated matrix
M = 3 NEW_LINE N = 3 NEW_LINE matrix = [ [ 12 , 23 , 34 ] , [ 45 , 56 , 67 ] , [ 78 , 89 , 91 ] ] NEW_LINE def rotateMatrix ( k ) : NEW_LINE INDENT global M , N , matrix NEW_LINE temp = [ 0 ] * M NEW_LINE k = k % M NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT for t in range ( 0 , M - k ) : NEW_LINE INDENT temp [ t ] = matrix [ i ] [ t ] NEW_LINE DEDENT for j in range ( M - k , M ) : NEW_LINE INDENT matrix [ i ] [ j - M + k ] = matrix [ i ] [ j ] NEW_LINE DEDENT for j in range ( k , M ) : NEW_LINE INDENT matrix [ i ] [ j ] = temp [ j - k ] NEW_LINE DEDENT DEDENT DEDENT def displayMatrix ( ) : NEW_LINE INDENT global M , N , matrix NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT for j in range ( 0 , M ) : NEW_LINE INDENT print ( " { } ▁ " . format ( matrix [ i ] [ j ] ) , end = " " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT k = 2 NEW_LINE rotateMatrix ( k ) NEW_LINE displayMatrix ( ) NEW_LINE
Program to check Involutory Matrix | Program to implement involutory matrix . ; Function for matrix multiplication . ; Function to check involutory matrix . ; multiply function call . ; Driver Code ; Function call . If function return true then if part will execute otherwise else part will execute .
N = 3 ; NEW_LINE def multiply ( mat , res ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT for j in range ( N ) : NEW_LINE INDENT res [ i ] [ j ] = 0 ; NEW_LINE for k in range ( N ) : NEW_LINE INDENT res [ i ] [ j ] += mat [ i ] [ k ] * mat [ k ] [ j ] ; NEW_LINE DEDENT DEDENT DEDENT return res ; NEW_LINE DEDENT def InvolutoryMatrix ( mat ) : NEW_LINE INDENT res = [ [ 0 for i in range ( N ) ] for j in range ( N ) ] ; NEW_LINE res = multiply ( mat , res ) ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( N ) : NEW_LINE INDENT if ( i == j and res [ i ] [ j ] != 1 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT if ( i != j and res [ i ] [ j ] != 0 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT DEDENT return True ; NEW_LINE DEDENT mat = [ [ 1 , 0 , 0 ] , [ 0 , - 1 , 0 ] , [ 0 , 0 , - 1 ] ] ; NEW_LINE if ( InvolutoryMatrix ( mat ) ) : NEW_LINE INDENT print ( " Involutory ▁ Matrix " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ Involutory ▁ Matrix " ) ; NEW_LINE DEDENT
Interchange elements of first and last rows in matrix | Python code to swap the element of first and last row and display the result ; swapping of element between first and last rows ; input in the array ; printing the interchanged matrix
def interchangeFirstLast ( mat , n , m ) : NEW_LINE INDENT rows = n NEW_LINE for i in range ( n ) : NEW_LINE INDENT t = mat [ 0 ] [ i ] NEW_LINE mat [ 0 ] [ i ] = mat [ rows - 1 ] [ i ] NEW_LINE mat [ rows - 1 ] [ i ] = t NEW_LINE DEDENT DEDENT mat = [ [ 8 , 9 , 7 , 6 ] , [ 4 , 7 , 6 , 5 ] , [ 3 , 2 , 1 , 8 ] , [ 9 , 9 , 7 , 7 ] ] NEW_LINE n = 4 NEW_LINE m = 4 NEW_LINE interchangeFirstLast ( mat , n , m ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE INDENT print ( mat [ i ] [ j ] , end = " ▁ " ) NEW_LINE DEDENT print ( " " ) NEW_LINE DEDENT
Construct Binary Tree from given Parent Array representation | A node structure ; Creates a node with key as ' i ' . If i is root , then it changes root . If parent of i is not created , then it creates parent first ; If this node is already created ; Create a new node and set created [ i ] ; If ' i ' is root , change root pointer and return ; If parent is not created , then create parent first ; Find parent pointer ; If this is first child of parent ; If second child ; Creates tree from parent [ 0. . n - 1 ] and returns root of the created tree ; Create and array created [ ] to keep track of created nodes , initialize all entries as None ; Inorder traversal of tree ; Driver Method
class Node : NEW_LINE INDENT def __init__ ( self , key ) : NEW_LINE INDENT self . key = key NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def createNode ( parent , i , created , root ) : NEW_LINE INDENT if created [ i ] is not None : NEW_LINE INDENT return NEW_LINE DEDENT created [ i ] = Node ( i ) NEW_LINE if parent [ i ] == - 1 : NEW_LINE INDENT root [ 0 ] = created [ i ] NEW_LINE return NEW_LINE DEDENT if created [ parent [ i ] ] is None : NEW_LINE INDENT createNode ( parent , parent [ i ] , created , root ) NEW_LINE DEDENT p = created [ parent [ i ] ] NEW_LINE if p . left is None : NEW_LINE INDENT p . left = created [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT p . right = created [ i ] NEW_LINE DEDENT DEDENT def createTree ( parent ) : NEW_LINE INDENT n = len ( parent ) NEW_LINE created = [ None for i in range ( n + 1 ) ] NEW_LINE root = [ None ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT createNode ( parent , i , created , root ) NEW_LINE DEDENT return root [ 0 ] NEW_LINE DEDENT def inorder ( root ) : NEW_LINE INDENT if root is not None : NEW_LINE INDENT inorder ( root . left ) NEW_LINE print root . key , NEW_LINE inorder ( root . right ) NEW_LINE DEDENT DEDENT parent = [ - 1 , 0 , 0 , 1 , 1 , 3 , 5 ] NEW_LINE root = createTree ( parent ) NEW_LINE print " Inorder ▁ Traversal ▁ of ▁ constructed ▁ tree " NEW_LINE inorder ( root ) NEW_LINE
Row wise sorting in 2D array | Python3 code to sort 2D matrix row - wise ; loop for rows of matrix ; loop for column of matrix ; loop for comparison and swapping ; swapping of elements ; printing the sorted matrix ; Driver code
def sortRowWise ( m ) : NEW_LINE INDENT for i in range ( len ( m ) ) : NEW_LINE INDENT for j in range ( len ( m [ i ] ) ) : NEW_LINE INDENT for k in range ( len ( m [ i ] ) - j - 1 ) : NEW_LINE INDENT if ( m [ i ] [ k ] > m [ i ] [ k + 1 ] ) : NEW_LINE INDENT t = m [ i ] [ k ] NEW_LINE m [ i ] [ k ] = m [ i ] [ k + 1 ] NEW_LINE m [ i ] [ k + 1 ] = t NEW_LINE DEDENT DEDENT DEDENT DEDENT for i in range ( len ( m ) ) : NEW_LINE INDENT for j in range ( len ( m [ i ] ) ) : NEW_LINE INDENT print ( m [ i ] [ j ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT m = [ [ 9 , 8 , 7 , 1 ] , [ 7 , 3 , 0 , 2 ] , [ 9 , 5 , 3 , 2 ] , [ 6 , 3 , 1 , 2 ] ] NEW_LINE sortRowWise ( m ) NEW_LINE
Row wise sorting in 2D array | Python3 code to sort 2D matrix row - wise ; One by one sort individual rows . ; printing the sorted matrix ; Driver code
def sortRowWise ( m ) : NEW_LINE INDENT for i in range ( len ( m ) ) : NEW_LINE INDENT m [ i ] . sort ( ) NEW_LINE DEDENT for i in range ( len ( m ) ) : NEW_LINE INDENT for j in range ( len ( m [ i ] ) ) : NEW_LINE INDENT print ( m [ i ] [ j ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT return 0 NEW_LINE DEDENT m = [ [ 9 , 8 , 7 , 1 ] , [ 7 , 3 , 0 , 2 ] , [ 9 , 5 , 3 , 2 ] , [ 6 , 3 , 1 , 2 ] ] NEW_LINE sortRowWise ( m ) NEW_LINE
Program for Markov matrix | Python 3 code to check Markov Matrix ; Outer loop to access rows and inner to access columns ; Find sum of current row ; Matrix to check ; Calls the function check ( )
def checkMarkov ( m ) : NEW_LINE INDENT for i in range ( 0 , len ( m ) ) : NEW_LINE INDENT sm = 0 NEW_LINE for j in range ( 0 , len ( m [ i ] ) ) : NEW_LINE INDENT sm = sm + m [ i ] [ j ] NEW_LINE DEDENT if ( sm != 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT m = [ [ 0 , 0 , 1 ] , [ 0.5 , 0 , 0.5 ] , [ 1 , 0 , 0 ] ] NEW_LINE if ( checkMarkov ( m ) ) : NEW_LINE INDENT print ( " ▁ yes ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " ▁ no ▁ " ) NEW_LINE DEDENT
Program to check diagonal matrix and scalar matrix | Python3 Program to check if matrix is diagonal matrix or not . ; Function to check matrix is diagonal matrix or not . ; condition to check other elements except main diagonal are zero or not . ; Driver function
N = 4 NEW_LINE def isDiagonalMatrix ( mat ) : NEW_LINE INDENT for i in range ( 0 , N ) : NEW_LINE INDENT for j in range ( 0 , N ) : NEW_LINE INDENT if ( ( i != j ) and ( mat [ i ] [ j ] != 0 ) ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT DEDENT return True NEW_LINE DEDENT mat = [ [ 4 , 0 , 0 , 0 ] , [ 0 , 7 , 0 , 0 ] , [ 0 , 0 , 5 , 0 ] , [ 0 , 0 , 0 , 1 ] ] NEW_LINE if ( isDiagonalMatrix ( mat ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Program to check diagonal matrix and scalar matrix | Program to check matrix is scalar matrix or not . ; Function to check matrix is scalar matrix or not . ; Check all elements except main diagonal are zero or not . ; Check all diagonal elements are same or not . ; Driver function ; Function call
N = 4 NEW_LINE def isScalarMatrix ( mat ) : NEW_LINE INDENT for i in range ( 0 , N ) : NEW_LINE INDENT for j in range ( 0 , N ) : NEW_LINE INDENT if ( ( i != j ) and ( mat [ i ] [ j ] != 0 ) ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT DEDENT for i in range ( 0 , N - 1 ) : NEW_LINE INDENT if ( mat [ i ] [ i ] != mat [ i + 1 ] [ i + 1 ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT mat = [ [ 2 , 0 , 0 , 0 ] , [ 0 , 2 , 0 , 0 ] , [ 0 , 0 , 2 , 0 ] , [ 0 , 0 , 0 , 2 ] ] NEW_LINE if ( isScalarMatrix ( mat ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Construct a Binary Tree from Postorder and Inorder | Recursive function to construct binary of size n from Inorder traversal in [ ] and Postorder traversal post [ ] . Initial values of inStrt and inEnd should be 0 and n - 1. The function doesn 't do any error checking for cases where inorder and postorder do not form a tree ; Base case ; Pick current node from Postorder traversal using postIndex and decrement postIndex ; If this node has no children then return ; Else find the index of this node in Inorder traversal ; Using index in Inorder traversal , construct left and right subtress ; This function mainly initializes index of root and calls buildUtil ( ) ; Function to find index of value in arr [ start ... end ] . The function assumes that value is postsent in in [ ] ; Helper function that allocates a new node ; This funtcion is here just to test ; Driver code
def buildUtil ( In , post , inStrt , inEnd , pIndex ) : NEW_LINE INDENT if ( inStrt > inEnd ) : NEW_LINE INDENT return None NEW_LINE DEDENT node = newNode ( post [ pIndex [ 0 ] ] ) NEW_LINE pIndex [ 0 ] -= 1 NEW_LINE if ( inStrt == inEnd ) : NEW_LINE INDENT return node NEW_LINE DEDENT iIndex = search ( In , inStrt , inEnd , node . data ) NEW_LINE node . right = buildUtil ( In , post , iIndex + 1 , inEnd , pIndex ) NEW_LINE node . left = buildUtil ( In , post , inStrt , iIndex - 1 , pIndex ) NEW_LINE return node NEW_LINE DEDENT def buildTree ( In , post , n ) : NEW_LINE INDENT pIndex = [ n - 1 ] NEW_LINE return buildUtil ( In , post , 0 , n - 1 , pIndex ) NEW_LINE DEDENT def search ( arr , strt , end , value ) : NEW_LINE INDENT i = 0 NEW_LINE for i in range ( strt , end + 1 ) : NEW_LINE INDENT if ( arr [ i ] == value ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return i NEW_LINE DEDENT class newNode : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = self . right = None NEW_LINE DEDENT DEDENT def preOrder ( node ) : NEW_LINE INDENT if node == None : NEW_LINE INDENT return NEW_LINE DEDENT print ( node . data , end = " ▁ " ) NEW_LINE preOrder ( node . left ) NEW_LINE preOrder ( node . right ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT In = [ 4 , 8 , 2 , 5 , 1 , 6 , 3 , 7 ] NEW_LINE post = [ 8 , 4 , 5 , 2 , 6 , 7 , 3 , 1 ] NEW_LINE n = len ( In ) NEW_LINE root = buildTree ( In , post , n ) NEW_LINE print ( " Preorder ▁ of ▁ the ▁ constructed ▁ tree ▁ : " ) NEW_LINE preOrder ( root ) NEW_LINE DEDENT