codeparrot/xlcost-text-to-code huggingface dataset:
Dynamic Programming | Wildcard Pattern Matching | Linear Time and Constant Space | Function that matches input txt with given wildcard pattern ; empty pattern can only match with empty sting Base case ; step 1 initialize markers : ; For step - ( 2 , 5 ) ; For step - ( 3 ) ; For step - ( 4 ) ; For step - ( 5 ) ; For step - ( 6 ) ; For step - ( 7 ) ; Final Check ; Driver code
def stringmatch ( txt , pat , n , m ) : NEW_LINE INDENT if ( m == 0 ) : NEW_LINE INDENT return ( n == 0 ) NEW_LINE DEDENT i = 0 NEW_LINE j = 0 NEW_LINE index_txt = - 1 NEW_LINE index_pat = - 1 NEW_LINE while ( i < n - 2 ) : NEW_LINE INDENT if ( j < m and txt [ i ] == pat [ j ] ) : NEW_LINE INDENT i += 1 NEW_LINE j += 1 NEW_LINE DEDENT elif ( j < m and pat [ j ] == ' ? ' ) : NEW_LINE INDENT i += 1 NEW_LINE j += 1 NEW_LINE DEDENT elif ( j < m and pat [ j ] == ' * ' ) : NEW_LINE INDENT index_txt = i NEW_LINE index_pat = j NEW_LINE j += 1 NEW_LINE DEDENT elif ( index_pat != - 1 ) : NEW_LINE INDENT j = index_pat + 1 NEW_LINE i = index_txt + 1 NEW_LINE index_txt += 1 NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT while ( j < m and pat [ j ] == ' * ' ) : NEW_LINE INDENT j += 1 NEW_LINE DEDENT if ( j == m ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT strr = " baaabab " NEW_LINE pattern = " * * * * * ba * * * * * ab " NEW_LINE char pattern [ ] = " ba * * * * * ab " NEW_LINE char pattern [ ] = " ba ▁ * ▁ ab " NEW_LINE char pattern [ ] =   " a * ab NEW_LINE if ( stringmatch ( strr , pattern , len ( strr ) , len ( pattern ) ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT pattern2 = " a * * * * * ab " ; NEW_LINE if ( stringmatch ( strr , pattern2 , len ( strr ) , len ( pattern2 ) ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Find n | function to find nth stern ' diatomic series ; Initializing the DP array ; SET the Base case ; Traversing the array from 2 nd Element to nth Element ; Case 1 : for even n ; Case 2 : for odd n ; Driver program
def findSDSFunc ( n ) : NEW_LINE INDENT DP = [ 0 ] * ( n + 1 ) NEW_LINE DP [ 0 ] = 0 NEW_LINE DP [ 1 ] = 1 NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT if ( int ( i % 2 ) == 0 ) : NEW_LINE INDENT DP [ i ] = DP [ int ( i / 2 ) ] NEW_LINE DEDENT else : NEW_LINE INDENT DP [ i ] = ( DP [ int ( ( i - 1 ) / 2 ) ] + DP [ int ( ( i + 1 ) / 2 ) ] ) NEW_LINE DEDENT DEDENT return DP [ n ] NEW_LINE DEDENT n = 15 NEW_LINE print ( findSDSFunc ( n ) ) NEW_LINE
Check if any valid sequence is divisible by M | Python3 Program to Check if any valid sequence is divisible by M ; Calculate modulo for this call ; Base case ; check if sum is divisible by M ; check if the current state is already computed ; 1. Try placing '+ ; 2. Try placing '- ; calculate value of res for recursive case ; store the value for res for current states and return for parent call ; Driver code ; MAX is the Maximum value M can take
MAX = 100 NEW_LINE def isPossible ( n , index , modulo , M , arr , dp ) : NEW_LINE INDENT modulo = ( ( modulo % M ) + M ) % M NEW_LINE if ( index == n ) : NEW_LINE INDENT if ( modulo == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT return 0 NEW_LINE DEDENT if ( dp [ index ] [ modulo ] != - 1 ) : NEW_LINE INDENT return dp [ index ] [ modulo ] NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT placeAdd = isPossible ( n , index + 1 , modulo + arr [ index ] , M , arr , dp ) NEW_LINE DEDENT ' NEW_LINE INDENT placeMinus = isPossible ( n , index + 1 , modulo - arr [ index ] , M , arr , dp ) NEW_LINE res = bool ( placeAdd or placeMinus ) NEW_LINE dp [ index ] [ modulo ] = res NEW_LINE return res NEW_LINE DEDENT arr = [ 1 , 2 , 3 , 4 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE M = 4 NEW_LINE dp = [ [ - 1 ] * ( n + 1 ) ] * MAX NEW_LINE res = isPossible ( n , 1 , arr [ 0 ] , M , arr , dp ) NEW_LINE if ( res == True ) : NEW_LINE INDENT print ( " True " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " False " ) NEW_LINE DEDENT
Find common elements in three sorted arrays | This function prints common elements in ar1 ; Initialize starting indexes for ar1 [ ] , ar2 [ ] and ar3 [ ] ; Iterate through three arrays while all arrays have elements ; If x = y and y = z , print any of them and move ahead in all arrays ; x < y ; y < z ; We reach here when x > y and z < y , i . e . , z is smallest ; Driver program to check above function
def findCommon ( ar1 , ar2 , ar3 , n1 , n2 , n3 ) : NEW_LINE INDENT i , j , k = 0 , 0 , 0 NEW_LINE while ( i < n1 and j < n2 and k < n3 ) : NEW_LINE INDENT if ( ar1 [ i ] == ar2 [ j ] and ar2 [ j ] == ar3 [ k ] ) : NEW_LINE INDENT print ar1 [ i ] , NEW_LINE i += 1 NEW_LINE j += 1 NEW_LINE k += 1 NEW_LINE DEDENT elif ar1 [ i ] < ar2 [ j ] : NEW_LINE INDENT i += 1 NEW_LINE DEDENT elif ar2 [ j ] < ar3 [ k ] : NEW_LINE INDENT j += 1 NEW_LINE DEDENT else : NEW_LINE INDENT k += 1 NEW_LINE DEDENT DEDENT DEDENT ar1 = [ 1 , 5 , 10 , 20 , 40 , 80 ] NEW_LINE ar2 = [ 6 , 7 , 20 , 80 , 100 ] NEW_LINE ar3 = [ 3 , 4 , 15 , 20 , 30 , 70 , 80 , 120 ] NEW_LINE n1 = len ( ar1 ) NEW_LINE n2 = len ( ar2 ) NEW_LINE n3 = len ( ar3 ) NEW_LINE print " Common ▁ elements ▁ are " , NEW_LINE findCommon ( ar1 , ar2 , ar3 , n1 , n2 , n3 ) NEW_LINE
Dynamic Programming on Trees | Set | Python3 code to find the maximum path sum ; Function for dfs traversal and to store the maximum value in dp [ ] for every node till the leaves ; Initially dp [ u ] is always a [ u ] ; Stores the maximum value from nodes ; Traverse the tree ; If child is parent , then we continue without recursing further ; Call dfs for further traversal ; Store the maximum of previous visited node and present visited node ; Add the maximum value returned to the parent node ; Function that returns the maximum value ; Driver Code ; Number of nodes ; Adjacency list as a dictionary ; Create undirected edges initialize the tree given in the diagram ; Values of node 1 , 2 , 3. ... 14 ; Function call
dp = [ 0 ] * 100 NEW_LINE def dfs ( a , v , u , parent ) : NEW_LINE INDENT dp [ u ] = a [ u - 1 ] NEW_LINE maximum = 0 NEW_LINE for child in v [ u ] : NEW_LINE INDENT if child == parent : NEW_LINE INDENT continue NEW_LINE DEDENT dfs ( a , v , child , u ) NEW_LINE maximum = max ( maximum , dp [ child ] ) NEW_LINE DEDENT dp [ u ] += maximum NEW_LINE DEDENT def maximumValue ( a , v ) : NEW_LINE INDENT dfs ( a , v , 1 , 0 ) NEW_LINE return dp [ 1 ] NEW_LINE DEDENT def main ( ) : NEW_LINE INDENT n = 14 NEW_LINE v = { } NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT v [ i ] = [ ] NEW_LINE DEDENT v [ 1 ] . append ( 2 ) , v [ 2 ] . append ( 1 ) NEW_LINE v [ 1 ] . append ( 3 ) , v [ 3 ] . append ( 1 ) NEW_LINE v [ 1 ] . append ( 4 ) , v [ 4 ] . append ( 1 ) NEW_LINE v [ 2 ] . append ( 5 ) , v [ 5 ] . append ( 2 ) NEW_LINE v [ 2 ] . append ( 6 ) , v [ 6 ] . append ( 2 ) NEW_LINE v [ 3 ] . append ( 7 ) , v [ 7 ] . append ( 3 ) NEW_LINE v [ 4 ] . append ( 8 ) , v [ 8 ] . append ( 4 ) NEW_LINE v [ 4 ] . append ( 9 ) , v [ 9 ] . append ( 4 ) NEW_LINE v [ 4 ] . append ( 10 ) , v [ 10 ] . append ( 4 ) NEW_LINE v [ 5 ] . append ( 11 ) , v [ 11 ] . append ( 5 ) NEW_LINE v [ 5 ] . append ( 12 ) , v [ 12 ] . append ( 5 ) NEW_LINE v [ 7 ] . append ( 13 ) , v [ 13 ] . append ( 7 ) NEW_LINE v [ 7 ] . append ( 14 ) , v [ 14 ] . append ( 7 ) NEW_LINE a = [ 3 , 2 , 1 , 10 , 1 , 3 , 9 , 1 , 5 , 3 , 4 , 5 , 9 , 8 ] NEW_LINE print ( maximumValue ( a , v ) ) NEW_LINE DEDENT main ( ) NEW_LINE
Jacobsthal and Jacobsthal | Return nth Jacobsthal number . ; base case ; Return nth Jacobsthal - Lucas number . ; base case ; Driven Program
def Jacobsthal ( n ) : NEW_LINE INDENT dp = [ 0 ] * ( n + 1 ) NEW_LINE dp [ 0 ] = 0 NEW_LINE dp [ 1 ] = 1 NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT dp [ i ] = dp [ i - 1 ] + 2 * dp [ i - 2 ] NEW_LINE DEDENT return dp [ n ] NEW_LINE DEDENT def Jacobsthal_Lucas ( n ) : NEW_LINE INDENT dp = [ 0 ] * ( n + 1 ) NEW_LINE dp [ 0 ] = 2 NEW_LINE dp [ 1 ] = 1 NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT dp [ i ] = dp [ i - 1 ] + 2 * dp [ i - 2 ] ; NEW_LINE DEDENT return dp [ n ] NEW_LINE DEDENT n = 5 NEW_LINE print ( " Jacobsthal ▁ number : " , Jacobsthal ( n ) ) NEW_LINE print ( " Jacobsthal - Lucas ▁ number : " , Jacobsthal_Lucas ( n ) ) NEW_LINE
Find position of an element in a sorted array of infinite numbers | Binary search algorithm implementation ; function takes an infinite size array and a key to be searched and returns its position if found else - 1. We don 't know size of a[] and we can assume size to be infinite in this function. NOTE THAT THIS FUNCTION ASSUMES a[] TO BE OF INFINITE SIZE THEREFORE, THERE IS NO INDEX OUT OF BOUND CHECKING ; Find h to do binary search ; store previous high ; double high index ; update new val ; at this point we have updated low and high indices , thus use binary search between them ; Driver function
def binary_search ( arr , l , r , x ) : NEW_LINE INDENT if r >= l : NEW_LINE INDENT mid = l + ( r - l ) / 2 NEW_LINE if arr [ mid ] == x : NEW_LINE INDENT return mid NEW_LINE DEDENT if arr [ mid ] > x : NEW_LINE INDENT return binary_search ( arr , l , mid - 1 , x ) NEW_LINE DEDENT return binary_search ( arr , mid + 1 , r , x ) NEW_LINE DEDENT return - 1 NEW_LINE DEDENT def findPos ( a , key ) : NEW_LINE INDENT l , h , val = 0 , 1 , arr [ 0 ] NEW_LINE while val < key : NEW_LINE INDENT l = h NEW_LINE h = 2 * h NEW_LINE val = arr [ h ] NEW_LINE DEDENT return binary_search ( a , l , h , key ) NEW_LINE DEDENT arr = [ 3 , 5 , 7 , 9 , 10 , 90 , 100 , 130 , 140 , 160 , 170 ] NEW_LINE ans = findPos ( arr , 10 ) NEW_LINE if ans == - 1 : NEW_LINE INDENT print " Element ▁ not ▁ found " NEW_LINE DEDENT else : NEW_LINE INDENT print " Element ▁ found ▁ at ▁ index " , ans NEW_LINE DEDENT
Count of possible hexagonal walks | Python 3 implementation of counting number of possible hexagonal walks ; We initialize our origin with 1 ; For each N = 1 to 14 , we traverse in all possible direction . Using this 3D array we calculate the number of ways at each step and the total ways for a given step shall be found at ways [ step number ] [ 8 ] [ 8 ] because all the steps after that will be used to trace back to the original point index 0 : 0 according to the image . ; This array stores the number of ways possible for a given step ; Driver Code ; Preprocessing all possible ways
depth = 16 NEW_LINE ways = [ [ [ 0 for i in range ( 17 ) ] for i in range ( 17 ) ] for i in range ( 17 ) ] NEW_LINE def preprocess ( list , steps ) : NEW_LINE INDENT ways [ 0 ] [ 8 ] [ 8 ] = 1 NEW_LINE for N in range ( 1 , 16 , 1 ) : NEW_LINE INDENT for i in range ( 1 , depth , 1 ) : NEW_LINE INDENT for j in range ( 1 , depth , 1 ) : NEW_LINE INDENT ways [ N ] [ i ] [ j ] = ( ways [ N - 1 ] [ i ] [ j + 1 ] + ways [ N - 1 ] [ i ] [ j - 1 ] + ways [ N - 1 ] [ i + 1 ] [ j ] + ways [ N - 1 ] [ i - 1 ] [ j ] + ways [ N - 1 ] [ i + 1 ] [ j - 1 ] + ways [ N - 1 ] [ i - 1 ] [ j + 1 ] ) NEW_LINE DEDENT DEDENT list [ N ] = ways [ N ] [ 8 ] [ 8 ] NEW_LINE DEDENT print ( " Number ▁ of ▁ walks ▁ possible ▁ is / are " , list [ steps ] ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT list = [ 0 for i in range ( 16 ) ] NEW_LINE steps = 4 NEW_LINE preprocess ( list , steps ) NEW_LINE DEDENT
Check if possible to cross the matrix with given power | Python3 program to find if it is possible to cross the matrix with given power ; For each value of dp [ i ] [ j ] [ k ] ; For first cell and for each value of k ; For first cell of each row ; For first cell of each column ; For rest of the cell ; Down movement . ; Right movement . ; Diagonal movement . ; Finding maximum k . ; Driver Code
N = 105 NEW_LINE R = 3 NEW_LINE C = 4 NEW_LINE def maximumValue ( n , m , p , grid ) : NEW_LINE INDENT dp = [ [ [ False for i in range ( N ) ] for j in range ( N ) ] for k in range ( N ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE INDENT k = grid [ i ] [ j ] NEW_LINE while ( k <= p ) : NEW_LINE INDENT if ( i == 0 and j == 0 ) : NEW_LINE INDENT if ( k == grid [ i ] [ j ] ) : NEW_LINE INDENT dp [ i ] [ j ] [ k ] = True NEW_LINE DEDENT DEDENT elif ( i == 0 ) : NEW_LINE INDENT dp [ i ] [ j ] [ k ] = ( dp [ i ] [ j ] [ k ] or dp [ i ] [ j - 1 ] [ k - grid [ i ] [ j ] ] ) NEW_LINE DEDENT elif ( j == 0 ) : NEW_LINE INDENT dp [ i ] [ j ] [ k ] = ( dp [ i ] [ j ] [ k ] or dp [ i - 1 ] [ j ] [ k - grid [ i ] [ j ] ] ) NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] [ j ] [ k ] = ( dp [ i ] [ j ] [ k ] or dp [ i ] [ j - 1 ] [ k - grid [ i ] [ j ] ] ) NEW_LINE dp [ i ] [ j ] [ k ] = ( dp [ i ] [ j ] [ k ] or dp [ i - 1 ] [ j ] [ k - grid [ i ] [ j ] ] ) NEW_LINE dp [ i ] [ j ] [ k ] = ( dp [ i ] [ j ] [ k ] or dp [ i - 1 ] [ j - 1 ] [ k - grid [ i ] [ j ] ] ) NEW_LINE DEDENT k += 1 NEW_LINE DEDENT DEDENT DEDENT ans = k NEW_LINE while ( ans >= 0 ) : NEW_LINE INDENT if ( dp [ n - 1 ] [ m - 1 ] [ ans ] ) : NEW_LINE INDENT break NEW_LINE DEDENT ans -= 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT n = 3 NEW_LINE m = 4 NEW_LINE p = 9 NEW_LINE grid = [ [ 2 , 3 , 4 , 1 ] , [ 6 , 5 , 5 , 3 ] , [ 5 , 2 , 3 , 4 ] ] NEW_LINE print ( maximumValue ( n , m , p , grid ) ) NEW_LINE
Number of n digit stepping numbers | function that calculates the answer ; dp [ i ] [ j ] stores count of i digit stepping numbers ending with digit j . ; if n is 1 then answer will be 10. ; Initialize values for count of digits equal to 1. ; Compute values for count of digits more than 1. ; If ending digit is 0 ; If ending digit is 9 ; For other digits . ; stores the final answer ; Driver Code
def answer ( n ) : NEW_LINE INDENT dp = [ [ 0 for x in range ( 10 ) ] for y in range ( n + 1 ) ] ; NEW_LINE if ( n == 1 ) : NEW_LINE INDENT return 10 ; NEW_LINE DEDENT for j in range ( 10 ) : NEW_LINE INDENT dp [ 1 ] [ j ] = 1 ; NEW_LINE DEDENT for i in range ( 2 , n + 1 ) : NEW_LINE INDENT for j in range ( 10 ) : NEW_LINE INDENT if ( j == 0 ) : NEW_LINE INDENT dp [ i ] [ j ] = dp [ i - 1 ] [ j + 1 ] ; NEW_LINE DEDENT elif ( j == 9 ) : NEW_LINE INDENT dp [ i ] [ j ] = dp [ i - 1 ] [ j - 1 ] ; NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] [ j ] = ( dp [ i - 1 ] [ j - 1 ] + dp [ i - 1 ] [ j + 1 ] ) ; NEW_LINE DEDENT DEDENT DEDENT sum = 0 ; NEW_LINE for j in range ( 1 , 10 ) : NEW_LINE INDENT sum = sum + dp [ n ] [ j ] ; NEW_LINE DEDENT return sum ; NEW_LINE DEDENT n = 2 ; NEW_LINE print ( answer ( n ) ) ; NEW_LINE
Print Longest Palindromic Subsequence | Returns LCS X and Y ; Following steps build L [ m + 1 ] [ n + 1 ] in bottom up fashion . Note that L [ i ] [ j ] contains length of LCS of X [ 0. . i - 1 ] and Y [ 0. . j - 1 ] ; Following code is used to print LCS ; Create a string length index + 1 and fill it with \ 0 ; Start from the right - most - bottom - most corner and one by one store characters in lcs [ ] ; If current character in X [ ] and Y are same , then current character is part of LCS ; Put current character in result ; reduce values of i , j and index ; If not same , then find the larger of two and go in the direction of larger value ; Returns longest palindromic subsequence of str ; Find reverse of str ; Return LCS of str and its reverse ; Driver Code
def lcs_ ( X , Y ) : NEW_LINE INDENT m = len ( X ) NEW_LINE n = len ( Y ) NEW_LINE L = [ [ 0 ] * ( n + 1 ) ] * ( m + 1 ) NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT for j in range ( n + 1 ) : NEW_LINE INDENT if ( i == 0 or j == 0 ) : NEW_LINE INDENT L [ i ] [ j ] = 0 ; NEW_LINE DEDENT elif ( X [ i - 1 ] == Y [ j - 1 ] ) : NEW_LINE INDENT L [ i ] [ j ] = L [ i - 1 ] [ j - 1 ] + 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT L [ i ] [ j ] = max ( L [ i - 1 ] [ j ] , L [ i ] [ j - 1 ] ) ; NEW_LINE DEDENT DEDENT DEDENT index = L [ m ] [ n ] ; NEW_LINE lcs = [ "   " ] * ( index + 1 ) NEW_LINE i , j = m , n NEW_LINE while ( i > 0 and j > 0 ) : NEW_LINE INDENT if ( X [ i - 1 ] == Y [ j - 1 ] ) : NEW_LINE INDENT lcs [ index - 1 ] = X [ i - 1 ] NEW_LINE i -= 1 NEW_LINE j -= 1 NEW_LINE index -= 1 NEW_LINE DEDENT elif ( L [ i - 1 ] [ j ] > L [ i ] [ j - 1 ] ) : NEW_LINE INDENT i -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT j -= 1 NEW_LINE DEDENT DEDENT ans = " " NEW_LINE for x in range ( len ( lcs ) ) : NEW_LINE INDENT ans += lcs [ x ] NEW_LINE DEDENT return ans NEW_LINE DEDENT def longestPalSubseq ( string ) : NEW_LINE INDENT rev = string [ : : - 1 ] NEW_LINE return lcs_ ( string , rev ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " GEEKSFORGEEKS " ; NEW_LINE print ( longestPalSubseq ( string ) ) NEW_LINE DEDENT
Count all subsequences having product less than K | Function to count numbers of such subsequences having product less than k . ; number of subsequence using j - 1 terms ; if arr [ j - 1 ] > i it will surely make product greater thus it won 't contribute then ; number of subsequence using 1 to j - 1 terms and j - th term ; Driver code
def productSubSeqCount ( arr , k ) : NEW_LINE INDENT n = len ( arr ) NEW_LINE dp = [ [ 0 for i in range ( n + 1 ) ] for j in range ( k + 1 ) ] NEW_LINE for i in range ( 1 , k + 1 ) : NEW_LINE INDENT for j in range ( 1 , n + 1 ) : NEW_LINE INDENT dp [ i ] [ j ] = dp [ i ] [ j - 1 ] NEW_LINE if arr [ j - 1 ] <= i and arr [ j - 1 ] > 0 : NEW_LINE INDENT dp [ i ] [ j ] += dp [ i // arr [ j - 1 ] ] [ j - 1 ] + 1 NEW_LINE DEDENT DEDENT DEDENT return dp [ k ] [ n ] NEW_LINE DEDENT A = [ 1 , 2 , 3 , 4 ] NEW_LINE k = 10 NEW_LINE print ( productSubSeqCount ( A , k ) ) NEW_LINE
Find the element that appears once in an array where every other element appears twice | function to find the once appearing element in array ; Do XOR of all elements and return ; Driver code
def findSingle ( ar , n ) : NEW_LINE INDENT res = ar [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT res = res ^ ar [ i ] NEW_LINE DEDENT return res NEW_LINE DEDENT ar = [ 2 , 3 , 5 , 4 , 5 , 3 , 4 ] NEW_LINE print " Element ▁ occurring ▁ once ▁ is " , findSingle ( ar , len ( ar ) ) NEW_LINE
Count all triplets whose sum is equal to a perfect cube | Python 3 program to calculate all triplets whose sum is perfect cube . ; Function to calculate all occurrence of a number in a given range ; if i == 0 assign 1 to present state ; else add + 1 to current state with previous state ; Function to calculate triplets whose sum is equal to the perfect cube ; Initialize answer ; count all occurrence of third triplet in range from j + 1 to n ; Driver code
dp = [ [ 0 for i in range ( 15001 ) ] for j in range ( 1001 ) ] NEW_LINE def computeDpArray ( arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( 1 , 15001 , 1 ) : NEW_LINE INDENT if ( i == 0 ) : NEW_LINE INDENT dp [ i ] [ j ] = ( j == arr [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] [ j ] = dp [ i - 1 ] [ j ] + ( arr [ i ] == j ) NEW_LINE DEDENT DEDENT DEDENT DEDENT def countTripletSum ( arr , n ) : NEW_LINE INDENT computeDpArray ( arr , n ) NEW_LINE ans = 0 NEW_LINE for i in range ( 0 , n - 2 , 1 ) : NEW_LINE INDENT for j in range ( i + 1 , n - 1 , 1 ) : NEW_LINE INDENT for k in range ( 1 , 25 , 1 ) : NEW_LINE INDENT cube = k * k * k NEW_LINE rem = cube - ( arr [ i ] + arr [ j ] ) NEW_LINE if ( rem > 0 ) : NEW_LINE INDENT ans += dp [ n - 1 ] [ rem ] - dp [ j ] [ rem ] NEW_LINE DEDENT DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 5 , 1 , 20 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE print ( countTripletSum ( arr , n ) ) NEW_LINE DEDENT
Find the element that appears once in an array where every other element appears twice | function which find number ; applying the formula . ; driver code
def singleNumber ( nums ) : NEW_LINE INDENT return 2 * sum ( set ( nums ) ) - sum ( nums ) NEW_LINE DEDENT a = [ 2 , 3 , 5 , 4 , 5 , 3 , 4 ] NEW_LINE print ( int ( singleNumber ( a ) ) ) NEW_LINE a = [ 15 , 18 , 16 , 18 , 16 , 15 , 89 ] NEW_LINE print ( int ( singleNumber ( a ) ) ) NEW_LINE
Maximum Subarray Sum Excluding Certain Elements | Function to check the element present in array B ; Utility function for findMaxSubarraySum ( ) with the following parameters A = > Array A , B = > Array B , n = > Number of elements in Array A , m = > Number of elements in Array B ; set max_so_far to INT_MIN ; if the element is present in B , set current max to 0 and move to the next element ; Proceed as in Kadane 's Algorithm ; Wrapper for findMaxSubarraySumUtil ( ) ; This case will occour when all elements of A are present in B , thus no subarray can be formed ; Driver code ; Function call
INT_MIN = - 2147483648 NEW_LINE def isPresent ( B , m , x ) : NEW_LINE INDENT for i in range ( 0 , m ) : NEW_LINE INDENT if B [ i ] == x : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT def findMaxSubarraySumUtil ( A , B , n , m ) : NEW_LINE INDENT max_so_far = INT_MIN NEW_LINE curr_max = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if isPresent ( B , m , A [ i ] ) == True : NEW_LINE INDENT curr_max = 0 NEW_LINE continue NEW_LINE DEDENT curr_max = max ( A [ i ] , curr_max + A [ i ] ) NEW_LINE max_so_far = max ( max_so_far , curr_max ) NEW_LINE DEDENT return max_so_far NEW_LINE DEDENT def findMaxSubarraySum ( A , B , n , m ) : NEW_LINE INDENT maxSubarraySum = findMaxSubarraySumUtil ( A , B , n , m ) NEW_LINE if maxSubarraySum == INT_MIN : NEW_LINE INDENT print ( ' Maximum ▁ Subarray ▁ Sum ▁ cant ▁ be ▁ found ' ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ' The ▁ Maximum ▁ Subarray ▁ Sum ▁ = ' , maxSubarraySum ) NEW_LINE DEDENT DEDENT A = [ 3 , 4 , 5 , - 4 , 6 ] NEW_LINE B = [ 1 , 8 , 5 ] NEW_LINE n = len ( A ) NEW_LINE m = len ( B ) NEW_LINE findMaxSubarraySum ( A , B , n , m ) NEW_LINE
Number of n | Python3 program for counting n digit numbers with non decreasing digits ; Returns count of non - decreasing numbers with n digits . ; a [ i ] [ j ] = count of all possible number with i digits having leading digit as j ; Initialization of all 0 - digit number ; Initialization of all i - digit non - decreasing number leading with 9 ; for all digits we should calculate number of ways depending upon leading digits ; Driver Code
import numpy as np NEW_LINE def nonDecNums ( n ) : NEW_LINE INDENT a = np . zeros ( ( n + 1 , 10 ) ) NEW_LINE for i in range ( 10 ) : NEW_LINE INDENT a [ 0 ] [ i ] = 1 NEW_LINE DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT a [ i ] [ 9 ] = 1 NEW_LINE DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( 8 , - 1 , - 1 ) : NEW_LINE INDENT a [ i ] [ j ] = a [ i - 1 ] [ j ] + a [ i ] [ j + 1 ] NEW_LINE DEDENT DEDENT return int ( a [ n ] [ 0 ] ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 2 NEW_LINE print ( " Non - decreasing ▁ digits ▁ = ▁ " , nonDecNums ( n ) ) NEW_LINE DEDENT
Count Balanced Binary Trees of Height h | Python3 program to count number of balanced binary trees of height h . ; base cases ; Driver program
def countBT ( h ) : NEW_LINE INDENT MOD = 1000000007 NEW_LINE dp = [ 0 for i in range ( h + 1 ) ] NEW_LINE dp [ 0 ] = 1 NEW_LINE dp [ 1 ] = 1 NEW_LINE for i in range ( 2 , h + 1 ) : NEW_LINE INDENT dp [ i ] = ( dp [ i - 1 ] * ( ( 2 * dp [ i - 2 ] ) % MOD + dp [ i - 1 ] ) % MOD ) % MOD NEW_LINE DEDENT return dp [ h ] NEW_LINE DEDENT h = 3 NEW_LINE print ( " No . ▁ of ▁ balanced ▁ binary ▁ trees ▁ of ▁ height ▁ " + str ( h ) + " ▁ is : ▁ " + str ( countBT ( h ) ) ) NEW_LINE