codeparrot/xlcost-text-to-code huggingface dataset:
Number of co | Python3 program for the above approach ; Function to check whether given integers are co - prime or not ; Utility function to count number of co - prime pairs ; Traverse the array ; If co - prime ; Increment count ; Return count ; Function to count number of co - prime pairs ; Stores digits in string form ; Sort the list ; Keep two copies of list l ; Generate 2 digit numbers using d1 and d2 ; If current number does not exceed N ; Stores length of list ; Stores number of co - prime pairs ; Print number of co - prime pairs ; Driver Code ; Given value of N , d1 , d2 ; Function call to count number of co - prime pairs
from copy import deepcopy NEW_LINE import math NEW_LINE def coprime ( a , b ) : NEW_LINE INDENT return ( math . gcd ( a , b ) ) == 1 NEW_LINE DEDENT def numOfPairs ( arr , N ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( 0 , N - 1 ) : NEW_LINE INDENT for j in range ( i + 1 , N ) : NEW_LINE INDENT if ( coprime ( int ( arr [ i ] ) , int ( arr [ j ] ) ) ) : NEW_LINE INDENT count = count + 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT def noOfCoPrimePairs ( N , d1 , d2 ) : NEW_LINE INDENT l = [ ] NEW_LINE l . append ( str ( d1 ) ) NEW_LINE l . append ( str ( d2 ) ) NEW_LINE l . sort ( ) NEW_LINE if int ( N ) < int ( l [ 1 ] ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT total = temp2 = deepcopy ( l ) NEW_LINE flag = 0 NEW_LINE temp3 = [ ] NEW_LINE while len ( l [ 0 ] ) < 10 : NEW_LINE INDENT for i in range ( len ( l ) ) : NEW_LINE INDENT for j in range ( 2 ) : NEW_LINE INDENT if int ( l [ i ] + temp2 [ j ] ) > int ( N ) : NEW_LINE INDENT flag = 1 NEW_LINE break NEW_LINE DEDENT total . append ( l [ i ] + temp2 [ j ] ) NEW_LINE temp3 . append ( l [ i ] + temp2 [ j ] ) NEW_LINE DEDENT if flag == 1 : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if flag == 1 : NEW_LINE INDENT break NEW_LINE DEDENT l = deepcopy ( temp3 ) NEW_LINE temp3 = [ ] NEW_LINE DEDENT lenOfTotal = len ( total ) NEW_LINE ans = numOfPairs ( total , lenOfTotal ) NEW_LINE print ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 30 NEW_LINE d1 = 2 NEW_LINE d2 = 3 NEW_LINE noOfCoPrimePairs ( N , d1 , d2 ) NEW_LINE DEDENT
Minimize cost of placing tiles of dimensions 2 * 1 over a Matrix | Function to find the minimum cost in placing N tiles in a grid M [ ] [ ] ; Stores the minimum profit after placing i tiles ; Traverse the grid [ ] [ ] ; Update the orig_cost ; Traverse over the range [ 2 , N ] ; Place tiles horizentally or vertically ; Print the answer ; Driver Code
def tile_placing ( grid , N ) : NEW_LINE INDENT dp = [ 0 ] * ( N + 5 ) ; NEW_LINE orig_cost = 0 ; NEW_LINE for i in range ( 2 ) : NEW_LINE INDENT for j in range ( N ) : NEW_LINE INDENT orig_cost += grid [ i ] [ j ] ; NEW_LINE DEDENT DEDENT dp [ 0 ] = 0 ; NEW_LINE dp [ 1 ] = abs ( grid [ 0 ] [ 0 ] - grid [ 1 ] [ 0 ] ) ; NEW_LINE for i in range ( 2 , N + 1 ) : NEW_LINE INDENT dp [ i ] = max ( dp [ i - 1 ] + abs ( grid [ 0 ] [ i - 1 ] - grid [ 1 ] [ i - 1 ] ) , dp [ i - 2 ] + abs ( grid [ 0 ] [ i - 2 ] - grid [ 0 ] [ i - 1 ] ) + abs ( grid [ 1 ] [ i - 2 ] - grid [ 1 ] [ i - 1 ] ) ) ; NEW_LINE DEDENT print ( orig_cost - dp [ N ] , end = " " ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT M = [ [ 7 , 5 , 1 , 3 ] , [ 8 , 6 , 0 , 2 ] ] ; NEW_LINE N = len ( M [ 0 ] ) ; NEW_LINE tile_placing ( M , N ) ; NEW_LINE DEDENT
Print indices of pair of array elements required to be removed to split array into 3 equal sum subarrays | Function to check if array can be split into three equal sum subarrays by removing two elements ; Stores sum of all three subarrays ; Sum of left subarray ; Sum of middle subarray ; Sum of right subarray ; Check if sum of subarrays are equal ; Print the possible pair ; If no pair exists , print - 1 ; Driver code ; Given array ; Size of the array
def findSplit ( arr , N ) : NEW_LINE INDENT for l in range ( 1 , N - 3 , 1 ) : NEW_LINE INDENT for r in range ( l + 2 , N - 1 , 1 ) : NEW_LINE INDENT lsum = 0 NEW_LINE rsum = 0 NEW_LINE msum = 0 NEW_LINE for i in range ( 0 , l , 1 ) : NEW_LINE INDENT lsum += arr [ i ] NEW_LINE DEDENT for i in range ( l + 1 , r , 1 ) : NEW_LINE INDENT msum += arr [ i ] NEW_LINE DEDENT for i in range ( r + 1 , N , 1 ) : NEW_LINE INDENT rsum += arr [ i ] NEW_LINE DEDENT if ( lsum == rsum and rsum == msum ) : NEW_LINE INDENT print ( l , r ) NEW_LINE return NEW_LINE DEDENT DEDENT DEDENT print ( - 1 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 5 , 12 , 7 , 19 , 4 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE findSplit ( arr , N ) NEW_LINE DEDENT
Count the number of times a Bulb switches its state | Python program for the above approach ; Function to find the number of times a bulb switches its state ; count of 1 's ; Traverse the array ; update the array ; update the status of bulb ; Traverse the array Q [ ] ; stores previous state of the bulb ; Toggle the switch and update the count of 1 's ; if the bulb switches state ; Return count ; Input ; Queries ; Function call to find number of times the bulb toggles
import math NEW_LINE def solve ( A , n , Q , q ) : NEW_LINE INDENT one = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( A [ i ] == 1 ) : NEW_LINE INDENT one += 1 NEW_LINE DEDENT DEDENT glows = 0 NEW_LINE count = 0 NEW_LINE if ( one >= int ( math . ceil ( n / 2 ) ) ) : NEW_LINE INDENT glows = 1 NEW_LINE DEDENT for i in range ( 0 , q ) : NEW_LINE INDENT prev = glows NEW_LINE if ( A [ Q [ i ] - 1 ] == 1 ) : NEW_LINE INDENT one -= 1 NEW_LINE DEDENT if ( A [ Q [ i ] - 1 ] == 0 ) : NEW_LINE INDENT one += 1 NEW_LINE DEDENT A [ Q [ i ] - 1 ] ^= 1 NEW_LINE if ( one >= int ( math . ceil ( n / 2.0 ) ) ) : NEW_LINE INDENT glows = 1 NEW_LINE DEDENT else : NEW_LINE INDENT glows = 0 NEW_LINE DEDENT if ( prev != glows ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT n = 3 NEW_LINE arr = [ 1 , 1 , 0 ] NEW_LINE q = 3 NEW_LINE Q = [ 3 , 2 , 1 ] NEW_LINE print ( solve ( arr , n , Q , q ) ) NEW_LINE
Count array elements having sum of digits equal to K | Function to calculate the sum of digits of the number N ; Stores the sum of digits ; Return the sum ; Function to count array elements ; Store the count of array elements having sum of digits K ; Traverse the array ; If sum of digits is equal to K ; Increment the count ; Prthe count ; Given array ; Given value of K ; Size of the array ; Function call to count array elements having sum of digits equal to K
def sumOfDigits ( N ) : NEW_LINE INDENT sum = 0 NEW_LINE while ( N != 0 ) : NEW_LINE INDENT sum += N % 10 NEW_LINE N //= 10 NEW_LINE DEDENT return sum NEW_LINE DEDENT def elementsHavingDigitSumK ( arr , N , K ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( sumOfDigits ( arr [ i ] ) == K ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT print ( count ) NEW_LINE DEDENT arr = [ 23 , 54 , 87 , 29 , 92 , 62 ] NEW_LINE K = 11 NEW_LINE N = len ( arr ) NEW_LINE elementsHavingDigitSumK ( arr , N , K ) NEW_LINE
Postfix to Infix | Python3 program to find infix for a given postfix . ; Get Infix for a given postfix expression ; Push operands ; We assume that input is a valid postfix and expect an operator . ; There must be a single element in stack now which is the required infix . ; Driver Code
def isOperand ( x ) : NEW_LINE INDENT return ( ( x >= ' a ' and x <= ' z ' ) or ( x >= ' A ' and x <= ' Z ' ) ) NEW_LINE DEDENT def getInfix ( exp ) : NEW_LINE INDENT s = [ ] NEW_LINE for i in exp : NEW_LINE INDENT if ( isOperand ( i ) ) : NEW_LINE INDENT s . insert ( 0 , i ) NEW_LINE DEDENT else : NEW_LINE INDENT op1 = s [ 0 ] NEW_LINE s . pop ( 0 ) NEW_LINE op2 = s [ 0 ] NEW_LINE s . pop ( 0 ) NEW_LINE s . insert ( 0 , " ( " + op2 + i + op1 + " ) " ) NEW_LINE DEDENT DEDENT return s [ 0 ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT exp = " ab * c + " NEW_LINE print ( getInfix ( exp . strip ( ) ) ) NEW_LINE DEDENT
Change a Binary Tree so that every node stores sum of all nodes in left subtree | Python3 program to store sum of nodes in left subtree in every node Binary Tree Node utility that allocates a new Node with the given key ; Construct to create a new node ; Function to modify a Binary Tree so that every node stores sum of values in its left child including its own value ; Base cases ; Update left and right subtrees ; Add leftsum to current node ; Return sum of values under root ; Utility function to do inorder traversal ; Driver Code ; Let us con below tree 1 / \ 2 3 / \ \ 4 5 6
class newNode : NEW_LINE INDENT def __init__ ( self , key ) : NEW_LINE INDENT self . data = key NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def updatetree ( root ) : NEW_LINE INDENT if ( not root ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( root . left == None and root . right == None ) : NEW_LINE INDENT return root . data NEW_LINE DEDENT leftsum = updatetree ( root . left ) NEW_LINE rightsum = updatetree ( root . right ) NEW_LINE root . data += leftsum NEW_LINE return root . data + rightsum NEW_LINE DEDENT def inorder ( node ) : NEW_LINE INDENT if ( node == None ) : NEW_LINE INDENT return NEW_LINE DEDENT inorder ( node . left ) NEW_LINE print ( node . data , end = " ▁ " ) NEW_LINE inorder ( node . right ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 1 ) NEW_LINE root . left = newNode ( 2 ) NEW_LINE root . right = newNode ( 3 ) NEW_LINE root . left . left = newNode ( 4 ) NEW_LINE root . left . right = newNode ( 5 ) NEW_LINE root . right . right = newNode ( 6 ) NEW_LINE updatetree ( root ) NEW_LINE print ( " Inorder ▁ traversal ▁ of ▁ the ▁ modified ▁ tree ▁ is " ) NEW_LINE inorder ( root ) NEW_LINE DEDENT
The Stock Span Problem | Fills list S [ ] with span values ; Span value of first day is always 1 ; Calculate span value of remaining days by linearly checking previous days ; Initialize span value ; Traverse left while the next element on left is smaller than price [ i ] ; A utility function to print elements of array ; Driver program to test above function ; Fill the span values in list S [ ] ; print the calculated span values
def calculateSpan ( price , n , S ) : NEW_LINE INDENT S [ 0 ] = 1 NEW_LINE for i in range ( 1 , n , 1 ) : NEW_LINE INDENT S [ i ] = 1 NEW_LINE j = i - 1 NEW_LINE while ( j >= 0 ) and ( price [ i ] >= price [ j ] ) : NEW_LINE INDENT S [ i ] += 1 NEW_LINE j -= 1 NEW_LINE DEDENT DEDENT DEDENT def printArray ( arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT price = [ 10 , 4 , 5 , 90 , 120 , 80 ] NEW_LINE n = len ( price ) NEW_LINE S = [ None ] * n NEW_LINE calculateSpan ( price , n , S ) NEW_LINE printArray ( S , n ) NEW_LINE
The Stock Span Problem | A stack based efficient method to calculate s ; Create a stack and push index of fist element to it ; Span value of first element is always 1 ; Calculate span values for rest of the elements ; Pop elements from stack whlie stack is not empty and top of stack is smaller than price [ i ] ; If stack becomes empty , then price [ i ] is greater than all elements on left of it , i . e . price [ 0 ] , price [ 1 ] , . . price [ i - 1 ] . Else the price [ i ] is greater than elements after top of stack ; Push this element to stack ; A utility function to print elements of array ; Driver program to test above function ; Fill the span values in array S [ ] ; Print the calculated span values
def calculateSpan ( price , S ) : NEW_LINE INDENT n = len ( price ) NEW_LINE st = [ ] NEW_LINE st . append ( 0 ) NEW_LINE S [ 0 ] = 1 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT while ( len ( st ) > 0 and price [ st [ - 1 ] ] <= price [ i ] ) : NEW_LINE INDENT st . pop ( ) NEW_LINE DEDENT S [ i ] = i + 1 if len ( st ) <= 0 else ( i - st [ - 1 ] ) NEW_LINE st . append ( i ) NEW_LINE DEDENT DEDENT def printArray ( arr , n ) : NEW_LINE INDENT for i in range ( 0 , n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT price = [ 10 , 4 , 5 , 90 , 120 , 80 ] NEW_LINE S = [ 0 for i in range ( len ( price ) + 1 ) ] NEW_LINE calculateSpan ( price , S ) NEW_LINE printArray ( S , len ( price ) ) NEW_LINE
The Stock Span Problem | An efficient method to calculate stock span values implementing the same idea without using stack ; Span value of first element is always 1 ; Calculate span values for rest of the elements ; A utility function to print elements of array ; Driver code ; Fill the span values in array S [ ] ; Print the calculated span values
def calculateSpan ( A , n , ans ) : NEW_LINE INDENT ans [ 0 ] = 1 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT counter = 1 NEW_LINE while ( ( i - counter ) >= 0 and A [ i ] >= A [ i - counter ] ) : NEW_LINE INDENT counter += ans [ i - counter ] NEW_LINE DEDENT ans [ i ] = counter NEW_LINE DEDENT DEDENT def printArray ( arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = ' ▁ ' ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT price = [ 10 , 4 , 5 , 90 , 120 , 80 ] NEW_LINE n = len ( price ) NEW_LINE S = [ 0 ] * ( n ) NEW_LINE calculateSpan ( price , n , S ) NEW_LINE printArray ( S , n ) NEW_LINE
Next Greater Element | Function to print element and NGE pair for all elements of list ; Driver program to test above function
def printNGE ( arr ) : NEW_LINE INDENT for i in range ( 0 , len ( arr ) , 1 ) : NEW_LINE INDENT next = - 1 NEW_LINE for j in range ( i + 1 , len ( arr ) , 1 ) : NEW_LINE INDENT if arr [ i ] < arr [ j ] : NEW_LINE INDENT next = arr [ j ] NEW_LINE break NEW_LINE DEDENT DEDENT print ( str ( arr [ i ] ) + " ▁ - - ▁ " + str ( next ) ) NEW_LINE DEDENT DEDENT arr = [ 11 , 13 , 21 , 3 ] NEW_LINE printNGE ( arr ) NEW_LINE
Convert a Binary Tree into its Mirror Tree | Utility function to create a new tree node ; Change a tree so that the roles of the left and right pointers are swapped at every node . So the tree ... 4 / \ 2 5 / \ 1 3 is changed to ... 4 / \ 5 2 / \ 3 1 ; do the subtrees ; swap the pointers in this node ; Helper function to print Inorder traversal . ; Driver code ; Print inorder traversal of the input tree ; Convert tree to its mirror ; Print inorder traversal of the mirror tree
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 mirror ( node ) : NEW_LINE INDENT if ( node == None ) : NEW_LINE INDENT return NEW_LINE DEDENT else : NEW_LINE INDENT temp = node NEW_LINE mirror ( node . left ) NEW_LINE mirror ( node . right ) NEW_LINE temp = node . left NEW_LINE node . left = node . right NEW_LINE node . right = temp NEW_LINE DEDENT DEDENT def inOrder ( node ) : NEW_LINE INDENT if ( node == None ) : NEW_LINE INDENT return NEW_LINE DEDENT inOrder ( node . left ) NEW_LINE print ( node . data , end = " ▁ " ) NEW_LINE inOrder ( node . right ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT root = newNode ( 1 ) NEW_LINE root . left = newNode ( 2 ) NEW_LINE root . right = newNode ( 3 ) NEW_LINE root . left . left = newNode ( 4 ) NEW_LINE root . left . right = newNode ( 5 ) NEW_LINE print ( " Inorder ▁ traversal ▁ of ▁ the " , " constructed ▁ tree ▁ is " ) NEW_LINE inOrder ( root ) NEW_LINE mirror ( root ) NEW_LINE print ( " Inorder traversal of " , ▁ " the mirror treeis   " ) NEW_LINE inOrder ( root ) NEW_LINE DEDENT
Maximum product of indexes of next greater on left and right | Method to find the next greater value in left side ; Checking if current element is greater than top ; Pop the element till we can 't  get the larger value then  the current value ; Else push the element in the stack ; Method to find the next greater value in right ; Checking if current element is greater than top ; Pop the element till we can 't  get the larger value then  the current value ; Else push the element in the stack ; For each element storing the index of just greater element in left side ; For each element storing the index of just greater element in right side ; Finding the max index product ; Driver Code
def nextGreaterInLeft ( a ) : NEW_LINE INDENT left_index = [ 0 ] * len ( a ) NEW_LINE s = [ ] NEW_LINE for i in range ( len ( a ) ) : NEW_LINE INDENT while len ( s ) != 0 and a [ i ] >= a [ s [ - 1 ] ] : NEW_LINE INDENT s . pop ( ) NEW_LINE DEDENT if len ( s ) != 0 : NEW_LINE INDENT left_index [ i ] = s [ - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT left_index [ i ] = 0 NEW_LINE DEDENT s . append ( i ) NEW_LINE DEDENT return left_index NEW_LINE DEDENT def nextGreaterInRight ( a ) : NEW_LINE INDENT right_index = [ 0 ] * len ( a ) NEW_LINE s = [ ] NEW_LINE for i in range ( len ( a ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT while len ( s ) != 0 and a [ i ] >= a [ s [ - 1 ] ] : NEW_LINE INDENT s . pop ( ) NEW_LINE DEDENT if len ( s ) != 0 : NEW_LINE INDENT right_index [ i ] = s [ - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT right_index [ i ] = 0 NEW_LINE DEDENT s . append ( i ) NEW_LINE DEDENT return right_index NEW_LINE DEDENT def LRProduct ( arr ) : NEW_LINE INDENT left = nextGreaterInLeft ( arr ) NEW_LINE right = nextGreaterInRight ( arr ) NEW_LINE ans = - 1 NEW_LINE for i in range ( 1 , len ( left ) - 1 ) : NEW_LINE INDENT if left [ i ] == 0 or right [ i ] == 0 : NEW_LINE INDENT ans = max ( ans , 0 ) NEW_LINE DEDENT else : NEW_LINE INDENT temp = ( left [ i ] + 1 ) * ( right [ i ] + 1 ) NEW_LINE ans = max ( ans , temp ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT arr = [ 5 , 4 , 3 , 4 , 5 ] NEW_LINE print ( LRProduct ( arr ) ) NEW_LINE
The Celebrity Problem | Max ; Person with 2 is celebrity ; Returns - 1 if celebrity is not present . If present , returns id ( value from 0 to n - 1 ) ; The graph needs not be constructed as the edges can be found by using knows function degree array ; ; Query for all edges ; Set the degrees ; Find a person with indegree n - 1 and out degree 0 ; Driver code
N = 8 NEW_LINE MATRIX = [ [ 0 , 0 , 1 , 0 ] , [ 0 , 0 , 1 , 0 ] , [ 0 , 0 , 0 , 0 ] , [ 0 , 0 , 1 , 0 ] ] NEW_LINE def knows ( a , b ) : NEW_LINE INDENT return MATRIX [ a ] [ b ] NEW_LINE DEDENT def findCelebrity ( n ) : NEW_LINE INDENT indegree = [ 0 for x in range ( n ) ] NEW_LINE outdegree = [ 0 for x in range ( n ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT x = knows ( i , j ) NEW_LINE outdegree [ i ] += x NEW_LINE indegree [ j ] += x NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT if ( indegree [ i ] == n - 1 and outdegree [ i ] == 0 ) : NEW_LINE INDENT return i NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 4 NEW_LINE id_ = findCelebrity ( n ) NEW_LINE if id_ == - 1 : NEW_LINE INDENT print ( " No ▁ celebrity " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Celebrity ▁ ID " , id_ ) NEW_LINE DEDENT DEDENT
The Celebrity Problem | Max ; Person with 2 is celebrity ; Returns - 1 if a potential celebrity is not present . If present , returns id ( value from 0 to n - 1 ) . ; Base case ; Find the celebrity with n - 1 persons ; If there are no celebrities ; if the id knows the nth person then the id cannot be a celebrity , but nth person could be on ; if the id knows the nth person then the id cannot be a celebrity , but nth person could be one ; If there is no celebrity ; Returns - 1 if celebrity is not present . If present , returns id ( value from 0 to n - 1 ) . a wrapper over findCelebrity ; Find the celebrity ; Check if the celebrity found is really the celebrity ; Check the id is really the celebrity ; If the person is known to everyone . ; Driver code
N = 8 NEW_LINE MATRIX = [ [ 0 , 0 , 1 , 0 ] , [ 0 , 0 , 1 , 0 ] , [ 0 , 0 , 0 , 0 ] , [ 0 , 0 , 1 , 0 ] ] NEW_LINE def knows ( a , b ) : NEW_LINE INDENT return MATRIX [ a ] [ b ] NEW_LINE DEDENT def findPotentialCelebrity ( n ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT id_ = findPotentialCelebrity ( n - 1 ) NEW_LINE if ( id_ == - 1 ) : NEW_LINE INDENT return n - 1 NEW_LINE DEDENT elif knows ( id_ , n - 1 ) : NEW_LINE INDENT return n - 1 NEW_LINE DEDENT elif knows ( n - 1 , id_ ) : NEW_LINE INDENT return id_ NEW_LINE DEDENT return - 1 NEW_LINE DEDENT def Celebrity ( n ) : NEW_LINE INDENT id_ = findPotentialCelebrity ( n ) NEW_LINE if ( id_ == - 1 ) : NEW_LINE INDENT return id_ NEW_LINE DEDENT else : NEW_LINE INDENT c1 = 0 NEW_LINE c2 = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( i != id_ ) : NEW_LINE INDENT c1 += knows ( id_ , i ) NEW_LINE c2 += knows ( i , id_ ) NEW_LINE DEDENT DEDENT if ( c1 == 0 and c2 == n - 1 ) : NEW_LINE INDENT return id_ NEW_LINE DEDENT return - 1 NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 4 NEW_LINE id_ = Celebrity ( n ) NEW_LINE if id_ == - 1 : NEW_LINE INDENT print ( " No ▁ celebrity " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Celebrity ▁ ID " , id_ ) NEW_LINE DEDENT DEDENT
Convert a Binary Tree into its Mirror Tree | A binary tree node has data , pointer to left child and a pointer to right child Helper function that allocates a new node with the given data and None left and right pointers ; Change a tree so that the roles of the left and right pointers are swapped at every node . So the tree ... 4 / \ 2 5 / \ 1 3 is changed to ... 4 / \ 5 2 / \ 3 1 ; Do BFS . While doing BFS , keep swapping left and right children ; pop top node from queue ; swap left child with right child ; append left and right children ; Helper function to print Inorder traversal . ; Driver code ; Print inorder traversal of the input tree ; Convert tree to its mirror ; Print inorder traversal of the mirror tree
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 mirror ( root ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return NEW_LINE DEDENT q = [ ] NEW_LINE q . append ( root ) NEW_LINE while ( len ( q ) ) : NEW_LINE INDENT curr = q [ 0 ] NEW_LINE q . pop ( 0 ) NEW_LINE curr . left , curr . right = curr . right , curr . left NEW_LINE if ( curr . left ) : NEW_LINE INDENT q . append ( curr . left ) NEW_LINE DEDENT if ( curr . right ) : NEW_LINE INDENT q . append ( curr . right ) NEW_LINE DEDENT DEDENT DEDENT def inOrder ( node ) : NEW_LINE INDENT if ( node == None ) : NEW_LINE INDENT return NEW_LINE DEDENT inOrder ( node . left ) NEW_LINE print ( node . data , end = " ▁ " ) NEW_LINE inOrder ( node . right ) NEW_LINE DEDENT root = newNode ( 1 ) NEW_LINE root . left = newNode ( 2 ) NEW_LINE root . right = newNode ( 3 ) NEW_LINE root . left . left = newNode ( 4 ) NEW_LINE root . left . right = newNode ( 5 ) NEW_LINE print ( " Inorder ▁ traversal ▁ of ▁ the ▁ constructed ▁ tree ▁ is " ) NEW_LINE inOrder ( root ) NEW_LINE mirror ( root ) NEW_LINE print ( " Inorder traversal of the mirror tree is " ) NEW_LINE inOrder ( root ) NEW_LINE