codeparrot/xlcost-text-to-code huggingface dataset:
Largest sum Zigzag sequence in a matrix | Memoization based Python3 program to find the largest sum zigzag sequence ; Returns largest sum of a Zigzag sequence starting from ( i , j ) and ending at a bottom cell . ; If we have reached bottom ; Find the largest sum by considering all possible next elements in sequence . ; Returns largest possible sum of a Zizag sequence starting from top and ending at bottom . ; Consider all cells of top row as starting point ; Driver code
MAX = 100 ; NEW_LINE dp = [ [ 0 for i in range ( MAX ) ] for j in range ( MAX ) ] NEW_LINE def largestZigZagSumRec ( mat , i , j , n ) : NEW_LINE INDENT if ( dp [ i ] [ j ] != - 1 ) : NEW_LINE INDENT return dp [ i ] [ j ] ; NEW_LINE DEDENT if ( i == n - 1 ) : NEW_LINE INDENT dp [ i ] [ j ] = mat [ i ] [ j ] ; NEW_LINE return ( dp [ i ] [ j ] ) ; NEW_LINE DEDENT zzs = 0 ; NEW_LINE for k in range ( n ) : NEW_LINE INDENT if ( k != j ) : NEW_LINE INDENT zzs = max ( zzs , largestZigZagSumRec ( mat , i + 1 , k , n ) ) ; NEW_LINE DEDENT DEDENT dp [ i ] [ j ] = ( zzs + mat [ i ] [ j ] ) ; NEW_LINE return ( dp [ i ] [ j ] ) ; NEW_LINE DEDENT def largestZigZag ( mat , n ) : NEW_LINE INDENT for i in range ( MAX ) : NEW_LINE INDENT for k in range ( MAX ) : NEW_LINE INDENT dp [ i ] [ k ] = - 1 ; NEW_LINE DEDENT DEDENT res = 0 ; NEW_LINE for j in range ( n ) : NEW_LINE INDENT res = max ( res , largestZigZagSumRec ( mat , 0 , j , n ) ) ; NEW_LINE DEDENT return res ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 3 ; NEW_LINE mat = [ [ 4 , 2 , 1 ] , [ 3 , 9 , 6 ] , [ 11 , 3 , 15 ] ] ; NEW_LINE print ( " Largest ▁ zigzag ▁ sum : ▁ " , largestZigZag ( mat , n ) ) ; NEW_LINE DEDENT
Number of subsequences of the form a ^ i b ^ j c ^ k | Returns count of subsequences of the form a ^ i b ^ j c ^ k ; Initialize counts of different subsequences caused by different combination of 'a ; Initialize counts of different subsequences caused by different combination of ' a ' and different combination of 'b ; Initialize counts of different subsequences caused by different combination of ' a ' , ' b ' and ' c ' . ; Traverse all characters of given string ; If current character is ' a ' , then there are following possibilities : a ) Current character begins a new subsequence . b ) Current character is part of aCount subsequences . c ) Current character is not part of aCount subsequences . ; If current character is ' b ' , then there are following possibilities : a ) Current character begins a new subsequence of b 's with aCount subsequences. b) Current character is part of bCount subsequences. c) Current character is not  part of bCount subsequences. ; If current character is ' c ' , then there are following possibilities : a ) Current character begins a new subsequence of c 's with bCount subsequences. b) Current character is part of cCount subsequences. c) Current character is not  part of cCount subsequences. ; Driver code
def countSubsequences ( s ) : NEW_LINE ' NEW_LINE INDENT aCount = 0 NEW_LINE DEDENT ' NEW_LINE INDENT bCount = 0 NEW_LINE cCount = 0 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT if ( s [ i ] == ' a ' ) : NEW_LINE INDENT aCount = ( 1 + 2 * aCount ) NEW_LINE DEDENT elif ( s [ i ] == ' b ' ) : NEW_LINE INDENT bCount = ( aCount + 2 * bCount ) NEW_LINE DEDENT elif ( s [ i ] == ' c ' ) : NEW_LINE INDENT cCount = ( bCount + 2 * cCount ) NEW_LINE DEDENT DEDENT return cCount NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " abbc " NEW_LINE print ( countSubsequences ( s ) ) NEW_LINE DEDENT
Highway Billboard Problem | Python3 program to find maximum revenue by placing billboard on the highway with given constraints . ; Array to store maximum revenue at each miles . ; actual minimum distance between 2 billboards . ; check if all billboards are already placed . ; check if we have billboard for that particular mile . If not , copy the previous maximum revenue . ; we do have billboard for this mile . ; If current position is less than or equal to t , then we can have only one billboard . ; Else we may have to remove previously placed billboard ; Driver Code
def maxRevenue ( m , x , revenue , n , t ) : NEW_LINE INDENT maxRev = [ 0 ] * ( m + 1 ) NEW_LINE nxtbb = 0 ; NEW_LINE for i in range ( 1 , m + 1 ) : NEW_LINE INDENT if ( nxtbb < n ) : NEW_LINE INDENT if ( x [ nxtbb ] != i ) : NEW_LINE INDENT maxRev [ i ] = maxRev [ i - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT if ( i <= t ) : NEW_LINE INDENT maxRev [ i ] = max ( maxRev [ i - 1 ] , revenue [ nxtbb ] ) NEW_LINE DEDENT else : NEW_LINE INDENT maxRev [ i ] = max ( maxRev [ i - t - 1 ] + revenue [ nxtbb ] , maxRev [ i - 1 ] ) ; NEW_LINE DEDENT nxtbb += 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT maxRev [ i ] = maxRev [ i - 1 ] NEW_LINE DEDENT DEDENT return maxRev [ m ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT m = 20 NEW_LINE x = [ 6 , 7 , 12 , 13 , 14 ] NEW_LINE revenue = [ 5 , 6 , 5 , 3 , 1 ] NEW_LINE n = len ( x ) NEW_LINE t = 5 NEW_LINE print ( maxRevenue ( m , x , revenue , n , t ) ) NEW_LINE DEDENT
Finding the maximum square sub | Python 3 program to find maximum K such that K x K is a submatrix with equal elements . ; Returns size of the largest square sub - matrix with all same elements . ; If elements is at top row or first column , it wont form a square matrix 's bottom-right ; Check if adjacent elements are equal ; If not equal , then it will form a 1 x1 submatrix ; Update result at each ( i , j ) ; Driver Code
Row = 6 NEW_LINE Col = 6 NEW_LINE def largestKSubmatrix ( a ) : NEW_LINE INDENT dp = [ [ 0 for x in range ( Row ) ] for y in range ( Col ) ] NEW_LINE result = 0 NEW_LINE for i in range ( Row ) : NEW_LINE INDENT for j in range ( Col ) : NEW_LINE INDENT if ( i == 0 or j == 0 ) : NEW_LINE INDENT dp [ i ] [ j ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT if ( a [ i ] [ j ] == a [ i - 1 ] [ j ] and a [ i ] [ j ] == a [ i ] [ j - 1 ] and a [ i ] [ j ] == a [ i - 1 ] [ j - 1 ] ) : NEW_LINE INDENT dp [ i ] [ j ] = min ( min ( dp [ i - 1 ] [ j ] , dp [ i ] [ j - 1 ] ) , dp [ i - 1 ] [ j - 1 ] ) + 1 NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] [ j ] = 1 NEW_LINE DEDENT DEDENT result = max ( result , dp [ i ] [ j ] ) NEW_LINE DEDENT DEDENT return result NEW_LINE DEDENT a = [ [ 2 , 2 , 3 , 3 , 4 , 4 ] , [ 5 , 5 , 7 , 7 , 7 , 4 ] , [ 1 , 2 , 7 , 7 , 7 , 4 ] , [ 4 , 4 , 7 , 7 , 7 , 4 ] , [ 5 , 5 , 5 , 1 , 2 , 7 ] , [ 8 , 7 , 9 , 4 , 4 , 4 ] ] ; NEW_LINE print ( largestKSubmatrix ( a ) ) NEW_LINE
Number of subsequences in a string divisible by n | Returns count of subsequences of str divisible by n . ; division by n can leave only n remainder [ 0. . n - 1 ] . dp [ i ] [ j ] indicates number of subsequences in string [ 0. . i ] which leaves remainder j after division by n . ; Filling value for first digit in str ; start a new subsequence with index i ; exclude i 'th character from all the  current subsequences of string [0...i-1] ; include i 'th character in all the current  subsequences of string [0...i-1] ; Driver code
def countDivisibleSubseq ( str , n ) : NEW_LINE INDENT l = len ( str ) NEW_LINE dp = [ [ 0 for x in range ( l ) ] for y in range ( n ) ] NEW_LINE dp [ 0 ] [ ( ord ( str [ 0 ] ) - ord ( '0' ) ) % n ] += 1 NEW_LINE for i in range ( 1 , l ) : NEW_LINE INDENT dp [ i ] [ ( ord ( str [ i ] ) - ord ( '0' ) ) % n ] += 1 NEW_LINE for j in range ( n ) : NEW_LINE INDENT dp [ i ] [ j ] += dp [ i - 1 ] [ j ] NEW_LINE dp [ i ] [ ( j * 10 + ( ord ( str [ i ] ) - ord ( '0' ) ) ) % n ] += dp [ i - 1 ] [ j ] NEW_LINE DEDENT DEDENT return dp [ l - 1 ] [ 0 ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str = "1234" NEW_LINE n = 4 NEW_LINE print ( countDivisibleSubseq ( str , n ) ) NEW_LINE DEDENT
Size of array after repeated deletion of LIS | Function to construct Maximum Sum LIS ; L [ i ] - The Maximum Sum Increasing Subsequence that ends with arr [ i ] ; L [ 0 ] is equal to arr [ 0 ] ; start from index 1 ; for every j less than i ; L [ i ] = MaxSum ( L [ j ] ) + arr [ i ] where j < i and arr [ j ] < arr [ i ] ; L [ i ] ends with arr [ i ] ; set lis = LIS whose size is max among all ; The > sign makes sure that the LIS ending first is chose . ; Function to minimize array ; Find LIS of current array ; If all elements are in decreasing order ; Remove lis elements from current array . Note that both lis [ ] and arr [ ] are sorted in increasing order . ; If first element of lis [ ] is found ; Remove lis element from arr [ ] ; Erase first element of lis [ ] ; print remaining element of array ; print - 1 for empty array ; Driver function ; minimize array after deleting LIS
from typing import List NEW_LINE def findLIS ( arr : List [ int ] , n : int ) -> List [ int ] : NEW_LINE INDENT L = [ 0 ] * n NEW_LINE for i in range ( n ) : NEW_LINE INDENT L [ i ] = [ ] NEW_LINE DEDENT L [ 0 ] . append ( arr [ 0 ] ) NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT for j in range ( i ) : NEW_LINE INDENT if ( arr [ i ] > arr [ j ] and ( len ( L [ i ] ) < len ( L [ j ] ) ) ) : NEW_LINE INDENT L [ i ] = L [ j ] . copy ( ) NEW_LINE DEDENT DEDENT L [ i ] . append ( arr [ i ] ) NEW_LINE DEDENT maxSize = 1 NEW_LINE lis : List [ int ] = [ ] NEW_LINE for x in L : NEW_LINE INDENT if ( len ( x ) > maxSize ) : NEW_LINE INDENT lis = x . copy ( ) NEW_LINE maxSize = len ( x ) NEW_LINE DEDENT DEDENT return lis NEW_LINE DEDENT def minimize ( input : List [ int ] , n : int ) -> None : NEW_LINE INDENT arr = input . copy ( ) NEW_LINE while len ( arr ) : NEW_LINE INDENT lis = findLIS ( arr , len ( arr ) ) NEW_LINE if ( len ( lis ) < 2 ) : NEW_LINE INDENT break NEW_LINE DEDENT i = 0 NEW_LINE while i < len ( arr ) and len ( lis ) > 0 : NEW_LINE INDENT if ( arr [ i ] == lis [ 0 ] ) : NEW_LINE INDENT arr . remove ( arr [ i ] ) NEW_LINE i -= 1 NEW_LINE lis . remove ( lis [ 0 ] ) NEW_LINE DEDENT i += 1 NEW_LINE DEDENT DEDENT i = 0 NEW_LINE while i < len ( arr ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE i += 1 NEW_LINE DEDENT if ( i == 0 ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT input = [ 3 , 2 , 6 , 4 , 5 , 1 ] NEW_LINE n = len ( input ) NEW_LINE minimize ( input , n ) NEW_LINE DEDENT
Probability of getting at least K heads in N tosses of Coins | Naive approach in Python3 to find probability of at least k heads ; Returns probability of getting at least k heads in n tosses . ; Probability of getting exactly i heads out of n heads ; Note : 1 << n = pow ( 2 , n ) ; Preprocess all factorial only upto 19 , as after that it will overflow ; Driver code ; Probability of getting 2 head out of 3 coins ; Probability of getting 3 head out of 6 coins ; Probability of getting 12 head out of 18 coins
MAX = 21 NEW_LINE fact = [ 0 ] * MAX NEW_LINE def probability ( k , n ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( k , n + 1 ) : NEW_LINE INDENT ans += fact [ n ] / ( fact [ i ] * fact [ n - i ] ) NEW_LINE DEDENT ans = ans / ( 1 << n ) NEW_LINE return ans NEW_LINE DEDENT def precompute ( ) : NEW_LINE INDENT fact [ 0 ] = 1 NEW_LINE fact [ 1 ] = 1 NEW_LINE for i in range ( 2 , 20 ) : NEW_LINE INDENT fact [ i ] = fact [ i - 1 ] * i NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT precompute ( ) NEW_LINE print ( probability ( 2 , 3 ) ) NEW_LINE print ( probability ( 3 , 6 ) ) NEW_LINE print ( probability ( 12 , 18 ) ) NEW_LINE DEDENT
Count binary strings with k times appearing adjacent two set bits | Python3 program to count number of binary strings with k times appearing consecutive 1 's. ; dp [ i ] [ j ] [ 0 ] stores count of binary strings of length i with j consecutive 1 ' s ▁ and ▁ ending ▁ at ▁ 0 . ▁ ▁ dp [ i ] [ j ] [1 ] ▁ stores ▁ count ▁ of ▁ binary ▁ ▁ strings ▁ of ▁ length ▁ i ▁ with ▁ j ▁ consecutive ▁ ▁ 1' s and ending at 1. ; If n = 1 and k = 0. ; number of adjacent 1 's can not exceed i-1 ; Driver Code
def countStrings ( n , k ) : NEW_LINE INDENT dp = [ [ [ 0 , 0 ] for __ in range ( k + 1 ) ] for _ in range ( n + 1 ) ] NEW_LINE dp [ 1 ] [ 0 ] [ 0 ] = 1 NEW_LINE dp [ 1 ] [ 0 ] [ 1 ] = 1 NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT for j in range ( k + 1 ) : NEW_LINE INDENT dp [ i ] [ j ] [ 0 ] = ( dp [ i - 1 ] [ j ] [ 0 ] + dp [ i - 1 ] [ j ] [ 1 ] ) NEW_LINE dp [ i ] [ j ] [ 1 ] = dp [ i - 1 ] [ j ] [ 0 ] NEW_LINE if j >= 1 : NEW_LINE INDENT dp [ i ] [ j ] [ 1 ] += dp [ i - 1 ] [ j - 1 ] [ 1 ] NEW_LINE DEDENT DEDENT DEDENT return dp [ n ] [ k ] [ 0 ] + dp [ n ] [ k ] [ 1 ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 NEW_LINE k = 2 NEW_LINE print ( countStrings ( n , k ) ) NEW_LINE DEDENT
Check if all people can vote on two machines | Returns true if n people can vote using two machines in x time . ; calculate total sum i . e total time taken by all people ; if total time is less than x then all people can definitely vote hence return true ; sort the list ; declare a list presum of same size as that of a and initialize it with 0 ; prefixsum for first element will be element itself ; fill the array ; Set i and j and check if array from i to j - 1 gives sum <= x ; Driver code
def canVote ( a , n , x ) : NEW_LINE INDENT total_sum = 0 NEW_LINE for i in range ( len ( a ) ) : NEW_LINE INDENT total_sum += a [ i ] NEW_LINE DEDENT if ( total_sum <= x ) : NEW_LINE INDENT return True NEW_LINE DEDENT a . sort ( ) NEW_LINE presum = [ 0 for i in range ( len ( a ) ) ] NEW_LINE presum [ 0 ] = a [ 0 ] NEW_LINE for i in range ( 1 , len ( presum ) ) : NEW_LINE INDENT presum [ i ] = presum [ i - 1 ] + a [ i ] NEW_LINE DEDENT for i in range ( 0 , len ( presum ) ) : NEW_LINE INDENT for j in range ( i + 1 , len ( presum ) ) : NEW_LINE INDENT arr1_sum = ( presum [ i ] + ( total_sum - presum [ j ] ) ) NEW_LINE if ( ( arr1_sum <= x ) and ( total_sum - arr1_sum ) <= x ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT DEDENT return False NEW_LINE DEDENT n = 3 NEW_LINE x = 4 NEW_LINE a = [ 2 , 4 , 2 ] NEW_LINE if ( canVote ( a , n , x ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT
Friends Pairing Problem | Python3 program for solution of friends pairing problem Using Recursion ; Returns count of ways n people can remain single or paired up . ; Driver Code
dp = [ - 1 ] * 1000 NEW_LINE def countFriendsPairings ( n ) : NEW_LINE INDENT global dp NEW_LINE if ( dp [ n ] != - 1 ) : NEW_LINE INDENT return dp [ n ] NEW_LINE DEDENT if ( n > 2 ) : NEW_LINE INDENT dp [ n ] = ( countFriendsPairings ( n - 1 ) + ( n - 1 ) * countFriendsPairings ( n - 2 ) ) NEW_LINE return dp [ n ] NEW_LINE DEDENT else : NEW_LINE INDENT dp [ n ] = n NEW_LINE return dp [ n ] NEW_LINE DEDENT DEDENT n = 4 NEW_LINE print ( countFriendsPairings ( n ) ) NEW_LINE
Friends Pairing Problem | Python3 soln using mathematical approach factorial array is stored dynamically ; Returns count of ways n people can remain single or paired up . ; pow of 1 will always be one ; Driver Code pre - compute factorial
fact = [ 1 ] NEW_LINE def preComputeFact ( n ) : NEW_LINE INDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT fact . append ( ( fact [ i - 1 ] * i ) ) NEW_LINE DEDENT DEDENT def countFriendsPairings ( n ) : NEW_LINE INDENT ones = n NEW_LINE twos = 1 NEW_LINE ans = 0 NEW_LINE while ( ones >= 0 ) : NEW_LINE INDENT ans = ans + ( fact [ n ] // ( twos * fact [ ones ] * fact [ ( n - ones ) // 2 ] ) ) NEW_LINE ones = ones - 2 NEW_LINE twos = twos * 2 NEW_LINE DEDENT return ( ans ) NEW_LINE DEDENT preComputeFact ( 1000 ) NEW_LINE n = 4 NEW_LINE print ( countFriendsPairings ( n ) ) NEW_LINE
Maximum path sum in a triangle . | Python program for Dynamic Programming implementation of Max sum problem in a triangle ; Function for finding maximum sum ; loop for bottom - up calculation ; for each element , check both elements just below the number and below right to the number add the maximum of them to it ; return the top element which stores the maximum sum ; Driver program to test above function
N = 3 NEW_LINE def maxPathSum ( tri , m , n ) : NEW_LINE INDENT for i in range ( m - 1 , - 1 , - 1 ) : NEW_LINE INDENT for j in range ( i + 1 ) : NEW_LINE INDENT if ( tri [ i + 1 ] [ j ] > tri [ i + 1 ] [ j + 1 ] ) : NEW_LINE INDENT tri [ i ] [ j ] += tri [ i + 1 ] [ j ] NEW_LINE DEDENT else : NEW_LINE INDENT tri [ i ] [ j ] += tri [ i + 1 ] [ j + 1 ] NEW_LINE DEDENT DEDENT DEDENT return tri [ 0 ] [ 0 ] NEW_LINE DEDENT tri = [ [ 1 , 0 , 0 ] , [ 4 , 8 , 0 ] , [ 1 , 5 , 3 ] ] NEW_LINE print ( maxPathSum ( tri , 2 , 2 ) ) NEW_LINE
Find Maximum dot product of two arrays with insertion of 0 's | Function compute Maximum Dot Product and return it ; Create 2D Matrix that stores dot product dp [ i + 1 ] [ j + 1 ] stores product considering B [ 0. . i ] and A [ 0. . . j ] . Note that since all m > n , we fill values in upper diagonal of dp [ ] [ ] ; Traverse through all elements of B [ ] ; Consider all values of A [ ] with indexes greater than or equal to i and compute dp [ i ] [ j ] ; Two cases arise 1 ) Include A [ j ] 2 ) Exclude A [ j ] ( insert 0 in B [ ] ) ; return Maximum Dot Product ; Driver Code
def MaxDotProduct ( A , B , m , n ) : NEW_LINE INDENT dp = [ [ 0 for i in range ( m + 1 ) ] for j in range ( n + 1 ) ] NEW_LINE for i in range ( 1 , n + 1 , 1 ) : NEW_LINE INDENT for j in range ( i , m + 1 , 1 ) : NEW_LINE INDENT dp [ i ] [ j ] = max ( ( dp [ i - 1 ] [ j - 1 ] + ( A [ j - 1 ] * B [ i - 1 ] ) ) , dp [ i ] [ j - 1 ] ) NEW_LINE DEDENT DEDENT return dp [ n ] [ m ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 2 , 3 , 1 , 7 , 8 ] NEW_LINE B = [ 3 , 6 , 7 ] NEW_LINE m = len ( A ) NEW_LINE n = len ( B ) NEW_LINE print ( MaxDotProduct ( A , B , m , n ) ) NEW_LINE DEDENT
LCS ( Longest Common Subsequence ) of three strings | Python3 program to find LCS of three strings ; Returns length of LCS for X [ 0. . m - 1 ] , Y [ 0. . n - 1 ] and Z [ 0. . o - 1 ] ; Driver code
X = " AGGT12" NEW_LINE Y = "12TXAYB " NEW_LINE Z = "12XBA " NEW_LINE dp = [ [ [ - 1 for i in range ( 100 ) ] for j in range ( 100 ) ] for k in range ( 100 ) ] NEW_LINE def lcsOf3 ( i , j , k ) : NEW_LINE INDENT if ( i == - 1 or j == - 1 or k == - 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( dp [ i ] [ j ] [ k ] != - 1 ) : NEW_LINE INDENT return dp [ i ] [ j ] [ k ] NEW_LINE DEDENT if ( X [ i ] == Y [ j ] and Y [ j ] == Z [ k ] ) : NEW_LINE INDENT dp [ i ] [ j ] [ k ] = 1 + lcsOf3 ( i - 1 , j - 1 , k - 1 ) NEW_LINE return dp [ i ] [ j ] [ k ] NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] [ j ] [ k ] = max ( max ( lcsOf3 ( i - 1 , j , k ) , lcsOf3 ( i , j - 1 , k ) ) , lcsOf3 ( i , j , k - 1 ) ) NEW_LINE return dp [ i ] [ j ] [ k ] NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT m = len ( X ) NEW_LINE n = len ( Y ) NEW_LINE o = len ( Z ) NEW_LINE print ( " Length ▁ of ▁ LCS ▁ is " , lcsOf3 ( m - 1 , n - 1 , o - 1 ) ) NEW_LINE DEDENT
Minimum number of elements which are not part of Increasing or decreasing subsequence in array | Python3 program to return minimum number of elements which are not part of increasing or decreasing subsequences . ; Return minimum number of elements which is not part of any of the sequence . ; If already calculated , return value . ; If whole array is traversed . ; calculating by considering element as part of decreasing sequence . ; calculating by considering element as part of increasing sequence . ; If cannot be calculated for decreasing sequence . ; After considering once by decreasing sequence , now try for increasing sequence . ; If element cannot be part of any of the sequence . ; After considering element as part of increasing and decreasing sequence trying as not part of any of the sequence . ; Wrapper Function ; Adding two number at the end of array , so that increasing and decreasing sequence can be made . MAX - 2 index is assigned INT_MAX for decreasing sequence because / next number of sequence must be less than it . Similarly , for Increasing sequence INT_MIN is assigned to MAX - 1 index . ; Driver code
MAX = 102 NEW_LINE def countMin ( arr , dp , n , dec , inc , i ) : NEW_LINE INDENT if dp [ dec ] [ inc ] [ i ] != - 1 : NEW_LINE INDENT return dp [ dec ] [ inc ] [ i ] NEW_LINE DEDENT if i == n : NEW_LINE INDENT return 0 NEW_LINE DEDENT if arr [ i ] < arr [ dec ] : NEW_LINE INDENT dp [ dec ] [ inc ] [ i ] = countMin ( arr , dp , n , i , inc , i + 1 ) NEW_LINE DEDENT if arr [ i ] > arr [ inc ] : NEW_LINE INDENT if dp [ dec ] [ inc ] [ i ] == - 1 : NEW_LINE INDENT dp [ dec ] [ inc ] [ i ] = countMin ( arr , dp , n , dec , i , i + 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT dp [ dec ] [ inc ] [ i ] = min ( countMin ( arr , dp , n , dec , i , i + 1 ) , dp [ dec ] [ inc ] [ i ] ) NEW_LINE DEDENT DEDENT if dp [ dec ] [ inc ] [ i ] == - 1 : NEW_LINE INDENT dp [ dec ] [ inc ] [ i ] = 1 + countMin ( arr , dp , n , dec , inc , i + 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT dp [ dec ] [ inc ] [ i ] = min ( 1 + countMin ( arr , dp , n , dec , inc , i + 1 ) , dp [ dec ] [ inc ] [ i ] ) NEW_LINE DEDENT return dp [ dec ] [ inc ] [ i ] NEW_LINE DEDENT def wrapper ( arr , n ) : NEW_LINE INDENT arr [ MAX - 2 ] = 1000000000 NEW_LINE arr [ MAX - 1 ] = - 1000000000 NEW_LINE dp = [ [ [ - 1 for i in range ( MAX ) ] for i in range ( MAX ) ] for i in range ( MAX ) ] NEW_LINE return countMin ( arr , dp , n , MAX - 2 , MAX - 1 , 0 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 12 NEW_LINE arr = [ 7 , 8 , 1 , 2 , 4 , 6 , 3 , 5 , 2 , 1 , 8 , 7 ] NEW_LINE for i in range ( MAX ) : NEW_LINE INDENT arr . append ( 0 ) NEW_LINE DEDENT print ( wrapper ( arr , n ) ) NEW_LINE DEDENT
Find all distinct subset ( or subsequence ) sums of an array | sum denotes the current sum of the subset currindex denotes the index we have reached in the given array ; This function mainly calls recursive function distSumRec ( ) to generate distinct sum subsets . And finally prints the generated subsets . ; Print the result ; Driver code
def distSumRec ( arr , n , sum , currindex , s ) : NEW_LINE INDENT if ( currindex > n ) : NEW_LINE INDENT return NEW_LINE DEDENT if ( currindex == n ) : NEW_LINE INDENT s . add ( sum ) NEW_LINE return NEW_LINE DEDENT distSumRec ( arr , n , sum + arr [ currindex ] , currindex + 1 , s ) NEW_LINE distSumRec ( arr , n , sum , currindex + 1 , s ) NEW_LINE DEDENT def printDistSum ( arr , n ) : NEW_LINE INDENT s = set ( ) NEW_LINE distSumRec ( arr , n , 0 , 0 , s ) NEW_LINE for i in s : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 3 , 4 , 5 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE printDistSum ( arr , n ) NEW_LINE DEDENT
Find all distinct subset ( or subsequence ) sums of an array | Uses Dynamic Programming to find distinct subset Sums ; dp [ i ] [ j ] would be true if arr [ 0. . i - 1 ] has a subset with Sum equal to j . ; There is always a subset with 0 Sum ; Fill dp [ ] [ ] in bottom up manner ; Sums that were achievable without current array element ; Print last row elements ; Driver code
def printDistSum ( arr , n ) : NEW_LINE INDENT Sum = sum ( arr ) NEW_LINE dp = [ [ False for i in range ( Sum + 1 ) ] for i in range ( n + 1 ) ] NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT dp [ i ] [ 0 ] = True NEW_LINE DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT dp [ i ] [ arr [ i - 1 ] ] = True NEW_LINE for j in range ( 1 , Sum + 1 ) : NEW_LINE INDENT if ( dp [ i - 1 ] [ j ] == True ) : NEW_LINE INDENT dp [ i ] [ j ] = True NEW_LINE dp [ i ] [ j + arr [ i - 1 ] ] = True NEW_LINE DEDENT DEDENT DEDENT for j in range ( Sum + 1 ) : NEW_LINE INDENT if ( dp [ n ] [ j ] == True ) : NEW_LINE INDENT print ( j , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT arr = [ 2 , 3 , 4 , 5 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE printDistSum ( arr , n ) NEW_LINE