codeparrot/xlcost-text-to-code huggingface dataset:
Count of numbers between range having only non | Function to return the count of required numbers from 0 to num ; Last position ; If this result is already computed simply return it ; Maximum limit upto which we can place digit . If tight is 1 , means number has already become smaller so we can place any digit , otherwise num [ pos ] ; If the current digit is zero and nonz is 1 , we can 't place it ; Function to convert x into its digit vector and uses count ( ) function to return the required count ; Initialize dp ; Driver code ; n is the sum of digits and number should be divisible by m ; States - position , sum , rem , tight sum can have values upto 162 , if we are dealing with numbers upto 10 ^ 18 when all 18 digits are 9 , then sum is 18 * 9 = 162
def count ( pos , Sum , rem , tight , nonz , num ) : NEW_LINE INDENT if pos == len ( num ) : NEW_LINE INDENT if rem == 0 and Sum == n : NEW_LINE INDENT return 1 NEW_LINE DEDENT return 0 NEW_LINE DEDENT if dp [ pos ] [ Sum ] [ rem ] [ tight ] != - 1 : NEW_LINE INDENT return dp [ pos ] [ Sum ] [ rem ] [ tight ] NEW_LINE DEDENT ans = 0 NEW_LINE if tight : NEW_LINE INDENT limit = 9 NEW_LINE DEDENT else : NEW_LINE INDENT limit = num [ pos ] NEW_LINE DEDENT for d in range ( 0 , limit + 1 ) : NEW_LINE INDENT if d == 0 and nonz : NEW_LINE INDENT continue NEW_LINE DEDENT currSum = Sum + d NEW_LINE currRem = ( rem * 10 + d ) % m NEW_LINE currF = int ( tight or ( d < num [ pos ] ) ) NEW_LINE ans += count ( pos + 1 , currSum , currRem , currF , nonz or d , num ) NEW_LINE DEDENT dp [ pos ] [ Sum ] [ rem ] [ tight ] = ans NEW_LINE return ans NEW_LINE DEDENT def solve ( x ) : NEW_LINE INDENT num = [ ] NEW_LINE global dp NEW_LINE while x > 0 : NEW_LINE INDENT num . append ( x % 10 ) NEW_LINE x //= 10 NEW_LINE DEDENT num . reverse ( ) NEW_LINE dp = [ [ [ [ - 1 , - 1 ] for i in range ( M ) ] for j in range ( 165 ) ] for k in range ( M ) ] NEW_LINE return count ( 0 , 0 , 0 , 0 , 0 , num ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT L , R = 1 , 100 NEW_LINE n , m , M = 8 , 2 , 20 NEW_LINE dp = [ ] NEW_LINE print ( solve ( R ) - solve ( L ) ) NEW_LINE DEDENT
Count of Numbers in Range where the number does not contain more than K non zero digits | This function returns the count of required numbers from 0 to num ; Last position ; If count of non zero digits is less than or equal to K ; If this result is already computed simply return it ; Maximum limit upto which we can place digit . If tight is 1 , means number has already become smaller so we can place any digit , otherwise num [ pos ] ; If the current digit is nonzero increment currCnt ; At this position , number becomes smaller ; Next recursive call ; This function converts a number into its digit vector and uses above function to compute the answer ; Initialize dp ; Driver Code ; states - position , count , tight ; K is the number of non zero digits
def countInRangeUtil ( pos , cnt , tight , num ) : NEW_LINE INDENT if pos == len ( num ) : NEW_LINE INDENT if cnt <= K : NEW_LINE INDENT return 1 NEW_LINE DEDENT return 0 NEW_LINE DEDENT if dp [ pos ] [ cnt ] [ tight ] != - 1 : NEW_LINE INDENT return dp [ pos ] [ cnt ] [ tight ] NEW_LINE DEDENT ans = 0 NEW_LINE limit = 9 if tight else num [ pos ] NEW_LINE for dig in range ( limit + 1 ) : NEW_LINE INDENT currCnt = cnt NEW_LINE if dig != 0 : NEW_LINE INDENT currCnt += 1 NEW_LINE DEDENT currTight = tight NEW_LINE if dig < num [ pos ] : NEW_LINE INDENT currTight = 1 NEW_LINE DEDENT ans += countInRangeUtil ( pos + 1 , currCnt , currTight , num ) NEW_LINE DEDENT dp [ pos ] [ cnt ] [ tight ] = ans NEW_LINE return dp [ pos ] [ cnt ] [ tight ] NEW_LINE DEDENT def countInRange ( x ) : NEW_LINE INDENT global dp , K , M NEW_LINE num = [ ] NEW_LINE while x : NEW_LINE INDENT num . append ( x % 10 ) NEW_LINE x //= 10 NEW_LINE DEDENT num . reverse ( ) NEW_LINE dp = [ [ [ - 1 , - 1 ] for i in range ( M ) ] for j in range ( M ) ] NEW_LINE return countInRangeUtil ( 0 , 0 , 0 , num ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT dp = [ ] NEW_LINE M = 20 NEW_LINE K = 0 NEW_LINE L = 1 NEW_LINE R = 1000 NEW_LINE K = 3 NEW_LINE print ( countInRange ( R ) - countInRange ( L - 1 ) ) NEW_LINE L = 9995 NEW_LINE R = 10005 NEW_LINE K = 2 NEW_LINE print ( countInRange ( R ) - countInRange ( L - 1 ) ) NEW_LINE DEDENT
Balanced expressions such that given positions have opening brackets | Set 2 | Python3 implementation of above approach using memoization ; Open brackets at position 1 ; Function to find Number of proper bracket expressions ; If open - closed brackets < 0 ; If index reaches the end of expression ; If brackets are balanced ; If already stored in dp ; If the current index has assigned open bracket ; Move forward increasing the length of open brackets ; Move forward by inserting open as well as closed brackets on that index ; return the answer ; DP array to precompute the answer ; Calling the find function to calculate the answer
N = 1000 ; NEW_LINE dp = [ [ - 1 for x in range ( N ) ] for y in range ( N ) ] ; NEW_LINE adj = [ 1 , 0 , 0 , 0 ] ; NEW_LINE def find ( index , openbrk , n ) : NEW_LINE INDENT if ( openbrk < 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( index == n ) : NEW_LINE INDENT if ( openbrk == 0 ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT DEDENT if ( dp [ index ] [ openbrk ] != - 1 ) : NEW_LINE INDENT return dp [ index ] [ openbrk ] ; NEW_LINE DEDENT if ( adj [ index ] == 1 ) : NEW_LINE INDENT dp [ index ] [ openbrk ] = find ( index + 1 , openbrk + 1 , n ) ; NEW_LINE DEDENT else : NEW_LINE INDENT dp [ index ] [ openbrk ] = ( find ( index + 1 , openbrk + 1 , n ) + find ( index + 1 , openbrk - 1 , n ) ) ; NEW_LINE DEDENT return dp [ index ] [ openbrk ] ; NEW_LINE DEDENT n = 2 ; NEW_LINE print ( find ( 0 , 0 , 2 * n ) ) ; NEW_LINE
Maximize array elements upto given number | variable to store maximum value that can be obtained . ; If entire array is traversed , then compare current value in num to overall maximum obtained so far . ; Case 1 : Subtract current element from value so far if result is greater than or equal to zero . ; Case 2 : Add current element to value so far if result is less than or equal to maxLimit . ; def to find maximum possible value that can be obtained using array elements and given number . ; variable to store current index position . ; call to utility def to find maximum possible value that can be obtained . ; Driver code
ans = 0 ; NEW_LINE def findMaxValUtil ( arr , n , num , maxLimit , ind ) : NEW_LINE INDENT global ans NEW_LINE if ( ind == n ) : NEW_LINE INDENT ans = max ( ans , num ) NEW_LINE return NEW_LINE DEDENT if ( num - arr [ ind ] >= 0 ) : NEW_LINE INDENT findMaxValUtil ( arr , n , num - arr [ ind ] , maxLimit , ind + 1 ) NEW_LINE DEDENT if ( num + arr [ ind ] <= maxLimit ) : NEW_LINE INDENT findMaxValUtil ( arr , n , num + arr [ ind ] , maxLimit , ind + 1 ) NEW_LINE DEDENT DEDENT def findMaxVal ( arr , n , num , maxLimit ) : NEW_LINE INDENT global ans NEW_LINE ind = 0 NEW_LINE findMaxValUtil ( arr , n , num , maxLimit , ind ) NEW_LINE return ans NEW_LINE DEDENT num = 1 NEW_LINE arr = [ 3 , 10 , 6 , 4 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE maxLimit = 15 NEW_LINE print ( findMaxVal ( arr , n , num , maxLimit ) ) NEW_LINE
Print equal sum sets of array ( Partition problem ) | Set 1 | Function to print the equal sum sets of the array . ; Print set 1. ; Print set 2. ; Utility function to find the sets of the array which have equal sum . ; If entire array is traversed , compare both the sums . ; If sums are equal print both sets and return true to show sets are found . ; If sums are not equal then return sets are not found . ; Add current element to set 1. ; Recursive call after adding current element to set 1. ; If this inclusion results in an equal sum sets partition then return true to show desired sets are found . ; If not then backtrack by removing current element from set1 and include it in set 2. ; Recursive call after including current element to set 2 and removing the element from set 2 if it returns False ; Return true if array arr can be partitioned into two equal sum sets or not . ; Calculate sum of elements in array . ; If sum is odd then array cannot be partitioned . ; Declare vectors to store both the sets . ; Find both the sets . ; Driver code
def printSets ( set1 , set2 ) : NEW_LINE INDENT for i in range ( 0 , len ( set1 ) ) : NEW_LINE INDENT print ( " { } ▁ " . format ( set1 [ i ] ) , end = " " ) ; NEW_LINE DEDENT print ( " " ) NEW_LINE for i in range ( 0 , len ( set2 ) ) : NEW_LINE INDENT print ( " { } ▁ " . format ( set2 [ i ] ) , end = " " ) ; NEW_LINE DEDENT print ( " " ) NEW_LINE DEDENT def findSets ( arr , n , set1 , set2 , sum1 , sum2 , pos ) : NEW_LINE INDENT if ( pos == n ) : NEW_LINE INDENT if ( sum1 == sum2 ) : NEW_LINE INDENT printSets ( set1 , set2 ) NEW_LINE return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT set1 . append ( arr [ pos ] ) NEW_LINE res = findSets ( arr , n , set1 , set2 , sum1 + arr [ pos ] , sum2 , pos + 1 ) NEW_LINE if ( res ) : NEW_LINE INDENT return res NEW_LINE DEDENT set1 . pop ( ) NEW_LINE set2 . append ( arr [ pos ] ) NEW_LINE res = findSets ( arr , n , set1 , set2 , sum1 , sum2 + arr [ pos ] , pos + 1 ) NEW_LINE if not res : NEW_LINE INDENT set2 . pop ( ) NEW_LINE DEDENT return res NEW_LINE DEDENT def isPartitionPoss ( arr , n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE DEDENT if ( sum % 2 != 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT set1 = [ ] NEW_LINE set2 = [ ] NEW_LINE return findSets ( arr , n , set1 , set2 , 0 , 0 , 0 ) NEW_LINE DEDENT arr = [ 5 , 5 , 1 , 11 ] NEW_LINE n = len ( arr ) NEW_LINE if ( isPartitionPoss ( arr , n ) == False ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT
Maximum subarray sum in O ( n ) using prefix sum | Python3 program to find out maximum subarray sum in linear time using prefix sum . ; Function to compute maximum subarray sum in linear time . ; Initialize minimum prefix sum to 0. ; Initialize maximum subarray sum so far to - infinity . ; Initialize and compute the prefix sum array . ; loop through the array keep track of minimum prefix sum so far and maximum subarray sum . ; Test case 1 ; Test case 2
import math NEW_LINE def maximumSumSubarray ( arr , n ) : NEW_LINE INDENT min_prefix_sum = 0 NEW_LINE res = - math . inf NEW_LINE prefix_sum = [ ] NEW_LINE prefix_sum . append ( arr [ 0 ] ) NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT prefix_sum . append ( prefix_sum [ i - 1 ] + arr [ i ] ) NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT res = max ( res , prefix_sum [ i ] - min_prefix_sum ) NEW_LINE min_prefix_sum = min ( min_prefix_sum , prefix_sum [ i ] ) NEW_LINE DEDENT return res NEW_LINE DEDENT arr1 = [ - 2 , - 3 , 4 , - 1 , - 2 , 1 , 5 , - 3 ] NEW_LINE n1 = len ( arr1 ) NEW_LINE print ( maximumSumSubarray ( arr1 , n1 ) ) NEW_LINE arr2 = [ 4 , - 8 , 9 , - 4 , 1 , - 8 , - 1 , 6 ] NEW_LINE n2 = len ( arr2 ) NEW_LINE print ( maximumSumSubarray ( arr2 , n2 ) ) NEW_LINE
Counting numbers of n digits that are monotone | Considering all possible digits as { 1 , 2 , 3 , . .9 } ; DP [ i ] [ j ] is going to store monotone numbers of length i + 1 considering j + 1 digits . ; Unit length numbers ; Single digit numbers ; Filling rest of the entries in bottom up manner . ; Driver code
DP_s = 9 NEW_LINE def getNumMonotone ( ln ) : NEW_LINE INDENT DP = [ [ 0 ] * DP_s for i in range ( ln ) ] NEW_LINE for i in range ( DP_s ) : NEW_LINE INDENT DP [ 0 ] [ i ] = i + 1 NEW_LINE DEDENT for i in range ( ln ) : NEW_LINE INDENT DP [ i ] [ 0 ] = 1 NEW_LINE DEDENT for i in range ( 1 , ln ) : NEW_LINE INDENT for j in range ( 1 , DP_s ) : NEW_LINE INDENT DP [ i ] [ j ] = DP [ i - 1 ] [ j ] + DP [ i ] [ j - 1 ] NEW_LINE DEDENT DEDENT return DP [ ln - 1 ] [ DP_s - 1 ] NEW_LINE DEDENT print ( getNumMonotone ( 10 ) ) NEW_LINE
Newman | To declare array import module array ; To store values of sequence in array ; Driver code
import array NEW_LINE def sequence ( n ) : NEW_LINE INDENT f = array . array ( ' i ' , [ 0 , 1 , 1 ] ) NEW_LINE for i in range ( 3 , n + 1 ) : NEW_LINE INDENT r = f [ f [ i - 1 ] ] + f [ i - f [ i - 1 ] ] NEW_LINE f . append ( r ) ; NEW_LINE DEDENT return r NEW_LINE DEDENT def main ( ) : NEW_LINE INDENT n = 10 NEW_LINE print sequence ( n ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT main ( ) NEW_LINE DEDENT
Hosoya 's Triangle | Python3 code to print Hosoya 's triangle of height n. ; Base case ; Recursive step ; Print the Hosoya triangle of height n . ; Driven Code
def Hosoya ( n , m ) : NEW_LINE INDENT if ( ( n == 0 and m == 0 ) or ( n == 1 and m == 0 ) or ( n == 1 and m == 1 ) or ( n == 2 and m == 1 ) ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if n > m : NEW_LINE INDENT return Hosoya ( n - 1 , m ) NEW_LINE INDENT + Hosoya ( n - 2 , m ) NEW_LINE DEDENT DEDENT elif m == n : NEW_LINE INDENT return Hosoya ( n - 1 , m - 1 ) NEW_LINE INDENT + Hosoya ( n - 2 , m - 2 ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT def printHosoya ( n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 ) : NEW_LINE INDENT print ( Hosoya ( i , j ) , end = " ▁ " ) NEW_LINE DEDENT print ( " " , ▁ end ▁ = ▁ " " ) NEW_LINE DEDENT DEDENT n = 5 NEW_LINE printHosoya ( n ) NEW_LINE
Eulerian Number | Return euleriannumber A ( n , m ) ; Driver code
def eulerian ( n , m ) : NEW_LINE INDENT if ( m >= n or n == 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( m == 0 ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT return ( ( n - m ) * eulerian ( n - 1 , m - 1 ) + ( m + 1 ) * eulerian ( n - 1 , m ) ) NEW_LINE DEDENT n = 3 NEW_LINE m = 1 NEW_LINE print ( eulerian ( n , m ) ) NEW_LINE
Largest divisible pairs subset | ''function to find the longest Subsequence ; dp [ i ] is going to store size of largest divisible subset beginning with a [ i ] . ; Since last element is largest , d [ n - 1 ] is 1 ; Fill values for smaller elements ; Find all multiples of a [ i ] and consider the multiple that has largest subset beginning with it . ; Return maximum value from dp [ ] ; Driver Code
def largestSubset ( a , n ) : NEW_LINE INDENT dp = [ 0 for i in range ( n ) ] NEW_LINE dp [ n - 1 ] = 1 ; NEW_LINE for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT mxm = 0 ; NEW_LINE for j in range ( i + 1 , n ) : NEW_LINE INDENT if a [ j ] % a [ i ] == 0 or a [ i ] % a [ j ] == 0 : NEW_LINE INDENT mxm = max ( mxm , dp [ j ] ) NEW_LINE DEDENT DEDENT dp [ i ] = 1 + mxm NEW_LINE DEDENT return max ( dp ) NEW_LINE DEDENT a = [ 1 , 3 , 6 , 13 , 17 , 18 ] NEW_LINE n = len ( a ) NEW_LINE print ( largestSubset ( a , n ) ) NEW_LINE
How to solve a Dynamic Programming Problem ? | This function returns the number of arrangements to form 'n ; lookup dictionary / hashmap is initialized ; Base cases negative number can 't be  produced, return 0 ; 0 can be produced by not taking any number whereas 1 can be produced by just taking 1 ; Checking if number of way for producing n is already calculated or not if calculated , return that , otherwise calulcate and then return
' NEW_LINE def solve ( n , lookup = { } ) : NEW_LINE INDENT if n < 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT if n == 0 : NEW_LINE INDENT return 1 NEW_LINE DEDENT if n not in lookup : NEW_LINE INDENT lookup [ n ] = ( solve ( n - 1 ) + solve ( n - 3 ) + solve ( n - 5 ) ) NEW_LINE DEDENT return lookup [ n ] NEW_LINE DEDENT
Friends Pairing Problem | Returns count of ways n people can remain single or paired up . ; Filling dp [ ] in bottom - up manner using recursive formula explained above . ; Driver code
def countFriendsPairings ( n ) : NEW_LINE INDENT dp = [ 0 for i in range ( n + 1 ) ] NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT if ( i <= 2 ) : NEW_LINE INDENT dp [ i ] = i NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] = dp [ i - 1 ] + ( i - 1 ) * dp [ i - 2 ] NEW_LINE DEDENT DEDENT return dp [ n ] NEW_LINE DEDENT n = 4 NEW_LINE print ( countFriendsPairings ( n ) ) NEW_LINE
Friends Pairing Problem | Returns count of ways n people can remain single or paired up . ; Driver code
def countFriendsPairings ( n ) : NEW_LINE INDENT a , b , c = 1 , 2 , 0 ; NEW_LINE if ( n <= 2 ) : NEW_LINE INDENT return n ; NEW_LINE DEDENT for i in range ( 3 , n + 1 ) : NEW_LINE INDENT c = b + ( i - 1 ) * a ; NEW_LINE a = b ; NEW_LINE b = c ; NEW_LINE DEDENT return c ; NEW_LINE DEDENT n = 4 ; NEW_LINE print ( countFriendsPairings ( n ) ) ; NEW_LINE
Check whether row or column swaps produce maximum size binary sub | Python3 program to find maximum binary sub - matrix with row swaps and column swaps . ; Precompute the number of consecutive 1 below the ( i , j ) in j - th column and the number of consecutive 1 s on right side of ( i , j ) in i - th row . ; Travesing the 2d matrix from top - right . ; If ( i , j ) contain 0 , do nothing ; Counting consecutive 1 on right side ; Travesing the 2d matrix from bottom - left . ; If ( i , j ) contain 0 , do nothing ; Counting consecutive 1 down to ( i , j ) . ; Return maximum size submatrix with row swap allowed . ; Copying the column ; Sort the copied array ; Find maximum submatrix size . ; Return maximum size submatrix with column swap allowed . ; Copying the row . ; Sort the copied array ; Find maximum submatrix size . ; Solving for row swap and column swap ; Comparing both . ; Driver Code
R , C = 5 , 3 NEW_LINE def precompute ( mat , ryt , dwn ) : NEW_LINE INDENT for j in range ( C - 1 , - 1 , - 1 ) : NEW_LINE INDENT for i in range ( 0 , R ) : NEW_LINE INDENT if mat [ i ] [ j ] == 0 : NEW_LINE INDENT ryt [ i ] [ j ] = 0 NEW_LINE DEDENT else : NEW_LINE INDENT ryt [ i ] [ j ] = ryt [ i ] [ j + 1 ] + 1 NEW_LINE DEDENT DEDENT DEDENT for i in range ( R - 1 , - 1 , - 1 ) : NEW_LINE INDENT for j in range ( 0 , C ) : NEW_LINE INDENT if mat [ i ] [ j ] == 0 : NEW_LINE INDENT dwn [ i ] [ j ] = 0 NEW_LINE DEDENT else : NEW_LINE INDENT dwn [ i ] [ j ] = dwn [ i + 1 ] [ j ] + 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT def solveRowSwap ( ryt ) : NEW_LINE INDENT b = [ 0 ] * R NEW_LINE ans = 0 NEW_LINE for j in range ( 0 , C ) : NEW_LINE INDENT for i in range ( 0 , R ) : NEW_LINE INDENT b [ i ] = ryt [ i ] [ j ] NEW_LINE DEDENT b . sort ( ) NEW_LINE for i in range ( 0 , R ) : NEW_LINE INDENT ans = max ( ans , b [ i ] * ( R - i ) ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT def solveColumnSwap ( dwn ) : NEW_LINE INDENT b = [ 0 ] * C NEW_LINE ans = 0 NEW_LINE for i in range ( 0 , R ) : NEW_LINE INDENT for j in range ( 0 , C ) : NEW_LINE INDENT b [ j ] = dwn [ i ] [ j ] NEW_LINE DEDENT b . sort ( ) NEW_LINE for i in range ( 0 , C ) : NEW_LINE INDENT ans = max ( ans , b [ i ] * ( C - i ) ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT def findMax1s ( mat ) : NEW_LINE INDENT ryt = [ [ 0 for i in range ( C + 2 ) ] for j in range ( R + 2 ) ] NEW_LINE dwn = [ [ 0 for i in range ( C + 2 ) ] for j in range ( R + 2 ) ] NEW_LINE precompute ( mat , ryt , dwn ) NEW_LINE rswap = solveRowSwap ( ryt ) NEW_LINE cswap = solveColumnSwap ( dwn ) NEW_LINE if rswap > cswap : print ( " Row Swap " , rswap ) NEW_LINE else : print ( " Column Swap " , cswap ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT mat = [ [ 0 , 0 , 0 ] , [ 1 , 1 , 0 ] , [ 1 , 1 , 0 ] , [ 0 , 0 , 0 ] , [ 1 , 1 , 0 ] ] NEW_LINE findMax1s ( mat ) NEW_LINE DEDENT
LCS ( Longest Common Subsequence ) of three strings | Returns length of LCS for X [ 0. . m - 1 ] , Y [ 0. . n - 1 ] and Z [ 0. . o - 1 ] ; Following steps build L [ m + 1 ] [ n + 1 ] [ o + 1 ] in bottom up fashion . Note that L [ i ] [ j ] [ k ] contains length of LCS of X [ 0. . i - 1 ] and Y [ 0. . j - 1 ] and Z [ 0. . ... k - 1 ] ; L [ m ] [ n ] [ o ] contains length of LCS for X [ 0. . n - 1 ] and Y [ 0. . m - 1 ] and Z [ 0. . o - 1 ] ; Driver program to test above function
def lcsOf3 ( X , Y , Z , m , n , o ) : NEW_LINE INDENT L = [ [ [ 0 for i in range ( o + 1 ) ] for j in range ( n + 1 ) ] for k in range ( m + 1 ) ] NEW_LINE for i in range ( m + 1 ) : NEW_LINE INDENT for j in range ( n + 1 ) : NEW_LINE INDENT for k in range ( o + 1 ) : NEW_LINE INDENT if ( i == 0 or j == 0 or k == 0 ) : NEW_LINE INDENT L [ i ] [ j ] [ k ] = 0 NEW_LINE DEDENT elif ( X [ i - 1 ] == Y [ j - 1 ] and X [ i - 1 ] == Z [ k - 1 ] ) : NEW_LINE INDENT L [ i ] [ j ] [ k ] = L [ i - 1 ] [ j - 1 ] [ k - 1 ] + 1 NEW_LINE DEDENT else : NEW_LINE INDENT L [ i ] [ j ] [ k ] = max ( max ( L [ i - 1 ] [ j ] [ k ] , L [ i ] [ j - 1 ] [ k ] ) , L [ i ] [ j ] [ k - 1 ] ) NEW_LINE DEDENT DEDENT DEDENT DEDENT return L [ m ] [ n ] [ o ] NEW_LINE DEDENT X = ' AGGT12' NEW_LINE Y = '12TXAYB ' NEW_LINE Z = '12XBA ' NEW_LINE m = len ( X ) NEW_LINE n = len ( Y ) NEW_LINE o = len ( Z ) NEW_LINE print ( ' Length ▁ of ▁ LCS ▁ is ' , lcsOf3 ( X , Y , Z , m , n , o ) ) NEW_LINE
Longest subsequence such that difference between adjacents is one | ''Driver code
def longestSubsequence ( A , N ) : NEW_LINE INDENT L = [ 1 ] * N NEW_LINE hm = { } NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT if abs ( A [ i ] - A [ i - 1 ] ) == 1 : NEW_LINE INDENT L [ i ] = 1 + L [ i - 1 ] NEW_LINE DEDENT elif hm . get ( A [ i ] + 1 , 0 ) or hm . get ( A [ i ] - 1 , 0 ) : NEW_LINE INDENT L [ i ] = 1 + max ( hm . get ( A [ i ] + 1 , 0 ) , hm . get ( A [ i ] - 1 , 0 ) ) NEW_LINE DEDENT hm [ A [ i ] ] = L [ i ] NEW_LINE DEDENT return max ( L ) NEW_LINE DEDENT A = [ 1 , 2 , 3 , 4 , 5 , 3 , 2 ] NEW_LINE N = len ( A ) NEW_LINE print ( longestSubsequence ( A , N ) ) NEW_LINE
Printing Shortest Common Supersequence | returns shortest supersequence of X and Y ; dp [ i ] [ j ] contains length of shortest supersequence for X [ 0. . i - 1 ] and Y [ 0. . j - 1 ] ; Fill table in bottom up manner ; Below steps follow recurrence relation ; string to store the shortest supersequence ; Start from the bottom right corner and one by one push characters in output string ; If current character in X and Y are same , then current character is part of shortest supersequence ; Put current character in result ; reduce values of i , j and index ; If current character in X and Y are different ; Put current character of Y in result ; reduce values of j and index ; Put current character of X in result ; reduce values of i and index ; If Y reaches its end , put remaining characters of X in the result string ; If X reaches its end , put remaining characters of Y in the result string ; reverse the string and return it ; Driver Code
def printShortestSuperSeq ( x , y ) : NEW_LINE INDENT m = len ( x ) NEW_LINE n = len ( y ) NEW_LINE dp = [ [ 0 for i in range ( n + 1 ) ] for j in range ( n + 1 ) ] NEW_LINE for i in range ( m + 1 ) : NEW_LINE INDENT for j in range ( n + 1 ) : NEW_LINE INDENT if i == 0 : NEW_LINE INDENT dp [ i ] [ j ] = j NEW_LINE DEDENT elif j == 0 : NEW_LINE INDENT dp [ i ] [ j ] = i NEW_LINE DEDENT elif x [ i - 1 ] == y [ j - 1 ] : NEW_LINE INDENT dp [ i ] [ j ] = 1 + dp [ i - 1 ] [ j - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] [ j ] = 1 + min ( dp [ i - 1 ] [ j ] , dp [ i ] [ j - 1 ] ) NEW_LINE DEDENT DEDENT DEDENT string = " " NEW_LINE i = m NEW_LINE j = n NEW_LINE while i > 0 and j > 0 : NEW_LINE INDENT if x [ i - 1 ] == y [ j - 1 ] : NEW_LINE INDENT string += x [ i - 1 ] NEW_LINE i -= 1 NEW_LINE j -= 1 NEW_LINE DEDENT elif dp [ i - 1 ] [ j ] > dp [ i ] [ j - 1 ] : NEW_LINE INDENT string += y [ j - 1 ] NEW_LINE j -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT string += x [ i - 1 ] NEW_LINE i -= 1 NEW_LINE DEDENT DEDENT while i > 0 : NEW_LINE INDENT string += x [ i - 1 ] NEW_LINE i -= 1 NEW_LINE DEDENT while j > 0 : NEW_LINE INDENT string += y [ j - 1 ] NEW_LINE j -= 1 NEW_LINE DEDENT string = list ( string ) NEW_LINE string . reverse ( ) NEW_LINE return ' ' . join ( string ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT x = " AGGTAB " NEW_LINE y = " GXTXAYB " NEW_LINE print ( printShortestSuperSeq ( x , y ) ) NEW_LINE DEDENT