codeparrot/xlcost-text-to-code huggingface dataset:
Word Ladder ( Length of shortest chain to reach a target word ) | Python3 program to find length of the shortest chain transformation from source to target ; Returns length of shortest chain to reach ' target ' from ' start ' using minimum number of adjacent moves . D is dictionary ; If the target is not present in the dictionary ; To store the current chain length and the length of the words ; Push the starting word into the queue ; While the queue is non - empty ; Increment the chain length ; Current size of the queue ; Since the queue is being updated while it is being traversed so only the elements which were already present in the queue before the start of this loop will be traversed for now ; Remove the first word from the queue ; For every character of the word ; Retain the original character at the current position ; Replace the current character with every possible lowercase alphabet ; If the new word is equal to the target word ; Remove the word from the set if it is found in it ; And push the newly generated word which will be a part of the chain ; Restore the original character at the current position ; Driver code ; Make dictionary
from collections import deque NEW_LINE def shortestChainLen ( start , target , D ) : NEW_LINE INDENT if start == target : NEW_LINE return 0 NEW_LINE if target not in D : NEW_LINE INDENT return 0 NEW_LINE DEDENT level , wordlength = 0 , len ( start ) NEW_LINE Q = deque ( ) NEW_LINE Q . append ( start ) NEW_LINE while ( len ( Q ) > 0 ) : NEW_LINE INDENT level += 1 NEW_LINE sizeofQ = len ( Q ) NEW_LINE for i in range ( sizeofQ ) : NEW_LINE INDENT word = [ j for j in Q . popleft ( ) ] NEW_LINE for pos in range ( wordlength ) : NEW_LINE INDENT orig_char = word [ pos ] NEW_LINE for c in range ( ord ( ' a ' ) , ord ( ' z ' ) + 1 ) : NEW_LINE INDENT word [ pos ] = chr ( c ) NEW_LINE if ( " " . join ( word ) == target ) : NEW_LINE INDENT return level + 1 NEW_LINE DEDENT if ( " " . join ( word ) not in D ) : NEW_LINE INDENT continue NEW_LINE DEDENT del D [ " " . join ( word ) ] NEW_LINE Q . append ( " " . join ( word ) ) NEW_LINE DEDENT word [ pos ] = orig_char NEW_LINE DEDENT DEDENT DEDENT return 0 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT D = { } NEW_LINE D [ " poon " ] = 1 NEW_LINE D [ " plee " ] = 1 NEW_LINE D [ " same " ] = 1 NEW_LINE D [ " poie " ] = 1 NEW_LINE D [ " plie " ] = 1 NEW_LINE D [ " poin " ] = 1 NEW_LINE D [ " plea " ] = 1 NEW_LINE start = " toon " NEW_LINE target = " plea " NEW_LINE print ( " Length ▁ of ▁ shortest ▁ chain ▁ is : ▁ " , shortestChainLen ( start , target , D ) ) NEW_LINE DEDENT
Find if an array of strings can be chained to form a circle | Set 2 | Python3 code to check if cyclic order is possible among strings under given constrainsts ; Utility method for a depth first search among vertices ; Returns true if all vertices are strongly connected i . e . can be made as loop ; Initialize all vertices as not visited ; Perform a dfs from s ; Now loop through all characters ; I character is marked ( i . e . it was first or last character of some string ) then it should be visited in last dfs ( as for looping , graph should be strongly connected ) ; If we reach that means graph is connected ; return true if an order among strings is possible ; Create an empty graph ; Initialize all vertices as not marked ; Initialize indegree and outdegree of every vertex as 0. ; Process all strings one by one ; Find first and last characters ; Mark the characters ; Increase indegree and outdegree count ; Add an edge in graph ; If for any character indegree is not equal to outdegree then ordering is not possible ; Driver code
M = 26 NEW_LINE def dfs ( g , u , visit ) : NEW_LINE INDENT visit [ u ] = True NEW_LINE for i in range ( len ( g [ u ] ) ) : NEW_LINE INDENT if ( not visit [ g [ u ] [ i ] ] ) : NEW_LINE INDENT dfs ( g , g [ u ] [ i ] , visit ) NEW_LINE DEDENT DEDENT DEDENT def isConnected ( g , mark , s ) : NEW_LINE INDENT visit = [ False for i in range ( M ) ] NEW_LINE dfs ( g , s , visit ) NEW_LINE for i in range ( M ) : NEW_LINE INDENT if ( mark [ i ] and ( not visit [ i ] ) ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def possibleOrderAmongString ( arr , N ) : NEW_LINE INDENT g = { } NEW_LINE mark = [ False for i in range ( M ) ] NEW_LINE In = [ 0 for i in range ( M ) ] NEW_LINE out = [ 0 for i in range ( M ) ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT f = ( ord ( arr [ i ] [ 0 ] ) - ord ( ' a ' ) ) NEW_LINE l = ( ord ( arr [ i ] [ - 1 ] ) - ord ( ' a ' ) ) NEW_LINE mark [ f ] = True NEW_LINE mark [ l ] = True NEW_LINE In [ l ] += 1 NEW_LINE out [ f ] += 1 NEW_LINE if f not in g : NEW_LINE INDENT g [ f ] = [ ] NEW_LINE DEDENT g [ f ] . append ( l ) NEW_LINE DEDENT for i in range ( M ) : NEW_LINE INDENT if ( In [ i ] != out [ i ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return isConnected ( g , mark , ord ( arr [ 0 ] [ 0 ] ) - ord ( ' a ' ) ) NEW_LINE DEDENT arr = [ " ab " , " bc " , " cd " , " de " , " ed " , " da " ] NEW_LINE N = len ( arr ) NEW_LINE if ( possibleOrderAmongString ( arr , N ) == False ) : NEW_LINE INDENT print ( " Ordering ▁ not ▁ possible " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Ordering ▁ is ▁ possible " ) NEW_LINE DEDENT
Dynamic Connectivity | Set 1 ( Incremental ) | Finding the root of node i ; union of two nodes a and b ; union based on rank ; Returns true if two nodes have same root ; Performing an operation according to query type ; type 1 query means checking if node x and y are connected or not ; If roots of x and y is same then yes is the answer ; type 2 query refers union of x and y ; If x and y have different roots then union them ; Driver Code ; No . of nodes ; The following two arrays are used to implement disjoset data structure . arr [ ] holds the parent nodes while rank array holds the rank of subset ; initializing both array and rank ; number of queries
def root ( arr , i ) : NEW_LINE INDENT while ( arr [ i ] != i ) : NEW_LINE INDENT arr [ i ] = arr [ arr [ i ] ] NEW_LINE i = arr [ i ] NEW_LINE DEDENT return i NEW_LINE DEDENT def weighted_union ( arr , rank , a , b ) : NEW_LINE INDENT root_a = root ( arr , a ) NEW_LINE root_b = root ( arr , b ) NEW_LINE if ( rank [ root_a ] < rank [ root_b ] ) : NEW_LINE INDENT arr [ root_a ] = arr [ root_b ] NEW_LINE rank [ root_b ] += rank [ root_a ] NEW_LINE DEDENT else : NEW_LINE INDENT arr [ root_b ] = arr [ root_a ] NEW_LINE rank [ root_a ] += rank [ root_b ] NEW_LINE DEDENT DEDENT def areSame ( arr , a , b ) : NEW_LINE INDENT return ( root ( arr , a ) == root ( arr , b ) ) NEW_LINE DEDENT def query ( type , x , y , arr , rank ) : NEW_LINE INDENT if ( type == 1 ) : NEW_LINE INDENT if ( areSame ( arr , x , y ) == True ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT elif ( type == 2 ) : NEW_LINE INDENT if ( areSame ( arr , x , y ) == False ) : NEW_LINE INDENT weighted_union ( arr , rank , x , y ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 7 NEW_LINE arr = [ None ] * n NEW_LINE rank = [ None ] * n NEW_LINE for i in range ( n ) : NEW_LINE INDENT arr [ i ] = i NEW_LINE rank [ i ] = 1 NEW_LINE DEDENT q = 11 NEW_LINE query ( 1 , 0 , 1 , arr , rank ) NEW_LINE query ( 2 , 0 , 1 , arr , rank ) NEW_LINE query ( 2 , 1 , 2 , arr , rank ) NEW_LINE query ( 1 , 0 , 2 , arr , rank ) NEW_LINE query ( 2 , 0 , 2 , arr , rank ) NEW_LINE query ( 2 , 2 , 3 , arr , rank ) NEW_LINE query ( 2 , 3 , 4 , arr , rank ) NEW_LINE query ( 1 , 0 , 5 , arr , rank ) NEW_LINE query ( 2 , 4 , 5 , arr , rank ) NEW_LINE query ( 2 , 5 , 6 , arr , rank ) NEW_LINE query ( 1 , 2 , 6 , arr , rank ) NEW_LINE DEDENT
Ford | Python program for implementation of Ford Fulkerson algorithm ; Returns true if there is a path from source ' s ' to sink ' t ' in residual graph . Also fills parent [ ] to store the path ; Mark all the vertices as not visited ; Create a queue for BFS ; Standard BFS Loop ; If we find a connection to the sink node , then there is no point in BFS anymore We just have to set its parent and can return true ; We didn 't reach sink in BFS starting  from source, so return false ; Returns tne maximum flow from s to t in the given graph ; This class represents a directed graph using adjacency matrix representation ; This array is filled by BFS and to store path ; There is no flow initially ; Augment the flow while there is path from source to sink ; Find minimum residual capacity of the edges along the path filled by BFS . Or we can say find the maximum flow through the path found . ; update residual capacities of the edges and reverse edges along the path ; Add path flow to overall flow ; Return the overall flow ; Create a graph given in the above diagram
from collections import defaultdict NEW_LINE INDENT def BFS ( self , s , t , parent ) : NEW_LINE INDENT visited = [ False ] * ( self . ROW ) NEW_LINE queue = [ ] NEW_LINE queue . append ( s ) NEW_LINE visited [ s ] = True NEW_LINE while queue : NEW_LINE INDENT u = queue . pop ( 0 ) NEW_LINE for ind , val in enumerate ( self . graph [ u ] ) : NEW_LINE INDENT if visited [ ind ] == False and val > 0 : NEW_LINE INDENT if ind == t : NEW_LINE INDENT visited [ ind ] = True NEW_LINE return True NEW_LINE DEDENT queue . append ( ind ) NEW_LINE visited [ ind ] = True NEW_LINE parent [ ind ] = u NEW_LINE DEDENT DEDENT DEDENT return False NEW_LINE DEDENT def FordFulkerson ( self , source , sink ) : NEW_LINE DEDENT class Graph : NEW_LINE INDENT def __init__ ( self , graph ) : NEW_LINE INDENT self . graph = graph NEW_LINE self . ROW = len ( graph ) NEW_LINE parent = [ - 1 ] * ( self . ROW ) NEW_LINE DEDENT DEDENT max_flow = 0 NEW_LINE INDENT while self . BFS ( source , sink , parent ) : NEW_LINE INDENT path_flow = float ( " Inf " ) NEW_LINE s = sink NEW_LINE while ( s != source ) : NEW_LINE INDENT path_flow = min ( path_flow , self . graph [ parent [ s ] ] [ s ] ) NEW_LINE s = parent [ s ] NEW_LINE DEDENT v = sink NEW_LINE while ( v != source ) : NEW_LINE INDENT u = parent [ v ] NEW_LINE self . graph [ u ] [ v ] -= path_flow NEW_LINE self . graph [ v ] [ u ] += path_flow NEW_LINE v = parent [ v ] NEW_LINE DEDENT max_flow += path_flow NEW_LINE DEDENT return max_flow NEW_LINE DEDENT graph = [ [ 0 , 16 , 13 , 0 , 0 , 0 ] , [ 0 , 0 , 10 , 12 , 0 , 0 ] , [ 0 , 4 , 0 , 0 , 14 , 0 ] , [ 0 , 0 , 9 , 0 , 0 , 20 ] , [ 0 , 0 , 0 , 7 , 0 , 4 ] , [ 0 , 0 , 0 , 0 , 0 , 0 ] ] NEW_LINE g = Graph ( graph ) NEW_LINE source = 0 ; sink = 5 NEW_LINE print ( " The ▁ maximum ▁ possible ▁ flow ▁ is ▁ % d ▁ " % g . FordFulkerson ( source , sink ) ) NEW_LINE
Channel Assignment Problem | A Depth First Search based recursive function that returns true if a matching for vertex u is possible ; Try every receiver one by one ; If sender u has packets to send to receiver v and receiver v is not already mapped to any other sender just check if the number of packets is greater than '0' because only one packet can be sent in a time frame anyways ; Mark v as visited ; If receiver ' v ' is not assigned to any sender OR previously assigned sender for receiver v ( which is matchR [ v ] ) has an alternate receiver available . Since v is marked as visited in the above line , matchR [ v ] in the following recursive call will not get receiver ' v ' again ; Returns maximum number of packets that can be sent parallely in 1 time slot from sender to receiver ; Initially all receivers are not mapped to any senders ; Count of receivers assigned to senders ; Mark all receivers as not seen for next sender ; Find if the sender ' u ' can be assigned to the receiver ; Driver Code
def bpm ( table , u , seen , matchR ) : NEW_LINE INDENT global M , N NEW_LINE for v in range ( N ) : NEW_LINE INDENT if ( table [ u ] [ v ] > 0 and not seen [ v ] ) : NEW_LINE INDENT seen [ v ] = True NEW_LINE if ( matchR [ v ] < 0 or bpm ( table , matchR [ v ] , seen , matchR ) ) : NEW_LINE INDENT matchR [ v ] = u NEW_LINE return True NEW_LINE DEDENT DEDENT DEDENT return False NEW_LINE DEDENT def maxBPM ( table ) : NEW_LINE INDENT global M , N NEW_LINE matchR = [ - 1 ] * N NEW_LINE result = 0 NEW_LINE for u in range ( M ) : NEW_LINE INDENT seen = [ 0 ] * N NEW_LINE if ( bpm ( table , u , seen , matchR ) ) : NEW_LINE INDENT result += 1 NEW_LINE DEDENT DEDENT print ( " The ▁ number ▁ of ▁ maximum ▁ packets ▁ sent " , " in ▁ the ▁ time ▁ slot ▁ is " , result ) NEW_LINE for x in range ( N ) : NEW_LINE INDENT if ( matchR [ x ] + 1 != 0 ) : NEW_LINE INDENT print ( " T " , matchR [ x ] + 1 , " - > ▁ R " , x + 1 ) NEW_LINE DEDENT DEDENT return result NEW_LINE DEDENT M = 3 NEW_LINE N = 4 NEW_LINE table = [ [ 0 , 2 , 0 ] , [ 3 , 0 , 1 ] , [ 2 , 4 , 0 ] ] NEW_LINE max_flow = maxBPM ( table ) NEW_LINE
Prim 's algorithm using priority_queue in STL | If v is not in MST and weight of ( u , v ) is smaller than current key of v ; Updating key of v
if ( inMST [ v ] == False and key [ v ] > weight ) : NEW_LINE INDENT key [ v ] = weight NEW_LINE pq . append ( [ key [ v ] , v ] ) NEW_LINE parent [ v ] = u NEW_LINE DEDENT
Graph implementation using STL for competitive programming | Set 2 ( Weighted graph ) | To add an edge ; Print adjacency list representaion ot graph ; Driver code
def addEdge ( adj , u , v , wt ) : NEW_LINE INDENT adj [ u ] . append ( [ v , wt ] ) NEW_LINE adj [ v ] . append ( [ u , wt ] ) NEW_LINE return adj NEW_LINE DEDENT def printGraph ( adj , V ) : NEW_LINE INDENT v , w = 0 , 0 NEW_LINE for u in range ( V ) : NEW_LINE INDENT print ( " Node " , u , " makes ▁ an ▁ edge ▁ with " ) NEW_LINE for it in adj [ u ] : NEW_LINE INDENT v = it [ 0 ] NEW_LINE w = it [ 1 ] NEW_LINE print ( " TABSYMBOL Node " , v , " with ▁ edge ▁ weight ▁ = " , w ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT V = 5 NEW_LINE adj = [ [ ] for i in range ( V ) ] NEW_LINE adj = addEdge ( adj , 0 , 1 , 10 ) NEW_LINE adj = addEdge ( adj , 0 , 4 , 20 ) NEW_LINE adj = addEdge ( adj , 1 , 2 , 30 ) NEW_LINE adj = addEdge ( adj , 1 , 3 , 40 ) NEW_LINE adj = addEdge ( adj , 1 , 4 , 50 ) NEW_LINE adj = addEdge ( adj , 2 , 3 , 60 ) NEW_LINE adj = addEdge ( adj , 3 , 4 , 70 ) NEW_LINE printGraph ( adj , V ) NEW_LINE DEDENT
K Centers Problem | Set 1 ( Greedy Approximate Algorithm ) | Python3 program for the above approach ; index of city having the maximum distance to it 's  closest center ; updating the distance of the cities to their closest centers ; updating the index of the city with the maximum distance to it 's closest center ; Printing the maximum distance of a city to a center that is our answer print ( ) ; Printing the cities that were chosen to be made centers ; Driver Code ; Function Call
def maxindex ( dist , n ) : NEW_LINE INDENT mi = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( dist [ i ] > dist [ mi ] ) : NEW_LINE INDENT mi = i NEW_LINE DEDENT DEDENT return mi NEW_LINE DEDENT def selectKcities ( n , weights , k ) : NEW_LINE INDENT dist = [ 0 ] * n NEW_LINE centers = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT dist [ i ] = 10 ** 9 NEW_LINE DEDENT max = 0 NEW_LINE for i in range ( k ) : NEW_LINE INDENT centers . append ( max ) NEW_LINE for j in range ( n ) : NEW_LINE INDENT dist [ j ] = min ( dist [ j ] , weights [ max ] [ j ] ) NEW_LINE DEDENT max = maxindex ( dist , n ) NEW_LINE DEDENT print ( dist [ max ] ) NEW_LINE for i in centers : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 4 NEW_LINE weights = [ [ 0 , 4 , 8 , 5 ] , [ 4 , 0 , 10 , 7 ] , [ 8 , 10 , 0 , 9 ] , [ 5 , 7 , 9 , 0 ] ] NEW_LINE k = 2 NEW_LINE selectKcities ( n , weights , k ) NEW_LINE DEDENT
Hierholzer 's Algorithm for directed graph | Python3 program to print Eulerian circuit in given directed graph using Hierholzer algorithm ; adj represents the adjacency list of the directed graph edge_count represents the number of edges emerging from a vertex ; find the count of edges to keep track of unused edges ; empty graph Maintain a stack to keep vertices ; vector to store final circuit ; start from any vertex ; Current vertex ; If there 's remaining edge ; Push the vertex ; Find the next vertex using an edge ; and remove that edge ; Move to next vertex ; back - track to find remaining circuit ; Back - tracking ; we 've got the circuit, now print it in reverse ; Driver Code ; Input Graph 1 ; Build the edges ; Input Graph 2
def printCircuit ( adj ) : NEW_LINE INDENT edge_count = dict ( ) NEW_LINE for i in range ( len ( adj ) ) : NEW_LINE INDENT edge_count [ i ] = len ( adj [ i ] ) NEW_LINE DEDENT if len ( adj ) == 0 : NEW_LINE INDENT return NEW_LINE DEDENT curr_path = [ ] NEW_LINE circuit = [ ] NEW_LINE curr_path . append ( 0 ) NEW_LINE curr_v = 0 NEW_LINE while len ( curr_path ) : NEW_LINE INDENT if edge_count [ curr_v ] : NEW_LINE INDENT curr_path . append ( curr_v ) NEW_LINE next_v = adj [ curr_v ] [ - 1 ] NEW_LINE edge_count [ curr_v ] -= 1 NEW_LINE adj [ curr_v ] . pop ( ) NEW_LINE curr_v = next_v NEW_LINE DEDENT else : NEW_LINE INDENT circuit . append ( curr_v ) NEW_LINE curr_v = curr_path [ - 1 ] NEW_LINE curr_path . pop ( ) NEW_LINE DEDENT DEDENT for i in range ( len ( circuit ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT print ( circuit [ i ] , end = " " ) NEW_LINE if i : NEW_LINE INDENT print ( " ▁ - > ▁ " , end = " " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT adj1 = [ 0 ] * 3 NEW_LINE for i in range ( 3 ) : NEW_LINE INDENT adj1 [ i ] = [ ] NEW_LINE DEDENT adj1 [ 0 ] . append ( 1 ) NEW_LINE adj1 [ 1 ] . append ( 2 ) NEW_LINE adj1 [ 2 ] . append ( 0 ) NEW_LINE printCircuit ( adj1 ) NEW_LINE print ( ) NEW_LINE adj2 = [ 0 ] * 7 NEW_LINE for i in range ( 7 ) : NEW_LINE INDENT adj2 [ i ] = [ ] NEW_LINE DEDENT adj2 [ 0 ] . append ( 1 ) NEW_LINE adj2 [ 0 ] . append ( 6 ) NEW_LINE adj2 [ 1 ] . append ( 2 ) NEW_LINE adj2 [ 2 ] . append ( 0 ) NEW_LINE adj2 [ 2 ] . append ( 3 ) NEW_LINE adj2 [ 3 ] . append ( 4 ) NEW_LINE adj2 [ 4 ] . append ( 2 ) NEW_LINE adj2 [ 4 ] . append ( 5 ) NEW_LINE adj2 [ 5 ] . append ( 0 ) NEW_LINE adj2 [ 6 ] . append ( 4 ) NEW_LINE printCircuit ( adj2 ) NEW_LINE print ( ) NEW_LINE DEDENT
Perfect Binary Tree Specific Level Order Traversal | Set 2 | Linked List node ; Given a perfect binary tree , print its nodes in specific level order ; for level order traversal ; Stack to print reverse ; vector to store the level ; considering size of the level ; push data of the node of a particular level to vector ; push vector containing a level in Stack ; print the Stack ; Finally pop all Nodes from Stack and prints them . ; finally print root ; Driver code
class Node : 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 specific_level_order_traversal ( root ) : NEW_LINE INDENT q = [ ] NEW_LINE s = [ ] NEW_LINE q . append ( root ) NEW_LINE sz = 0 NEW_LINE while ( len ( q ) > 0 ) : NEW_LINE INDENT v = [ ] NEW_LINE sz = len ( q ) NEW_LINE i = 0 NEW_LINE while ( i < sz ) : NEW_LINE INDENT temp = q [ 0 ] NEW_LINE q . pop ( 0 ) NEW_LINE v . append ( temp . data ) NEW_LINE if ( temp . left != None ) : NEW_LINE INDENT q . append ( temp . left ) NEW_LINE DEDENT if ( temp . right != None ) : NEW_LINE INDENT q . append ( temp . right ) NEW_LINE DEDENT i = i + 1 NEW_LINE DEDENT s . append ( v ) NEW_LINE DEDENT while ( len ( s ) > 0 ) : NEW_LINE INDENT v = s [ - 1 ] NEW_LINE s . pop ( ) NEW_LINE i = 0 NEW_LINE j = len ( v ) - 1 NEW_LINE while ( i < j ) : NEW_LINE INDENT print ( v [ i ] , " ▁ " , v [ j ] , end = " ▁ " ) NEW_LINE j = j - 1 NEW_LINE i = i + 1 NEW_LINE DEDENT DEDENT print ( root . data ) NEW_LINE DEDENT root = Node ( 1 ) NEW_LINE root . left = Node ( 2 ) NEW_LINE root . right = Node ( 3 ) NEW_LINE print ( " Specific ▁ Level ▁ Order ▁ traversal ▁ of ▁ binary ▁ tree ▁ is " ) NEW_LINE specific_level_order_traversal ( root ) NEW_LINE
Number of Triangles in an Undirected Graph | Number of vertices in the graph ; Utility function for matrix multiplication ; Utility function to calculate trace of a matrix ( sum ofdiagnonal elements ) ; Utility function for calculating number of triangles in graph ; To Store graph ^ 2 ; To Store graph ^ 3 ; Initialising aux matrices with 0 ; aux2 is graph ^ 2 now printMatrix ( aux2 ) ; after this multiplication aux3 is graph ^ 3 printMatrix ( aux3 ) ; Driver Code
V = 4 NEW_LINE def multiply ( A , B , C ) : NEW_LINE INDENT global V NEW_LINE for i in range ( V ) : NEW_LINE INDENT for j in range ( V ) : NEW_LINE INDENT C [ i ] [ j ] = 0 NEW_LINE for k in range ( V ) : NEW_LINE INDENT C [ i ] [ j ] += A [ i ] [ k ] * B [ k ] [ j ] NEW_LINE DEDENT DEDENT DEDENT DEDENT def getTrace ( graph ) : NEW_LINE INDENT global V NEW_LINE trace = 0 NEW_LINE for i in range ( V ) : NEW_LINE INDENT trace += graph [ i ] [ i ] NEW_LINE DEDENT return trace NEW_LINE DEDENT def triangleInGraph ( graph ) : NEW_LINE INDENT global V NEW_LINE aux2 = [ [ None ] * V for i in range ( V ) ] NEW_LINE aux3 = [ [ None ] * V for i in range ( V ) ] NEW_LINE for i in range ( V ) : NEW_LINE INDENT for j in range ( V ) : NEW_LINE INDENT aux2 [ i ] [ j ] = aux3 [ i ] [ j ] = 0 NEW_LINE DEDENT DEDENT multiply ( graph , graph , aux2 ) NEW_LINE multiply ( graph , aux2 , aux3 ) NEW_LINE trace = getTrace ( aux3 ) NEW_LINE return trace // 6 NEW_LINE DEDENT graph = [ [ 0 , 1 , 1 , 0 ] , [ 1 , 0 , 1 , 1 ] , [ 1 , 1 , 0 , 1 ] , [ 0 , 1 , 1 , 0 ] ] NEW_LINE print ( " Total ▁ number ▁ of ▁ Triangle ▁ in ▁ Graph ▁ : " , triangleInGraph ( graph ) ) NEW_LINE
Subsequence queries after removing substrings | arrays to store results of preprocessing ; function to preprocess the strings ; initialize it as 0. ; store subsequence count in forward direction ; store number of matches till now ; store subsequence count in backward direction ; store number of matches till now ; function that gives the output ; length of remaining String A is less than B 's length ; Driver Code ; two queries
fwd = [ 0 ] * 100 NEW_LINE bwd = [ 0 ] * 100 NEW_LINE def preProcess ( a , b ) : NEW_LINE INDENT n = len ( a ) NEW_LINE j = 0 NEW_LINE for i in range ( 1 , len ( a ) + 1 ) : NEW_LINE INDENT if j < len ( b ) and a [ i - 1 ] == b [ j ] : NEW_LINE INDENT j += 1 NEW_LINE DEDENT fwd [ i ] = j NEW_LINE DEDENT j = 0 NEW_LINE for i in range ( len ( a ) , 0 , - 1 ) : NEW_LINE INDENT if ( j < len ( b ) and a [ i - 1 ] == b [ len ( b ) - j - 1 ] ) : NEW_LINE INDENT j += 1 NEW_LINE DEDENT bwd [ i ] = j NEW_LINE DEDENT DEDENT def query ( a , b , x , y ) : NEW_LINE INDENT if ( x - 1 + len ( a ) - y ) < len ( b ) : NEW_LINE INDENT print ( " No " ) NEW_LINE return NEW_LINE DEDENT if ( fwd [ x - 1 ] + bwd [ y + 1 ] ) >= len ( b ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = " abcabcxy " NEW_LINE b = " acy " NEW_LINE preProcess ( a , b ) NEW_LINE x = 2 NEW_LINE y = 5 NEW_LINE query ( a , b , x , y ) NEW_LINE x = 3 NEW_LINE y = 6 NEW_LINE query ( a , b , x , y ) NEW_LINE DEDENT
Count subsequence of length three in a given string | Function to find number of occurrences of a subsequence of length three in a string ; variable to store no of occurrences ; loop to find first character ; loop to find 2 nd character ; loop to find 3 rd character ; increment count if subsequence is found ; Driver Code
def findOccurrences ( str , substr ) : NEW_LINE INDENT counter = 0 NEW_LINE for i in range ( 0 , len ( str ) ) : NEW_LINE INDENT if ( str [ i ] == substr [ 0 ] ) : NEW_LINE INDENT for j in range ( i + 1 , len ( str ) ) : NEW_LINE INDENT if ( str [ j ] == substr [ 1 ] ) : NEW_LINE INDENT for k in range ( j + 1 , len ( str ) ) : NEW_LINE INDENT if ( str [ k ] == substr [ 2 ] ) : NEW_LINE INDENT counter = counter + 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT DEDENT return counter NEW_LINE DEDENT str = " GFGFGYSYIOIWIN " NEW_LINE substr = " GFG " NEW_LINE print ( findOccurrences ( str , substr ) ) NEW_LINE