codeparrot/xlcost-text-to-code huggingface dataset:
Maximum sum alternating subsequence | Return sum of maximum sum alternating sequence starting with arr [ 0 ] and is first decreasing . ; handling the edge case ; Stores sum of decreasing and increasing sub - sequence ; store sum of increasing and decreasing sun - sequence ; As per question , first element must be part of solution . ; Traverse remaining elements of array ; IF current sub - sequence is decreasing the update dec [ j ] if needed . dec [ i ] by current inc [ j ] + arr [ i ] ; Revert the flag , if first decreasing is found ; If next element is greater but flag should be 1 i . e . this element should be counted after the first decreasing element gets counted ; If current sub - sequence is increasing then update inc [ i ] ; Find maximum sum in b / w inc [ ] and dec [ ] ; Return maximum sum alternate sun - sequence ; Driver program
def maxAlternateSum ( arr , n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return arr [ 0 ] NEW_LINE DEDENT min = arr [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( min > arr [ i ] ) : NEW_LINE INDENT min = arr [ i ] NEW_LINE DEDENT DEDENT if ( arr [ 0 ] == min ) : NEW_LINE INDENT return arr [ 0 ] NEW_LINE DEDENT dec = [ 0 for i in range ( n + 1 ) ] NEW_LINE inc = [ 0 for i in range ( n + 1 ) ] NEW_LINE dec [ 0 ] = inc [ 0 ] = arr [ 0 ] NEW_LINE flag = 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT for j in range ( i ) : NEW_LINE INDENT if ( arr [ j ] > arr [ i ] ) : NEW_LINE INDENT dec [ i ] = max ( dec [ i ] , inc [ j ] + arr [ i ] ) NEW_LINE flag = 1 NEW_LINE DEDENT elif ( arr [ j ] < arr [ i ] and flag == 1 ) : NEW_LINE INDENT inc [ i ] = max ( inc [ i ] , dec [ j ] + arr [ i ] ) NEW_LINE DEDENT DEDENT DEDENT result = - 2147483648 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( result < inc [ i ] ) : NEW_LINE INDENT result = inc [ i ] NEW_LINE DEDENT if ( result < dec [ i ] ) : NEW_LINE INDENT result = dec [ i ] NEW_LINE DEDENT DEDENT return result NEW_LINE DEDENT arr = [ 8 , 2 , 3 , 5 , 7 , 9 , 10 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Maximum ▁ sum ▁ = ▁ " , maxAlternateSum ( arr , n ) ) NEW_LINE
Padovan Sequence | Function to calculate padovan number P ( n ) ; 0 th , 1 st and 2 nd number of the series are 1 ; Driver Code
def pad ( n ) : NEW_LINE INDENT pPrevPrev , pPrev , pCurr , pNext = 1 , 1 , 1 , 1 NEW_LINE for i in range ( 3 , n + 1 ) : NEW_LINE INDENT pNext = pPrevPrev + pPrev NEW_LINE pPrevPrev = pPrev NEW_LINE pPrev = pCurr NEW_LINE pCurr = pNext NEW_LINE DEDENT return pNext ; NEW_LINE DEDENT print pad ( 12 ) NEW_LINE
Find length of the largest region in Boolean Matrix | A function to check if a given cell ( row , col ) can be included in DFS ; row number is in range , column number is in range and value is 1 and not yet visited ; A utility function to do DFS for a 2D boolean matrix . It only considers the 8 neighbours as adjacent vertices ; These arrays are used to get row and column numbers of 8 neighbours of a given cell ; Mark this cell as visited ; Recur for all connected neighbours ; increment region length by one ; The main function that returns largest length region of a given boolean 2D matrix ; Make a bool array to mark visited cells . Initially all cells are unvisited ; Initialize result as 0 and travesle through the all cells of given matrix ; If a cell with value 1 is not ; visited yet , then new region found ; maximum region ; Driver Code ; Function call
def isSafe ( M , row , col , visited ) : NEW_LINE INDENT global ROW , COL NEW_LINE return ( ( row >= 0 ) and ( row < ROW ) and ( col >= 0 ) and ( col < COL ) and ( M [ row ] [ col ] and not visited [ row ] [ col ] ) ) NEW_LINE DEDENT def DFS ( M , row , col , visited , count ) : NEW_LINE INDENT rowNbr = [ - 1 , - 1 , - 1 , 0 , 0 , 1 , 1 , 1 ] NEW_LINE colNbr = [ - 1 , 0 , 1 , - 1 , 1 , - 1 , 0 , 1 ] NEW_LINE visited [ row ] [ col ] = True NEW_LINE for k in range ( 8 ) : NEW_LINE INDENT if ( isSafe ( M , row + rowNbr [ k ] , col + colNbr [ k ] , visited ) ) : NEW_LINE INDENT count [ 0 ] += 1 NEW_LINE DFS ( M , row + rowNbr [ k ] , col + colNbr [ k ] , visited , count ) NEW_LINE DEDENT DEDENT DEDENT def largestRegion ( M ) : NEW_LINE INDENT global ROW , COL NEW_LINE visited = [ [ 0 ] * COL for i in range ( ROW ) ] NEW_LINE result = - 999999999999 NEW_LINE for i in range ( ROW ) : NEW_LINE INDENT for j in range ( COL ) : NEW_LINE INDENT if ( M [ i ] [ j ] and not visited [ i ] [ j ] ) : NEW_LINE INDENT count = [ 1 ] NEW_LINE DFS ( M , i , j , visited , count ) NEW_LINE result = max ( result , count [ 0 ] ) NEW_LINE DEDENT DEDENT DEDENT return result NEW_LINE DEDENT ROW = 4 NEW_LINE COL = 5 NEW_LINE M = [ [ 0 , 0 , 1 , 1 , 0 ] , [ 1 , 0 , 1 , 1 , 0 ] , [ 0 , 1 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 , 1 ] ] NEW_LINE print ( largestRegion ( M ) ) NEW_LINE
Lucas Numbers | Iterative function ; declaring base values for positions 0 and 1 ; generating number ; Driver Code
def lucas ( n ) : NEW_LINE INDENT a = 2 NEW_LINE b = 1 NEW_LINE if ( n == 0 ) : NEW_LINE INDENT return a NEW_LINE DEDENT for i in range ( 2 , n + 1 ) : NEW_LINE INDENT c = a + b NEW_LINE a = b NEW_LINE b = c NEW_LINE DEDENT return b NEW_LINE DEDENT n = 9 NEW_LINE print ( lucas ( n ) ) NEW_LINE
Recursively break a number in 3 parts to get maximum sum | A Dynamic programming based Python program to find maximum sum by recursively breaking a number in 3 parts . ; Function to find the maximum sum ; base conditions ; Fill in bottom - up manner using recursive formula . ; Driver program to run the case
MAX = 1000000 NEW_LINE def breakSum ( n ) : NEW_LINE INDENT dp = [ 0 ] * ( n + 1 ) NEW_LINE dp [ 0 ] = 0 NEW_LINE dp [ 1 ] = 1 NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT dp [ i ] = max ( dp [ int ( i / 2 ) ] + dp [ int ( i / 3 ) ] + dp [ int ( i / 4 ) ] , i ) ; NEW_LINE DEDENT return dp [ n ] NEW_LINE DEDENT n = 24 NEW_LINE print ( breakSum ( n ) ) NEW_LINE
Longest repeating and non | Returns the longest repeating non - overlapping substring in str ; building table in bottom - up manner ; ( j - i ) > LCSRe [ i - 1 ] [ j - 1 ] to remove overlapping ; updating maximum length of the substring and updating the finishing index of the suffix ; If we have non - empty result , then insert all characters from first character to last character of string ; Driver Code
def longestRepeatedSubstring ( str ) : NEW_LINE INDENT n = len ( str ) NEW_LINE LCSRe = [ [ 0 for x in range ( n + 1 ) ] for y in range ( n + 1 ) ] NEW_LINE index = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( i + 1 , n + 1 ) : NEW_LINE INDENT if ( str [ i - 1 ] == str [ j - 1 ] and LCSRe [ i - 1 ] [ j - 1 ] < ( j - i ) ) : NEW_LINE INDENT LCSRe [ i ] [ j ] = LCSRe [ i - 1 ] [ j - 1 ] + 1 NEW_LINE if ( LCSRe [ i ] [ j ] > res_length ) : NEW_LINE INDENT res_length = LCSRe [ i ] [ j ] NEW_LINE index = max ( i , index ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT LCSRe [ i ] [ j ] = 0 NEW_LINE DEDENT DEDENT DEDENT if ( res_length > 0 ) : NEW_LINE INDENT for i in range ( index - res_length + 1 , index + 1 ) : NEW_LINE INDENT res = res + str [ i - 1 ] NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str = " geeksforgeeks " NEW_LINE print ( longestRepeatedSubstring ( str ) ) NEW_LINE DEDENT
Minimum cost to fill given weight in a bag | Python3 program to find minimum cost to get exactly W Kg with given packets ; Returns the best obtainable price for a rod of length n and price [ ] as prices of different pieces ; Build the table val [ ] in bottom up manner and return the last entry from the table ; Driver code
import sys NEW_LINE def minCost ( cost , n ) : NEW_LINE INDENT dp = [ 0 for i in range ( n + 1 ) ] NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT min_cost = sys . maxsize NEW_LINE for j in range ( i ) : NEW_LINE INDENT if j < len ( cost ) and cost [ j ] != - 1 : NEW_LINE INDENT min_cost = min ( min_cost , cost [ j ] + dp [ i - j - 1 ] ) NEW_LINE DEDENT DEDENT dp [ i ] = min_cost NEW_LINE DEDENT return dp [ n ] NEW_LINE DEDENT cost = [ 10 , - 1 , - 1 , - 1 , - 1 ] NEW_LINE W = len ( cost ) NEW_LINE print ( minCost ( cost , W ) ) NEW_LINE
Printing Maximum Sum Increasing Subsequence | Utility function to calculate sum of all vector elements ; Function to construct Maximum Sum Increasing Subsequence ; L [ i ] - The Maximum Sum Increasing Subsequence that ends with arr [ i ] ; L [ 0 ] is equal to arr [ 0 ] ; start from index 1 ; for every j less than i ; L [ i ] = { MaxSum ( L [ j ] ) } + arr [ i ] where j < i and arr [ j ] < arr [ i ] ; L [ i ] ends with arr [ i ] ; L [ i ] now stores Maximum Sum Increasing Subsequence of arr [ 0. . i ] that ends with arr [ i ] ;  ; find max ; max will contain result ; Driver Code ; construct and prMax Sum IS of arr
def findSum ( arr ) : NEW_LINE INDENT summ = 0 NEW_LINE for i in arr : NEW_LINE INDENT summ += i NEW_LINE DEDENT return summ NEW_LINE DEDENT def printMaxSumIS ( arr , n ) : NEW_LINE INDENT L = [ [ ] for i in range ( n ) ] NEW_LINE L [ 0 ] . append ( arr [ 0 ] ) NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT for j in range ( i ) : NEW_LINE INDENT if ( ( arr [ i ] > arr [ j ] ) and ( findSum ( L [ i ] ) < findSum ( L [ j ] ) ) ) : NEW_LINE INDENT for e in L [ j ] : NEW_LINE INDENT if e not in L [ i ] : NEW_LINE INDENT L [ i ] . append ( e ) NEW_LINE DEDENT DEDENT DEDENT DEDENT L [ i ] . append ( arr [ i ] ) NEW_LINE DEDENT res = L [ 0 ] NEW_LINE DEDENT / * res = L [ 0 ] ; * / NEW_LINE INDENT for x in L : NEW_LINE INDENT if ( findSum ( x ) > findSum ( res ) ) : NEW_LINE INDENT res = x NEW_LINE DEDENT DEDENT for i in res : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT arr = [ 3 , 2 , 6 , 4 , 5 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE printMaxSumIS ( arr , n ) NEW_LINE
Construction of Longest Increasing Subsequence ( LIS ) and printing LIS sequence | Utility function to print LIS ; Function to construct and print Longest Increasing Subsequence ; L [ i ] - The longest increasing sub - sequence ends with arr [ i ] ; L [ 0 ] is equal to arr [ 0 ] ; start from index 1 ; do for every j less than i ; L [ i ] = { Max ( L [ j ] ) } + arr [ i ] where j < i and arr [ j ] < arr [ i ] ; L [ i ] ends with arr [ i ] ; L [ i ] now stores increasing sub - sequence of arr [ 0. . i ] that ends with arr [ i ] ; LIS will be max of all increasing sub - sequences of arr ; max will contain LIS ; Driver Code ; construct and print LIS of arr
def printLIS ( arr : list ) : NEW_LINE INDENT for x in arr : NEW_LINE INDENT print ( x , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT def constructPrintLIS ( arr : list , n : int ) : NEW_LINE INDENT l = [ [ ] for i in range ( n ) ] NEW_LINE l [ 0 ] . append ( arr [ 0 ] ) NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT for j in range ( i ) : NEW_LINE INDENT if arr [ i ] > arr [ j ] and ( len ( l [ i ] ) < len ( l [ j ] ) + 1 ) : NEW_LINE INDENT l [ i ] = l [ j ] . copy ( ) NEW_LINE DEDENT DEDENT l [ i ] . append ( arr [ i ] ) NEW_LINE DEDENT maxx = l [ 0 ] NEW_LINE for x in l : NEW_LINE INDENT if len ( x ) > len ( maxx ) : NEW_LINE INDENT maxx = x NEW_LINE DEDENT DEDENT printLIS ( maxx ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 3 , 2 , 6 , 4 , 5 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE constructPrintLIS ( arr , n ) NEW_LINE DEDENT
Find if string is K | Returns length of LCS for X [ 0. . m - 1 ] , Y [ 0. . n - 1 ] ; 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 and Y ; find if given string is K - Palindrome or not ; Find reverse of string ; find longest palindromic subsequence of given string ; If the difference between longest palindromic subsequence and the original string is less than equal to k , then the string is k - palindrome ;  ; Driver program
def lcs ( X , Y , m , n ) : NEW_LINE INDENT L = [ [ 0 ] * ( n + 1 ) for _ 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 not i or not j : 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 return L [ m ] [ n ] NEW_LINE DEDENT def isKPal ( string , k ) : NEW_LINE INDENT n = len ( string ) NEW_LINE revStr = string [ : : - 1 ] NEW_LINE lps = lcs ( string , revStr , n , n ) NEW_LINE return ( n - lps <= k ) NEW_LINE DEDENT / * Swap values of left and right * / NEW_LINE string = " abcdeca " NEW_LINE k = 2 NEW_LINE print ( " Yes " if isKPal ( string , k ) else " No " ) NEW_LINE
Wildcard Pattern Matching | Function that matches input strr with given wildcard pattern ; empty pattern can only match with empty string ; lookup table for storing results of subproblems ; empty pattern can match with empty string ; Only ' * ' can match with empty string ; fill the table in bottom - up fashion ; Two cases if we see a ' * ' a ) We ignore a * aTM character and move to next character in the pattern , i . e . , a * aTM indicates an empty sequence . b ) ' * ' character matches with ith character in input ; Current characters are considered as matching in two cases ( a ) current character of pattern is ' ? ' ( b ) characters actually match ; If characters don 't match ; Driver code ; char pattern [ ] = " ba * * * * * ab " char pattern [ ] = " ba * ab " char pattern [ ] = " a * ab " char pattern [ ] = " a * * * * * ab " char pattern [ ] = " * a * * * * * ab " char pattern [ ] = " ba * ab * * * * " char pattern [ ] = " * * * * " char pattern [ ] = " * " char pattern [ ] = " aa ? ab " char pattern [ ] = " b * b " char pattern [ ] = " a * a " char pattern [ ] = " baaabab " char pattern [ ] = " ? baaabab " char pattern [ ] = " * baaaba * "
def strrmatch ( strr , pattern , n , m ) : NEW_LINE INDENT if ( m == 0 ) : NEW_LINE INDENT return ( n == 0 ) NEW_LINE DEDENT lookup = [ [ False for i in range ( m + 1 ) ] for j in range ( n + 1 ) ] NEW_LINE lookup [ 0 ] [ 0 ] = True NEW_LINE for j in range ( 1 , m + 1 ) : NEW_LINE INDENT if ( pattern [ j - 1 ] == ' * ' ) : NEW_LINE INDENT lookup [ 0 ] [ j ] = lookup [ 0 ] [ j - 1 ] NEW_LINE DEDENT DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( 1 , m + 1 ) : NEW_LINE INDENT if ( pattern [ j - 1 ] == ' * ' ) : NEW_LINE INDENT lookup [ i ] [ j ] = lookup [ i ] [ j - 1 ] or lookup [ i - 1 ] [ j ] NEW_LINE DEDENT elif ( pattern [ j - 1 ] == ' ? ' or strr [ i - 1 ] == pattern [ j - 1 ] ) : NEW_LINE INDENT lookup [ i ] [ j ] = lookup [ i - 1 ] [ j - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT lookup [ i ] [ j ] = False NEW_LINE DEDENT DEDENT DEDENT return lookup [ n ] [ m ] NEW_LINE DEDENT strr = " baaabab " NEW_LINE pattern = " * * * * * ba * * * * * ab " NEW_LINE if ( strrmatch ( strr , pattern , len ( strr ) , len ( pattern ) ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Find if string is K | Find if given string is K - Palindrome or not ; If first string is empty , the only option is to remove all characters of second string ; If second string is empty , the only option is to remove all characters of first string ; If last characters of two strings are same , ignore last characters and get count for remaining strings . ; If last characters are not same , 1. Remove last char from str1 and recur for m - 1 and n 2. Remove last char from str2 and recur for m and n - 1 Take minimum of above two operations res = 1 + min ( isKPalRec ( str1 , str2 , m - 1 , n ) , Remove from str1 ( isKPalRec ( str1 , str2 , m , n - 1 ) ) ) Remove from str2 ; Returns true if str is k palindrome . ; Driver program
def isKPalRec ( str1 , str2 , m , n ) : NEW_LINE INDENT if not m : return n NEW_LINE if not n : return m NEW_LINE if str1 [ m - 1 ] == str2 [ n - 1 ] : NEW_LINE INDENT return isKPalRec ( str1 , str2 , m - 1 , n - 1 ) NEW_LINE DEDENT return res NEW_LINE DEDENT def isKPal ( string , k ) : NEW_LINE INDENT revStr = string [ : : - 1 ] NEW_LINE l = len ( string ) NEW_LINE return ( isKPalRec ( string , revStr , l , l ) <= k * 2 ) NEW_LINE DEDENT string = " acdcb " NEW_LINE k = 2 NEW_LINE print ( " Yes " if isKPal ( string , k ) else " No " ) NEW_LINE
Minimum time to finish tasks without skipping two consecutive | arr [ ] represents time taken by n given tasks ; Corner Cases ; Initialize value for the case when there is only one task in task list . First task is included ; First task is exluded ; Process remaining n - 1 tasks ; Time taken if current task is included There are two possibilities ( a ) Previous task is also included ( b ) Previous task is not included ; Time taken when current task is not included . There is only one possibility that previous task is also included . ; Update incl and excl for next iteration ; Return maximum of two values for last task ; Driver code
def minTime ( arr , n ) : NEW_LINE INDENT if ( n <= 0 ) : return 0 NEW_LINE DEDENT incl = arr [ 0 ] NEW_LINE excl = 0 NEW_LINE INDENT for i in range ( 1 , n ) : NEW_LINE INDENT incl_new = arr [ i ] + min ( excl , incl ) NEW_LINE excl_new = incl NEW_LINE incl = incl_new NEW_LINE excl = excl_new NEW_LINE DEDENT return min ( incl , excl ) NEW_LINE DEDENT arr1 = [ 10 , 5 , 2 , 7 , 10 ] NEW_LINE n1 = len ( arr1 ) NEW_LINE print ( minTime ( arr1 , n1 ) ) NEW_LINE arr2 = [ 10 , 5 , 7 , 10 ] NEW_LINE n2 = len ( arr2 ) NEW_LINE print ( minTime ( arr2 , n2 ) ) NEW_LINE arr3 = [ 10 , 5 , 2 , 4 , 8 , 6 , 7 , 10 ] NEW_LINE n3 = len ( arr3 ) NEW_LINE print ( minTime ( arr3 , n3 ) ) NEW_LINE
Matrix Exponentiation | A utility function to multiply two matrices a [ ] [ ] and b [ ] [ ] . Multiplication result is stored back in b [ ] [ ] ; Creating an auxiliary matrix to store elements of the multiplication matrix ; storing the multiplication result in a [ ] [ ] ; Updating our matrix ; Function to compute F raise to power n - 2. ; Multiply it with initial values i . e with F ( 0 ) = 0 , F ( 1 ) = 1 , F ( 2 ) = 1 ; Multiply it with initial values i . e with F ( 0 ) = 0 , F ( 1 ) = 1 , F ( 2 ) = 1 ; Return n 'th term of a series defined using below recurrence relation. f(n) is defined as f(n) = f(n-1) + f(n-2) + f(n-3), n>=3 Base Cases : f(0) = 0, f(1) = 1, f(2) = 1 ; Driver code
def multiply ( a , b ) : NEW_LINE INDENT mul = [ [ 0 for x in range ( 3 ) ] for y in range ( 3 ) ] ; NEW_LINE for i in range ( 3 ) : NEW_LINE INDENT for j in range ( 3 ) : NEW_LINE INDENT mul [ i ] [ j ] = 0 ; NEW_LINE for k in range ( 3 ) : NEW_LINE INDENT mul [ i ] [ j ] += a [ i ] [ k ] * b [ k ] [ j ] ; NEW_LINE DEDENT DEDENT DEDENT for i in range ( 3 ) : NEW_LINE INDENT for j in range ( 3 ) : NEW_LINE a [ i ] [ j ] = mul [ i ] [ j ] ; NEW_LINE DEDENT return a ; NEW_LINE DEDENT def power ( F , n ) : NEW_LINE INDENT M = [ [ 1 , 1 , 1 ] , [ 1 , 0 , 0 ] , [ 0 , 1 , 0 ] ] ; NEW_LINE if ( n == 1 ) : NEW_LINE INDENT return F [ 0 ] [ 0 ] + F [ 0 ] [ 1 ] ; NEW_LINE DEDENT power ( F , int ( n / 2 ) ) ; NEW_LINE F = multiply ( F , F ) ; NEW_LINE if ( n % 2 != 0 ) : NEW_LINE INDENT F = multiply ( F , M ) ; NEW_LINE DEDENT return F [ 0 ] [ 0 ] + F [ 0 ] [ 1 ] ; NEW_LINE DEDENT def findNthTerm ( n ) : NEW_LINE INDENT F = [ [ 1 , 1 , 1 ] , [ 1 , 0 , 0 ] , [ 0 , 1 , 0 ] ] ; NEW_LINE return power ( F , n - 2 ) ; NEW_LINE DEDENT n = 5 ; NEW_LINE print ( " F ( 5 ) ▁ is " , findNthTerm ( n ) ) ; NEW_LINE
Count number of ways to fill a " n ▁ x ▁ 4" grid using "1 ▁ x ▁ 4" tiles | Returns count of count of ways to place 1 x 4 tiles on n x 4 grid . ; Create a table to store results of subproblems dp [ i ] stores count of ways for i x 4 grid . ; Fill the table from d [ 1 ] to dp [ n ] ; Base cases ; dp ( i - 1 ) : Place first tile horizontally dp ( n - 4 ) : Place first tile vertically which means 3 more tiles have to be placed vertically . ; Driver code to test above
def count ( n ) : NEW_LINE INDENT dp = [ 0 for _ in range ( n + 1 ) ] NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT if i <= 3 : NEW_LINE INDENT dp [ i ] = 1 NEW_LINE DEDENT elif i == 4 : NEW_LINE INDENT dp [ i ] = 2 NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] = dp [ i - 1 ] + dp [ i - 4 ] NEW_LINE DEDENT DEDENT return dp [ n ] NEW_LINE DEDENT n = 5 NEW_LINE print ( " Count ▁ of ▁ ways ▁ is " ) , NEW_LINE print ( count ( n ) ) NEW_LINE
Compute nCr % p | Set 1 ( Introduction and Dynamic Programming Solution ) | Returns nCr % p ; Optimization for the cases when r is large compared to n - r ; The array C is going to store last row of pascal triangle at the end . And last entry of last row is nCr . ; Top row of Pascal Triangle ; One by constructs remaining rows of Pascal Triangle from top to bottom ; Fill entries of current row using previous row values ; nCj = ( n - 1 ) Cj + ( n - 1 ) C ( j - 1 ) ; Driver Program
def nCrModp ( n , r , p ) : NEW_LINE INDENT if ( r > n - r ) : NEW_LINE INDENT r = n - r NEW_LINE DEDENT C = [ 0 for i in range ( r + 1 ) ] NEW_LINE C [ 0 ] = 1 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( min ( i , r ) , 0 , - 1 ) : NEW_LINE INDENT C [ j ] = ( C [ j ] + C [ j - 1 ] ) % p NEW_LINE DEDENT DEDENT return C [ r ] NEW_LINE DEDENT n = 10 NEW_LINE r = 2 NEW_LINE p = 13 NEW_LINE print ( ' Value ▁ of ▁ nCr ▁ % ▁ p ▁ is ' , nCrModp ( n , r , p ) ) NEW_LINE
Count Derangements ( Permutation such that no element appears in its original position ) |  ; Base Case ; Variables for storing previous values ; using above recursive formula ; Return result for n ; Driver Code
/ * Function to count NEW_LINE derangements * / NEW_LINE def countDer ( n ) : NEW_LINE INDENT if n == 1 or n == 2 : NEW_LINE return n - 1 ; NEW_LINE a = 0 NEW_LINE b = 1 NEW_LINE for i in range ( 3 , n + 1 ) : NEW_LINE INDENT cur = ( i - 1 ) * ( a + b ) NEW_LINE a = b NEW_LINE b = cur NEW_LINE DEDENT return b NEW_LINE DEDENT n = 4 NEW_LINE print ( " Count ▁ of ▁ Dearrangements ▁ is ▁ " , countDer ( n ) ) NEW_LINE
Bell Numbers ( Number of ways to Partition a Set ) |  ; Explicitly fill for j = 0 ; Fill for remaining values of j ; Driver program
/ * Function to find n ' th Bell Number * / NEW_LINE def bellNumber ( n ) : NEW_LINE INDENT bell = [ [ 0 for i in range ( n + 1 ) ] for j in range ( n + 1 ) ] NEW_LINE bell [ 0 ] [ 0 ] = 1 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT bell [ i ] [ 0 ] = bell [ i - 1 ] [ i - 1 ] NEW_LINE for j in range ( 1 , i + 1 ) : NEW_LINE INDENT bell [ i ] [ j ] = bell [ i - 1 ] [ j - 1 ] + bell [ i ] [ j - 1 ] NEW_LINE DEDENT DEDENT return bell [ n ] [ 0 ] NEW_LINE DEDENT for n in range ( 6 ) : NEW_LINE INDENT print ( ' Bell ▁ Number ' , n , ' is ' , bellNumber ( n ) ) NEW_LINE DEDENT
Count number of ways to cover a distance | Function returns count of ways to cover ' dist ' ; Initialize base values . There is one way to cover 0 and 1 distances and two ways to cover 2 distance ; Fill the count array in bottom up manner ; driver program
def printCountDP ( dist ) : NEW_LINE INDENT count = [ 0 ] * ( dist + 1 ) NEW_LINE count [ 0 ] = 1 NEW_LINE if dist >= 1 : NEW_LINE INDENT count [ 1 ] = 1 NEW_LINE DEDENT if dist >= 2 : NEW_LINE INDENT count [ 2 ] = 2 NEW_LINE DEDENT for i in range ( 3 , dist + 1 ) : NEW_LINE INDENT count [ i ] = ( count [ i - 1 ] + count [ i - 2 ] + count [ i - 3 ] ) NEW_LINE DEDENT return count [ dist ] ; NEW_LINE DEDENT dist = 4 ; NEW_LINE print ( printCountDP ( dist ) ) NEW_LINE
Count even length binary sequences with same sum of first and second half bits | Returns the count of even length sequences ; Calculate SUM ( ( nCr ) ^ 2 ) ; Compute nCr using nC ( r - 1 ) nCr / nC ( r - 1 ) = ( n + 1 - r ) / r ; ; Driver Code
def countSeq ( n ) : NEW_LINE INDENT nCr = 1 NEW_LINE res = 1 NEW_LINE for r in range ( 1 , n + 1 ) : NEW_LINE INDENT nCr = ( nCr * ( n + 1 - r ) ) / r ; NEW_LINE res += nCr * nCr ; NEW_LINE DEDENT return res ; NEW_LINE DEDENT n = 2 NEW_LINE print ( " Count ▁ of ▁ sequences ▁ is " ) , NEW_LINE print ( int ( countSeq ( n ) ) ) NEW_LINE