codeparrot/xlcost-text-to-code huggingface dataset:
Split the given string into Primes : Digit DP | Function to precompute all the primes upto 1000000 and store it in a set using Sieve of Eratosthenes ; Here str ( ) is used for converting int to string ; A function to find the minimum number of segments the given string can be divided such that every segment is a prime ; Declare a splitdp [ ] array and initialize to - 1 ; Call sieve function to store primes in primes array ; Build the DP table in a bottom - up manner ; If the prefix is prime then the prefix will be found in the prime set ; If the Given Prefix can be split into Primes then for the remaining string from i to j Check if Prime . If yes calculate the minimum split till j ; To check if the substring from i to j is a prime number or not ; If it is a prime , then update the dp array ; Return the minimum number of splits for the entire string ; Driver code
def getPrimesFromSeive ( primes ) : NEW_LINE INDENT prime = [ True ] * ( 1000001 ) NEW_LINE prime [ 0 ] , prime [ 1 ] = False , False NEW_LINE i = 2 NEW_LINE while ( i * i <= 1000000 ) : NEW_LINE INDENT if ( prime [ i ] == True ) : NEW_LINE INDENT for j in range ( i * i , 1000001 , i ) : NEW_LINE INDENT prime [ j ] = False NEW_LINE DEDENT DEDENT i += 1 NEW_LINE DEDENT for i in range ( 2 , 1000001 ) : NEW_LINE INDENT if ( prime [ i ] == True ) : NEW_LINE INDENT primes . append ( str ( i ) ) NEW_LINE DEDENT DEDENT DEDENT def splitIntoPrimes ( number ) : NEW_LINE INDENT numLen = len ( number ) NEW_LINE splitDP = [ - 1 ] * ( numLen + 1 ) NEW_LINE primes = [ ] NEW_LINE getPrimesFromSeive ( primes ) NEW_LINE for i in range ( 1 , numLen + 1 ) : NEW_LINE INDENT if ( i <= 6 and ( number [ 0 : i ] in primes ) ) : NEW_LINE INDENT splitDP [ i ] = 1 NEW_LINE DEDENT if ( splitDP [ i ] != - 1 ) : NEW_LINE INDENT j = 1 NEW_LINE while ( j <= 6 and ( i + j <= numLen ) ) : NEW_LINE INDENT if ( number [ i : i + j ] in primes ) : NEW_LINE INDENT if ( splitDP [ i + j ] == - 1 ) : NEW_LINE INDENT splitDP [ i + j ] = 1 + splitDP [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT splitDP [ i + j ] = min ( splitDP [ i + j ] , 1 + splitDP [ i ] ) NEW_LINE DEDENT DEDENT j += 1 NEW_LINE DEDENT DEDENT DEDENT return splitDP [ numLen ] NEW_LINE DEDENT print ( splitIntoPrimes ( "13499315" ) ) NEW_LINE print ( splitIntoPrimes ( "43" ) ) NEW_LINE
Number of ways to convert a character X to a string Y | Python3 implementation of the approach ; Function to find the modular - inverse ; While power > 1 ; Updating s and a ; Updating power ; Return the final answer ; Function to return the count of ways ; To store the final answer ; To store pre - computed factorials ; Computing factorials ; Loop to find the occurrences of x and update the ans ; Multiplying the answer by ( n - 1 ) ! ; Return the final answer ; Driver code
MOD = 1000000007 ; NEW_LINE def modInv ( a , p = MOD - 2 ) : NEW_LINE INDENT s = 1 ; NEW_LINE while ( p != 1 ) : NEW_LINE INDENT if ( p % 2 ) : NEW_LINE INDENT s = ( s * a ) % MOD ; NEW_LINE DEDENT a = ( a * a ) % MOD ; NEW_LINE p //= 2 ; NEW_LINE DEDENT return ( a * s ) % MOD ; NEW_LINE DEDENT def findCnt ( x , y ) : NEW_LINE INDENT ans = 0 ; NEW_LINE fact = [ 1 ] * ( len ( y ) + 1 ) ; NEW_LINE for i in range ( 1 , len ( y ) ) : NEW_LINE INDENT fact [ i ] = ( fact [ i - 1 ] * i ) % MOD ; NEW_LINE DEDENT for i in range ( len ( y ) ) : NEW_LINE INDENT if ( y [ i ] == x ) : NEW_LINE INDENT ans += ( modInv ( fact [ i ] ) * modInv ( fact [ len ( y ) - i - 1 ] ) ) % MOD ; NEW_LINE ans %= MOD ; NEW_LINE DEDENT DEDENT ans *= fact [ ( len ( y ) - 1 ) ] ; NEW_LINE ans %= MOD ; NEW_LINE return ans ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT x = ' a ' ; NEW_LINE y = " xxayy " ; NEW_LINE print ( findCnt ( x , y ) ) ; NEW_LINE DEDENT
Find the Largest divisor Subset in the Array | Function to find the required subsequence ; Sort the array ; Keep a count of the length of the subsequence and the previous element Set the initial values ; Maximum length of the subsequence and the last element ; Run a loop for every element ; Check for all the divisors ; If the element is a divisor and the length of subsequence will increase by adding j as previous element of i ; Increase the count ; Update the max count ; Get the last index of the subsequence ; Print the element ; Move the index to the previous element ; Driver code
def findSubSeq ( arr , n ) : NEW_LINE INDENT arr . sort ( ) ; NEW_LINE count = [ 1 ] * n ; NEW_LINE prev = [ - 1 ] * n ; NEW_LINE max = 0 ; NEW_LINE maxprev = - 1 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( arr [ i ] % arr [ j ] == 0 and count [ j ] + 1 > count [ i ] ) : NEW_LINE INDENT count [ i ] = count [ j ] + 1 ; NEW_LINE prev [ i ] = j ; NEW_LINE DEDENT DEDENT if ( max < count [ i ] ) : NEW_LINE INDENT max = count [ i ] ; NEW_LINE maxprev = i ; NEW_LINE DEDENT DEDENT i = maxprev ; NEW_LINE while ( i >= 0 ) : NEW_LINE INDENT if ( arr [ i ] != - 1 ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT i = prev [ i ] ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 5 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE findSubSeq ( arr , n ) ; NEW_LINE DEDENT
Minimum number of integers required such that each Segment contains at least one of them | Function to compute minimum number of points which cover all segments ; Sort the list of tuples by their second element . ; To store the solution ; Iterate over all the segments ; Get the start point of next segment ; Loop over all those segments whose start point is less than the end point of current segment ; Print the possibles values of M ; Driver Code ; Starting points of segments ; Ending points of segments ; Insert ranges in points [ ] ; Function Call
def minPoints ( points ) : NEW_LINE INDENT points . sort ( key = lambda x : x [ 1 ] ) NEW_LINE coordinates = [ ] NEW_LINE i = 0 NEW_LINE while i < n : NEW_LINE INDENT seg = points [ i ] [ 1 ] NEW_LINE coordinates . append ( seg ) NEW_LINE p = i + 1 NEW_LINE if p >= n : NEW_LINE INDENT break NEW_LINE DEDENT arrived = points [ p ] [ 0 ] NEW_LINE while seg >= arrived : NEW_LINE INDENT p += 1 NEW_LINE if p >= n : NEW_LINE INDENT break NEW_LINE DEDENT arrived = points [ p ] [ 0 ] NEW_LINE DEDENT i = p NEW_LINE DEDENT for point in coordinates : NEW_LINE INDENT print ( point , end = " ▁ " ) NEW_LINE DEDENT DEDENT n = 4 NEW_LINE start = [ 4 , 1 , 2 , 5 ] NEW_LINE end = [ 7 , 3 , 5 , 6 ] NEW_LINE points = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT tu = ( start [ i ] , end [ i ] ) NEW_LINE points . append ( tu ) NEW_LINE DEDENT minPoints ( points ) NEW_LINE
Optimally accommodate 0 s and 1 s from a Binary String into K buckets | Python3 implementation of the approach ; Function to find the minimum required sum using dynamic programming ; dp [ i ] [ j ] = minimum val of accommodation till j 'th index of the string  using i+1 number of buckets.  Final ans will be in dp[n-1][K-1]  Initialise dp with all states as 0 ; Corner cases ; Filling first row , if only 1 bucket then simple count number of zeros and ones and do the multiplication ; If k = 0 then this arrangement is not possible ; If no arrangement is possible then our answer will remain INT_MAX so return - 1 ; Driver code ; K buckets
import sys NEW_LINE def solve ( Str , K ) : NEW_LINE INDENT n = len ( Str ) NEW_LINE dp = [ [ 0 for i in range ( n ) ] for j in range ( K ) ] NEW_LINE if ( n < K ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT elif ( n == K ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT zeroes = 0 NEW_LINE ones = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( Str [ i ] == '0' ) : NEW_LINE INDENT zeroes += 1 NEW_LINE DEDENT else : NEW_LINE INDENT ones += 1 NEW_LINE DEDENT dp [ 0 ] [ i ] = ones * zeroes NEW_LINE DEDENT for s in range ( 1 , K ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT dp [ s ] [ i ] = sys . maxsize NEW_LINE ones = 0 NEW_LINE zeroes = 0 NEW_LINE for k in range ( i , - 1 , - 1 ) : NEW_LINE INDENT if ( Str [ k ] == '0' ) : NEW_LINE INDENT zeroes += 1 NEW_LINE DEDENT else : NEW_LINE INDENT ones += 1 NEW_LINE DEDENT temp = 0 NEW_LINE if ( k - 1 >= 0 ) : NEW_LINE INDENT temp = ones * zeroes + dp [ s - 1 ] [ k - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT temp = sys . maxsize NEW_LINE DEDENT dp [ s ] [ i ] = min ( dp [ s ] [ i ] , temp ) NEW_LINE DEDENT DEDENT DEDENT if ( dp [ K - 1 ] [ n - 1 ] == sys . maxsize ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT else : NEW_LINE INDENT return dp [ K - 1 ] [ n - 1 ] NEW_LINE DEDENT DEDENT S = "0101" NEW_LINE K = 2 NEW_LINE print ( solve ( S , K ) ) NEW_LINE
Queries for bitwise AND in the index range [ L , R ] of the given array | Python3 implementation of the approach ; Array to store bit - wise prefix count ; Function to find the prefix sum ; Loop for each bit ; Loop to find prefix count ; Function to answer query ; To store the answer ; Loop for each bit ; To store the number of variables with ith bit set ; Condition for ith bit of answer to be set ; Driver code
import numpy as np NEW_LINE MAX = 100000 NEW_LINE bitscount = 32 NEW_LINE prefix_count = np . zeros ( ( bitscount , MAX ) ) ; NEW_LINE def findPrefixCount ( arr , n ) : NEW_LINE INDENT for i in range ( 0 , bitscount ) : NEW_LINE INDENT prefix_count [ i ] [ 0 ] = ( ( arr [ 0 ] >> i ) & 1 ) ; NEW_LINE for j in range ( 1 , n ) : NEW_LINE INDENT prefix_count [ i ] [ j ] = ( ( arr [ j ] >> i ) & 1 ) ; NEW_LINE prefix_count [ i ] [ j ] += prefix_count [ i ] [ j - 1 ] ; NEW_LINE DEDENT DEDENT DEDENT def rangeOr ( l , r ) : NEW_LINE INDENT ans = 0 ; NEW_LINE for i in range ( bitscount ) : NEW_LINE INDENT x = 0 ; NEW_LINE if ( l == 0 ) : NEW_LINE INDENT x = prefix_count [ i ] [ r ] ; NEW_LINE DEDENT else : NEW_LINE INDENT x = prefix_count [ i ] [ r ] - prefix_count [ i ] [ l - 1 ] ; NEW_LINE DEDENT if ( x == r - l + 1 ) : NEW_LINE INDENT ans = ( ans | ( 1 << i ) ) ; NEW_LINE DEDENT DEDENT return ans ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 7 , 5 , 3 , 5 , 2 , 3 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE findPrefixCount ( arr , n ) ; NEW_LINE queries = [ [ 1 , 3 ] , [ 4 , 5 ] ] ; NEW_LINE q = len ( queries ) ; NEW_LINE for i in range ( q ) : NEW_LINE INDENT print ( rangeOr ( queries [ i ] [ 0 ] , queries [ i ] [ 1 ] ) ) ; NEW_LINE DEDENT DEDENT
Longest path in a directed Acyclic graph | Dynamic Programming | Function to traverse the DAG and apply Dynamic Programming to find the longest path ; Mark as visited ; Traverse for all its children ; If not visited ; Store the max of the paths ; Function to add an edge ; Function that returns the longest path ; Dp array ; Visited array to know if the node has been visited previously or not ; Call DFS for every unvisited vertex ; Traverse and find the maximum of all dp [ i ] ; Driver Code ; Example - 1
def dfs ( node , adj , dp , vis ) : NEW_LINE INDENT vis [ node ] = True NEW_LINE for i in range ( 0 , len ( adj [ node ] ) ) : NEW_LINE INDENT if not vis [ adj [ node ] [ i ] ] : NEW_LINE INDENT dfs ( adj [ node ] [ i ] , adj , dp , vis ) NEW_LINE DEDENT dp [ node ] = max ( dp [ node ] , 1 + dp [ adj [ node ] [ i ] ] ) NEW_LINE DEDENT DEDENT def addEdge ( adj , u , v ) : NEW_LINE INDENT adj [ u ] . append ( v ) NEW_LINE DEDENT def findLongestPath ( adj , n ) : NEW_LINE INDENT dp = [ 0 ] * ( n + 1 ) NEW_LINE vis = [ False ] * ( n + 1 ) NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT if not vis [ i ] : NEW_LINE INDENT dfs ( i , adj , dp , vis ) NEW_LINE DEDENT DEDENT ans = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT ans = max ( ans , dp [ i ] ) NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 5 NEW_LINE adj = [ [ ] for i in range ( n + 1 ) ] NEW_LINE addEdge ( adj , 1 , 2 ) NEW_LINE addEdge ( adj , 1 , 3 ) NEW_LINE addEdge ( adj , 3 , 2 ) NEW_LINE addEdge ( adj , 2 , 4 ) NEW_LINE addEdge ( adj , 3 , 4 ) NEW_LINE print ( findLongestPath ( adj , n ) ) NEW_LINE DEDENT
Largest subset of rectangles such that no rectangle fit in any other rectangle | Recursive function to get the largest subset ; Base case when it exceeds ; If the state has been visited previously ; Initialize ; No elements in subset yet ; First state which includes current index ; Second state which does not include current index ; If the rectangle fits in , then do not include the current index in subset ; First state which includes current index ; Second state which does not include current index ; Function to get the largest subset ; Sort the array ; Get the answer ; Driver code ; ( height , width ) pairs
def findLongest ( a , n , present , previous ) : NEW_LINE INDENT if present == n : NEW_LINE INDENT return 0 NEW_LINE DEDENT elif previous != - 1 : NEW_LINE INDENT if dp [ present ] [ previous ] != - 1 : NEW_LINE INDENT return dp [ present ] [ previous ] NEW_LINE DEDENT DEDENT ans = 0 NEW_LINE if previous == - 1 : NEW_LINE INDENT ans = 1 + findLongest ( a , n , present + 1 , present ) NEW_LINE ans = max ( ans , findLongest ( a , n , present + 1 , previous ) ) NEW_LINE DEDENT else : NEW_LINE INDENT h1 = a [ previous ] [ 0 ] NEW_LINE h2 = a [ present ] [ 0 ] NEW_LINE w1 = a [ previous ] [ 1 ] NEW_LINE w2 = a [ present ] [ 1 ] NEW_LINE if h1 <= h2 and w1 <= w2 : NEW_LINE INDENT ans = max ( ans , findLongest ( a , n , present + 1 , previous ) ) NEW_LINE DEDENT else : NEW_LINE INDENT ans = 1 + findLongest ( a , n , present + 1 , present ) NEW_LINE ans = max ( ans , findLongest ( a , n , present + 1 , previous ) ) NEW_LINE DEDENT DEDENT dp [ present ] [ previous ] = ans NEW_LINE return ans NEW_LINE DEDENT def getLongest ( a , n ) : NEW_LINE INDENT a . sort ( ) NEW_LINE ans = findLongest ( a , n , 0 , - 1 ) NEW_LINE return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ [ 1 , 5 ] , [ 2 , 4 ] , [ 1 , 1 ] , [ 3 , 3 ] ] NEW_LINE N = 10 NEW_LINE dp = [ [ - 1 for i in range ( N ) ] for j in range ( N ) ] NEW_LINE n = len ( a ) NEW_LINE print ( getLongest ( a , n ) ) NEW_LINE DEDENT
Maximum length subsequence such that adjacent elements in the subsequence have a common factor | Python3 implementation of the above approach ; to compute least prime divisor of i ; Function that returns the maximum length subsequence such that adjacent elements have a common factor . ; Initialize dp array with 1. ; p has appeared at least once . ; Update latest occurrence of prime p . ; Take maximum value as the answer . ; Driver code
import math as mt NEW_LINE N = 100005 NEW_LINE MAX = 1000002 NEW_LINE lpd = [ 0 for i in range ( MAX ) ] NEW_LINE def preCompute ( ) : NEW_LINE INDENT lpd [ 0 ] , lpd [ 1 ] = 1 , 1 NEW_LINE for i in range ( 2 , mt . ceil ( mt . sqrt ( MAX ) ) ) : NEW_LINE INDENT for j in range ( 2 * i , MAX , i ) : NEW_LINE INDENT if ( lpd [ j ] == 0 ) : NEW_LINE INDENT lpd [ j ] = i NEW_LINE DEDENT DEDENT DEDENT for i in range ( 2 , MAX ) : NEW_LINE INDENT if ( lpd [ i ] == 0 ) : NEW_LINE INDENT lpd [ i ] = i NEW_LINE DEDENT DEDENT DEDENT def maxLengthSubsequence ( arr , n ) : NEW_LINE INDENT dp = [ 1 for i in range ( N + 1 ) ] NEW_LINE pos = dict ( ) NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT while ( arr [ i ] > 1 ) : NEW_LINE INDENT p = lpd [ arr [ i ] ] NEW_LINE if ( p in pos . keys ( ) ) : NEW_LINE INDENT dp [ i ] = max ( dp [ i ] , 1 + dp [ pos [ p ] ] ) NEW_LINE DEDENT pos [ p ] = i NEW_LINE while ( arr [ i ] % p == 0 ) : NEW_LINE INDENT arr [ i ] //= p NEW_LINE DEDENT DEDENT DEDENT ans = 1 NEW_LINE for i in range ( 0 , n + 1 ) : NEW_LINE INDENT ans = max ( ans , dp [ i ] ) NEW_LINE DEDENT return ans NEW_LINE DEDENT arr = [ 13 , 2 , 8 , 6 , 3 , 1 , 9 ] NEW_LINE n = len ( arr ) NEW_LINE preCompute ( ) NEW_LINE print ( maxLengthSubsequence ( arr , n ) ) NEW_LINE
Number of ways to partition a string into two balanced subsequences | For maximum length of input string ; Declaring the DP table ; Function to calculate the number of valid assignments ; Return 1 if both subsequences are balanced ; Increment the count if it is an opening bracket ; Decrement the count if it a closing bracket ; Driver code ; Initial value for c_x and c_y is zero
MAX = 10 NEW_LINE F = [ [ [ - 1 for i in range ( MAX ) ] for j in range ( MAX ) ] for k in range ( MAX ) ] NEW_LINE def noOfAssignments ( S , n , i , c_x , c_y ) : NEW_LINE INDENT if F [ i ] [ c_x ] [ c_y ] != - 1 : NEW_LINE INDENT return F [ i ] [ c_x ] [ c_y ] NEW_LINE DEDENT if i == n : NEW_LINE INDENT F [ i ] [ c_x ] [ c_y ] = not c_x and not c_y NEW_LINE return F [ i ] [ c_x ] [ c_y ] NEW_LINE DEDENT if S [ i ] == ' ( ' : NEW_LINE INDENT F [ i ] [ c_x ] [ c_y ] = noOfAssignments ( S , n , i + 1 , c_x + 1 , c_y ) + noOfAssignments ( S , n , i + 1 , c_x , c_y + 1 ) NEW_LINE return F [ i ] [ c_x ] [ c_y ] NEW_LINE DEDENT F [ i ] [ c_x ] [ c_y ] = 0 NEW_LINE if c_x : NEW_LINE INDENT F [ i ] [ c_x ] [ c_y ] += noOfAssignments ( S , n , i + 1 , c_x - 1 , c_y ) NEW_LINE DEDENT if c_y : NEW_LINE INDENT F [ i ] [ c_x ] [ c_y ] += noOfAssignments ( S , n , i + 1 , c_x , c_y - 1 ) NEW_LINE DEDENT return F [ i ] [ c_x ] [ c_y ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = " ( ( ) ) " NEW_LINE n = len ( S ) NEW_LINE print ( noOfAssignments ( S , n , 0 , 0 , 0 ) ) NEW_LINE DEDENT
Gould 's Sequence | Utility function to count odd numbers in ith row of Pascals 's triangle ; Initialize count as zero ; Return 2 ^ count ; Function to generate gould 's Sequence ; loop to generate gould 's  Sequence up to n ; Driver code ; Get n ; Function call
def countOddNumber ( row_num ) : NEW_LINE INDENT count = 0 NEW_LINE while row_num != 0 : NEW_LINE INDENT count += row_num & 1 NEW_LINE row_num >>= 1 NEW_LINE DEDENT return ( 1 << count ) NEW_LINE DEDENT def gouldSequence ( n ) : NEW_LINE INDENT for row_num in range ( 0 , n ) : NEW_LINE INDENT print ( countOddNumber ( row_num ) , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 16 NEW_LINE gouldSequence ( n ) NEW_LINE DEDENT
Count numbers ( smaller than or equal to N ) with given digit sum | N can be max 10 ^ 18 and hence digitsum will be 162 maximum . ; If sum_so_far equals to given sum then return 1 else 0 ; Our constructed number should not become greater than N . ; If tight is true then it will also be true for ( i + 1 ) digit . ; Driver code
def solve ( i , tight , sum_so_far , Sum , number , length ) : NEW_LINE INDENT if i == length : NEW_LINE INDENT if sum_so_far == Sum : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT ans = dp [ i ] [ tight ] [ sum_so_far ] NEW_LINE if ans != - 1 : NEW_LINE INDENT return ans NEW_LINE DEDENT ans = 0 NEW_LINE for currdigit in range ( 0 , 10 ) : NEW_LINE INDENT currdigitstr = str ( currdigit ) NEW_LINE if not tight and currdigitstr > number [ i ] : NEW_LINE INDENT break NEW_LINE DEDENT ntight = tight or currdigitstr < number [ i ] NEW_LINE nsum_so_far = sum_so_far + currdigit NEW_LINE ans += solve ( i + 1 , ntight , nsum_so_far , Sum , number , length ) NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT count , Sum = 0 , 4 NEW_LINE number = "100" NEW_LINE dp = [ [ [ - 1 for i in range ( 162 ) ] for j in range ( 2 ) ] for k in range ( 18 ) ] NEW_LINE print ( solve ( 0 , 0 , 0 , Sum , number , len ( number ) ) ) NEW_LINE DEDENT
Longest dividing subsequence | lds ( ) returns the length of the longest dividing subsequence in arr [ ] of size n ; Compute optimized lds values in bottom up manner ; Return maximum value in lds [ ] ; Driver Code
def lds ( arr , n ) : NEW_LINE INDENT lds = [ 0 for i in range ( n ) ] NEW_LINE lds [ 0 ] = 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT lds [ i ] = 1 NEW_LINE for j in range ( i ) : NEW_LINE INDENT if ( lds [ j ] != 0 and arr [ i ] % arr [ j ] == 0 ) : NEW_LINE INDENT lds [ i ] = max ( lds [ i ] , lds [ j ] + 1 ) NEW_LINE DEDENT DEDENT DEDENT return max ( lds ) NEW_LINE DEDENT arr = [ 2 , 11 , 16 , 12 , 36 , 60 , 71 , 17 , 29 , 144 , 288 , 129 , 432 , 993 ] NEW_LINE print ( " Length ▁ of ▁ lds ▁ is " , lds ( arr , len ( arr ) ) ) 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 , Z , m , n , o ) : NEW_LINE INDENT global arr NEW_LINE if ( m == 0 or n == 0 or o == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( arr [ m - 1 ] [ n - 1 ] [ o - 1 ] != - 1 ) : NEW_LINE INDENT return arr [ m - 1 ] [ n - 1 ] [ o - 1 ] NEW_LINE DEDENT if ( X [ m - 1 ] == Y [ n - 1 ] and Y [ n - 1 ] == Z [ o - 1 ] ) : NEW_LINE INDENT arr [ m - 1 ] [ n - 1 ] [ o - 1 ] = 1 + lcs ( X , Y , Z , m - 1 , n - 1 , o - 1 ) NEW_LINE return arr [ m - 1 ] [ n - 1 ] [ o - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT arr [ m - 1 ] [ n - 1 ] [ o - 1 ] = max ( lcs ( X , Y , Z , m , n - 1 , o ) , max ( lcs ( X , Y , Z , m - 1 , n , o ) , lcs ( X , Y , Z , m , n , o - 1 ) ) ) NEW_LINE return arr [ m - 1 ] [ n - 1 ] [ o - 1 ] NEW_LINE DEDENT DEDENT arr = [ [ [ 0 for k in range ( 100 ) ] for j in range ( 100 ) ] for i in range ( 100 ) ] NEW_LINE for i in range ( 100 ) : NEW_LINE INDENT for j in range ( 100 ) : NEW_LINE INDENT for k in range ( 100 ) : NEW_LINE INDENT arr [ i ] [ j ] [ k ] = - 1 NEW_LINE DEDENT DEDENT DEDENT X = " geeks " NEW_LINE Y = " geeksfor " NEW_LINE Z = " geeksforgeeks " NEW_LINE m = len ( X ) NEW_LINE n = len ( Y ) NEW_LINE o = len ( Z ) NEW_LINE print ( " Length ▁ of ▁ LCS ▁ is ▁ " , lcs ( X , Y , Z , m , n , o ) ) NEW_LINE
Find the probability of reaching all points after N moves from point N | Function to calculate the probabilities ; Array where row represent the pass and the column represents the points on the line ; Initially the person can reach left or right with one move ; Calculate probabilities for N - 1 moves ; When the person moves from ith index in right direction when i moves has been done ; When the person moves from ith index in left direction when i moves has been done ; Print the arr ; Driver code
def printProbabilities ( n , left ) : NEW_LINE INDENT right = 1 - left ; NEW_LINE arr = [ [ 0 for j in range ( 2 * n + 1 ) ] for i in range ( n + 1 ) ] NEW_LINE arr [ 1 ] [ n + 1 ] = right ; NEW_LINE arr [ 1 ] [ n - 1 ] = left ; NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT for j in range ( 1 , 2 * n + 1 ) : NEW_LINE INDENT arr [ i ] [ j ] += ( arr [ i - 1 ] [ j - 1 ] * right ) ; NEW_LINE DEDENT for j in range ( 2 * n - 1 , - 1 , - 1 ) : NEW_LINE INDENT arr [ i ] [ j ] += ( arr [ i - 1 ] [ j + 1 ] * left ) ; NEW_LINE DEDENT DEDENT for i in range ( 2 * n + 1 ) : NEW_LINE INDENT print ( " { :5.4f } ▁ " . format ( arr [ n ] [ i ] ) , end = ' ▁ ' ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 2 ; NEW_LINE left = 0.5 ; NEW_LINE printProbabilities ( n , left ) ; NEW_LINE DEDENT