codeparrot/xlcost-text-to-code huggingface dataset:
Matrix Chain Multiplication ( A O ( N ^ 2 ) Solution ) | Matrix Mi has dimension p [ i - 1 ] x p [ i ] for i = 1. . n ; For simplicity of the program , one extra row and one extra column are allocated in dp [ ] [ ] . 0 th row and 0 th column of dp [ ] [ ] are not used ; cost is zero when multiplying one matrix . ; Simply following above recursive formula . ; Driver code
def MatrixChainOrder ( p , n ) : NEW_LINE INDENT dp = [ [ 0 for i in range ( n ) ] for i in range ( n ) ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT dp [ i ] [ i ] = 0 NEW_LINE DEDENT for L in range ( 1 , n - 1 ) : NEW_LINE INDENT for i in range ( n - L ) : NEW_LINE INDENT dp [ i ] [ i + L ] = min ( dp [ i + 1 ] [ i + L ] + p [ i - 1 ] * p [ i ] * p [ i + L ] , dp [ i ] [ i + L - 1 ] + p [ i - 1 ] * p [ i + L - 1 ] * p [ i + L ] ) NEW_LINE DEDENT DEDENT return dp [ 1 ] [ n - 1 ] NEW_LINE DEDENT arr = [ 10 , 20 , 30 , 40 , 30 ] NEW_LINE size = len ( arr ) NEW_LINE print ( " Minimum ▁ number ▁ of ▁ multiplications ▁ is " , MatrixChainOrder ( arr , size ) ) NEW_LINE
Count common subsequence in two strings | return the number of common subsequence in two strings ; for each character of S ; for each character in T ; if character are same in both the string ; Driver Code
def CommomSubsequencesCount ( s , t ) : NEW_LINE INDENT n1 = len ( s ) NEW_LINE n2 = len ( t ) NEW_LINE dp = [ [ 0 for i in range ( n2 + 1 ) ] for i in range ( n1 + 1 ) ] NEW_LINE for i in range ( 1 , n1 + 1 ) : NEW_LINE INDENT for j in range ( 1 , n2 + 1 ) : NEW_LINE INDENT if ( s [ i - 1 ] == t [ j - 1 ] ) : NEW_LINE INDENT dp [ i ] [ j ] = ( 1 + dp [ i ] [ j - 1 ] + dp [ i - 1 ] [ j ] ) NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] [ j ] = ( dp [ i ] [ j - 1 ] + dp [ i - 1 ] [ j ] - dp [ i - 1 ] [ j - 1 ] ) NEW_LINE DEDENT DEDENT DEDENT return dp [ n1 ] [ n2 ] NEW_LINE DEDENT s = " ajblqcpdz " NEW_LINE t = " aefcnbtdi " NEW_LINE print ( CommomSubsequencesCount ( s , t ) ) NEW_LINE
Minimum number of palindromes required to express N as a sum | Set 2 | A utility for creating palindrome ; checks if number of digits is odd or even if odd then neglect the last digit of _input in finding reverse as in case of odd number of digits middle element occur once ; Creates palindrome by just appending reverse of number to itself ; Function to generate palindromes ; Run two times for odd and even length palindromes ; Creates palindrome numbers with first half as i . Value of j decides whether we need an odd length or even length palindrome . ; Function to find the minimum number of palindromes required to express N as a sum ; Checking if the number is a palindrome ; Getting the list of all palindromes upto N ; Sorting the list of palindromes ; The answer is three if the control reaches till this point ; Driver code
def createPalindrome ( _input , isOdd ) : NEW_LINE INDENT n = palin = _input NEW_LINE if isOdd : NEW_LINE INDENT n //= 10 NEW_LINE DEDENT while n > 0 : NEW_LINE INDENT palin = palin * 10 + ( n % 10 ) NEW_LINE n //= 10 NEW_LINE DEDENT return palin NEW_LINE DEDENT def generatePalindromes ( N ) : NEW_LINE INDENT palindromes = [ ] NEW_LINE for j in range ( 0 , 2 ) : NEW_LINE INDENT i = 1 NEW_LINE number = createPalindrome ( i , j ) NEW_LINE while number <= N : NEW_LINE INDENT palindromes . append ( number ) NEW_LINE i += 1 NEW_LINE number = createPalindrome ( i , j ) NEW_LINE DEDENT DEDENT return palindromes NEW_LINE DEDENT def minimumNoOfPalindromes ( N ) : NEW_LINE INDENT b = a = str ( N ) NEW_LINE b = b [ : : - 1 ] NEW_LINE if a == b : NEW_LINE INDENT return 1 NEW_LINE DEDENT palindromes = generatePalindromes ( N ) NEW_LINE palindromes . sort ( ) NEW_LINE l , r = 0 , len ( palindromes ) - 1 NEW_LINE while l < r : NEW_LINE INDENT if palindromes [ l ] + palindromes [ r ] == N : NEW_LINE INDENT return 2 NEW_LINE DEDENT elif palindromes [ l ] + palindromes [ r ] < N : NEW_LINE INDENT l += 1 NEW_LINE DEDENT else : NEW_LINE INDENT r -= 1 NEW_LINE DEDENT DEDENT return 3 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 65 NEW_LINE print ( minimumNoOfPalindromes ( N ) ) NEW_LINE DEDENT
Minimum Cost to make two Numeric Strings Identical | Function to find weight of LCS ; if this state is already calculated then return ; adding required weight for particular match ; recurse for left and right child and store the max ; Function to calculate cost of string ; Driver code ; Minimum cost needed to make two strings identical
def lcs ( dp , a , b , m , n ) : NEW_LINE INDENT for i in range ( 100 ) : NEW_LINE INDENT for j in range ( 100 ) : NEW_LINE INDENT dp [ i ] [ j ] = - 1 NEW_LINE DEDENT DEDENT if ( m < 0 or n < 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( dp [ m ] [ n ] != - 1 ) : NEW_LINE INDENT return dp [ m ] [ n ] NEW_LINE DEDENT ans = 0 NEW_LINE if ( a [ m ] == b [ n ] ) : NEW_LINE INDENT ans = ( ord ( a [ m ] ) - 48 ) + lcs ( dp , a , b , m - 1 , n - 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT ans = max ( lcs ( dp , a , b , m - 1 , n ) , lcs ( dp , a , b , m , n - 1 ) ) NEW_LINE DEDENT dp [ m ] [ n ] = ans NEW_LINE return ans NEW_LINE DEDENT def costOfString ( s ) : NEW_LINE INDENT cost = 0 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT cost += ( ord ( s [ i ] ) - 48 ) NEW_LINE DEDENT return cost NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = "9142" NEW_LINE b = "1429" NEW_LINE dp = [ [ 0 for x in range ( 101 ) ] for y in range ( 101 ) ] NEW_LINE print ( costOfString ( a ) + costOfString ( b ) - 2 * lcs ( dp , a , b , len ( a ) - 1 , len ( b ) - 1 ) ) NEW_LINE DEDENT
Sum of elements of all partitions of number such that no element is less than K | Function that returns total number of valid partitions of integer N ; Global declaration of 2D dp array which will be later used for memoization ; Initializing 2D dp array with - 1 we will use this 2D array for memoization ; If this subproblem is already previously calculated , then directly return that answer ; If N < K , then no valid partition is possible ; If N is between K to 2 * K then there is only one partition and that is the number N itself ; Initialize answer with 1 as the number N itself is always a valid partition ; For loop to iterate over K to N and find number of possible valid partitions recursively . ; Memoization is done by storing this calculated answer ; Returning number of valid partitions ; Driver code ; Printing total number of valid partitions
def countPartitions ( n , k ) : NEW_LINE INDENT dp = [ [ 0 ] * 201 ] * 201 NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT for j in range ( n + 1 ) : NEW_LINE INDENT dp [ i ] [ j ] = - 1 NEW_LINE DEDENT DEDENT if ( dp [ n ] [ k ] >= 0 ) : NEW_LINE INDENT return dp [ n ] [ k ] NEW_LINE DEDENT if ( n < k ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( n < 2 * k ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT answer = 1 NEW_LINE for i in range ( k , n ) : NEW_LINE INDENT answer = ( answer + countPartitions ( n - i , i ) ) NEW_LINE DEDENT dp [ n ] [ k ] = answer NEW_LINE return answer NEW_LINE DEDENT n = 10 NEW_LINE k = 3 NEW_LINE print ( " Total ▁ Aggregate ▁ sum ▁ of ▁ all ▁ " " Valid ▁ Partitions : ▁ " , countPartitions ( n , k ) * n ) NEW_LINE
Number of Co | Python3 program to count the pairs whose sum of digits is co - prime ; Function to find the elements after doing the sum of digits ; Traverse from a to b ; Find the sum of the digits of the elements in the given range one by one ; Function to count the co - prime pairs ; Function to make the pairs by doing the sum of digits ; Count pairs that are co - primes ; Driver code ; Function to count the pairs
from math import gcd NEW_LINE def makePairs ( pairs , a , b ) : NEW_LINE INDENT for i in range ( a , b + 1 , 1 ) : NEW_LINE INDENT sumOfDigits = 0 NEW_LINE k = i NEW_LINE while ( k > 0 ) : NEW_LINE INDENT sumOfDigits += k % 10 NEW_LINE k = int ( k / 10 ) NEW_LINE DEDENT if ( sumOfDigits <= 162 ) : NEW_LINE INDENT pairs . append ( sumOfDigits ) NEW_LINE DEDENT DEDENT DEDENT def countCoPrime ( a , b ) : NEW_LINE INDENT pairs = [ ] NEW_LINE makePairs ( pairs , a , b ) NEW_LINE count = 0 NEW_LINE for i in range ( 0 , len ( pairs ) , 1 ) : NEW_LINE INDENT for j in range ( i + 1 , len ( pairs ) , 1 ) : NEW_LINE INDENT if ( gcd ( pairs [ i ] , pairs [ j ] ) == 1 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = 12 NEW_LINE b = 15 NEW_LINE print ( countCoPrime ( a , b ) ) NEW_LINE DEDENT
Number of Co | Python3 program to count the pairs whose sum of digits is co - prime ; Recursive function to return the frequency of numbers having their sum of digits i ; Returns 1 or 0 ; Returns value of the dp if already stored ; Loop from digit 0 to 9 ; To change the tight to 1 ; Calling the recursive function to find the frequency ; Function to find out frequency of numbers from 1 to N having their sum of digits from 1 to 162 and store them in array ; Number to string conversion ; Calling the recursive function and pushing it into array ; Function to find the pairs ; Calling the formArray function of a - 1 numbers ; Calling the formArray function of b numbers ; Subtracting the frequency of higher number array with lower number array and thus finding the range of numbers from a to b having sum from 1 to 162 ; To find out total number of pairs which are co - prime ; Driver code ; Function to count the pairs
import math NEW_LINE def recursive ( idx , sum , tight , st , dp , num ) : NEW_LINE INDENT if ( idx == num ) : NEW_LINE INDENT return sum == 0 NEW_LINE DEDENT if ( dp [ idx ] [ tight ] [ sum ] != - 1 ) : NEW_LINE INDENT return dp [ idx ] [ tight ] [ sum ] NEW_LINE DEDENT ans = 0 NEW_LINE for d in range ( 10 ) : NEW_LINE INDENT newTight = False NEW_LINE if ( tight and ord ( st [ idx ] ) - ord ( '0' ) < d ) : NEW_LINE INDENT continue NEW_LINE DEDENT if ( tight and ord ( st [ idx ] ) - ord ( '0' ) == d ) : NEW_LINE INDENT newTight = True NEW_LINE DEDENT if ( sum >= d ) : NEW_LINE INDENT ans += recursive ( idx + 1 , sum - d , newTight , st , dp , num ) NEW_LINE DEDENT DEDENT dp [ idx ] [ tight ] [ sum ] = ans NEW_LINE return dp [ idx ] [ tight ] [ sum ] NEW_LINE DEDENT def formArray ( N ) : NEW_LINE INDENT dp = [ [ [ - 1 for x in range ( 166 ) ] for y in range ( 2 ) ] for z in range ( 20 ) ] NEW_LINE st = str ( N ) NEW_LINE num = len ( st ) NEW_LINE arr = [ ] NEW_LINE for i in range ( 1 , 163 ) : NEW_LINE INDENT arr . append ( recursive ( 0 , i , 1 , st , dp , num ) ) NEW_LINE DEDENT return arr NEW_LINE DEDENT def findPair ( a , b ) : NEW_LINE INDENT arr_smaller = formArray ( a - 1 ) NEW_LINE arr_greater = formArray ( b ) NEW_LINE for i in range ( len ( arr_greater ) ) : NEW_LINE INDENT arr_greater [ i ] -= arr_smaller [ i ] NEW_LINE DEDENT ans = 0 NEW_LINE for i in range ( 1 , 163 ) : NEW_LINE INDENT for j in range ( i + 1 , 163 ) : NEW_LINE INDENT if ( math . gcd ( i , j ) == 1 ) : NEW_LINE INDENT ans = ( ans + arr_greater [ i - 1 ] * arr_greater [ j - 1 ] ) NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = 12 NEW_LINE b = 15 NEW_LINE print ( findPair ( a , b ) ) NEW_LINE DEDENT
Semiperfect Number | Python3 program to check if the number is semi - perfect or not ; code to find all the factors of the number excluding the number itself ; vector to store the factors ; note that this loop runs till sqrt ( n ) ; if the value of i is a factor ; condition to check the divisor is not the number itself ; return the vector ; Function to check if the number is semi - perfect or not ; find the divisors ; sorting the vector ; subset to check if no is semiperfect ; initialising 1 st column to true ; initializing 1 st row except zero position to 0 ; loop to find whether the number is semiperfect ; calculation to check if the number can be made by summation of divisors ; if not possible to make the number by any combination of divisors ; Driver Code
import math NEW_LINE def factors ( n ) : NEW_LINE INDENT v = [ ] NEW_LINE v . append ( 1 ) NEW_LINE sqt = int ( math . sqrt ( n ) ) NEW_LINE for i in range ( 2 , sqt + 1 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT v . append ( i ) NEW_LINE if ( n // i != i ) : NEW_LINE INDENT v . append ( n // i ) NEW_LINE DEDENT DEDENT DEDENT return v NEW_LINE DEDENT def check ( n ) : NEW_LINE INDENT v = [ ] NEW_LINE v = factors ( n ) NEW_LINE v . sort ( ) NEW_LINE r = len ( v ) NEW_LINE subset = [ [ 0 for i in range ( n + 1 ) ] for j in range ( r + 1 ) ] NEW_LINE for i in range ( r + 1 ) : NEW_LINE INDENT subset [ i ] [ 0 ] = True NEW_LINE DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT subset [ 0 ] [ i ] = False NEW_LINE DEDENT for i in range ( 1 , r + 1 ) : NEW_LINE INDENT for j in range ( 1 , n + 1 ) : NEW_LINE INDENT if ( j < v [ i - 1 ] ) : NEW_LINE INDENT subset [ i ] [ j ] = subset [ i - 1 ] [ j ] ; NEW_LINE DEDENT else : NEW_LINE INDENT subset [ i ] [ j ] = ( subset [ i - 1 ] [ j ] or subset [ i - 1 ] [ j - v [ i - 1 ] ] ) NEW_LINE DEDENT DEDENT DEDENT if ( ( subset [ r ] [ n ] ) == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT else : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 40 NEW_LINE if ( check ( n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Number of ways to represent a number as sum of k fibonacci numbers | To store fibonacci numbers 42 second number in fibonacci series largest possible integer ; Function to generate fibonacci series ; Recursive function to return the number of ways ; base condition ; for recursive function call ; Driver code
fib = [ 0 ] * 43 NEW_LINE def fibonacci ( ) : NEW_LINE INDENT fib [ 0 ] = 1 NEW_LINE fib [ 1 ] = 2 NEW_LINE for i in range ( 2 , 43 ) : NEW_LINE INDENT fib [ i ] = fib [ i - 1 ] + fib [ i - 2 ] NEW_LINE DEDENT DEDENT def rec ( x , y , last ) : NEW_LINE INDENT if y == 0 : NEW_LINE INDENT if x == 0 : NEW_LINE INDENT return 1 NEW_LINE DEDENT return 0 NEW_LINE DEDENT Sum , i = 0 , last NEW_LINE while i >= 0 and fib [ i ] * y >= x : NEW_LINE INDENT if fib [ i ] > x : NEW_LINE INDENT i -= 1 NEW_LINE continue NEW_LINE DEDENT Sum += rec ( x - fib [ i ] , y - 1 , i ) NEW_LINE i -= 1 NEW_LINE DEDENT return Sum NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT fibonacci ( ) NEW_LINE n , k = 13 , 3 NEW_LINE print ( " Possible ▁ ways ▁ are : " , rec ( n , k , 42 ) ) NEW_LINE DEDENT
Minimum cost to reach the top of the floor by climbing stairs | function to find the minimum cost to reach N - th floor ; declare an array ; base case ; initially to climb till 0 - th or 1 th stair ; iterate for finding the cost ; return the minimum ; Driver Code
def minimumCost ( cost , n ) : NEW_LINE INDENT dp = [ None ] * n NEW_LINE if n == 1 : NEW_LINE INDENT return cost [ 0 ] NEW_LINE DEDENT dp [ 0 ] = cost [ 0 ] NEW_LINE dp [ 1 ] = cost [ 1 ] NEW_LINE for i in range ( 2 , n ) : NEW_LINE INDENT dp [ i ] = min ( dp [ i - 1 ] , dp [ i - 2 ] ) + cost [ i ] NEW_LINE DEDENT return min ( dp [ n - 2 ] , dp [ n - 1 ] ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 16 , 19 , 10 , 12 , 18 ] NEW_LINE n = len ( a ) NEW_LINE print ( minimumCost ( a , n ) ) NEW_LINE DEDENT
Minimum cost to reach the top of the floor by climbing stairs | function to find the minimum cost to reach N - th floor ; traverse till N - th stair ; update the last two stairs value ; Driver Code
def minimumCost ( cost , n ) : NEW_LINE INDENT dp1 = 0 NEW_LINE dp2 = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT dp0 = cost [ i ] + min ( dp1 , dp2 ) NEW_LINE dp2 = dp1 NEW_LINE dp1 = dp0 NEW_LINE DEDENT return min ( dp1 , dp2 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 2 , 5 , 3 , 1 , 7 , 3 , 4 ] NEW_LINE n = len ( a ) NEW_LINE print ( minimumCost ( a , 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 ) ; base condition ; reaches the point ; if the state has been visited previously ; 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 , m , n , dp ) : 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 elif ( dp [ m ] [ n ] != - 1 ) : NEW_LINE INDENT return dp [ m ] [ n ] ; NEW_LINE DEDENT else : NEW_LINE INDENT num = m + n ; NEW_LINE if ( ( num & ( num - 1 ) ) == 0 ) : NEW_LINE INDENT dp [ m ] [ n ] = a [ m ] [ n ] + maxCost ( a , m - 1 , n - 1 , dp ) ; NEW_LINE return dp [ m ] [ n ] ; NEW_LINE DEDENT else : NEW_LINE INDENT dp [ m ] [ n ] = ( a [ m ] [ n ] + max ( maxCost ( a , m - 1 , n , dp ) , maxCost ( a , m , n - 1 , dp ) ) ) ; NEW_LINE return dp [ m ] [ n ] ; NEW_LINE DEDENT DEDENT DEDENT def answer ( a , n ) : NEW_LINE INDENT dp = [ [ 0 for i in range ( size ) ] for j in range ( size ) ] ; NEW_LINE for i in range ( size ) : NEW_LINE INDENT for j in range ( size ) : NEW_LINE INDENT dp [ i ] [ j ] = - 1 ; NEW_LINE DEDENT DEDENT return maxCost ( a , n - 1 , n - 1 , dp ) ; 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
Edit distance and LCS ( Longest Common Subsequence ) | Python 3 program to find Edit Distance ( when only two operations are allowed , insert and delete ) using LCS . ; Find LCS ; Edit distance is delete operations + insert operations . ; Driver Code
def editDistanceWith2Ops ( X , Y ) : NEW_LINE INDENT m = len ( X ) NEW_LINE n = len ( Y ) NEW_LINE L = [ [ 0 for x in range ( n + 1 ) ] for y in range ( m + 1 ) ] 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 ] ) : 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 lcs = L [ m ] [ n ] NEW_LINE return ( m - lcs ) + ( n - lcs ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT X = " abc " NEW_LINE Y = " acd " NEW_LINE print ( editDistanceWith2Ops ( X , Y ) ) NEW_LINE DEDENT
Longest Common Subsequence | DP using Memoization | Returns length of LCS for X [ 0. . m - 1 ] , Y [ 0. . n - 1 ] ; Driver Code ; Find the length of string
def lcs ( X , Y , m , n ) : NEW_LINE INDENT if ( m == 0 or n == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( X [ m - 1 ] == Y [ n - 1 ] ) : NEW_LINE INDENT return 1 + lcs ( X , Y , m - 1 , n - 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT return max ( lcs ( X , Y , m , n - 1 ) , lcs ( X , Y , m - 1 , n ) ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT X = " AGGTAB " NEW_LINE Y = " GXTXAYB " NEW_LINE m = len ( X ) NEW_LINE n = len ( Y ) NEW_LINE print ( " Length ▁ of ▁ LCS : " , lcs ( X , Y , m , n ) ) NEW_LINE DEDENT
Number of different cyclic paths of length N in a tetrahedron | Function to count the number of steps in a tetrahedron ; initially coming to B is B -> B ; cannot reach A , D or C ; iterate for all steps ; recurrence relation ; memoize previous values ; returns steps ; Driver code
def countPaths ( n ) : NEW_LINE INDENT zB = 1 NEW_LINE zADC = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT nzB = zADC * 3 NEW_LINE nzADC = ( zADC * 2 + zB ) NEW_LINE zB = nzB NEW_LINE zADC = nzADC NEW_LINE DEDENT return zB NEW_LINE DEDENT n = 3 NEW_LINE print ( countPaths ( n ) ) NEW_LINE
Number of subsets with sum divisible by m | Use Dynamic Programming to find sum of subsequences . ; Find sum pf array elements ; dp [ i ] [ j ] would be > 0 if arr [ 0. . i - 1 ] has a subsequence with sum equal to j . ; There is always sum equals zero ; Fill up the dp table ; Initialize the counter ; Check if the sum exists ; check sum is divisible by m ; Driver Code
def sumSubSequence ( arr , length , m ) : NEW_LINE INDENT summ = 0 NEW_LINE for i in arr : NEW_LINE INDENT summ += i NEW_LINE DEDENT dp = [ [ 0 for i in range ( summ + 1 ) ] for j in range ( length + 1 ) ] NEW_LINE for i in range ( length + 1 ) : NEW_LINE INDENT dp [ i ] [ 0 ] += 1 NEW_LINE DEDENT for i in range ( 1 , length + 1 ) : NEW_LINE INDENT dp [ i ] [ arr [ i - 1 ] ] += 1 NEW_LINE for j in range ( 1 , summ + 1 ) : NEW_LINE INDENT if dp [ i - 1 ] [ j ] > 0 : NEW_LINE INDENT dp [ i ] [ j ] += 1 NEW_LINE dp [ i ] [ j + arr [ i - 1 ] ] += 1 NEW_LINE DEDENT DEDENT DEDENT count = 0 NEW_LINE for i in range ( 1 , summ + 1 ) : NEW_LINE INDENT if dp [ length ] [ i ] > 0 : NEW_LINE INDENT if i % m == 0 : NEW_LINE INDENT count += dp [ length ] [ i ] NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 ] NEW_LINE m = 3 NEW_LINE length = len ( arr ) NEW_LINE print ( sumSubSequence ( arr , length , m ) ) NEW_LINE DEDENT
Longest Decreasing Subsequence | Function that returns the length of the longest decreasing subsequence ; Initialize LDS with 1 for all index The minimum LDS starting with any element is always 1 ; Compute LDS from every index in bottom up manner ; Select the maximum of all the LDS values ; returns the length of the LDS ; Driver Code
def lds ( arr , n ) : NEW_LINE INDENT lds = [ 0 ] * n NEW_LINE max = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT lds [ i ] = 1 NEW_LINE DEDENT for i in range ( 1 , n ) : NEW_LINE INDENT for j in range ( i ) : NEW_LINE INDENT if ( arr [ i ] < arr [ j ] and lds [ i ] < lds [ j ] + 1 ) : NEW_LINE INDENT lds [ i ] = lds [ j ] + 1 NEW_LINE DEDENT DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT if ( max < lds [ i ] ) : NEW_LINE INDENT max = lds [ i ] NEW_LINE DEDENT DEDENT return max NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 15 , 27 , 14 , 38 , 63 , 55 , 46 , 65 , 85 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Length ▁ of ▁ LDS ▁ is " , lds ( arr , n ) ) NEW_LINE DEDENT
Total number of decreasing paths in a matrix | Python3 program to count number of decreasing path in a matrix ; Function that returns the number of decreasing paths from a cell ( i , j ) ; checking if already calculated ; all possible paths ; counts the total number of paths ; In all four allowed direction . ; new co - ordinates ; Checking if not going out of matrix and next cell value is less than current cell value . ; function that returns the answer ; Function that counts the total decreasing path in the matrix ; Initialising dp [ ] [ ] to - 1. ; Calculating number of decreasing path from each cell . ; Driver Code ; function call that returns the count of decreasing paths in a matrix
MAX = 100 NEW_LINE def CountDecreasingPathsCell ( mat , dp , n , x , y ) : NEW_LINE INDENT if ( dp [ x ] [ y ] != - 1 ) : NEW_LINE INDENT return dp [ x ] [ y ] NEW_LINE DEDENT delta = [ [ 0 , 1 ] , [ 1 , 0 ] , [ - 1 , 0 ] , [ 0 , - 1 ] ] NEW_LINE newx , newy = 0 , 0 NEW_LINE ans = 1 NEW_LINE for i in range ( 4 ) : NEW_LINE INDENT newx = x + delta [ i ] [ 0 ] NEW_LINE newy = y + delta [ i ] [ 1 ] NEW_LINE if ( newx >= 0 and newx < n and newy >= 0 and newy < n and mat [ newx ] [ newy ] < mat [ x ] [ y ] ) : NEW_LINE INDENT ans += CountDecreasingPathsCell ( mat , dp , n , newx , newy ) NEW_LINE DEDENT DEDENT dp [ x ] [ y ] = ans NEW_LINE return dp [ x ] [ y ] NEW_LINE DEDENT def countDecreasingPathsMatrix ( n , mat ) : NEW_LINE INDENT dp = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT l = [ ] NEW_LINE for j in range ( n ) : NEW_LINE INDENT l . append ( - 1 ) NEW_LINE DEDENT dp . append ( l ) NEW_LINE DEDENT sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT sum += CountDecreasingPathsCell ( mat , dp , n , i , j ) NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT n = 2 NEW_LINE mat = [ [ 1 , 2 ] , [ 1 , 3 ] ] NEW_LINE print ( countDecreasingPathsMatrix ( n , mat ) ) NEW_LINE