codeparrot/xlcost-text-to-code huggingface dataset:
Maximum and Minimum Values of an Algebraic Expression | Python3 program to find the maximum and minimum values of an Algebraic expression of given form ; Finding sum of array elements ; shifting the integers by 50 so that they become positive ; dp [ i ] [ j ] represents true if sum j can be reachable by choosing i numbers ; if dp [ i ] [ j ] is true , that means it is possible to select i numbers from ( n + m ) numbers to sum upto j ; k can be at max n because the left expression has n numbers ; checking if a particular sum can be reachable by choosing n numbers ; getting the actual sum as we shifted the numbers by 50 to avoid negative indexing in array ; Driver Code
def minMaxValues ( arr , n , m ) : NEW_LINE INDENT sum = 0 NEW_LINE INF = 1000000000 NEW_LINE MAX = 50 NEW_LINE for i in range ( 0 , ( n + m ) ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE arr [ i ] += 50 NEW_LINE DEDENT dp = [ [ 0 for x in range ( MAX * MAX + 1 ) ] for y in range ( MAX + 1 ) ] NEW_LINE dp [ 0 ] [ 0 ] = 1 NEW_LINE for i in range ( 0 , ( n + m ) ) : NEW_LINE INDENT for k in range ( min ( n , i + 1 ) , 0 , - 1 ) : NEW_LINE INDENT for j in range ( 0 , MAX * MAX + 1 ) : NEW_LINE INDENT if ( dp [ k - 1 ] [ j ] ) : NEW_LINE INDENT dp [ k ] [ j + arr [ i ] ] = 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT max_value = - 1 * INF NEW_LINE min_value = INF NEW_LINE for i in range ( 0 , MAX * MAX + 1 ) : NEW_LINE INDENT if ( dp [ n ] [ i ] ) : NEW_LINE INDENT temp = i - 50 * n NEW_LINE max_value = max ( max_value , temp * ( sum - temp ) ) NEW_LINE min_value = min ( min_value , temp * ( sum - temp ) ) NEW_LINE DEDENT DEDENT print ( " Maximum Value : { } Minimum Value : { } " . format ( max_value , min_value ) ) NEW_LINE DEDENT n = 2 NEW_LINE m = 2 NEW_LINE arr = [ 1 , 2 , 3 , 4 ] NEW_LINE minMaxValues ( arr , n , m ) NEW_LINE
Check if any valid sequence is divisible by M | Python3 program to check if any valid sequence is divisible by M ; 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
def isPossible ( n , index , Sum , M , arr , dp ) : NEW_LINE INDENT global MAX NEW_LINE if index == n : NEW_LINE INDENT if ( Sum % M ) == 0 : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT if dp [ index ] [ Sum ] != - 1 : NEW_LINE INDENT return dp [ index ] [ Sum ] NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT placeAdd = isPossible ( n , index + 1 , Sum + arr [ index ] , M , arr , dp ) NEW_LINE DEDENT ' NEW_LINE INDENT placeMinus = isPossible ( n , index + 1 , Sum - arr [ index ] , M , arr , dp ) NEW_LINE res = placeAdd or placeMinus NEW_LINE dp [ index ] [ Sum ] = res NEW_LINE return res NEW_LINE DEDENT MAX = 1000 NEW_LINE arr = [ 1 , 2 , 3 , 4 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE M = 4 NEW_LINE dp = [ [ - 1 ] * MAX for i in range ( n + 1 ) ] NEW_LINE res = isPossible ( n , 0 , 0 , M , arr , dp ) NEW_LINE if res : NEW_LINE INDENT print ( True ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( False ) NEW_LINE DEDENT
Golomb sequence | Return the nth element of Golomb sequence ; base case ; Recursive Step ; Print the first n term of Golomb Sequence ; Finding first n terms of Golomb Sequence . ; Driver Code
def findGolomb ( n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT return 1 + findGolomb ( n - findGolomb ( findGolomb ( n - 1 ) ) ) NEW_LINE DEDENT def printGolomb ( n ) : NEW_LINE INDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT print ( findGolomb ( i ) , end = " ▁ " ) NEW_LINE DEDENT DEDENT n = 9 NEW_LINE printGolomb ( n ) NEW_LINE
Printing Items in 0 / 1 Knapsack | Prints the items which are put in a knapsack of capacity W ; Build table K [ ] [ ] in bottom up manner ; stores the result of Knapsack ; either the result comes from the top ( K [ i - 1 ] [ w ] ) or from ( val [ i - 1 ] + K [ i - 1 ] [ w - wt [ i - 1 ] ] ) as in Knapsack table . If it comes from the latter one / it means the item is included . ; This item is included . ; Since this weight is included its value is deducted ; Driver code
def printknapSack ( W , wt , val , n ) : NEW_LINE INDENT K = [ [ 0 for w in range ( W + 1 ) ] for i in range ( n + 1 ) ] NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT for w in range ( W + 1 ) : NEW_LINE INDENT if i == 0 or w == 0 : NEW_LINE INDENT K [ i ] [ w ] = 0 NEW_LINE DEDENT elif wt [ i - 1 ] <= w : NEW_LINE INDENT K [ i ] [ w ] = max ( val [ i - 1 ] + K [ i - 1 ] [ w - wt [ i - 1 ] ] , K [ i - 1 ] [ w ] ) NEW_LINE DEDENT else : NEW_LINE INDENT K [ i ] [ w ] = K [ i - 1 ] [ w ] NEW_LINE DEDENT DEDENT DEDENT res = K [ n ] [ W ] NEW_LINE print ( res ) NEW_LINE w = W NEW_LINE for i in range ( n , 0 , - 1 ) : NEW_LINE INDENT if res <= 0 : NEW_LINE INDENT break NEW_LINE DEDENT if res == K [ i - 1 ] [ w ] : NEW_LINE INDENT continue NEW_LINE DEDENT else : NEW_LINE INDENT print ( wt [ i - 1 ] ) NEW_LINE res = res - val [ i - 1 ] NEW_LINE w = w - wt [ i - 1 ] NEW_LINE DEDENT DEDENT DEDENT val = [ 60 , 100 , 120 ] NEW_LINE wt = [ 10 , 20 , 30 ] NEW_LINE W = 50 NEW_LINE n = len ( val ) NEW_LINE printknapSack ( W , wt , val , n ) NEW_LINE
s | To sort the array and return the answer ; sort the array ; Fill all stated with - 1 when only one element ; As dp [ 0 ] = 0 ( base case ) so min no of elements to be removed are n - 1 elements ; Iterate from 1 to n - 1 ; Driver code
def removals ( arr , n , k ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE dp = [ 0 for i in range ( n ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT dp [ i ] = - 1 NEW_LINE DEDENT ans = n - 1 NEW_LINE dp [ 0 ] = 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT dp [ i ] = i NEW_LINE j = dp [ i - 1 ] NEW_LINE while ( j != i and arr [ i ] - arr [ j ] > k ) : NEW_LINE j += 1 NEW_LINE dp [ i ] = min ( dp [ i ] , j ) NEW_LINE ans = min ( ans , ( n - ( i - j + 1 ) ) ) NEW_LINE DEDENT return ans NEW_LINE DEDENT a = [ 1 , 3 , 4 , 9 , 10 , 11 , 12 , 17 , 20 ] NEW_LINE n = len ( a ) NEW_LINE k = 4 NEW_LINE print ( removals ( a , n , k ) ) NEW_LINE
Maximum number of segments of lengths a , b and c | function to find the maximum number of segments ; stores the maximum number of segments each index can have ;  ; 0 th index will have 0 segments base case ; traverse for all possible segments till n ; conditions if ( i + a <= n ) : avoid buffer overflow ; if ( i + b <= n ) : avoid buffer overflow ; if ( i + c <= n ) : avoid buffer overflow ; Driver code
def maximumSegments ( n , a , b , c ) : NEW_LINE INDENT dp = [ - 1 ] * ( n + 10 ) NEW_LINE DEDENT / * initialize with - 1 * / NEW_LINE INDENT dp [ 0 ] = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( dp [ i ] != - 1 ) : NEW_LINE INDENT dp [ i + a ] = max ( dp [ i ] + 1 , dp [ i + a ] ) NEW_LINE dp [ i + b ] = max ( dp [ i ] + 1 , dp [ i + b ] ) NEW_LINE dp [ i + c ] = max ( dp [ i ] + 1 , dp [ i + c ] ) NEW_LINE DEDENT DEDENT return dp [ n ] NEW_LINE DEDENT n = 7 NEW_LINE a = 5 NEW_LINE b = 2 NEW_LINE c = 5 NEW_LINE print ( maximumSegments ( n , a , b , c ) ) NEW_LINE
Maximize the sum of selected numbers from an array to make it empty | function to maximize the sum of selected numbers ; maximum in the sequence ; stores the occurrences of the numbers ; marks the occurrence of every number in the sequence ; ans to store the result ; Using the above mentioned approach ; if occurence is greater than 0 ; add it to ans ; decrease i - 1 th element by 1 ; decrease ith element by 1 ; decrease i ; Driver code
def maximizeSum ( a , n ) : NEW_LINE INDENT maximum = max ( a ) NEW_LINE ans = dict . fromkeys ( range ( 0 , n + 1 ) , 0 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT ans [ a [ i ] ] += 1 NEW_LINE DEDENT result = 0 NEW_LINE i = maximum NEW_LINE while i > 0 : NEW_LINE INDENT if ans [ i ] > 0 : NEW_LINE INDENT result += i ; NEW_LINE ans [ i - 1 ] -= 1 ; NEW_LINE ans [ i ] -= 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT i -= 1 ; NEW_LINE DEDENT DEDENT return result ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 1 , 2 , 3 ] NEW_LINE n = len ( a ) NEW_LINE print ( maximizeSum ( a , n ) ) NEW_LINE DEDENT
Print n terms of Newman | Function to find the n - th element ; Declare array to store sequence ; driver code
def sequence ( n ) : NEW_LINE INDENT f = [ 0 , 1 , 1 ] NEW_LINE print ( f [ 1 ] , end = " ▁ " ) , NEW_LINE print ( f [ 2 ] , end = " ▁ " ) , NEW_LINE for i in range ( 3 , n + 1 ) : NEW_LINE INDENT f . append ( f [ f [ i - 1 ] ] + f [ i - f [ i - 1 ] ] ) NEW_LINE print ( f [ i ] , end = " ▁ " ) , NEW_LINE DEDENT DEDENT n = 13 NEW_LINE sequence ( n ) NEW_LINE
LCS formed by consecutive segments of at least length K | Returns the length of the longest common subsequence with a minimum of length of K consecutive segments ; length of strings ; declare the lcs and cnt array ; iterate from i = 1 to n and j = 1 to j = m ; stores the maximum of lcs [ i - 1 ] [ j ] and lcs [ i ] [ j - 1 ] ; when both the characters are equal of s1 and s2 ; when length of common segment is more than k , then update lcs answer by adding that segment to the answer ; formulate for all length of segments to get the longest subsequence with consecutive Common Segment of length of min k length ; update lcs value by adding segment length ; Driver code
def longestSubsequenceCommonSegment ( k , s1 , s2 ) : NEW_LINE INDENT n = len ( s1 ) NEW_LINE m = len ( s2 ) NEW_LINE lcs = [ [ 0 for x in range ( m + 1 ) ] for y in range ( n + 1 ) ] NEW_LINE cnt = [ [ 0 for x in range ( m + 1 ) ] for y in range ( n + 1 ) ] NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( 1 , m + 1 ) : NEW_LINE INDENT lcs [ i ] [ j ] = max ( lcs [ i - 1 ] [ j ] , lcs [ i ] [ j - 1 ] ) NEW_LINE if ( s1 [ i - 1 ] == s2 [ j - 1 ] ) : NEW_LINE INDENT cnt [ i ] [ j ] = cnt [ i - 1 ] [ j - 1 ] + 1 ; NEW_LINE DEDENT if ( cnt [ i ] [ j ] >= k ) : NEW_LINE INDENT for a in range ( k , cnt [ i ] [ j ] + 1 ) : NEW_LINE INDENT lcs [ i ] [ j ] = max ( lcs [ i ] [ j ] , lcs [ i - a ] [ j - a ] + a ) NEW_LINE DEDENT DEDENT DEDENT DEDENT return lcs [ n ] [ m ] NEW_LINE DEDENT k = 4 NEW_LINE s1 = " aggasdfa " NEW_LINE s2 = " aggajasdfa " NEW_LINE print ( longestSubsequenceCommonSegment ( k , s1 , s2 ) ) NEW_LINE
Check for possible path in 2D matrix | Python3 program to find if there is path from top left to right bottom ; to find the path from top left to bottom right ; directions ; queue ; insert the top right corner . ; until queue is empty ; mark as visited ; destination is reached . ; check all four directions ; using the direction array ; not blocked and valid ; Given array ; path from arr [ 0 ] [ 0 ] to arr [ row ] [ col ]
row = 5 NEW_LINE col = 5 NEW_LINE def isPath ( arr ) : NEW_LINE INDENT Dir = [ [ 0 , 1 ] , [ 0 , - 1 ] , [ 1 , 0 ] , [ - 1 , 0 ] ] NEW_LINE q = [ ] NEW_LINE q . append ( ( 0 , 0 ) ) NEW_LINE while ( len ( q ) > 0 ) : NEW_LINE INDENT p = q [ 0 ] NEW_LINE q . pop ( 0 ) NEW_LINE arr [ p [ 0 ] ] [ p [ 1 ] ] = - 1 NEW_LINE if ( p == ( row - 1 , col - 1 ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT for i in range ( 4 ) : NEW_LINE INDENT a = p [ 0 ] + Dir [ i ] [ 0 ] NEW_LINE b = p [ 1 ] + Dir [ i ] [ 1 ] NEW_LINE if ( a >= 0 and b >= 0 and a < row and b < col and arr [ a ] [ b ] != - 1 ) : NEW_LINE INDENT q . append ( ( a , b ) ) NEW_LINE DEDENT DEDENT DEDENT return False NEW_LINE DEDENT arr = [ [ 0 , 0 , 0 , - 1 , 0 ] , [ - 1 , 0 , 0 , - 1 , - 1 ] , [ 0 , 0 , 0 , - 1 , 0 ] , [ - 1 , 0 , - 1 , 0 , - 1 ] , [ 0 , 0 , - 1 , 0 , 0 ] ] NEW_LINE if ( isPath ( arr ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Next Smaller Element | prints element and NSE pair for all elements of arr [ ] of size n ; push the first element to stack ; iterate for rest of the elements ; if stack is not empty , then pop an element from stack . If the popped element is greater than next , then a ) print the pair b ) keep popping while elements are greater and stack is not empty ; push next to stack so that we can find next smaller for it ; After iterating over the loop , the remaining elements in stack do not have the next smaller element , so print - 1 for them ;
def printNSE ( arr , n ) : NEW_LINE INDENT s = [ ] NEW_LINE mp = { } NEW_LINE s . append ( arr [ 0 ] ) NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( len ( s ) == 0 ) : NEW_LINE INDENT s . append ( arr [ i ] ) NEW_LINE continue NEW_LINE DEDENT while ( len ( s ) != 0 and s [ - 1 ] > arr [ i ] ) : NEW_LINE INDENT mp [ s [ - 1 ] ] = arr [ i ] NEW_LINE s . pop ( ) NEW_LINE DEDENT s . append ( arr [ i ] ) NEW_LINE DEDENT while ( len ( s ) != 0 ) : NEW_LINE INDENT mp [ s [ - 1 ] ] = - 1 NEW_LINE s . pop ( ) NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , " - - - > " , mp [ arr [ i ] ] ) NEW_LINE DEDENT DEDENT / * Driver code * / NEW_LINE arr = [ 11 , 13 , 21 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE printNSE ( arr , n ) NEW_LINE
Entringer Number | Return Entringer Number E ( n , k ) ; Base cases ; Finding dp [ i ] [ j ] ; Driven Program
def zigzag ( n , k ) : NEW_LINE INDENT dp = [ [ 0 for x in range ( k + 1 ) ] for y in range ( n + 1 ) ] NEW_LINE dp [ 0 ] [ 0 ] = 1 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT dp [ i ] [ 0 ] = 0 NEW_LINE DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( 1 , k + 1 ) : NEW_LINE INDENT dp [ i ] [ j ] = ( dp [ i ] [ j - 1 ] + dp [ i - 1 ] [ i - j ] ) NEW_LINE DEDENT DEDENT return dp [ n ] [ k ] NEW_LINE DEDENT n = 4 NEW_LINE k = 3 NEW_LINE print ( zigzag ( n , k ) ) NEW_LINE
Lobb Number | Returns value of Binomial Coefficient C ( n , k ) ; Calculate value of Binomial Coefficient in bottom up manner ; Base Cases ; Calculate value using previously stored values ; Return the Lm , n Lobb Number . ; Driven Program
def binomialCoeff ( n , k ) : NEW_LINE INDENT C = [ [ 0 for j in range ( k + 1 ) ] for i in range ( n + 1 ) ] NEW_LINE for i in range ( 0 , n + 1 ) : NEW_LINE INDENT for j in range ( 0 , min ( i , k ) + 1 ) : NEW_LINE INDENT if ( j == 0 or j == i ) : NEW_LINE INDENT C [ i ] [ j ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT C [ i ] [ j ] = ( C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ) NEW_LINE DEDENT DEDENT DEDENT return C [ n ] [ k ] NEW_LINE DEDENT def lobb ( n , m ) : NEW_LINE INDENT return ( ( ( 2 * m + 1 ) * binomialCoeff ( 2 * n , m + n ) ) / ( m + n + 1 ) ) NEW_LINE DEDENT n = 5 NEW_LINE m = 3 NEW_LINE print ( int ( lobb ( n , m ) ) ) NEW_LINE
Count of arrays having consecutive element with different values | Return the number of lists with given constraints . ; Initialising dp [ 0 ] and dp [ 1 ] ; Computing f ( i ) for each 2 <= i <= n . ; Driven code
def countarray ( n , k , x ) : NEW_LINE INDENT dp = list ( ) NEW_LINE dp . append ( 0 ) NEW_LINE dp . append ( 1 ) NEW_LINE i = 2 NEW_LINE while i < n : NEW_LINE INDENT dp . append ( ( k - 2 ) * dp [ i - 1 ] + ( k - 1 ) * dp [ i - 2 ] ) NEW_LINE i = i + 1 NEW_LINE DEDENT return ( ( k - 1 ) * dp [ n - 2 ] if x == 1 else dp [ n - 1 ] ) NEW_LINE DEDENT n = 4 NEW_LINE k = 3 NEW_LINE x = 2 NEW_LINE print ( countarray ( n , k , x ) ) NEW_LINE
Number of palindromic subsequences of length k where k <= 3 | Python3 program to count number of subsequences of given length . ; Precompute the prefix and suffix array . ; Precompute the prefix 2D array ; Precompute the Suffix 2D array . ; Find the number of palindromic subsequence of length k ; If k is 1. ; If k is 2 ; Adding all the products of prefix array ; For k greater than 2. Adding all the products of value of prefix and suffix array . ; Driven Program
MAX = 100 NEW_LINE MAX_CHAR = 26 NEW_LINE def precompute ( s , n , l , r ) : NEW_LINE INDENT l [ ord ( s [ 0 ] ) - ord ( ' a ' ) ] [ 0 ] = 1 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT for j in range ( MAX_CHAR ) : NEW_LINE INDENT l [ j ] [ i ] += l [ j ] [ i - 1 ] NEW_LINE DEDENT l [ ord ( s [ i ] ) - ord ( ' a ' ) ] [ i ] += 1 NEW_LINE DEDENT r [ ord ( s [ n - 1 ] ) - ord ( ' a ' ) ] [ n - 1 ] = 1 NEW_LINE k = n - 2 NEW_LINE while ( k >= 0 ) : NEW_LINE INDENT for j in range ( MAX_CHAR ) : NEW_LINE INDENT r [ j ] [ k ] += r [ j ] [ k + 1 ] NEW_LINE DEDENT r [ ord ( s [ k ] ) - ord ( ' a ' ) ] [ k ] += 1 NEW_LINE k -= 1 NEW_LINE DEDENT DEDENT def countPalindromes ( k , n , l , r ) : NEW_LINE INDENT ans = 0 NEW_LINE if ( k == 1 ) : NEW_LINE INDENT for i in range ( MAX_CHAR ) : NEW_LINE INDENT ans += l [ i ] [ n - 1 ] NEW_LINE DEDENT return ans NEW_LINE DEDENT if ( k == 2 ) : NEW_LINE INDENT for i in range ( MAX_CHAR ) : NEW_LINE INDENT ans += ( ( l [ i ] [ n - 1 ] * ( l [ i ] [ n - 1 ] - 1 ) ) / 2 ) NEW_LINE DEDENT return ans NEW_LINE DEDENT for i in range ( 1 , n - 1 ) : NEW_LINE INDENT for j in range ( MAX_CHAR ) : NEW_LINE INDENT ans += l [ j ] [ i - 1 ] * r [ j ] [ i + 1 ] NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT s = " aabab " NEW_LINE k = 2 NEW_LINE n = len ( s ) NEW_LINE l = [ [ 0 for x in range ( MAX ) ] for y in range ( MAX_CHAR ) ] NEW_LINE r = [ [ 0 for x in range ( MAX ) ] for y in range ( MAX_CHAR ) ] NEW_LINE precompute ( s , n , l , r ) NEW_LINE print ( countPalindromes ( k , n , l , r ) ) NEW_LINE
Minimum cost to make Longest Common Subsequence of length k | Python3 program to calculate Minimum cost to make Longest Common Subsequence of length k ; Return Minimum cost to make LCS of length k ; If k is 0 ; If length become less than 0 , return big number ; If state already calculated ; Finding cost ;  ; Driver Code
N = 30 NEW_LINE def solve ( X , Y , l , r , k , dp ) : NEW_LINE INDENT if k == 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT if l < 0 or r < 0 : NEW_LINE INDENT return 1000000000 NEW_LINE DEDENT if dp [ l ] [ r ] [ k ] != - 1 : NEW_LINE INDENT return dp [ l ] [ r ] [ k ] NEW_LINE DEDENT cost = ( ( ord ( X [ l ] ) - ord ( ' a ' ) ) ^ ( ord ( Y [ r ] ) - ord ( ' a ' ) ) ) NEW_LINE DEDENT / * Finding minimum cost and saving the state value * / NEW_LINE INDENT dp [ l ] [ r ] [ k ] = min ( [ cost + solve ( X , Y , l - 1 , r - 1 , k - 1 , dp ) , solve ( X , Y , l - 1 , r , k , dp ) , solve ( X , Y , l , r - 1 , k , dp ) ] ) NEW_LINE return dp [ l ] [ r ] [ k ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT X = " abble " NEW_LINE Y = " pie " NEW_LINE n = len ( X ) NEW_LINE m = len ( Y ) NEW_LINE k = 2 NEW_LINE dp = [ [ [ - 1 ] * N for __ in range ( N ) ] for ___ in range ( N ) ] NEW_LINE ans = solve ( X , Y , n - 1 , m - 1 , k , dp ) NEW_LINE print ( - 1 if ans == 1000000000 else ans ) NEW_LINE DEDENT
Maximum sum path in a matrix from top to bottom | Python3 implementation to find the maximum sum path in a matrix ; function to find the maximum sum path in a matric ; if there is a single elementif there is a single element only only ; dp [ ] [ ] matrix to store the results of each iteration ; base case , copying elements of last row ; building up the dp [ ] [ ] matrix from bottom to the top row ; finding the maximum diagonal element in the ( i + 1 ) th row if that cell exists ; adding that ' max ' element to the mat [ i ] [ j ] element ; finding the maximum value from the first row of dp [ ] [ ] ; required maximum sum ; Driver program to test above
SIZE = 10 NEW_LINE INT_MIN = - 10000000 NEW_LINE def maxSum ( mat , n ) : NEW_LINE INDENT if n == 1 : NEW_LINE INDENT return mat [ 0 ] [ 0 ] NEW_LINE DEDENT dp = [ [ 0 for i in range ( n ) ] for i in range ( n ) ] NEW_LINE maxSum = INT_MIN NEW_LINE for j in range ( n ) : NEW_LINE INDENT dp [ n - 1 ] [ j ] = mat [ n - 1 ] [ j ] NEW_LINE DEDENT for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT maxi = INT_MIN NEW_LINE if ( ( ( ( j - 1 ) >= 0 ) and ( maxi < dp [ i + 1 ] [ j - 1 ] ) ) ) : NEW_LINE INDENT maxi = dp [ i + 1 ] [ j - 1 ] NEW_LINE DEDENT if ( ( ( ( j + 1 ) < n ) and ( maxi < dp [ i + 1 ] [ j + 1 ] ) ) ) : NEW_LINE INDENT maxi = dp [ i + 1 ] [ j + 1 ] NEW_LINE DEDENT dp [ i ] [ j ] = mat [ i ] [ j ] + maxi NEW_LINE DEDENT DEDENT for j in range ( n ) : NEW_LINE INDENT if ( maxSum < dp [ 0 ] [ j ] ) : NEW_LINE INDENT maxSum = dp [ 0 ] [ j ] NEW_LINE DEDENT DEDENT return maxSum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT mat = [ [ 5 , 6 , 1 , 7 ] , [ - 2 , 10 , 8 , - 1 ] , [ 3 , - 7 , - 9 , 11 ] , [ 12 , - 4 , 2 , 6 ] ] NEW_LINE n = 4 NEW_LINE print ( " Maximum ▁ Sum = " , maxSum ( mat , n ) ) NEW_LINE DEDENT
Longest Repeated Subsequence | This function mainly returns LCS ( str , str ) with a condition that same characters at same index are not considered . ; This part of code is same as below post it fills dp [ ] [ ] https : www . geeksforgeeks . org / longest - repeating - subsequence / OR the code mentioned above ; This part of code finds the result string using dp [ ] [ ] Initialize result ; Traverse dp [ ] [ ] from bottom right ; If this cell is same as diagonally adjacent cell just above it , then same characters are present at str [ i - 1 ] and str [ j - 1 ] . Append any of them to result . ; Otherwise we move to the side that gave us maximum result . ; Since we traverse dp [ ] [ ] from bottom , we get result in reverse order . ; Driver Program
def longestRepeatedSubSeq ( str ) : NEW_LINE INDENT n = len ( str ) NEW_LINE dp = [ [ 0 for i in range ( n + 1 ) ] for j in range ( n + 1 ) ] NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( 1 , n + 1 ) : NEW_LINE INDENT if ( str [ i - 1 ] == str [ j - 1 ] and i != j ) : NEW_LINE INDENT dp [ i ] [ j ] = 1 + dp [ i - 1 ] [ j - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] [ j ] = max ( dp [ i ] [ j - 1 ] , dp [ i - 1 ] [ j ] ) NEW_LINE DEDENT DEDENT DEDENT res = ' ' NEW_LINE i = n NEW_LINE j = n NEW_LINE while ( i > 0 and j > 0 ) : NEW_LINE INDENT if ( dp [ i ] [ j ] == dp [ i - 1 ] [ j - 1 ] + 1 ) : NEW_LINE INDENT res += str [ i - 1 ] NEW_LINE i -= 1 NEW_LINE j -= 1 NEW_LINE DEDENT elif ( dp [ i ] [ j ] == dp [ i - 1 ] [ j ] ) : NEW_LINE INDENT i -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT j -= 1 NEW_LINE DEDENT DEDENT res = ' ' . join ( reversed ( res ) ) NEW_LINE return res NEW_LINE DEDENT str = ' AABEBCDD ' NEW_LINE print ( longestRepeatedSubSeq ( str ) ) NEW_LINE
Sub | Tree traversal to compute minimum difference ; Initial min difference is the color of node ; Traversing its children ; Not traversing the parent ; If the child is Adding positively to difference , we include it in the answer Otherwise , we leave the sub - tree and include 0 ( nothing ) in the answer ; DFS for colour difference : 1 colour - 2 colour ; Minimum colour difference is maximum answer value ; Clearing the current value to check for colour2 as well ; Interchanging the colours ; DFS for colour difference : 2 colour - 1 colour ; Checking if colour2 makes the minimum colour difference ; Nodes ; Adjacency list representation ; Edges ; Index represent the colour of that node There is no Node 0 , so we start from index 1 to N ; Printing the result
def dfs ( node , parent , tree , colour , answer ) : NEW_LINE INDENT answer [ node ] = colour [ node ] NEW_LINE for u in tree [ node ] : NEW_LINE INDENT if ( u == parent ) : NEW_LINE INDENT continue NEW_LINE DEDENT dfs ( u , node , tree , colour , answer ) NEW_LINE answer [ node ] += max ( answer [ u ] , 0 ) NEW_LINE DEDENT DEDENT def maxDiff ( tree , colour , N ) : NEW_LINE INDENT answer = [ 0 for _ in range ( N + 1 ) ] NEW_LINE dfs ( 1 , 0 , tree , colour , answer ) NEW_LINE high = 0 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT high = max ( high , answer [ i ] ) NEW_LINE answer [ i ] = 0 NEW_LINE DEDENT for i in range ( 1 , N + 1 ) : NEW_LINE INDENT if colour [ i ] == - 1 : NEW_LINE INDENT colour [ i ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT colour [ i ] = - 1 NEW_LINE DEDENT DEDENT dfs ( 1 , 0 , tree , colour , answer ) NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT high = max ( high , answer [ i ] ) NEW_LINE DEDENT return high NEW_LINE DEDENT N = 5 NEW_LINE tree = [ list ( ) for _ in range ( N + 1 ) ] NEW_LINE tree [ 1 ] . append ( 2 ) NEW_LINE tree [ 2 ] . append ( 1 ) NEW_LINE tree [ 1 ] . append ( 3 ) NEW_LINE tree [ 3 ] . append ( 1 ) NEW_LINE tree [ 2 ] . append ( 4 ) NEW_LINE tree [ 4 ] . append ( 2 ) NEW_LINE tree [ 3 ] . append ( 5 ) NEW_LINE tree [ 5 ] . append ( 3 ) NEW_LINE colour = [ 0 , 1 , 1 , - 1 , - 1 , 1 ] NEW_LINE print ( maxDiff ( tree , colour , N ) ) NEW_LINE