codeparrot/xlcost-text-to-code huggingface dataset:
Minimum number of operations required to sum to binary string S | Function to return the minimum operations required to sum to a number reprented by the binary string S ; Reverse the string to consider it from LSB to MSB ; initialise the dp table ; If S [ 0 ] = '0' , there is no need to perform any operation ; If S [ 0 ] = '1' , just perform a single operation ( i . e Add 2 ^ 0 ) ; Irrespective of the LSB , dp [ 0 ] [ 1 ] is always 1 as there is always the need of making the suffix of the binary string of the form "11 . . . . 1" as suggested by the definition of dp [ i ] [ 1 ] ; Transition from dp [ i - 1 ] [ 0 ] ; 1. Transition from dp [ i - 1 ] [ 1 ] by just doing 1 extra operation of subtracting 2 ^ i 2. Transition from dp [ i - 1 ] [ 0 ] by just doing 1 extra operation of subtracting 2 ^ ( i + 1 ) ; Transition from dp [ i - 1 ] [ 1 ] ; 1. Transition from dp [ i - 1 ] [ 1 ] by just doing 1 extra operation of adding 2 ^ ( i + 1 ) 2. Transition from dp [ i - 1 ] [ 0 ] by just doing 1 extra operation of adding 2 ^ i ; Driver Code
def findMinOperations ( S ) : NEW_LINE INDENT S = S [ : : - 1 ] NEW_LINE n = len ( S ) NEW_LINE dp = [ [ 0 ] * 2 ] * ( n + 1 ) NEW_LINE if ( S [ 0 ] == '0' ) : NEW_LINE INDENT dp [ 0 ] [ 0 ] = 0 NEW_LINE DEDENT else : NEW_LINE INDENT dp [ 0 ] [ 0 ] = 1 NEW_LINE DEDENT dp [ 0 ] [ 1 ] = 1 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( S [ i ] == '0' ) : NEW_LINE INDENT dp [ i ] [ 0 ] = dp [ i - 1 ] [ 0 ] NEW_LINE dp [ i ] [ 1 ] = 1 + min ( dp [ i - 1 ] [ 1 ] , dp [ i - 1 ] [ 0 ] ) NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] [ 1 ] = dp [ i - 1 ] [ 1 ] ; NEW_LINE dp [ i ] [ 0 ] = 1 + min ( dp [ i - 1 ] [ 0 ] , dp [ i - 1 ] [ 1 ] ) NEW_LINE DEDENT DEDENT return dp [ n - 1 ] [ 0 ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = "100" NEW_LINE print ( findMinOperations ( S ) ) NEW_LINE S = "111" ; NEW_LINE print ( findMinOperations ( S ) ) NEW_LINE DEDENT
K | Function that finds the Nth element of K - Fibonacci series ; If N is less than K then the element is '1 ; first k elements are 1 ; ( K + 1 ) th element is K ; find the elements of the K - Fibonacci series ; subtract the element at index i - k - 1 and add the element at index i - i from the sum ( sum contains the sum of previous ' K ' elements ) ; set the new sum ; Driver code ; get the Nth value of K - Fibonacci series
def solve ( N , K ) : NEW_LINE INDENT Array = [ 0 ] * ( N + 1 ) NEW_LINE DEDENT ' NEW_LINE INDENT if ( N <= K ) : NEW_LINE INDENT print ( "1" ) NEW_LINE return NEW_LINE DEDENT i = 0 NEW_LINE sm = K NEW_LINE for i in range ( 1 , K + 1 ) : NEW_LINE INDENT Array [ i ] = 1 NEW_LINE DEDENT Array [ i + 1 ] = sm NEW_LINE for i in range ( K + 2 , N + 1 ) : NEW_LINE INDENT Array [ i ] = sm - Array [ i - K - 1 ] + Array [ i - 1 ] NEW_LINE sm = Array [ i ] NEW_LINE DEDENT print ( Array [ N ] ) NEW_LINE DEDENT N = 4 NEW_LINE K = 2 NEW_LINE solve ( N , K ) NEW_LINE
Minimum sum possible of any bracket sequence of length N | Python 3 program to find the Minimum sum possible of any bracket sequence of length N using the given values for brackets ; DP array ; Recursive function to check for correct bracket expression ; Not a proper bracket expression ; If reaches at end ; If proper bracket expression ; if not , return max ; If already visited ; To find out minimum sum ; Driver Code
MAX_VAL = 10000000 NEW_LINE dp = [ [ - 1 for i in range ( 100 ) ] for i in range ( 100 ) ] NEW_LINE def find ( index , openbrk , n , adj ) : NEW_LINE INDENT if ( openbrk < 0 ) : NEW_LINE INDENT return MAX_VAL NEW_LINE DEDENT if ( index == n ) : NEW_LINE INDENT if ( openbrk == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT return MAX_VAL NEW_LINE DEDENT DEDENT if ( dp [ index ] [ openbrk ] != - 1 ) : NEW_LINE INDENT return dp [ index ] [ openbrk ] NEW_LINE DEDENT dp [ index ] [ openbrk ] = min ( adj [ index ] [ 1 ] + find ( index + 1 , openbrk + 1 , n , adj ) , adj [ index ] [ 0 ] + find ( index + 1 , openbrk - 1 , n , adj ) ) NEW_LINE return dp [ index ] [ openbrk ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 4 ; NEW_LINE adj = [ [ 5000 , 3000 ] , [ 6000 , 2000 ] , [ 8000 , 1000 ] , [ 9000 , 6000 ] ] NEW_LINE print ( find ( 1 , 1 , n , adj ) + adj [ 0 ] [ 1 ] ) NEW_LINE DEDENT
Understanding The Coin Change Problem With Dynamic Programming | We have input values of N and an array Coins that holds all of the coins . We use data type of because long we want to be able to test large values without integer overflow ; Create the ways array to 1 plus the amount to stop overflow ; Set the first way to 1 because its 0 and there is 1 way to make 0 with 0 coins ; Go through all of the coins ; Make a comparison to each index value of ways with the coin value . ; Update the ways array ; return the value at the Nth position of the ways array . ; Driver code
def getNumberOfWays ( N , Coins ) : NEW_LINE INDENT ways = [ 0 ] * ( N + 1 ) ; NEW_LINE ways [ 0 ] = 1 ; NEW_LINE for i in range ( len ( Coins ) ) : NEW_LINE INDENT for j in range ( len ( ways ) ) : NEW_LINE INDENT if ( Coins [ i ] <= j ) : NEW_LINE INDENT ways [ j ] += ways [ ( int ) ( j - Coins [ i ] ) ] ; NEW_LINE DEDENT DEDENT DEDENT return ways [ N ] ; NEW_LINE DEDENT def printArray ( coins ) : NEW_LINE INDENT for i in coins : NEW_LINE INDENT print ( i ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT Coins = [ 1 , 5 , 10 ] ; NEW_LINE print ( " The ▁ Coins ▁ Array : " ) ; NEW_LINE printArray ( Coins ) ; NEW_LINE print ( " Solution : " , end = " " ) ; NEW_LINE print ( getNumberOfWays ( 12 , Coins ) ) ; NEW_LINE DEDENT
Minimum cost to buy N kilograms of sweet for M persons | Function to find the minimum cost of sweets ; Defining the sweet array ; DP array to store the values ; Since index starts from 1 we reassign the array into sweet ; Assigning base cases for dp array ; At 0 it is free ; Package not available for desirable amount of sweets ; Buying the ' k ' kg package and assigning it to dp array ; If no solution , select from previous k - 1 packages ; If solution does not exist ; Print the solution ; Driver Function ; Calling the desired function
def find ( m , n , adj ) : NEW_LINE INDENT sweet = [ 0 ] * ( n + 1 ) NEW_LINE dp = [ [ [ 0 for i in range ( n + 1 ) ] for i in range ( n + 1 ) ] for i in range ( n + 1 ) ] NEW_LINE sweet [ 0 ] = 0 NEW_LINE for i in range ( 1 , m + 1 ) : NEW_LINE INDENT sweet [ i ] = adj [ i - 1 ] NEW_LINE DEDENT for i in range ( m + 1 ) : NEW_LINE INDENT for k in range ( n + 1 ) : NEW_LINE INDENT dp [ i ] [ 0 ] [ k ] = 0 NEW_LINE DEDENT for k in range ( 1 , n + 1 ) : NEW_LINE INDENT dp [ i ] [ k ] [ 0 ] = - 1 NEW_LINE DEDENT DEDENT for i in range ( m + 1 ) : NEW_LINE INDENT for j in range ( 1 , n + 1 ) : NEW_LINE INDENT for k in range ( 1 , n + 1 ) : NEW_LINE INDENT dp [ i ] [ j ] [ k ] = - 1 NEW_LINE if ( i > 0 and j >= k and sweet [ k ] > 0 and dp [ i - 1 ] [ j - k ] [ k ] != - 1 ) : NEW_LINE INDENT dp [ i ] [ j ] [ k ] = dp [ i - 1 ] [ j - k ] [ k ] + sweet [ k ] NEW_LINE DEDENT if ( dp [ i ] [ j ] [ k ] == - 1 or ( dp [ i ] [ j ] [ k - 1 ] != - 1 and dp [ i ] [ j ] [ k ] > dp [ i ] [ j ] [ k - 1 ] ) ) : NEW_LINE INDENT dp [ i ] [ j ] [ k ] = dp [ i ] [ j ] [ k - 1 ] NEW_LINE DEDENT DEDENT DEDENT DEDENT if ( dp [ m ] [ n ] [ n ] == - 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT return dp [ m ] [ n ] [ n ] NEW_LINE DEDENT DEDENT m = 3 NEW_LINE adj = [ 2 , 1 , 3 , 0 , 4 , 10 ] NEW_LINE n = len ( adj ) NEW_LINE print ( find ( m , n , adj ) ) NEW_LINE
Maximum length of segments of 0 ' s ▁ and ▁ 1' s | Recursive Function to find total length of the array where 1 is greater than zero ; If reaches till end ; If dp is saved ; Finding for each length ; If the character scanned is 1 ; If one is greater than zero , add total length scanned till now ; Continue with next length ; Return the value for start index ; Driver Code ; Size of string ; Calling the function to find the value of function
def find ( start , adj , n , dp ) : NEW_LINE INDENT if ( start == n ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( dp [ start ] != - 1 ) : NEW_LINE INDENT return dp [ start ] NEW_LINE DEDENT dp [ start ] = 0 NEW_LINE one = 0 NEW_LINE zero = 0 NEW_LINE for k in range ( start , n , 1 ) : NEW_LINE INDENT if ( adj [ k ] == '1' ) : NEW_LINE INDENT one += 1 NEW_LINE DEDENT else : NEW_LINE INDENT zero += 1 NEW_LINE DEDENT if ( one > zero ) : NEW_LINE INDENT dp [ start ] = max ( dp [ start ] , find ( k + 1 , adj , n , dp ) + k - start + 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT dp [ start ] = max ( dp [ start ] , find ( k + 1 , adj , n , dp ) ) NEW_LINE DEDENT DEDENT return dp [ start ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT adj = "100110001010001" NEW_LINE n = len ( adj ) NEW_LINE dp = [ - 1 for i in range ( n + 1 ) ] NEW_LINE print ( find ( 0 , adj , n , dp ) ) NEW_LINE DEDENT
Length of longest common subsequence containing vowels | function to check whether ' ch ' is a vowel or not ; function to find the length of longest common subsequence which contains all vowel characters ; 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 ] ; L [ m ] [ n ] contains length of LCS for X [ 0. . n - 1 ] and Y [ 0. . m - 1 ] which contains all vowel characters ; Driver Code
def isVowel ( ch ) : NEW_LINE INDENT if ( ch == ' a ' or ch == ' e ' or ch == ' i ' or ch == ' o ' or ch == ' u ' ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def lcs ( X , Y , m , n ) : NEW_LINE INDENT L = [ [ 0 for i in range ( n + 1 ) ] for j in range ( m + 1 ) ] NEW_LINE i , j = 0 , 0 NEW_LINE for i in range ( m + 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 ] ) and isVowel ( X [ i - 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 return L [ m ] [ n ] NEW_LINE DEDENT X = " aieef " NEW_LINE Y = " klaief " NEW_LINE m = len ( X ) NEW_LINE n = len ( Y ) NEW_LINE print ( " Length ▁ of ▁ LCS ▁ = " , lcs ( X , Y , m , n ) ) NEW_LINE
Minimum number of single digit primes required whose sum is equal to N | function to check if i - th index is valid or not ; function to find the minimum number of single digit prime numbers required which when summed up equals to a given number N . ; Not possible ; Driver Code
def check ( i , val ) : NEW_LINE INDENT if i - val < 0 : NEW_LINE INDENT return False NEW_LINE DEDENT return True NEW_LINE DEDENT def MinimumPrimes ( n ) : NEW_LINE INDENT dp = [ 10 ** 9 ] * ( n + 1 ) NEW_LINE dp [ 0 ] = dp [ 2 ] = dp [ 3 ] = dp [ 5 ] = dp [ 7 ] = 1 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT if check ( i , 2 ) : NEW_LINE INDENT dp [ i ] = min ( dp [ i ] , 1 + dp [ i - 2 ] ) NEW_LINE DEDENT if check ( i , 3 ) : NEW_LINE INDENT dp [ i ] = min ( dp [ i ] , 1 + dp [ i - 3 ] ) NEW_LINE DEDENT if check ( i , 5 ) : NEW_LINE INDENT dp [ i ] = min ( dp [ i ] , 1 + dp [ i - 5 ] ) NEW_LINE DEDENT if check ( i , 7 ) : NEW_LINE INDENT dp [ i ] = min ( dp [ i ] , 1 + dp [ i - 7 ] ) NEW_LINE DEDENT DEDENT if dp [ n ] == 10 ** 9 : NEW_LINE INDENT return - 1 NEW_LINE DEDENT else : NEW_LINE INDENT return dp [ n ] NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 12 NEW_LINE minimal = MinimumPrimes ( n ) NEW_LINE if minimal != - 1 : NEW_LINE INDENT print ( " Minimum ▁ number ▁ of ▁ single ▁ digit ▁ primes ▁ required ▁ : ▁ " , minimal ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ possible " ) NEW_LINE DEDENT DEDENT
Find all distinct subset ( or subsequence ) sums of an array | Set | Function to print all th distinct sum ; Declare a boolean array of size equal to total sum of the array ; Fill the first row beforehand ; dp [ j ] will be true only if sum j can be formed by any possible addition of numbers in given array upto index i , otherwise false ; Iterate from maxSum to 1 and avoid lookup on any other row ; Do not change the dp array for j less than arr [ i ] ; If dp [ j ] is true then print ; Function to find the total sum and print the distinct sum ; find the sum of array elements ; Function to print all the distinct sum ; Driver Code
def subsetSum ( arr , n , maxSum ) : NEW_LINE INDENT dp = [ False for i in range ( maxSum + 1 ) ] NEW_LINE dp [ arr [ 0 ] ] = True NEW_LINE for i in range ( 1 , n , 1 ) : NEW_LINE INDENT j = maxSum NEW_LINE while ( j >= 1 ) : NEW_LINE INDENT if ( arr [ i ] <= j ) : NEW_LINE INDENT if ( arr [ i ] == j or dp [ j ] or dp [ ( j - arr [ i ] ) ] ) : NEW_LINE INDENT dp [ j ] = True NEW_LINE DEDENT else : NEW_LINE INDENT dp [ j ] = False NEW_LINE DEDENT DEDENT j -= 1 NEW_LINE DEDENT DEDENT print ( 0 , end = " ▁ " ) NEW_LINE for j in range ( maxSum + 1 ) : NEW_LINE INDENT if ( dp [ j ] == True ) : NEW_LINE INDENT print ( j , end = " ▁ " ) NEW_LINE DEDENT DEDENT print ( "21" ) NEW_LINE DEDENT def printDistinct ( a , n ) : NEW_LINE INDENT maxSum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT maxSum += a [ i ] NEW_LINE DEDENT subsetSum ( a , n , maxSum ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 3 , 4 , 5 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE printDistinct ( arr , n ) NEW_LINE DEDENT
Sudo Placement [ 1.5 ] | Wolfish | Python program for SP - Wolfish ; Function to find the maxCost of path from ( n - 1 , n - 1 ) to ( 0 , 0 ) | recursive approach ; base condition ; reaches the point ; i + j ; check if it is a power of 2 , then only move diagonally ; if not a power of 2 then move side - wise ; Function to return the maximum cost ; calling dp function to get the answer ; Driver Code ; Function calling to get the answer
size = 1000 NEW_LINE def maxCost ( a : list , m : int , n : int ) -> int : NEW_LINE INDENT if n < 0 or m < 0 : NEW_LINE INDENT return int ( - 1e9 ) NEW_LINE DEDENT elif m == 0 and n == 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT num = m + n NEW_LINE if ( num & ( num - 1 ) ) == 0 : NEW_LINE INDENT return a [ m ] [ n ] + maxCost ( a , m - 1 , n - 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT return a [ m ] [ n ] + max ( maxCost ( a , m - 1 , n ) , maxCost ( a , m , n - 1 ) ) NEW_LINE DEDENT DEDENT DEDENT def answer ( a : list , n : int ) -> int : NEW_LINE INDENT return maxCost ( a , n - 1 , n - 1 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ [ 1 , 2 , 3 , 1 ] , [ 4 , 5 , 6 , 1 ] , [ 7 , 8 , 9 , 1 ] , [ 1 , 1 , 1 , 1 ] ] NEW_LINE n = 4 NEW_LINE print ( answer ( a , n ) ) NEW_LINE DEDENT
Longest Common Subsequence | DP using Memoization | Python3 program to memoize recursive implementation of LCS problem ; Returns length of LCS for X [ 0. . m - 1 ] , Y [ 0. . n - 1 ] memoization applied in recursive solution ; base case ; if the same state has already been computed ; if equal , then we store the value of the function call ; store it in arr to avoid further repetitive work in future function calls ; store it in arr to avoid further repetitive work in future function calls ; Driver Code ; assign - 1 to all positions
maximum = 1000 NEW_LINE def lcs ( X , Y , m , n , dp ) : NEW_LINE INDENT if ( m == 0 or n == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( dp [ m - 1 ] [ n - 1 ] != - 1 ) : NEW_LINE INDENT return dp [ m - 1 ] [ n - 1 ] NEW_LINE DEDENT if ( X [ m - 1 ] == Y [ n - 1 ] ) : NEW_LINE INDENT dp [ m - 1 ] [ n - 1 ] = 1 + lcs ( X , Y , m - 1 , n - 1 , dp ) NEW_LINE return dp [ m - 1 ] [ n - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT dp [ m - 1 ] [ n - 1 ] = max ( lcs ( X , Y , m , n - 1 , dp ) , lcs ( X , Y , m - 1 , n , dp ) ) NEW_LINE return dp [ m - 1 ] [ n - 1 ] NEW_LINE DEDENT DEDENT X = " AGGTAB " NEW_LINE Y = " GXTXAYB " NEW_LINE m = len ( X ) NEW_LINE n = len ( Y ) NEW_LINE dp = [ [ - 1 for i in range ( maximum ) ] for i in range ( m ) ] NEW_LINE print ( " Length ▁ of ▁ LCS : " , lcs ( X , Y , m , n , dp ) ) NEW_LINE
Stepping Numbers | Prints all stepping numbers reachable from num and in range [ n , m ] ; If Stepping Number is in the range [ n , m ] then display ; If Stepping Number is 0 or greater than m , then return ; Get the last digit of the currently visited Stepping Number ; There can be 2 cases either digit to be appended is lastDigit + 1 or lastDigit - 1 ; If lastDigit is 0 then only possible digit after 0 can be 1 for a Stepping Number ; If lastDigit is 9 then only possible digit after 9 can be 8 for a Stepping Number ; Method displays all the stepping numbers in range [ n , m ] ; For every single digit Number ' i ' find all the Stepping Numbers starting with i ; Driver code ; Display Stepping Numbers in the range [ n , m ]
def dfs ( n , m , stepNum ) : NEW_LINE INDENT if ( stepNum <= m and stepNum >= n ) : NEW_LINE INDENT print ( stepNum , end = " ▁ " ) NEW_LINE DEDENT if ( stepNum == 0 or stepNum > m ) : NEW_LINE INDENT return NEW_LINE DEDENT lastDigit = stepNum % 10 NEW_LINE stepNumA = stepNum * 10 + ( lastDigit - 1 ) NEW_LINE stepNumB = stepNum * 10 + ( lastDigit + 1 ) NEW_LINE if ( lastDigit == 0 ) : NEW_LINE INDENT dfs ( n , m , stepNumB ) NEW_LINE DEDENT elif ( lastDigit == 9 ) : NEW_LINE INDENT dfs ( n , m , stepNumA ) NEW_LINE DEDENT else : NEW_LINE INDENT dfs ( n , m , stepNumA ) NEW_LINE dfs ( n , m , stepNumB ) NEW_LINE DEDENT DEDENT def displaySteppingNumbers ( n , m ) : NEW_LINE INDENT for i in range ( 10 ) : NEW_LINE INDENT dfs ( n , m , i ) NEW_LINE DEDENT DEDENT n , m = 0 , 21 NEW_LINE displaySteppingNumbers ( n , m ) NEW_LINE
Memoization ( 1D , 2D and 3D ) | Returns length of LCS for X [ 0. . m - 1 ] , Y [ 0. . n - 1 ] memoization applied in recursive solution ; base case ; if the same state has already been computed ; if equal , then we store the value of the function call ; store it in arr to avoid further repetitive work in future function calls ; store it in arr to avoid further repetitive work in future function calls ;  ; Driver code
def lcs ( X , Y , m , n ) : NEW_LINE INDENT global arr NEW_LINE if ( m == 0 or n == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( arr [ m - 1 ] [ n - 1 ] != - 1 ) : NEW_LINE INDENT return arr [ m - 1 ] [ n - 1 ] NEW_LINE DEDENT if ( X [ m - 1 ] == Y [ n - 1 ] ) : NEW_LINE INDENT arr [ m - 1 ] [ n - 1 ] = 1 + lcs ( X , Y , m - 1 , n - 1 ) NEW_LINE return arr [ m - 1 ] [ n - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT arr [ m - 1 ] [ n - 1 ] = max ( lcs ( X , Y , m , n - 1 ) , lcs ( X , Y , m - 1 , n ) ) NEW_LINE return arr [ m - 1 ] [ n - 1 ] NEW_LINE DEDENT DEDENT / * Utility function to get max of 2 integers * / NEW_LINE arr = [ [ 0 ] * 1000 ] * 1000 NEW_LINE for i in range ( 0 , 1000 ) : NEW_LINE INDENT for j in range ( 0 , 1000 ) : NEW_LINE INDENT arr [ i ] [ j ] = - 1 NEW_LINE DEDENT DEDENT X = " AGGTAB " NEW_LINE Y = " GXTXAYB " NEW_LINE m = len ( X ) NEW_LINE n = len ( Y ) NEW_LINE print ( " Length ▁ of ▁ LCS ▁ is ▁ " , lcs ( X , Y , m , n ) ) NEW_LINE
Number of Unique BST with a given key | Dynamic Programming | Function to find number of unique BST ; DP to store the number of unique BST with key i ; Base case ; fill the dp table in top - down approach . ; n - i in right * i - 1 in left ; Driver Code
def numberOfBST ( n ) : NEW_LINE INDENT dp = [ 0 ] * ( n + 1 ) NEW_LINE dp [ 0 ] , dp [ 1 ] = 1 , 1 NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT for j in range ( 1 , i + 1 ) : NEW_LINE INDENT dp [ i ] = dp [ i ] + ( dp [ i - j ] * dp [ j - 1 ] ) NEW_LINE DEDENT DEDENT return dp [ n ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 3 NEW_LINE print ( " Number ▁ of ▁ structurally ▁ Unique ▁ BST ▁ with " , n , " keys ▁ are ▁ : " , numberOfBST ( n ) ) NEW_LINE DEDENT
Minimum splits in a binary string such that every substring is a power of 4 or 6. | Python 3 program for Minimum splits in a string such that substring is a power of 4 or 6. ; Function to find if given number is power of another number or not . ; Divide given number repeatedly by base value . ; return False not a power ; Function to find minimum number of partitions of given binary string so that each partition is power of 4 or 6. ; DP table to store results of partitioning done at differentindices . ; If the last digit is 1 , hence 4 ^ 0 = 1 and 6 ^ 0 = 1 ; Fix starting position for partition ; Binary representation with leading zeroes is not allowed . ; Iterate for all different partitions starting from i ; Find integer value of current binary partition . ; Check if the value is a power of 4 or 6 or not apply recurrence relation ; If no partitions are possible , then make dp [ i ] = - 1 to represent this . ; Driver code
import sys NEW_LINE def isPowerOf ( val , base ) : NEW_LINE INDENT while ( val > 1 ) : NEW_LINE INDENT if ( val % base != 0 ) : NEW_LINE val //= base NEW_LINE DEDENT return True NEW_LINE DEDENT def numberOfPartitions ( binaryNo ) : NEW_LINE INDENT n = len ( binaryNo ) NEW_LINE dp = [ 0 ] * n NEW_LINE if ( ( ord ( binaryNo [ n - 1 ] ) - ord ( '0' ) ) == 0 ) : NEW_LINE INDENT dp [ n - 1 ] = - 1 NEW_LINE DEDENT else : NEW_LINE INDENT dp [ n - 1 ] = 1 NEW_LINE DEDENT for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT val = 0 NEW_LINE if ( ( ord ( binaryNo [ i ] ) - ord ( '0' ) ) == 0 ) : NEW_LINE INDENT dp [ i ] = - 1 NEW_LINE continue NEW_LINE DEDENT dp [ i ] = sys . maxsize NEW_LINE for j in range ( i , n ) : NEW_LINE INDENT val = ( val * 2 ) + ( ord ( binaryNo [ j ] ) - ord ( '0' ) ) NEW_LINE if ( isPowerOf ( val , 4 ) or isPowerOf ( val , 6 ) ) : NEW_LINE INDENT if ( j == n - 1 ) : NEW_LINE INDENT dp [ i ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT if ( dp [ j + 1 ] != - 1 ) : NEW_LINE INDENT dp [ i ] = min ( dp [ i ] , dp [ j + 1 ] + 1 ) NEW_LINE DEDENT DEDENT DEDENT DEDENT if ( dp [ i ] == sys . maxsize ) : NEW_LINE INDENT dp [ i ] = - 1 NEW_LINE DEDENT DEDENT return dp [ 0 ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT binaryNo = "100110110" NEW_LINE print ( numberOfPartitions ( binaryNo ) ) NEW_LINE DEDENT
Sum of product of r and rth Binomial Coefficient ( r * nCr ) | Return summation of r * nCr ; Driver Code
def summation ( n ) : NEW_LINE INDENT return n << ( n - 1 ) ; NEW_LINE DEDENT n = 2 ; NEW_LINE print ( summation ( n ) ) ; NEW_LINE
Maximum average sum partition of an array | Python3 program for maximum average sum partition ; bottom up approach to calculate score ; storing averages from starting to each i ; ; Driver Code ; atmost partitioning size
MAX = 1000 NEW_LINE memo = [ [ 0.0 for i in range ( MAX ) ] for i in range ( MAX ) ] NEW_LINE def score ( n , A , k ) : NEW_LINE INDENT if ( memo [ n ] [ k ] > 0 ) : NEW_LINE INDENT return memo [ n ] [ k ] NEW_LINE DEDENT sum = 0 NEW_LINE i = n - 1 NEW_LINE while ( i > 0 ) : NEW_LINE INDENT sum += A [ i ] NEW_LINE memo [ n ] [ k ] = max ( memo [ n ] [ k ] , score ( i , A , k - 1 ) + int ( sum / ( n - i ) ) ) NEW_LINE i -= 1 NEW_LINE DEDENT return memo [ n ] [ k ] NEW_LINE DEDENT def largestSumOfAverages ( A , K ) : NEW_LINE INDENT n = len ( A ) NEW_LINE sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += A [ i ] NEW_LINE memo [ i + 1 ] [ 1 ] = int ( sum / ( i + 1 ) ) NEW_LINE DEDENT return score ( n , A , K ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 9 , 1 , 2 , 3 , 9 ] NEW_LINE K = 3 NEW_LINE print ( largestSumOfAverages ( A , K ) ) NEW_LINE DEDENT