codeparrot/xlcost-text-to-code huggingface dataset:
Swap the elements between any two given quadrants of a Matrix | Python3 program for the above approach ; Function to iterate over the X quadrant and swap its element with Y quadrant ; Iterate over X quadrant ; Swap operations ; Function to swap the elements of the two given quadrants ; For Swapping 1 st and 2 nd Quadrant ; For Swapping 1 st and 3 rd Quadrant ; For Swapping 1 st and 4 th Quadrant ; For Swapping 2 nd and 3 rd Quadrant ; For Swapping 2 nd and 4 th Quadrant ; For Swapping 3 rd and 4 th Quadrant ; Print the resultant matrix ; Function to print the matrix ; Iterate over the rows ; Iterate over the cols ; Given matrix ; Given quadrants ; Function Call
N , M = 6 , 6 NEW_LINE def swap ( mat , startx_X , starty_X , startx_Y , starty_Y ) : NEW_LINE INDENT row , col = 0 , 0 NEW_LINE i = startx_X NEW_LINE while ( bool ( True ) ) : NEW_LINE INDENT col = 0 NEW_LINE j = startx_X NEW_LINE while ( bool ( True ) ) : NEW_LINE INDENT temp = mat [ i ] [ j ] NEW_LINE mat [ i ] [ j ] = mat [ startx_Y + row ] [ starty_Y + col ] NEW_LINE mat [ startx_Y + row ] [ starty_Y + col ] = temp NEW_LINE col += 1 NEW_LINE if col >= M // 2 : NEW_LINE INDENT break NEW_LINE DEDENT j += 1 NEW_LINE DEDENT row += 1 NEW_LINE if row >= N // 2 : NEW_LINE INDENT break NEW_LINE DEDENT i += 1 NEW_LINE DEDENT DEDENT def swapQuadOfMatrix ( mat , X , Y ) : NEW_LINE INDENT if ( X == 1 and Y == 2 ) : NEW_LINE INDENT swap ( mat , 0 , 0 , 0 , M // 2 ) NEW_LINE DEDENT elif ( X == 1 and Y == 3 ) : NEW_LINE INDENT swap ( mat , 0 , 0 , N // 2 , 0 ) NEW_LINE DEDENT elif ( X == 1 and Y == 4 ) : NEW_LINE INDENT swap ( mat , 0 , 0 , N // 2 , M // 2 ) NEW_LINE DEDENT elif ( X == 2 and Y == 3 ) : NEW_LINE INDENT swap ( mat , 0 , M // 2 , N // 2 , 0 ) NEW_LINE DEDENT elif ( X == 2 and Y == 4 ) : NEW_LINE INDENT swap ( mat , 0 , M // 2 , N // 2 , M // 2 ) NEW_LINE DEDENT elif ( X == 3 and Y == 4 ) : NEW_LINE INDENT swap ( mat , N // 2 , 0 , N // 2 , M // 2 ) NEW_LINE DEDENT printMat ( mat ) NEW_LINE DEDENT def printMat ( mat ) : NEW_LINE INDENT 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 DEDENT mat = [ [ 1 , 2 , 3 , 4 , 5 , 6 ] , [ 7 , 8 , 9 , 10 , 11 , 12 ] , [ 13 , 14 , 15 , 16 , 17 , 18 ] , [ 19 , 20 , 21 , 22 , 23 , 24 ] , [ 25 , 26 , 27 , 28 , 29 , 30 ] , [ 31 , 32 , 33 , 34 , 35 , 36 ] ] NEW_LINE X , Y = 1 , 4 NEW_LINE swapQuadOfMatrix ( mat , X , Y ) NEW_LINE
Bottom View of a Binary Tree | Tree node class ; Constructor of tree node ; Method that prints the bottom view . ; Initialize a variable ' hd ' with 0 for the root element . ; TreeMap which stores key value pair sorted on key value ; Queue to store tree nodes in level order traversal ; Assign initialized horizontal distance value to root node and add it to the queue . ; In STL , append ( ) is used enqueue an item ; Loop until the queue is empty ( standard level order loop ) ; In STL , pop ( ) is used dequeue an item ; Extract the horizontal distance value from the dequeued tree node . ; Put the dequeued tree node to TreeMap having key as horizontal distance . Every time we find a node having same horizontal distance we need to replace the data in the map . ; If the dequeued node has a left child , add it to the queue with a horizontal distance hd - 1. ; If the dequeued node has a right child , add it to the queue with a horizontal distance hd + 1. ; Traverse the map elements using the iterator . ; Driver Code
class Node : NEW_LINE INDENT def __init__ ( self , key ) : NEW_LINE INDENT self . data = key NEW_LINE self . hd = 1000000 NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def bottomView ( root ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return NEW_LINE DEDENT hd = 0 NEW_LINE m = dict ( ) NEW_LINE q = [ ] NEW_LINE root . hd = hd NEW_LINE q . append ( root ) NEW_LINE while ( len ( q ) != 0 ) : NEW_LINE INDENT temp = q [ 0 ] NEW_LINE q . pop ( 0 ) NEW_LINE hd = temp . hd NEW_LINE m [ hd ] = temp . data NEW_LINE if ( temp . left != None ) : NEW_LINE INDENT temp . left . hd = hd - 1 NEW_LINE q . append ( temp . left ) NEW_LINE DEDENT if ( temp . right != None ) : NEW_LINE INDENT temp . right . hd = hd + 1 NEW_LINE q . append ( temp . right ) NEW_LINE DEDENT DEDENT for i in sorted ( m . keys ( ) ) : NEW_LINE INDENT print ( m [ i ] , end = ' ▁ ' ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = Node ( 20 ) NEW_LINE root . left = Node ( 8 ) NEW_LINE root . right = Node ( 22 ) NEW_LINE root . left . left = Node ( 5 ) NEW_LINE root . left . right = Node ( 3 ) NEW_LINE root . right . left = Node ( 4 ) NEW_LINE root . right . right = Node ( 25 ) NEW_LINE root . left . right . left = Node ( 10 ) NEW_LINE root . left . right . right = Node ( 14 ) NEW_LINE print ( " Bottom ▁ view ▁ of ▁ the ▁ given ▁ binary ▁ tree ▁ : " ) NEW_LINE bottomView ( root ) NEW_LINE DEDENT
Minimum increments by index value required to obtain at least two equal Array elements | Function to calculate the minimum number of steps required ; Stores minimum difference ; Driver Code
def incrementCount ( arr , N ) : NEW_LINE INDENT mini = arr [ 0 ] - arr [ 1 ] NEW_LINE for i in range ( 2 , N ) : NEW_LINE INDENT mini = min ( mini , arr [ i - 1 ] - arr [ i ] ) NEW_LINE DEDENT print ( mini ) NEW_LINE DEDENT N = 3 NEW_LINE arr = [ 12 , 8 , 4 ] NEW_LINE incrementCount ( arr , N ) NEW_LINE
Minimum operations required to convert all characters of a String to a given Character | Function to find the minimum number of operations required ; Maximum number of characters that can be changed in one operation ; If length of the less than maximum number of characters that can be changed in an operation ; Set the last index as the index for the operation ; Otherwise ; If size of the is equal to the maximum number of characters in an operation ; Find the number of operations required ; Find the starting position ; Print i - th index ; Shift to next index ; Otherwise ; Find the number of operations required ; If n % div exceeds k ; Print i - th index ; Shift to next index ; Driver Code
def countOperations ( n , k ) : NEW_LINE INDENT div = 2 * k + 1 NEW_LINE if ( n // 2 <= k ) : NEW_LINE INDENT print ( 1 ) NEW_LINE if ( n > k ) : NEW_LINE INDENT print ( k + 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( n ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( n % div == 0 ) : NEW_LINE INDENT oprn = n // div NEW_LINE print ( oprn ) NEW_LINE pos = k + 1 NEW_LINE print ( pos , end = " ▁ " ) NEW_LINE for i in range ( 1 , oprn + 1 ) : NEW_LINE INDENT print ( pos , end = " ▁ " ) NEW_LINE pos += div NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT oprn = n // div + 1 NEW_LINE print ( oprn ) NEW_LINE pos = n % div NEW_LINE if ( n % div > k ) : NEW_LINE INDENT pos -= k NEW_LINE DEDENT for i in range ( 1 , oprn + 1 ) : NEW_LINE INDENT print ( pos , end = " ▁ " ) NEW_LINE pos += div NEW_LINE DEDENT DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " edfreqwsazxet " NEW_LINE ch = ' $ ' NEW_LINE n = len ( str ) NEW_LINE k = 4 NEW_LINE countOperations ( n , k ) NEW_LINE DEDENT
Length of largest subsequence consisting of a pair of alternating digits | Function to find the length of the largest subsequence consisting of a pair of alternating digits ; Variable initialization ; Nested loops for iteration ; Check if i is not equal to j ; Initialize length as 0 ; Iterate from 0 till the size of the string ; Increment length ; Increment length ; Update maxi ; Check if maxi is not equal to 1 the print otherwise pr0 ; Driver Code ; Given string ; Function call
def largestSubsequence ( s ) : NEW_LINE INDENT maxi = 0 NEW_LINE for i in range ( 10 ) : NEW_LINE INDENT for j in range ( 10 ) : NEW_LINE INDENT if ( i != j ) : NEW_LINE INDENT lenn = 0 NEW_LINE prev1 = chr ( j + ord ( '0' ) ) NEW_LINE for k in range ( len ( s ) ) : NEW_LINE INDENT if ( s [ k ] == chr ( i + ord ( '0' ) ) and prev1 == chr ( j + ord ( '0' ) ) ) : NEW_LINE INDENT prev1 = s [ k ] NEW_LINE lenn += 1 NEW_LINE DEDENT elif ( s [ k ] == chr ( j + ord ( '0' ) ) and prev1 == chr ( i + ord ( '0' ) ) ) : NEW_LINE INDENT prev1 = s [ k ] NEW_LINE lenn += 1 NEW_LINE DEDENT DEDENT maxi = max ( lenn , maxi ) NEW_LINE DEDENT DEDENT DEDENT if ( maxi != 1 ) : NEW_LINE INDENT print ( maxi ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( 0 ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = "1542745249842" NEW_LINE largestSubsequence ( s ) NEW_LINE DEDENT
Bottom View of a Binary Tree | Tree node class ; Constructor of tree node ; printBottomViewUtil function ; Base case ; If current level is more than or equal to maximum level seen so far for the same horizontal distance or horizontal distance is seen for the first time , update the dictionary ; recur for left subtree by decreasing horizontal distance and increasing level by 1 ; recur for right subtree by increasing horizontal distance and increasing level by 1 ; printBottomView function ; Create a dictionary where key -> relative horizontal distance of the node from root node and value -> pair containing node 's  value and its level ; Traverse the dictionary in sorted order of their keys and print the bottom view ; Driver Code
class Node : NEW_LINE INDENT def __init__ ( self , key = None , left = None , right = None ) : NEW_LINE INDENT self . data = key NEW_LINE self . left = left NEW_LINE self . right = right NEW_LINE DEDENT DEDENT def printBottomViewUtil ( root , d , hd , level ) : NEW_LINE INDENT if root is None : NEW_LINE INDENT return NEW_LINE DEDENT if hd in d : NEW_LINE INDENT if level >= d [ hd ] [ 1 ] : NEW_LINE INDENT d [ hd ] = [ root . data , level ] NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT d [ hd ] = [ root . data , level ] NEW_LINE DEDENT printBottomViewUtil ( root . left , d , hd - 1 , level + 1 ) NEW_LINE printBottomViewUtil ( root . right , d , hd + 1 , level + 1 ) NEW_LINE DEDENT def printBottomView ( root ) : NEW_LINE INDENT d = dict ( ) NEW_LINE printBottomViewUtil ( root , d , 0 , 0 ) NEW_LINE for i in sorted ( d . keys ( ) ) : NEW_LINE INDENT print ( d [ i ] [ 0 ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = Node ( 20 ) NEW_LINE root . left = Node ( 8 ) NEW_LINE root . right = Node ( 22 ) NEW_LINE root . left . left = Node ( 5 ) NEW_LINE root . left . right = Node ( 3 ) NEW_LINE root . right . left = Node ( 4 ) NEW_LINE root . right . right = Node ( 25 ) NEW_LINE root . left . right . left = Node ( 10 ) NEW_LINE root . left . right . right = Node ( 14 ) NEW_LINE print ( " Bottom ▁ view ▁ of ▁ the ▁ given ▁ binary ▁ tree ▁ : " ) NEW_LINE printBottomView ( root ) NEW_LINE DEDENT
Find maximum GCD value from root to leaf in a Binary tree | Initialise to update the maximum gcd value from all the path ; Node structure ; Initialize constructor ; Function to find gcd of a and b ; Function to find the gcd of a path ; Function to find the maximum value of gcd from root to leaf in a Binary tree ; Check if root is not null ; Find the maximum gcd of path value and store in global maxm variable ; Traverse left of binary tree ; Traverse right of the binary tree ; Driver Code ; Given Tree ; Function call ; Print the maximum AND value
global maxm NEW_LINE maxm = 0 NEW_LINE class Node : NEW_LINE INDENT def __init__ ( self , x ) : NEW_LINE INDENT self . val = x NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def gcd ( a , b ) : NEW_LINE INDENT if ( b == 0 ) : NEW_LINE INDENT return a NEW_LINE DEDENT return gcd ( b , a % b ) NEW_LINE DEDENT def find_gcd ( arr ) : NEW_LINE INDENT if ( len ( arr ) == 1 ) : NEW_LINE INDENT return arr [ 0 ] NEW_LINE DEDENT g = arr [ 0 ] NEW_LINE for i in range ( 1 , len ( arr ) ) : NEW_LINE INDENT g = gcd ( g , arr [ i ] ) NEW_LINE DEDENT return g NEW_LINE DEDENT def maxm_gcd ( root , ans ) : NEW_LINE INDENT global maxm NEW_LINE if ( not root ) : NEW_LINE INDENT return NEW_LINE DEDENT if ( root . left == None and root . right == None ) : NEW_LINE INDENT ans . append ( root . val ) NEW_LINE maxm = max ( find_gcd ( ans ) , maxm ) NEW_LINE return NEW_LINE DEDENT ans . append ( root . val ) NEW_LINE maxm_gcd ( root . left , ans ) NEW_LINE maxm_gcd ( root . right , ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = Node ( 15 ) NEW_LINE root . left = Node ( 3 ) NEW_LINE root . right = Node ( 7 ) NEW_LINE root . left . left = Node ( 15 ) NEW_LINE root . left . right = Node ( 1 ) NEW_LINE root . right . left = Node ( 31 ) NEW_LINE root . right . right = Node ( 9 ) NEW_LINE maxm_gcd ( root , [ ] ) NEW_LINE print ( maxm ) NEW_LINE DEDENT
Count of pairs with sum N from first N natural numbers | Funciton to calculate the value of count ; Stores the count of pairs ; Set the two pointers ; Check if the sum of pirs is equal to n ; Increase the count of pairs ; Move to the next pair ; Driver code
def numberOfPairs ( n ) : NEW_LINE INDENT count = 0 NEW_LINE i = 1 NEW_LINE j = n - 1 NEW_LINE while ( i < j ) : NEW_LINE INDENT if ( i + j ) == n : NEW_LINE count += 1 NEW_LINE i += 1 NEW_LINE j -= 1 NEW_LINE DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 8 NEW_LINE print ( numberOfPairs ( n ) ) NEW_LINE DEDENT
Count of ways to generate a Matrix with product of each row and column as 1 or | Function to return the number of possible ways ; Check if product can be - 1 ; driver code
def Solve ( N , M ) : NEW_LINE INDENT temp = ( N - 1 ) * ( M - 1 ) NEW_LINE ans = pow ( 2 , temp ) NEW_LINE if ( ( N + M ) % 2 != 0 ) : NEW_LINE INDENT print ( ans ) NEW_LINE else : NEW_LINE print ( 2 * ans ) NEW_LINE if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N , M = 3 , 3 NEW_LINE Solve ( N , M ) NEW_LINE DEDENT DEDENT DEDENT
Program to count leaf nodes in a binary tree | A Binary tree node ; Function to get the count of leaf nodes in binary tree ; create a tree ; get leaf count of the abve tree
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 getLeafCount ( node ) : NEW_LINE INDENT if node is None : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( node . left is None and node . right is None ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT return getLeafCount ( node . left ) + getLeafCount ( node . right ) NEW_LINE DEDENT DEDENT root = Node ( 1 ) NEW_LINE root . left = Node ( 2 ) NEW_LINE root . right = Node ( 3 ) NEW_LINE root . left . left = Node ( 4 ) NEW_LINE root . left . right = Node ( 5 ) NEW_LINE print " Leaf ▁ count ▁ of ▁ the ▁ tree ▁ is ▁ % d " % ( getLeafCount ( root ) ) NEW_LINE
Make a palindromic string from given string | Function to find winner of the game ; Array to Maintain frequency of the characters in S initialise freq array with 0 ; Maintain count of all distinct characters ; Finding frequency of each character ; Count unique duplicate characters ; Loop to count the unique duplicate characters ; Condition for Player - 1 to be winner ; Else Player - 2 is always winner ; Driven Code ; Function call
def palindromeWinner ( S ) : NEW_LINE INDENT freq = [ 0 for i in range ( 0 , 26 ) ] NEW_LINE count = 0 NEW_LINE for i in range ( 0 , len ( S ) ) : NEW_LINE INDENT if ( freq [ ord ( S [ i ] ) - 97 ] == 0 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT freq [ ord ( S [ i ] ) - 97 ] += 1 NEW_LINE DEDENT unique = 0 NEW_LINE duplicate = 0 NEW_LINE for i in range ( 0 , 26 ) : NEW_LINE INDENT if ( freq [ i ] == 1 ) : NEW_LINE INDENT unique += 1 NEW_LINE DEDENT elif ( freq [ i ] >= 2 ) : NEW_LINE INDENT duplicate += 1 NEW_LINE DEDENT DEDENT if ( unique == 1 and ( unique + duplicate ) == count ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT return 2 NEW_LINE DEDENT S = " abcbc " ; NEW_LINE print ( " Player - " , palindromeWinner ( S ) ) NEW_LINE
Number of substrings with length divisible by the number of 1 's in it | Python3 program to count number of substring under given condition ; Function return count of such substring ; Selection of adequate x value ; Store where 1 's are located ; If there are no ones , then answer is 0 ; For ease of implementation ; Count storage ; Iterate on all k values less than fixed x ; Keeps a count of 1 's occured  during string traversal ; Iterate on string and modify the totCount ; If this character is 1 ; Add to the final sum / count ; Increase totCount at exterior position ; Reduce totCount at index + k * n ; Slightly modified prefix sum storage ; Number of 1 's till i-1 ; Traversing over string considering each position and finding bounds and count using the inequalities ; Calculating bounds for l and r ; If valid then add to answer ; Driver code
import math NEW_LINE def countOfSubstrings ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE x = int ( math . sqrt ( n ) ) NEW_LINE ones = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( s [ i ] == '1' ) : NEW_LINE INDENT ones . append ( i ) NEW_LINE DEDENT DEDENT if ( len ( ones ) == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT ones . append ( n ) NEW_LINE totCount = [ 0 ] * ( n * x + n ) NEW_LINE sum = 0 NEW_LINE for k in range ( x + 1 ) : NEW_LINE INDENT now = 0 NEW_LINE totCount [ k * n ] += 1 NEW_LINE for j in range ( 1 , n + 1 ) : NEW_LINE INDENT if ( s [ j - 1 ] == '1' ) : NEW_LINE INDENT now += 1 NEW_LINE DEDENT index = j - k * now NEW_LINE sum += totCount [ index + k * n ] NEW_LINE totCount [ index + k * n ] += 1 NEW_LINE DEDENT now = 0 NEW_LINE totCount [ k * n ] -= 1 NEW_LINE for j in range ( 1 , n + 1 ) : NEW_LINE INDENT if ( s [ j - 1 ] == '1' ) : NEW_LINE INDENT now += 1 NEW_LINE DEDENT index = j - k * now NEW_LINE totCount [ index + k * n ] -= 1 NEW_LINE DEDENT DEDENT prefix_sum = [ - 1 ] * n NEW_LINE cnt = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT prefix_sum [ i ] = cnt NEW_LINE if ( s [ i ] == '1' ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT DEDENT for k in range ( n ) : NEW_LINE INDENT j = 1 NEW_LINE while ( j <= ( n // x ) and prefix_sum [ k ] + j <= cnt ) : NEW_LINE INDENT l = ( ones [ prefix_sum [ k ] + j - 1 ] - k + 1 ) NEW_LINE r = ones [ prefix_sum [ k ] + j ] - k NEW_LINE l = max ( l , j * ( x + 1 ) ) NEW_LINE if ( l <= r ) : NEW_LINE INDENT sum += r // j - ( l - 1 ) // j NEW_LINE DEDENT j += 1 NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = "1111100000" NEW_LINE print ( countOfSubstrings ( S ) ) NEW_LINE DEDENT
Find the largest number smaller than integer N with maximum number of set bits | Function to return the largest number less than N ; Iterate through all the numbers ; Find the number of set bits for the current number ; Check if this number has the highest set bits ; Return the result ; Driver code
def largestNum ( n ) : NEW_LINE INDENT num = 0 ; NEW_LINE max_setBits = 0 ; NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT setBits = bin ( i ) . count ( '1' ) ; NEW_LINE if ( setBits >= max_setBits ) : NEW_LINE INDENT num = i ; NEW_LINE max_setBits = setBits ; NEW_LINE DEDENT DEDENT return num ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 345 ; NEW_LINE print ( largestNum ( N ) ) ; NEW_LINE DEDENT
Iterative program to count leaf nodes in a Binary Tree | Python3 program to count leaf nodes in a Binary Tree ; Helper function that allocates a new Node with the given data and None left and right pointers . ; Function to get the count of leaf Nodes in a binary tree ; If tree is empty ; Initialize empty queue . ; Do level order traversal starting from root Initialize count of leaves ; Driver Code ; 1 / \ 2 3 / \ 4 5 Let us create Binary Tree shown in above example ; get leaf count of the above created tree
from queue import Queue NEW_LINE 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 getLeafCount ( node ) : NEW_LINE INDENT if ( not node ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT q = Queue ( ) NEW_LINE count = 0 NEW_LINE q . put ( node ) NEW_LINE while ( not q . empty ( ) ) : NEW_LINE INDENT temp = q . queue [ 0 ] NEW_LINE q . get ( ) NEW_LINE if ( temp . left != None ) : NEW_LINE INDENT q . put ( temp . left ) NEW_LINE DEDENT if ( temp . right != None ) : NEW_LINE INDENT q . put ( temp . right ) NEW_LINE DEDENT if ( temp . left == None and temp . right == None ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count 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 ( getLeafCount ( root ) ) NEW_LINE DEDENT
Find the Kth smallest element in the sorted generated array | Function to return the Kth element in B [ ] ; Initialize the count Array ; Reduce N repeatedly to half its value ; Add count to start ; Subtract same count after end index ; Store each element of Array [ ] with their count ; Sort the elements wrt value ; If Kth element is in range of element [ i ] return element [ i ] ; If K is out of bound ; Driver code
def solve ( Array , N , K ) : NEW_LINE INDENT count_Arr = [ 0 ] * ( N + 2 ) ; NEW_LINE factor = 1 ; NEW_LINE size = N ; NEW_LINE while ( size ) : NEW_LINE INDENT start = 1 ; NEW_LINE end = size ; NEW_LINE count_Arr [ 1 ] += factor * N ; NEW_LINE count_Arr [ end + 1 ] -= factor * N ; NEW_LINE factor += 1 ; NEW_LINE size //= 2 ; NEW_LINE DEDENT for i in range ( 2 , N + 1 ) : NEW_LINE INDENT count_Arr [ i ] += count_Arr [ i - 1 ] ; NEW_LINE DEDENT element = [ ] ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT element . append ( ( Array [ i ] , count_Arr [ i + 1 ] ) ) ; NEW_LINE DEDENT element . sort ( ) ; NEW_LINE start = 1 ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT end = start + element [ i ] [ 1 ] - 1 ; NEW_LINE if ( K >= start and K <= end ) : NEW_LINE INDENT return element [ i ] [ 0 ] ; NEW_LINE DEDENT start += element [ i ] [ 1 ] ; NEW_LINE DEDENT return - 1 ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 4 , 5 , 1 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE K = 13 ; NEW_LINE print ( solve ( arr , N , K ) ) ; NEW_LINE DEDENT
Maximum number that can be display on Seven Segment Display using N segments | Function to print maximum number that can be formed using N segments ; If n is odd ; use 3 three segment to print 7 ; remaining to print 1 ; If n is even ; print n / 2 1 s . ; Driver 's Code
def printMaxNumber ( n ) : NEW_LINE INDENT if ( n % 2 == 1 ) : NEW_LINE INDENT print ( "7" , end = " " ) ; NEW_LINE for i in range ( int ( ( n - 3 ) / 2 ) ) : NEW_LINE INDENT print ( "1" , end = " " ) ; NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT for i in range ( n / 2 ) : NEW_LINE INDENT print ( "1" , end = " " ) ; NEW_LINE DEDENT DEDENT DEDENT n = 5 ; NEW_LINE printMaxNumber ( n ) ; NEW_LINE
Find the sum of digits of a number at even and odd places | Function to find the sum of the odd and even positioned digits in a number ; To store the respective sums ; Converting integer to string ; Traversing the string ; Driver code
def getSum ( n ) : NEW_LINE INDENT sumOdd = 0 NEW_LINE sumEven = 0 NEW_LINE num = str ( n ) NEW_LINE for i in range ( len ( num ) ) : NEW_LINE INDENT if ( i % 2 == 0 ) : NEW_LINE INDENT sumOdd = sumOdd + int ( num [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT sumEven = sumEven + int ( num [ i ] ) NEW_LINE DEDENT DEDENT print ( " Sum ▁ odd ▁ = ▁ " , sumOdd ) NEW_LINE print ( " Sum ▁ even ▁ = ▁ " , sumEven ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 457892 NEW_LINE getSum ( n ) NEW_LINE DEDENT