codeparrot/xlcost-text-to-code huggingface dataset:
Iterative program to count leaf nodes in a Binary Tree | Node class ; Program to count leaves ; If the node itself is " None " return 0 , as there are no leaves ; It the node is a leaf then both right and left children will be " None " ; Now we count the leaves in the left and right subtrees and return the sum ; Driver Code ; get leaf count of the above created tree
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 countLeaves ( node ) : NEW_LINE INDENT if ( node == None ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( node . left == None and node . right == None ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT return countLeaves ( node . left ) + countLeaves ( node . right ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT 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 ( countLeaves ( root ) ) NEW_LINE DEDENT
Partition an array such into maximum increasing segments | Python 3 program to divide into maximum number of segments ; Returns the maximum number of sorted subarrays in a valid partition ; Find minimum value from right for every index ; Finding the shortest prefix such that all the elements in the prefix are less than or equal to the elements in the rest of the array . ; if current max is less than the right prefix min , we increase number of partitions . ; Driver code ; Find minimum value from right for every index
import sys NEW_LINE def sorted_partitions ( arr , n ) : NEW_LINE INDENT right_min = [ 0 ] * ( n + 1 ) NEW_LINE right_min [ n ] = sys . maxsize NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT right_min [ i ] = min ( right_min [ i + 1 ] , arr [ i ] ) NEW_LINE DEDENT partitions = 0 NEW_LINE current_max = arr [ 0 ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT current_max = max ( current_max , arr [ i ] ) NEW_LINE if ( current_max <= right_min [ i + 1 ] ) : NEW_LINE INDENT partitions += 1 NEW_LINE DEDENT DEDENT return partitions NEW_LINE DEDENT arr = [ 3 , 1 , 2 , 4 , 100 , 7 , 9 ] NEW_LINE n = len ( arr ) NEW_LINE ans = sorted_partitions ( arr , n ) NEW_LINE print ( ans ) NEW_LINE
Minimize Cost with Replacement with other allowed | Function returns the minimum cost of the array ; Driver Code
def getMinCost ( arr , n ) : NEW_LINE INDENT min_ele = min ( arr ) NEW_LINE return min_ele * ( n - 1 ) NEW_LINE DEDENT arr = [ 4 , 2 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE print ( getMinCost ( arr , n ) ) NEW_LINE
Minimum swaps required to make a binary string alternating | function to count minimum swaps required to make binary String alternating ; stores total number of ones ; stores total number of zeroes ; checking impossible condition ; odd length string ; number of even positions ; stores number of zeroes and ones at even positions ; even length string ; stores number of ones at odd and even position respectively ; Driver code
def countMinSwaps ( s ) : NEW_LINE INDENT N = len ( s ) NEW_LINE one = 0 NEW_LINE zero = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( s [ i ] == '1' ) : NEW_LINE INDENT one += 1 NEW_LINE DEDENT else : NEW_LINE INDENT zero += 1 NEW_LINE DEDENT DEDENT if ( one > zero + 1 or zero > one + 1 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT if ( N % 2 ) : NEW_LINE INDENT num = ( N + 1 ) / 2 NEW_LINE one_even = 0 NEW_LINE zero_even = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( i % 2 == 0 ) : NEW_LINE INDENT if ( s [ i ] == '1' ) : NEW_LINE INDENT one_even += 1 NEW_LINE DEDENT else : NEW_LINE INDENT zero_even += 1 NEW_LINE DEDENT DEDENT DEDENT if ( one > zero ) : NEW_LINE INDENT return num - one_even NEW_LINE DEDENT else : NEW_LINE INDENT return num - zero_even NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT one_odd = 0 NEW_LINE one_even = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( s [ i ] == '1' ) : NEW_LINE INDENT if ( i % 2 ) : NEW_LINE INDENT one_odd += 1 NEW_LINE DEDENT else : NEW_LINE INDENT one_even += 1 NEW_LINE DEDENT DEDENT DEDENT return min ( N // 2 - one_odd , N // 2 - one_even ) NEW_LINE DEDENT DEDENT s = "111000" NEW_LINE print ( countMinSwaps ( s ) ) NEW_LINE
Check if it is possible to return to the starting position after moving in the given directions | Main method ; n = 0 Count of North s = 0 Count of South e = 0 Count of East w = 0 Count of West
st = " NNNWEWESSS " NEW_LINE length = len ( st ) NEW_LINE for i in range ( length ) : NEW_LINE INDENT if ( st [ i ] == " N " ) : NEW_LINE INDENT n += 1 NEW_LINE DEDENT if ( st [ i ] == " S " ) : NEW_LINE INDENT s += 1 NEW_LINE DEDENT if ( st [ i ] == " W " ) : NEW_LINE INDENT w += 1 NEW_LINE DEDENT if ( st [ i ] == " E " ) : NEW_LINE INDENT e += 1 NEW_LINE DEDENT DEDENT if ( n == s and w == e ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT
Minimum cost to make array size 1 by removing larger of pairs | function to calculate the minimum cost ; Minimum cost is n - 1 multiplied with minimum element . ; driver code
def cost ( a , n ) : NEW_LINE INDENT return ( ( n - 1 ) * min ( a ) ) NEW_LINE DEDENT a = [ 4 , 3 , 2 ] NEW_LINE n = len ( a ) NEW_LINE print ( cost ( a , n ) ) NEW_LINE
Count Non | class that allocates a new node with the given data and None left and right pointers . ; Computes the number of non - leaf nodes in a tree . ; Base cases . ; If root is Not None and its one of its child is also not None ; 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 DEDENT DEDENT def countNonleaf ( root ) : NEW_LINE INDENT if ( root == None or ( root . left == None and root . right == None ) ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT return ( 1 + countNonleaf ( root . left ) + countNonleaf ( root . 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 ( countNonleaf ( root ) ) NEW_LINE DEDENT
Minimum cost for acquiring all coins with k extra coins allowed with every coin | Python3 program to acquire all n coins ; function to calculate min cost ; sort the coins value ; calculate no . of coins needed ; calculate sum of all selected coins ; Driver code
import math NEW_LINE def minCost ( coin , n , k ) : NEW_LINE INDENT coin . sort ( ) NEW_LINE coins_needed = math . ceil ( 1.0 * n // ( k + 1 ) ) ; NEW_LINE ans = 0 NEW_LINE for i in range ( coins_needed - 1 + 1 ) : NEW_LINE INDENT ans += coin [ i ] NEW_LINE DEDENT return ans NEW_LINE DEDENT coin = [ 8 , 5 , 3 , 10 , 2 , 1 , 15 , 25 ] NEW_LINE n = len ( coin ) NEW_LINE k = 3 NEW_LINE print ( minCost ( coin , n , k ) ) NEW_LINE
Program for First Fit algorithm in Memory Management | Function to allocate memory to blocks as per First fit algorithm ; Stores block id of the block allocated to a process ; pick each process and find suitable blocks according to its size ad assign to it ; allocate block j to p [ i ] process ; Reduce available memory in this block . ; Driver code
def firstFit ( blockSize , m , processSize , n ) : NEW_LINE INDENT allocation = [ - 1 ] * n NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE INDENT if blockSize [ j ] >= processSize [ i ] : NEW_LINE INDENT allocation [ i ] = j NEW_LINE blockSize [ j ] -= processSize [ i ] NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT print ( " ▁ Process ▁ No . ▁ Process ▁ Size ▁ Block ▁ no . " ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( " ▁ " , i + 1 , " ▁ " , processSize [ i ] , " ▁ " , end = " ▁ " ) NEW_LINE if allocation [ i ] != - 1 : NEW_LINE INDENT print ( allocation [ i ] + 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ Allocated " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT blockSize = [ 100 , 500 , 200 , 300 , 600 ] NEW_LINE processSize = [ 212 , 417 , 112 , 426 ] NEW_LINE m = len ( blockSize ) NEW_LINE n = len ( processSize ) NEW_LINE firstFit ( blockSize , m , processSize , n ) NEW_LINE DEDENT
Greedy Algorithm to find Minimum number of Coins | Python3 program to find minimum number of denominations ; All denominations of Indian Currency ; Initialize Result ; Traverse through all denomination ; Find denominations ; Print result ; Driver Code
def findMin ( V ) : NEW_LINE INDENT deno = [ 1 , 2 , 5 , 10 , 20 , 50 , 100 , 500 , 1000 ] NEW_LINE n = len ( deno ) NEW_LINE ans = [ ] NEW_LINE i = n - 1 NEW_LINE while ( i >= 0 ) : NEW_LINE INDENT while ( V >= deno [ i ] ) : NEW_LINE INDENT V -= deno [ i ] NEW_LINE ans . append ( deno [ i ] ) NEW_LINE DEDENT i -= 1 NEW_LINE DEDENT for i in range ( len ( ans ) ) : NEW_LINE INDENT print ( ans [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 93 NEW_LINE print ( " Following ▁ is ▁ minimal ▁ number " , " of ▁ change ▁ for " , n , " : ▁ " , end = " " ) NEW_LINE findMin ( n ) NEW_LINE DEDENT
Minimize count of array elements to be removed such that at least K elements are equal to their index values | Function to minimize the removals of array elements such that atleast K elements are equal to their indices ; Store the array as 1 - based indexing Copy of first array ; Make a dp - table of ( N * N ) size ; Delete the current element ; Take the current element ; Check for the minimum removals ; Driver Code
def MinimumRemovals ( a , N , K ) : NEW_LINE INDENT b = [ 0 for i in range ( N + 1 ) ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT b [ i + 1 ] = a [ i ] NEW_LINE DEDENT dp = [ [ 0 for i in range ( N + 1 ) ] for j in range ( N + 1 ) ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( i + 1 ) : NEW_LINE INDENT dp [ i + 1 ] [ j ] = max ( dp [ i + 1 ] [ j ] , dp [ i ] [ j ] ) NEW_LINE dp [ i + 1 ] [ j + 1 ] = max ( dp [ i + 1 ] [ j + 1 ] , dp [ i ] [ j ] + ( 1 if ( b [ i + 1 ] == j + 1 ) else 0 ) ) NEW_LINE DEDENT DEDENT j = N NEW_LINE while ( j >= 0 ) : NEW_LINE INDENT if ( dp [ N ] [ j ] >= K ) : NEW_LINE INDENT return ( N - j ) NEW_LINE DEDENT j -= 1 NEW_LINE DEDENT return - 1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 5 , 1 , 3 , 2 , 3 ] NEW_LINE K = 2 NEW_LINE N = len ( arr ) NEW_LINE print ( MinimumRemovals ( arr , N , K ) ) NEW_LINE DEDENT
Count of Arrays of size N having absolute difference between adjacent elements at most 1 | Function to find the count of possible arrays such that the absolute difference between any adjacent elements is atmost 1 ; Stores the dp states where dp [ i ] [ j ] represents count of arrays of length i + 1 having their last element as j ; Case where 1 st array element is missing ; All integers in range [ 1 , M ] are reachable ; Only reachable integer is arr [ 0 ] ; Iterate through all values of i ; If arr [ i ] is not missing ; Only valid value of j is arr [ i ] ; If arr [ i ] is missing ; Iterate through all possible values of j in range [ 1 , M ] ; Stores the count of valid arrays ; Calculate the total count of valid arrays ; Return answer ; Driver Code ; Function Call
def countArray ( arr , N , M ) : NEW_LINE INDENT dp = [ [ 0 for i in range ( M + 2 ) ] for j in range ( N ) ] NEW_LINE if ( arr [ 0 ] == - 1 ) : NEW_LINE INDENT for j in range ( 1 , M + 1 , 1 ) : NEW_LINE INDENT dp [ 0 ] [ j ] = 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT dp [ 0 ] [ arr [ 0 ] ] = 1 NEW_LINE DEDENT for i in range ( 1 , N , 1 ) : NEW_LINE INDENT if ( arr [ i ] != - 1 ) : NEW_LINE INDENT j = arr [ i ] NEW_LINE dp [ i ] [ j ] += dp [ i - 1 ] [ j - 1 ] + dp [ i - 1 ] [ j ] + dp [ i - 1 ] [ j + 1 ] NEW_LINE DEDENT if ( arr [ i ] == - 1 ) : NEW_LINE INDENT for j in range ( 1 , M + 1 , 1 ) : NEW_LINE INDENT dp [ i ] [ j ] += dp [ i - 1 ] [ j - 1 ] + dp [ i - 1 ] [ j ] + dp [ i - 1 ] [ j + 1 ] NEW_LINE DEDENT DEDENT DEDENT arrCount = 0 NEW_LINE for j in range ( 1 , M + 1 , 1 ) : NEW_LINE INDENT arrCount += dp [ N - 1 ] [ j ] NEW_LINE DEDENT return arrCount NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 4 , - 1 , 2 , 1 , - 1 , - 1 ] NEW_LINE N = len ( arr ) NEW_LINE M = 10 NEW_LINE print ( countArray ( arr , N , M ) ) NEW_LINE DEDENT
Maximize sum of product of neighbouring elements of the element removed from Array | Function to calculate maximum possible score using the given operations ; Iterate through all possible len1gths of the subarray ; Iterate through all the possible starting indices i having len1gth len1 ; Stores the rightmost index of the current subarray ; Initial dp [ i ] [ j ] will be 0. ; Iterate through all possible values of k in range [ i + 1 , j - 1 ] ; Return the answer ; Driver Code ; Function Call ; Function Call
def maxMergingScore ( A , N ) : NEW_LINE INDENT dp = [ [ 0 for i in range ( 101 ) ] for j in range ( 101 ) ] NEW_LINE for len1 in range ( 1 , N , 1 ) : NEW_LINE INDENT for i in range ( 0 , N - len1 , 1 ) : NEW_LINE INDENT j = i + len1 NEW_LINE dp [ i ] [ j ] = 0 NEW_LINE for k in range ( i + 1 , j , 1 ) : NEW_LINE INDENT dp [ i ] [ j ] = max ( dp [ i ] [ j ] , dp [ i ] [ k ] + dp [ k ] [ j ] + A [ i ] * A [ j ] ) NEW_LINE DEDENT DEDENT DEDENT return dp [ 0 ] [ N - 1 ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 4 NEW_LINE A = [ 1 , 2 , 3 , 4 ] NEW_LINE print ( maxMergingScore ( A , N ) ) NEW_LINE N = 2 NEW_LINE B = [ 1 , 55 ] NEW_LINE print ( maxMergingScore ( B , N ) ) NEW_LINE DEDENT
Longest subarray with all even or all odd elements | Function to calculate longest substring with odd or even elements ; Initializing dp Initializing dp with 1 ; ans will store the final answer ; Traversing the array from index 1 to N - 1 ; Checking both current and previous element is even or odd ; Updating dp with ( previous dp value ) + 1 ; Storing max element so far to ans ; Returning the final answer ; Input ; Function call
def LongestOddEvenSubarray ( A , N ) : NEW_LINE INDENT dp = 1 NEW_LINE ans = 1 NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT if ( ( A [ i ] % 2 == 0 and A [ i - 1 ] % 2 == 0 ) or ( A [ i ] % 2 != 0 and A [ i - 1 ] % 2 != 0 ) ) : NEW_LINE INDENT dp = dp + 1 NEW_LINE ans = max ( ans , dp ) NEW_LINE DEDENT else : NEW_LINE INDENT dp = 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT A = [ 2 , 5 , 7 , 2 , 4 , 6 , 8 , 3 ] NEW_LINE N = len ( A ) NEW_LINE print ( LongestOddEvenSubarray ( A , N ) ) NEW_LINE
Count of N | python program for the above approach ; Function to find number of ' N ' digit numbers such that the element is mean of sum of its adjacent digits ; If digit = n + 1 , a valid n - digit number has been formed ; If the state has already been computed ; If current position is 1 , then any digit from [ 1 - 9 ] can be placed . If n = 1 , 0 can be also placed . ; If current position is 2 , then any digit from [ 1 - 9 ] can be placed . ; previous digit selected is the mean . ; mean = ( current + prev2 ) / 2 current = ( 2 * mean ) - prev2 ; Check if current and current + 1 can be valid placements ; return answer ; Given Input ; Function call
dp = [ [ [ - 1 for i in range ( 10 ) ] for col in range ( 20 ) ] for row in range ( 100 ) ] NEW_LINE def countOfNumbers ( digit , prev1 , prev2 , n ) : NEW_LINE INDENT if ( digit == n + 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT val = dp [ digit ] [ prev1 ] [ prev2 ] NEW_LINE if ( val != - 1 ) : NEW_LINE INDENT return val NEW_LINE DEDENT val = 0 NEW_LINE if ( digit == 1 ) : NEW_LINE INDENT start = 1 NEW_LINE if ( n == 1 ) : NEW_LINE INDENT start = 0 NEW_LINE DEDENT for i in range ( start , 10 ) : NEW_LINE INDENT val += countOfNumbers ( digit + 1 , i , prev1 , n ) NEW_LINE DEDENT DEDENT elif ( digit == 2 ) : NEW_LINE INDENT for i in range ( 0 , 10 ) : NEW_LINE INDENT val += countOfNumbers ( digit + 1 , i , prev1 , n ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT mean = prev1 NEW_LINE current = ( 2 * mean ) - prev2 NEW_LINE if ( current >= 0 and current <= 9 ) : NEW_LINE INDENT val += countOfNumbers ( digit + 1 , current , prev1 , n ) NEW_LINE DEDENT if ( ( current + 1 ) >= 0 and ( current + 1 ) <= 9 ) : NEW_LINE INDENT val += countOfNumbers ( digit + 1 , current + 1 , prev1 , n ) NEW_LINE DEDENT DEDENT return val NEW_LINE DEDENT n = 2 NEW_LINE print ( countOfNumbers ( 1 , 0 , 0 , n ) ) NEW_LINE
Count half nodes in a Binary tree ( Iterative and Recursive ) | A node structure ; Iterative Method to count half nodes of binary tree ; Base Case ; Create an empty queue for level order traversal ; initialize count for half nodes ; Enqueue left child ; Enqueue right child ; Driver Program to test above function
class Node : 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 gethalfCount ( root ) : NEW_LINE INDENT if root is None : NEW_LINE INDENT return 0 NEW_LINE DEDENT queue = [ ] NEW_LINE queue . append ( root ) NEW_LINE count = 0 NEW_LINE while ( len ( queue ) > 0 ) : NEW_LINE INDENT node = queue . pop ( 0 ) NEW_LINE if node . left is not None and node . right is None or node . left is None and node . right is not None : NEW_LINE INDENT count = count + 1 NEW_LINE DEDENT if node . left is not None : NEW_LINE INDENT queue . append ( node . left ) NEW_LINE DEDENT if node . right is not None : NEW_LINE INDENT queue . append ( node . right ) NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT root = Node ( 2 ) NEW_LINE root . left = Node ( 7 ) NEW_LINE root . right = Node ( 5 ) NEW_LINE root . left . right = Node ( 6 ) NEW_LINE root . left . right . left = Node ( 1 ) NEW_LINE root . left . right . right = Node ( 11 ) NEW_LINE root . right . right = Node ( 9 ) NEW_LINE root . right . right . left = Node ( 4 ) NEW_LINE print " % d " % ( gethalfCount ( root ) ) NEW_LINE
Count N | Python3 program for the above approach ; Function to count N digit numbers whose digits are less than or equal to the absolute difference of previous two digits ; If all digits are traversed ; If the state has already been computed ; If the current digit is 1 , any digit from [ 1 - 9 ] can be placed . If N == 1 , 0 can also be placed . ; If the current digit is 2 , any digit from [ 0 - 9 ] can be placed ; For other digits , any digit from 0 to abs ( prev1 - prev2 ) can be placed ; Return the answer ; Driver code ; Input ; Function call
dp = [ [ [ - 1 for i in range ( 10 ) ] for j in range ( 10 ) ] for k in range ( 50 ) ] NEW_LINE def countOfNumbers ( digit , prev1 , prev2 , N ) : NEW_LINE INDENT if ( digit == N + 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( dp [ digit ] [ prev1 ] [ prev2 ] != - 1 ) : NEW_LINE INDENT return dp [ digit ] [ prev1 ] [ prev2 ] NEW_LINE DEDENT dp [ digit ] [ prev1 ] [ prev2 ] = 0 NEW_LINE if ( digit == 1 ) : NEW_LINE INDENT term = 0 if N == 1 else 1 NEW_LINE for j in range ( term , 10 , 1 ) : NEW_LINE INDENT dp [ digit ] [ prev1 ] [ prev2 ] += countOfNumbers ( digit + 1 , j , prev1 , N ) NEW_LINE DEDENT DEDENT elif ( digit == 2 ) : NEW_LINE INDENT for j in range ( 10 ) : NEW_LINE INDENT dp [ digit ] [ prev1 ] [ prev2 ] += countOfNumbers ( digit + 1 , j , prev1 , N ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT for j in range ( abs ( prev1 - prev2 ) + 1 ) : NEW_LINE INDENT dp [ digit ] [ prev1 ] [ prev2 ] += countOfNumbers ( digit + 1 , j , prev1 , N ) NEW_LINE DEDENT DEDENT return dp [ digit ] [ prev1 ] [ prev2 ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 3 NEW_LINE print ( countOfNumbers ( 1 , 0 , 0 , N ) ) NEW_LINE DEDENT
Count number of unique ways to paint a N x 3 grid | Function to count the number of ways to paint N * 3 grid based on given conditions ; Count of ways to pain a row with same colored ends ; Count of ways to pain a row with different colored ends ; Traverse up to ( N - 1 ) th row ; For same colored ends ; For different colored ends ; Print the total number of ways ; Driver Code ; Function call
def waysToPaint ( n ) : NEW_LINE INDENT same = 6 NEW_LINE diff = 6 NEW_LINE for _ in range ( n - 1 ) : NEW_LINE INDENT sameTmp = 3 * same + 2 * diff NEW_LINE diffTmp = 2 * same + 2 * diff NEW_LINE same = sameTmp NEW_LINE diff = diffTmp NEW_LINE DEDENT print ( same + diff ) NEW_LINE DEDENT N = 2 NEW_LINE waysToPaint ( N ) NEW_LINE
Maximize sum by selecting X different | Function to find maximum sum of at most N with different index array elements such that at most X are from A [ ] , Y are from B [ ] and Z are from C [ ] ; Base Cases ; Selecting i - th element from A [ ] ; Selecting i - th element from B [ ] ; Selecting i - th element from C [ ] ; i - th elements not selected from any of the arrays ; Select the maximum sum from all the possible calls ; Driver Code ; Given X , Y and Z ; Given A [ ] ; Given B [ ] ; Given C [ ] ; Given Size ; Function Call
def FindMaxS ( X , Y , Z , n ) : NEW_LINE INDENT global A , B , C NEW_LINE if ( X < 0 or Y < 0 or Z < 0 ) : NEW_LINE INDENT return - 10 ** 9 NEW_LINE DEDENT if ( n < 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT ch = A [ n ] + FindMaxS ( X - 1 , Y , Z , n - 1 ) NEW_LINE ca = B [ n ] + FindMaxS ( X , Y - 1 , Z , n - 1 ) NEW_LINE co = C [ n ] + FindMaxS ( X , Y , Z - 1 , n - 1 ) NEW_LINE no = FindMaxS ( X , Y , Z , n - 1 ) NEW_LINE maximum = max ( ch , max ( ca , max ( co , no ) ) ) NEW_LINE return maximum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT X = 1 NEW_LINE Y = 1 NEW_LINE Z = 1 NEW_LINE A = [ 10 , 0 , 5 ] NEW_LINE B = [ 5 , 10 , 0 ] NEW_LINE C = [ 0 , 5 , 10 ] NEW_LINE n = len ( B ) NEW_LINE print ( FindMaxS ( X , Y , Z , n - 1 ) ) NEW_LINE DEDENT
Rearrange array by interchanging positions of even and odd elements in the given array | Function to replace each even element by odd and vice - versa in a given array ; Traverse array ; If current element is even then swap it with odd ; Perform Swap ; Change the sign ; If current element is odd then swap it with even ; Perform Swap ; Change the sign ; Marked element positive ; Print final array ; Driver Code ; Given array arr [ ] ; Function Call
def replace ( arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( arr [ i ] >= 0 and arr [ j ] >= 0 and arr [ i ] % 2 == 0 and arr [ j ] % 2 != 0 ) : NEW_LINE INDENT tmp = arr [ i ] NEW_LINE arr [ i ] = arr [ j ] NEW_LINE arr [ j ] = tmp NEW_LINE arr [ j ] = - arr [ j ] NEW_LINE break NEW_LINE DEDENT elif ( arr [ i ] >= 0 and arr [ j ] >= 0 and arr [ i ] % 2 != 0 and arr [ j ] % 2 == 0 ) : NEW_LINE INDENT tmp = arr [ i ] NEW_LINE arr [ i ] = arr [ j ] NEW_LINE arr [ j ] = tmp NEW_LINE arr [ j ] = - arr [ j ] NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT arr [ i ] = abs ( arr [ i ] ) NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 3 , 2 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE replace ( arr , n ) NEW_LINE DEDENT
Count half nodes in a Binary tree ( Iterative and Recursive ) | A node structure ; Function to get the count of half Nodes in a binary tree ; 2 / \ 7 5 \ \ 6 9 / \ / 1 11 4 Let us create Binary Tree shown in above example
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 gethalfCount ( root ) : NEW_LINE INDENT if root == None : NEW_LINE INDENT return 0 NEW_LINE DEDENT res = 0 NEW_LINE if ( root . left == None and root . right != None ) or ( root . left != None and root . right == None ) : NEW_LINE INDENT res += 1 NEW_LINE DEDENT res += ( gethalfCount ( root . left ) + gethalfCount ( root . right ) ) NEW_LINE return res NEW_LINE DEDENT root = newNode ( 2 ) NEW_LINE root . left = newNode ( 7 ) NEW_LINE root . right = newNode ( 5 ) NEW_LINE root . left . right = newNode ( 6 ) NEW_LINE root . left . right . left = newNode ( 1 ) NEW_LINE root . left . right . right = newNode ( 11 ) NEW_LINE root . right . right = newNode ( 9 ) NEW_LINE root . right . right . left = newNode ( 4 ) NEW_LINE print ( gethalfCount ( root ) ) NEW_LINE