codeparrot/xlcost-text-to-code huggingface dataset:
Sum of cost of all paths to reach a given cell in a Matrix | Python3 implementation to find the sum of cost of all paths to reach ( M , N ) ; Function for computing combination ; Function to find the factorial of N ; Loop to find the factorial of a given number ; Function for coumputing the sum of all path cost ; Loop to find the contribution of each ( i , j ) in the all possible ways ; Count number of times ( i , j ) visited ; Add the contribution of grid [ i ] [ j ] in the result ; Driver code ; Function Call
Col = 3 ; NEW_LINE def nCr ( n , r ) : NEW_LINE INDENT return fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ; NEW_LINE DEDENT def fact ( n ) : NEW_LINE INDENT res = 1 ; NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT res = res * i ; NEW_LINE DEDENT return res ; NEW_LINE DEDENT def sumPathCost ( grid , m , n ) : NEW_LINE INDENT sum = 0 ; NEW_LINE count = 0 ; NEW_LINE for i in range ( 0 , m + 1 ) : NEW_LINE INDENT for j in range ( 0 , n + 1 ) : NEW_LINE INDENT count = ( nCr ( i + j , i ) * nCr ( m + n - i - j , m - i ) ) ; NEW_LINE sum += count * grid [ i ] [ j ] ; NEW_LINE DEDENT DEDENT return sum ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT m = 2 ; NEW_LINE n = 2 ; NEW_LINE grid = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] ; NEW_LINE print ( int ( sumPathCost ( grid , m , n ) ) ) ; NEW_LINE DEDENT
Count numbers in a range with digit sum divisible by K having first and last digit different | Python3 program to count numbers in a range with digit sum divisible by K having first and last digit different ; For calculating the upper bound of the sequence . ; Checking whether the sum of digits = 0 or not and the corner case as number equal to 1 ; If the state is visited then return the answer of this state directly . ; For checking whether digit to be placed is up to upper bound at the position pos or upto 9 ; Calculating new digit sum modulo k ; Check if there is a prefix of 0 s and current digit != 0 ; Then current digit will be the starting digit ; Update the boolean flag that the starting digit has been found ; At n - 1 , check if current digit and starting digit are the same then no need to calculate this answer ; Else find the answer ; Function to find the required count ; Setting up the upper bound ; calculating F ( R ) ; Setting up the upper bound ; Calculating F ( L - 1 ) ; Driver code
K = 0 NEW_LINE N = 0 NEW_LINE v = [ ] NEW_LINE dp = [ [ [ [ [ - 1 for i in range ( 2 ) ] for j in range ( 2 ) ] for k in range ( 10 ) ] for l in range ( 1000 ) ] for m in range ( 20 ) ] NEW_LINE def init ( x ) : NEW_LINE INDENT global K NEW_LINE global N NEW_LINE global v NEW_LINE v = [ ] NEW_LINE while ( x > 0 ) : NEW_LINE INDENT v . append ( x % 10 ) NEW_LINE x //= 10 NEW_LINE DEDENT v = v [ : : - 1 ] NEW_LINE N = len ( v ) NEW_LINE DEDENT def fun ( pos , sum , st , check , f ) : NEW_LINE INDENT global N NEW_LINE global v NEW_LINE if ( pos == N ) : NEW_LINE INDENT return ( sum == 0 and check == 1 ) NEW_LINE DEDENT if ( dp [ pos ] [ sum ] [ st ] [ check ] [ f ] != - 1 ) : NEW_LINE INDENT return dp [ pos ] [ sum ] [ st ] [ check ] [ f ] NEW_LINE DEDENT lmt = 9 NEW_LINE if ( f == False ) : NEW_LINE INDENT lmt = v [ pos ] NEW_LINE DEDENT ans = 0 NEW_LINE for digit in range ( lmt + 1 ) : NEW_LINE INDENT nf = f NEW_LINE new_sum = ( sum + digit ) % K NEW_LINE new_check = check NEW_LINE new_st = st NEW_LINE if ( f == 0 and digit < lmt ) : NEW_LINE INDENT nf = 1 NEW_LINE DEDENT if ( check == 0 and digit != 0 ) : NEW_LINE INDENT new_st = digit NEW_LINE new_check = 1 NEW_LINE DEDENT if ( pos == N - 1 and new_st == digit ) : NEW_LINE INDENT continue NEW_LINE DEDENT ans += fun ( pos + 1 , new_sum , new_st , new_check , nf ) NEW_LINE dp [ pos ] [ sum ] [ st ] [ check ] [ f ] = ans NEW_LINE DEDENT return ans NEW_LINE DEDENT def findCount ( L , R , K ) : NEW_LINE INDENT init ( R ) NEW_LINE r_ans = fun ( 0 , 0 , 0 , 0 , 0 ) NEW_LINE init ( L - 1 ) NEW_LINE r_ans = 0 NEW_LINE l_ans = fun ( 0 , 0 , 0 , 0 , 0 ) NEW_LINE print ( l_ans - r_ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT L = 10 NEW_LINE R = 20 NEW_LINE K = 2 NEW_LINE findCount ( L , R , K ) NEW_LINE DEDENT
Split a binary string into K subsets minimizing sum of products of occurrences of 0 and 1 | Python3 program to split a given String into K segments such that the sum of product of occurence of characters in them is minimized ; Function to return the minimum sum of products of occurences of 0 and 1 in each segments ; Store the length of the String ; Not possible to generate subsets greater than the length of String ; If the number of subsets equals the length ; Precompute the sum of products for all index ; Calculate the minimum sum of products for K subsets ; Driver code
import sys NEW_LINE def minSumProd ( S , K ) : NEW_LINE INDENT Len = len ( S ) ; NEW_LINE if ( K > Len ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT if ( K == Len ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT dp = [ 0 ] * Len ; NEW_LINE count_zero = 0 ; NEW_LINE count_one = 0 ; NEW_LINE for j in range ( 0 , Len , 1 ) : NEW_LINE INDENT if ( S [ j ] == '0' ) : NEW_LINE INDENT count_zero += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT count_one += 1 ; NEW_LINE DEDENT dp [ j ] = count_zero * count_one ; NEW_LINE DEDENT for i in range ( 1 , K ) : NEW_LINE INDENT for j in range ( Len - 1 , i - 1 , - 1 ) : NEW_LINE INDENT count_zero = 0 ; NEW_LINE count_one = 0 ; NEW_LINE dp [ j ] = sys . maxsize ; NEW_LINE for k in range ( j , i - 1 , - 1 ) : NEW_LINE INDENT if ( S [ k ] == '0' ) : NEW_LINE INDENT count_zero += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT count_one += 1 ; NEW_LINE DEDENT dp [ j ] = min ( dp [ j ] , count_zero * count_one + dp [ k - 1 ] ) ; NEW_LINE DEDENT DEDENT DEDENT return dp [ Len - 1 ] ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = "1011000110110100" ; NEW_LINE K = 5 ; NEW_LINE print ( minSumProd ( S , K ) ) ; NEW_LINE DEDENT
Minimize prize count required such that smaller value gets less prize in an adjacent pair | Function to find the minimum number of required such that adjacent smaller elements gets less number of prizes ; Loop to compute the smaller elements at the left ; Loop to find the smaller elements at the right ; Loop to find the minimum prizes required ; Driver code
def minPrizes ( arr , n ) : NEW_LINE INDENT dpLeft = [ 0 ] * n NEW_LINE dpLeft [ 0 ] = 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if arr [ i ] > arr [ i - 1 ] : NEW_LINE INDENT dpLeft [ i ] = dpLeft [ i - 1 ] + 1 NEW_LINE DEDENT else : NEW_LINE INDENT dpLeft [ i ] = 1 NEW_LINE DEDENT DEDENT dpRight = [ 0 ] * n NEW_LINE dpRight [ - 1 ] = 1 NEW_LINE for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT if arr [ i ] > arr [ i + 1 ] : NEW_LINE INDENT dpRight [ i ] = dpRight [ i + 1 ] + 1 NEW_LINE DEDENT else : NEW_LINE INDENT dpRight [ i ] = 1 NEW_LINE DEDENT DEDENT totalPrizes = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT totalPrizes += max ( dpLeft [ i ] , dpRight [ i ] ) NEW_LINE DEDENT print ( totalPrizes ) NEW_LINE DEDENT arr = [ 1 , 2 , 2 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE minPrizes ( arr , n ) NEW_LINE
Maximum sum subsequence with values differing by at least 2 | Python3 program to find maximum sum subsequence with values differing by at least 2 ; Function to find maximum sum subsequence such that two adjacent values elements are not selected ; Map to store the frequency of array elements ; Make a dp array to store answer upto i th value ; Base cases ; Iterate for all possible values of arr [ i ] ; Return the last value ; Driver code
from collections import defaultdict NEW_LINE def get_max_sum ( arr , n ) : NEW_LINE INDENT freq = defaultdict ( lambda : 0 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT freq [ arr [ i ] ] += 1 NEW_LINE DEDENT dp = [ 0 ] * 100001 NEW_LINE dp [ 0 ] = 0 NEW_LINE dp [ 1 ] = freq [ 0 ] NEW_LINE for i in range ( 2 , 100000 + 1 ) : NEW_LINE INDENT dp [ i ] = max ( dp [ i - 1 ] , dp [ i - 2 ] + i * freq [ i ] ) NEW_LINE DEDENT return dp [ 100000 ] NEW_LINE DEDENT N = 3 NEW_LINE arr = [ 2 , 2 , 3 ] NEW_LINE print ( get_max_sum ( arr , N ) ) NEW_LINE
Minimum flips required to keep all 1 s together in a Binary string | Python implementation for Minimum number of flips required in a binary string such that all the 1 aTMs are together ; Length of the binary string ; Initial state of the dp dp [ 0 ] [ 0 ] will be 1 if the current bit is 1 and we have to flip it ; Initial state of the dp dp [ 0 ] [ 1 ] will be 1 if the current bit is 0 and we have to flip it ; dp [ i ] [ 0 ] = Flips required to make all previous bits zero + Flip required to make current bit zero ; dp [ i ] [ 1 ] = minimum flips required to make all previous states 0 or make previous states 1 satisfying the condition ; Minimum of answer and flips required to make all bits 0 ; Driver code
def minFlip ( a ) : NEW_LINE INDENT n = len ( a ) NEW_LINE dp = [ [ 0 , 0 ] for i in range ( n ) ] NEW_LINE dp [ 0 ] [ 0 ] = int ( a [ 0 ] == '1' ) NEW_LINE dp [ 0 ] [ 1 ] = int ( a [ 0 ] == '0' ) NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT dp [ i ] [ 0 ] = dp [ i - 1 ] [ 0 ] + int ( a [ i ] == '1' ) NEW_LINE dp [ i ] [ 1 ] = min ( dp [ i - 1 ] ) + int ( a [ i ] == '0' ) NEW_LINE DEDENT answer = 10 ** 18 NEW_LINE for i in range ( n ) : NEW_LINE INDENT answer = min ( answer , dp [ i ] [ 1 ] + dp [ n - 1 ] [ 0 ] - dp [ i ] [ 0 ] ) NEW_LINE DEDENT return min ( answer , dp [ n - 1 ] [ 0 ] ) NEW_LINE DEDENT s = "1100111000101" NEW_LINE print ( minFlip ( s ) ) NEW_LINE
Length of longest increasing index dividing subsequence | Function to find the length of the longest increasing sub - sequence such that the index of the element is divisible by index of previous element ; Initialize the dp [ ] array with 1 as a single element will be of 1 length ; Traverse the given array ; If the index is divisible by the previous index ; If increasing subsequence identified ; Longest length is stored ; Driver code
def LIIDS ( arr , N ) : NEW_LINE INDENT dp = [ ] NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT dp . append ( 1 ) NEW_LINE DEDENT ans = 0 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT j = i + i NEW_LINE while j <= N : NEW_LINE INDENT if j < N and i < N and arr [ j ] > arr [ i ] : NEW_LINE INDENT dp [ j ] = max ( dp [ j ] , dp [ i ] + 1 ) NEW_LINE DEDENT j += i NEW_LINE DEDENT if i < N : NEW_LINE INDENT ans = max ( ans , dp [ i ] ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT arr = [ 1 , 2 , 3 , 7 , 9 , 10 ] NEW_LINE N = len ( arr ) NEW_LINE print ( LIIDS ( arr , N ) ) NEW_LINE
Count of subarrays having exactly K perfect square numbers | Python3 program to count of subarrays having exactly K perfect square numbers . ; A utility function to check if the number n is perfect square or not ; Find floating point value of square root of x . ; If square root is an integer ; Function to find number of subarrays with sum exactly equal to k ; STL map to store number of subarrays starting from index zero having particular value of sum . ; To store the sum of element traverse so far ; Add current element to currsum ; If currsum = K , then a new subarray is found ; If currsum > K then find the no . of subarrays with sum currsum - K and exclude those subarrays ; Add currsum to count of different values of sum ; Return the final result ; Function to count the subarray with K perfect square numbers ; Update the array element ; If current element is perfect square then update the arr [ i ] to 1 ; Else change arr [ i ] to 0 ; Function Call ; Driver Code ; Function Call
from collections import defaultdict NEW_LINE import math NEW_LINE def isPerfectSquare ( x ) : NEW_LINE INDENT sr = math . sqrt ( x ) NEW_LINE return ( ( sr - math . floor ( sr ) ) == 0 ) NEW_LINE DEDENT def findSubarraySum ( arr , n , K ) : NEW_LINE INDENT prevSum = defaultdict ( int ) NEW_LINE res = 0 NEW_LINE currsum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT currsum += arr [ i ] NEW_LINE if ( currsum == K ) : NEW_LINE INDENT res += 1 NEW_LINE DEDENT if ( ( currsum - K ) in prevSum ) : NEW_LINE INDENT res += ( prevSum [ currsum - K ] ) NEW_LINE DEDENT prevSum [ currsum ] += 1 NEW_LINE DEDENT return res NEW_LINE DEDENT def countSubarray ( arr , n , K ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT if ( isPerfectSquare ( arr [ i ] ) ) : NEW_LINE INDENT arr [ i ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT arr [ i ] = 0 NEW_LINE DEDENT DEDENT print ( findSubarraySum ( arr , n , K ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 4 , 9 , 2 ] NEW_LINE K = 2 NEW_LINE N = len ( arr ) NEW_LINE countSubarray ( arr , N , K ) NEW_LINE DEDENT
Number of ways to split N as sum of K numbers from the given range | Python3 implementation to count the number of ways to divide N in K groups such that each group has elements in range [ L , R ] ; Function to count the number of ways to divide the number N in K groups such that each group has number of elements in range [ L , R ] ; Base Case ; If N is divides completely into less than k groups ; Put all possible values greater equal to prev ; Function to count the number of ways to divide the number N ; Driver Code
mod = 1000000007 NEW_LINE def calculate ( pos , left , k , L , R ) : NEW_LINE INDENT if ( pos == k ) : NEW_LINE INDENT if ( left == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT if ( left == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT answer = 0 NEW_LINE for i in range ( L , R + 1 ) : NEW_LINE INDENT if ( i > left ) : NEW_LINE INDENT break NEW_LINE DEDENT answer = ( answer + calculate ( pos + 1 , left - i , k , L , R ) ) % mod NEW_LINE DEDENT return answer NEW_LINE DEDENT def countWaystoDivide ( n , k , L , R ) : NEW_LINE INDENT return calculate ( 0 , n , k , L , R ) NEW_LINE DEDENT N = 12 NEW_LINE K = 3 NEW_LINE L = 1 NEW_LINE R = 5 NEW_LINE print ( countWaystoDivide ( N , K , L , R ) ) NEW_LINE
Check if it is possible to get given sum by taking one element from each row | Function that prints whether is it possible to make sum equal to K ; Base case ; Condition if we can make sum equal to current column by using above rows ; Iterate through current column and check whether we can make sum less than or equal to k ; Printing whether is it possible or not ; Driver Code
def PossibleSum ( n , m , v , k ) : NEW_LINE INDENT dp = [ [ 0 ] * ( k + 1 ) for i in range ( n + 1 ) ] NEW_LINE dp [ 0 ] [ 0 ] = 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( k + 1 ) : NEW_LINE INDENT if dp [ i ] [ j ] == 1 : NEW_LINE INDENT for d in range ( m ) : NEW_LINE INDENT if ( j + v [ i ] [ d ] ) <= k : NEW_LINE INDENT dp [ i + 1 ] [ j + v [ i ] [ d ] ] = 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT if dp [ n ] [ k ] == 1 : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT N = 2 NEW_LINE M = 10 NEW_LINE K = 5 NEW_LINE arr = [ [ 4 , 0 , 15 , 3 , 2 , 20 , 10 , 1 , 5 , 4 ] , [ 4 , 0 , 10 , 3 , 2 , 25 , 4 , 1 , 5 , 4 ] ] NEW_LINE PossibleSum ( N , M , arr , K ) NEW_LINE
Modify array by merging elements with addition such that it consists of only Primes . | DP array to store the ans for max 20 elements ; To check whether the number is prime or not ; Function to check whether the array can be modify so that there are only primes ; If curr is prime and all elements are visited , return true ; If all elements are not visited , set curr = 0 , to search for new prime sum ; If all elements are visited ; If the current sum is not prime return false ; If this state is already calculated , return the answer directly ; Try all state of mask ; If ith index is not set ; Add the current element and set ith index and recur ; If subset can be formed then return true ; After every possibility of mask , if the subset is not formed , return false by memoizing . ; Driver code
dp = [ 0 ] * ( 1 << 20 ) NEW_LINE def isprime ( n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( 2 , n + 1 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def solve ( arr , curr , mask , n ) : NEW_LINE INDENT if ( isprime ( curr ) ) : NEW_LINE INDENT if ( mask == ( 1 << n ) - 1 ) : NEW_LINE INDENT return True NEW_LINE DEDENT curr = 0 NEW_LINE DEDENT if ( mask == ( 1 << n ) - 1 ) : NEW_LINE INDENT if ( isprime ( curr ) == False ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT if ( dp [ mask ] != False ) : NEW_LINE INDENT return dp [ mask ] NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT if ( ( mask & 1 << i ) == False ) : NEW_LINE INDENT if ( solve ( arr , curr + arr [ i ] , mask 1 << i , n ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT DEDENT return ( dp [ mask ] == False ) NEW_LINE DEDENT arr = [ 3 , 6 , 7 , 13 ] NEW_LINE n = len ( arr ) NEW_LINE if ( solve ( arr , 0 , 0 , n ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT
Maximum sum in circular array such that no two elements are adjacent | Set 2 | Store the maximum possible at each index ; When i exceeds the index of the last element simply return 0 ; If the value has already been calculated , directly return it from the dp array ; The next states are don 't take  this element and go to (i + 1)th state  else take this element  and go to (i + 2)th state ; function to find the max value ; subarr contains elements from 0 to arr . size ( ) - 2 ; Initializing all the values with - 1 ; Calculating maximum possible sum for first case ; subarr contains elements from 1 to arr . size ( ) - 1 ; Re - initializing all values with - 1 ; Calculating maximum possible sum for second case ; Printing the maximum between them ; Driver code
dp = [ ] NEW_LINE def maxSum ( i , subarr ) : NEW_LINE INDENT if ( i >= len ( subarr ) ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( dp [ i ] != - 1 ) : NEW_LINE INDENT return dp [ i ] NEW_LINE DEDENT dp [ i ] = max ( maxSum ( i + 1 , subarr ) , subarr [ i ] + maxSum ( i + 2 , subarr ) ) NEW_LINE return dp [ i ] NEW_LINE DEDENT def Func ( arr ) : NEW_LINE INDENT subarr = arr NEW_LINE subarr . pop ( ) NEW_LINE global dp NEW_LINE dp = [ - 1 ] * ( len ( subarr ) ) NEW_LINE max1 = maxSum ( 0 , subarr ) NEW_LINE subarr = arr NEW_LINE subarr = subarr [ : ] NEW_LINE del dp NEW_LINE dp = [ - 1 ] * ( len ( subarr ) ) NEW_LINE max2 = maxSum ( 0 , subarr ) NEW_LINE print ( max ( max1 , max2 ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 1 ] NEW_LINE Func ( arr ) NEW_LINE DEDENT
Count all square sub | Python3 program to count total number of k x k sub matrix whose sum is greater than the given number S ; Function to create prefix sum matrix from the given matrix ; Store first value in table ; Initialize first row of matrix ; Initialize first column of matrix ; Initialize rest table with sum ; Utility function to count the submatrix whose sum is greater than the S ; Loop to iterate over all the possible positions of the given matrix mat [ ] [ ] ; Condition to check , if K x K is first sub matrix ; Condition to check sub - matrix has no margin at top ; Condition when sub matrix has no margin at left ; Condition when submatrix has margin at top and left ; Increment count , If sub matrix sum is greater than S ; Function to count submatrix of size k x k such that sum if greater than or equal to S ; For loop to initialize prefix sum matrix with zero , initially ; Function to create the prefix sum matrix ; Driver Code ; Print total number of sub matrix
dim = 5 NEW_LINE def createTable ( mtrx , k , p , dp ) : NEW_LINE INDENT dp [ 0 ] [ 0 ] = mtrx [ 0 ] [ 0 ] NEW_LINE for j in range ( 1 , dim ) : NEW_LINE INDENT dp [ 0 ] [ j ] = mtrx [ 0 ] [ j ] + dp [ 0 ] [ j - 1 ] NEW_LINE DEDENT for i in range ( 1 , dim ) : NEW_LINE INDENT dp [ i ] [ 0 ] = mtrx [ i ] [ 0 ] + dp [ i - 1 ] [ 0 ] NEW_LINE DEDENT for i in range ( 1 , dim ) : NEW_LINE INDENT for j in range ( 1 , dim ) : NEW_LINE INDENT dp [ i ] [ j ] = ( mtrx [ i ] [ j ] + dp [ i - 1 ] [ j ] + dp [ i ] [ j - 1 ] - dp [ i - 1 ] [ j - 1 ] ) NEW_LINE DEDENT DEDENT DEDENT def countSubMatrixUtil ( dp , k , p ) : NEW_LINE INDENT count = 0 NEW_LINE subMatSum = 0 NEW_LINE for i in range ( k - 1 , dim ) : NEW_LINE INDENT for j in range ( k - 1 , dim , 1 ) : NEW_LINE INDENT if ( i == ( k - 1 ) or j == ( k - 1 ) ) : NEW_LINE INDENT if ( i == ( k - 1 ) and j == ( k - 1 ) ) : NEW_LINE INDENT subMatSum = dp [ i ] [ j ] NEW_LINE DEDENT elif ( i == ( k - 1 ) ) : NEW_LINE INDENT subMatSum = dp [ i ] [ j ] - dp [ i ] [ j - k ] NEW_LINE DEDENT else : NEW_LINE INDENT subMatSum = dp [ i ] [ j ] - dp [ i - k ] [ j ] NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT subMatSum = ( dp [ i ] [ j ] - dp [ i - k ] [ j ] - dp [ i ] [ j - k ] + dp [ i - k ] [ j - k ] ) NEW_LINE DEDENT if ( subMatSum >= p ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT def countSubMatrix ( mtrx , k , p ) : NEW_LINE INDENT dp = [ [ 0 for i in range ( dim ) ] for j in range ( dim ) ] NEW_LINE for i in range ( dim ) : NEW_LINE INDENT for j in range ( dim ) : NEW_LINE INDENT dp [ i ] [ j ] = 0 NEW_LINE DEDENT DEDENT createTable ( mtrx , k , p , dp ) NEW_LINE return countSubMatrixUtil ( dp , k , p ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT mtrx = [ [ 1 , 7 , 1 , 1 , 1 ] , [ 2 , 2 , 2 , 2 , 2 ] , [ 3 , 9 , 6 , 7 , 3 ] , [ 4 , 3 , 2 , 4 , 5 ] , [ 5 , 1 , 5 , 3 , 1 ] ] NEW_LINE k = 3 NEW_LINE p = 35 NEW_LINE print ( countSubMatrix ( mtrx , k , p ) ) NEW_LINE DEDENT
Word Break Problem | DP | Unordered_map used for storing the sentences the key string can be broken into ; Unordered_set used to store the dictionary . ; Utility function used for printing the obtained result ; Utility function for appending new words to already existing strings ; Loop to find the append string which can be broken into ; Utility function for word Break ; Condition to check if the subproblem is already computed ; If the whole word is a dictionary word then directly append into the result array for the string ; Loop to iterate over the substring ; If the substring is present into the dictionary then recurse for other substring of the string ; Store the subproblem into the map res is an reference so we need to assign an reference to something if its keep on changing res values changes after it start going through combine method you can check if you had a doubt so here we just clone res ; Master wordBreak function converts the string vector to unordered_set ; Clear the previous stored data ; Driver Code
mp = dict ( ) NEW_LINE dict_t = set ( ) NEW_LINE def printResult ( A ) : NEW_LINE INDENT for i in range ( len ( A ) ) : NEW_LINE INDENT print ( A [ i ] ) NEW_LINE DEDENT DEDENT def combine ( prev , word ) : NEW_LINE INDENT for i in range ( len ( prev ) ) : NEW_LINE INDENT prev [ i ] += " ▁ " + word ; NEW_LINE DEDENT return prev ; NEW_LINE DEDENT def wordBreakUtil ( s ) : NEW_LINE INDENT if ( s in mp ) : NEW_LINE INDENT return mp [ s ] ; NEW_LINE DEDENT res = [ ] NEW_LINE if ( s in dict_t ) : NEW_LINE INDENT res . append ( s ) ; NEW_LINE DEDENT for i in range ( 1 , len ( s ) ) : NEW_LINE INDENT word = s [ i : ] ; NEW_LINE if ( word in dict_t ) : NEW_LINE INDENT rem = s [ : i ] NEW_LINE prev = combine ( wordBreakUtil ( rem ) , word ) ; NEW_LINE for i in prev : NEW_LINE INDENT res . append ( i ) NEW_LINE DEDENT DEDENT DEDENT x = [ ] NEW_LINE for i in res : NEW_LINE x . append ( i ) NEW_LINE mp [ s ] = x ; NEW_LINE return res ; NEW_LINE DEDENT def wordBreak ( s , wordDict ) : NEW_LINE INDENT mp . clear ( ) ; NEW_LINE dict_t . clear ( ) ; NEW_LINE for i in wordDict : NEW_LINE INDENT dict_t . add ( i ) NEW_LINE DEDENT return wordBreakUtil ( s ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT wordDict1 = [ " cat " , " cats " , " and " , " sand " , " dog " ] NEW_LINE printResult ( wordBreak ( " catsanddog " , wordDict1 ) ) ; NEW_LINE DEDENT