codeparrot/xlcost-text-to-code huggingface dataset:
Maximize occurrences of values between L and R on sequential addition of Array elements with modulo H | Function that prints the number of times X gets a value between L and R ; Base condition ; Condition if X can be made equal to j after i additions ; Compute value of X after adding arr [ i ] ; Compute value of X after adding arr [ i ] - 1 ; Update dp as the maximum value ; Compute maximum answer from all possible cases ; Printing maximum good occurrence of X ; Driver Code
def goodInteger ( arr , n , h , l , r ) : NEW_LINE INDENT dp = [ [ - 1 for i in range ( h ) ] for j in range ( n + 1 ) ] NEW_LINE dp [ 0 ] [ 0 ] = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( h ) : NEW_LINE INDENT if ( dp [ i ] [ j ] != - 1 ) : NEW_LINE INDENT h1 = ( j + arr [ i ] ) % h NEW_LINE h2 = ( j + arr [ i ] - 1 ) % h NEW_LINE dp [ i + 1 ] [ h1 ] = max ( dp [ i + 1 ] [ h1 ] , dp [ i ] [ j ] + ( h1 >= l and h1 <= r ) ) NEW_LINE dp [ i + 1 ] [ h2 ] = max ( dp [ i + 1 ] [ h2 ] , dp [ i ] [ j ] + ( h2 >= l and h2 <= r ) ) NEW_LINE DEDENT DEDENT DEDENT ans = 0 NEW_LINE for i in range ( h ) : NEW_LINE INDENT if ( dp [ n ] [ i ] != - 1 ) : NEW_LINE INDENT ans = max ( ans , dp [ n ] [ i ] ) NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 16 , 17 , 14 , 20 , 20 , 11 , 22 ] NEW_LINE H = 24 NEW_LINE L = 21 NEW_LINE R = 23 NEW_LINE size = len ( A ) NEW_LINE goodInteger ( A , size , H , L , R ) NEW_LINE DEDENT
Longest subarray whose elements can be made equal by maximum K increments | Function to find maximum possible length of subarray ; Stores the sum of elements that needs to be added to the sub array ; Stores the index of the current position of subarray ; Stores the maximum length of subarray . ; Maximum element from each subarray length ; Previous index of the current subarray of maximum length ; Deque to store the indices of maximum element of each sub array ; For each array element , find the maximum length of required subarray ; Traverse the deque and update the index of maximum element . ; If it is first element then update maximum and dp [ ] ; Else check if current element exceeds max ; Update max and dp [ ] ; Update the index of the current maximum length subarray ; While current element being added to dp [ ] array exceeds K ; Update index of current position and the previous position ; Remove elements from deque and update the maximum element ; Update the maximum length of the required subarray . ; Driver code
def validSubArrLength ( arr , N , K ) : NEW_LINE INDENT dp = [ 0 for i in range ( N + 1 ) ] NEW_LINE pos = 0 NEW_LINE ans = 0 NEW_LINE mx = 0 NEW_LINE pre = 0 NEW_LINE q = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT while ( len ( q ) and arr [ len ( q ) - 1 ] < arr [ i ] ) : NEW_LINE INDENT q . remove ( q [ len ( q ) - 1 ] ) NEW_LINE DEDENT q . append ( i ) NEW_LINE if ( i == 0 ) : NEW_LINE INDENT mx = arr [ i ] NEW_LINE dp [ i ] = arr [ i ] NEW_LINE DEDENT elif ( mx <= arr [ i ] ) : NEW_LINE INDENT dp [ i ] = dp [ i - 1 ] + arr [ i ] NEW_LINE mx = arr [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] = dp [ i - 1 ] + arr [ i ] NEW_LINE DEDENT if ( pre == 0 ) : NEW_LINE INDENT pos = 0 NEW_LINE DEDENT else : NEW_LINE INDENT pos = pre - 1 NEW_LINE DEDENT while ( ( i - pre + 1 ) * mx - ( dp [ i ] - dp [ pos ] ) > K and pre < i ) : NEW_LINE INDENT pos = pre NEW_LINE pre += 1 NEW_LINE while ( len ( q ) and q [ 0 ] < pre and pre < i ) : NEW_LINE INDENT q . remove ( q [ 0 ] ) NEW_LINE mx = arr [ q [ 0 ] ] NEW_LINE DEDENT DEDENT ans = max ( ans , i - pre + 1 ) NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 6 NEW_LINE K = 8 NEW_LINE arr = [ 2 , 7 , 1 , 3 , 4 , 5 ] NEW_LINE print ( validSubArrLength ( arr , N , K ) ) NEW_LINE DEDENT
Partition of a set into K subsets with equal sum using BitMask and DP | Function to check whether K required partitions are possible or not ; Return true as the entire array is the answer ; If total number of partitions exceeds size of the array ; If the array sum is not divisible by K ; No such partitions are possible ; Required sum of each subset ; Initialize dp array with - 1 ; Sum of empty subset is zero ; Iterate over all subsets / masks ; If current mask is invalid , continue ; Iterate over all array elements ; Check if the current element can be added to the current subset / mask ; Transition ; Driver Code
def isKPartitionPossible ( arr , N , K ) : NEW_LINE INDENT if ( K == 1 ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( N < K ) : NEW_LINE INDENT return False NEW_LINE DEDENT sum = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE DEDENT if ( sum % K != 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT target = sum / K NEW_LINE dp = [ 0 for i in range ( 1 << 15 ) ] NEW_LINE for i in range ( ( 1 << N ) ) : NEW_LINE INDENT dp [ i ] = - 1 NEW_LINE DEDENT dp [ 0 ] = 0 NEW_LINE for mask in range ( ( 1 << N ) ) : NEW_LINE INDENT if ( dp [ mask ] == - 1 ) : NEW_LINE INDENT continue NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT if ( ( mask & ( 1 << i ) == 0 ) and dp [ mask ] + arr [ i ] <= target ) : NEW_LINE INDENT dp [ mask ( 1 << i ) ] = ( ( dp [ mask ] + arr [ i ] ) % target ) NEW_LINE DEDENT DEDENT DEDENT if ( dp [ ( 1 << N ) - 1 ] == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 1 , 4 , 5 , 3 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE K = 3 NEW_LINE if ( isKPartitionPossible ( arr , N , K ) ) : NEW_LINE INDENT print ( " Partitions ▁ into ▁ equal ▁ " \ " sum ▁ is ▁ possible . " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Partitions ▁ into ▁ equal ▁ sum ▁ " \ " is ▁ not ▁ possible . " ) NEW_LINE DEDENT DEDENT
Minimum cost path in a Matrix by moving only on value difference of X | Python3 implementation to find the minimum number of operations required to move from ( 1 , 1 ) to ( N , M ) ; Function to find the minimum operations required to move to bottom - right cell of matrix ; Condition to check if the current cell is the bottom - right cell of the matrix ; Condition to check if the current cell is out of matrix ; Condition to check if the current indices is already computed ; Condition to check that the movement with the current value is not possible ; Recursive call to compute the number of operation required to compute the value ; Function to find the minimum number of operations required to reach the bottom - right cell ; Loop to iterate over every possible cell of the matrix ; Driver Code ; Function Call
MAX = 1e18 NEW_LINE v = [ [ 0 , 0 ] ] * ( 151 ) NEW_LINE dp = [ [ - 1 for i in range ( 151 ) ] for i in range ( 151 ) ] NEW_LINE def min_operation ( i , j , val , x ) : NEW_LINE INDENT if ( i == n - 1 and j == m - 1 ) : NEW_LINE INDENT if ( val > v [ i ] [ j ] ) : NEW_LINE INDENT dp [ i ] [ j ] = MAX NEW_LINE return MAX NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] [ j ] = v [ i ] [ j ] - val NEW_LINE return dp [ i ] [ j ] NEW_LINE DEDENT DEDENT if ( i == n or j == m ) : NEW_LINE INDENT dp [ i ] [ j ] = MAX NEW_LINE return MAX NEW_LINE DEDENT if ( dp [ i ] [ j ] != - 1 ) : NEW_LINE INDENT return dp [ i ] [ j ] NEW_LINE DEDENT if ( val > v [ i ] [ j ] ) : NEW_LINE INDENT dp [ i ] [ j ] = MAX NEW_LINE return MAX NEW_LINE DEDENT tmp = v [ i ] [ j ] - val NEW_LINE tmp += min ( min_operation ( i + 1 , j , val + x , x ) , min_operation ( i , j + 1 , val + x , x ) ) NEW_LINE dp [ i ] [ j ] = tmp NEW_LINE return tmp NEW_LINE DEDENT def solve ( x ) : NEW_LINE INDENT ans = 10 ** 19 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE INDENT val = v [ i ] [ j ] - x * ( i + j ) NEW_LINE for ii in range ( 151 ) : NEW_LINE INDENT for jj in range ( 151 ) : NEW_LINE INDENT dp [ ii ] [ jj ] = - 1 NEW_LINE DEDENT DEDENT val = min_operation ( 0 , 0 , val , x ) NEW_LINE ans = min ( ans , val ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 2 NEW_LINE m = 2 NEW_LINE x = 3 NEW_LINE v [ 0 ] = [ 15 , 153 ] NEW_LINE v [ 1 ] = [ 135 , 17 ] NEW_LINE print ( solve ( x ) ) NEW_LINE DEDENT
Minimum count of numbers required from given array to represent S | Python3 implementation to find the minimum number of sequence required from array such that their sum is equal to given S ; Function to find the minimum elements required to get the sum of given value S ; Condition if the sequence is found ; Condition when no such sequence found ; Calling for without choosing the current index value ; Calling for after choosing the current index value ; Function for every array ; Driver code
import sys NEW_LINE def printAllSubsetsRec ( arr , n , v , Sum ) : NEW_LINE INDENT if ( Sum == 0 ) : NEW_LINE INDENT return len ( v ) NEW_LINE DEDENT if ( Sum < 0 ) : NEW_LINE INDENT return sys . maxsize NEW_LINE DEDENT if ( n == 0 ) : NEW_LINE INDENT return sys . maxsize NEW_LINE DEDENT x = printAllSubsetsRec ( arr , n - 1 , v , Sum ) NEW_LINE v . append ( arr [ n - 1 ] ) NEW_LINE y = printAllSubsetsRec ( arr , n , v , Sum - arr [ n - 1 ] ) NEW_LINE v . pop ( len ( v ) - 1 ) NEW_LINE return min ( x , y ) NEW_LINE DEDENT def printAllSubsets ( arr , n , Sum ) : NEW_LINE INDENT v = [ ] NEW_LINE return printAllSubsetsRec ( arr , n , v , Sum ) NEW_LINE DEDENT arr = [ 2 , 1 , 4 , 3 , 5 , 6 ] NEW_LINE Sum = 6 NEW_LINE n = len ( arr ) NEW_LINE print ( printAllSubsets ( arr , n , Sum ) ) NEW_LINE
Minimum count of numbers required from given array to represent S | Function to find the count of minimum length of the sequence ; Loop to initialize the array as infinite in the row 0 ; Loop to find the solution by pre - computation for the sequence ; Minimum possible for the previous minimum value of the sequence ; Driver Code
def Count ( S , m , n ) : NEW_LINE INDENT table = [ [ 0 for i in range ( n + 1 ) ] for i in range ( m + 1 ) ] NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT table [ 0 ] [ i ] = 10 ** 9 - 1 NEW_LINE DEDENT for i in range ( 1 , m + 1 ) : NEW_LINE INDENT for j in range ( 1 , n + 1 ) : NEW_LINE INDENT if ( S [ i - 1 ] > j ) : NEW_LINE INDENT table [ i ] [ j ] = table [ i - 1 ] [ j ] NEW_LINE DEDENT else : NEW_LINE INDENT table [ i ] [ j ] = min ( table [ i - 1 ] [ j ] , table [ i ] [ j - S [ i - 1 ] ] + 1 ) NEW_LINE DEDENT DEDENT DEDENT return table [ m ] [ n ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 9 , 6 , 5 , 1 ] NEW_LINE m = len ( arr ) NEW_LINE print ( Count ( arr , m , 11 ) ) NEW_LINE DEDENT
Maximize product of same | Python3 implementation to maximize product of same - indexed elements of same size subsequences ; Utility function to find the maximum ; Utility function to find the maximum scalar product from the equal length sub - sequences taken from the two given array ; Return a very small number if index is invalid ; If the sub - problem is already evaluated , then return it ; Take the maximum of all the recursive cases ; Function to find maximum scalar product from same size sub - sequences taken from the two given array ; Initialize a 2 - D array for memoization ; Driver Code
INF = 10000000 NEW_LINE def maximum ( A , B , C , D ) : NEW_LINE INDENT return max ( max ( A , B ) , max ( C , D ) ) NEW_LINE DEDENT def maxProductUtil ( X , Y , A , B , dp ) : NEW_LINE INDENT if ( X < 0 or Y < 0 ) : NEW_LINE INDENT return - INF NEW_LINE DEDENT if ( dp [ X ] [ Y ] != - 1 ) : NEW_LINE INDENT return dp [ X ] [ Y ] NEW_LINE DEDENT dp [ X ] [ Y ] = maximum ( A [ X ] * B [ Y ] + maxProductUtil ( X - 1 , Y - 1 , A , B , dp ) , A [ X ] * B [ Y ] , maxProductUtil ( X - 1 , Y , A , B , dp ) , maxProductUtil ( X , Y - 1 , A , B , dp ) ) NEW_LINE return dp [ X ] [ Y ] NEW_LINE DEDENT def maxProduct ( A , N , B , M ) : NEW_LINE INDENT dp = [ [ - 1 for i in range ( m ) ] for i in range ( n ) ] NEW_LINE return maxProductUtil ( N - 1 , M - 1 , A , B , dp ) NEW_LINE DEDENT a = [ - 2 , 6 , - 2 , - 5 ] NEW_LINE b = [ - 3 , 4 , - 2 , 8 ] NEW_LINE n = len ( a ) NEW_LINE m = len ( b ) NEW_LINE print ( maxProduct ( a , n , b , m ) ) NEW_LINE
Check if one string can be converted to other using given operation | Function that prints whether is it possible to make a equal to T by performing given operations ; Base case , if we put the last character at front of A ; Base case , if we put the last character at back of A ; Condition if current sequence is matchable ; Condition for front move to ( i - 1 ) th character ; Condition for back move to ( i - 1 ) th character ; Condition if it is possible to make A equal to T ; Print final answer ; Driver Code
def twoStringsEquality ( s , t ) : NEW_LINE INDENT n = len ( s ) NEW_LINE dp = [ [ 0 for i in range ( n + 1 ) ] for i in range ( n ) ] NEW_LINE if ( s [ n - 1 ] == t [ 0 ] ) : NEW_LINE INDENT dp [ n - 1 ] [ 1 ] = 1 NEW_LINE DEDENT if ( s [ n - 1 ] == t [ n - 1 ] ) : NEW_LINE INDENT dp [ n - 1 ] [ 0 ] = 1 NEW_LINE DEDENT for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT for j in range ( n - i + 1 ) : NEW_LINE INDENT if ( dp [ i ] [ j ] ) : NEW_LINE INDENT if ( s [ i - 1 ] == t [ j ] ) : NEW_LINE INDENT dp [ i - 1 ] [ j + 1 ] = 1 NEW_LINE DEDENT if ( s [ i - 1 ] == t [ i + j - 1 ] ) : NEW_LINE INDENT dp [ i - 1 ] [ j ] = 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT ans = False NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT if ( dp [ 0 ] [ i ] == 1 ) : NEW_LINE INDENT ans = True NEW_LINE break NEW_LINE DEDENT DEDENT if ( ans == True ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " abab " NEW_LINE T = " baab " NEW_LINE twoStringsEquality ( S , T ) NEW_LINE DEDENT
Maximize sum of all elements which are not a part of the Longest Increasing Subsequence | Function to find maximum sum ; Find total sum of array ; Maintain a 2D array ; Update the dp array along with sum in the second row ; In case of greater length update the length along with sum ; In case of equal length find length update length with minimum sum ; Find the sum that need to be subtracted from total sum ; Return the sum ; Driver code
def findSum ( arr , n ) : NEW_LINE INDENT totalSum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT totalSum += arr [ i ] NEW_LINE DEDENT dp = [ [ 0 ] * n for i in range ( 2 ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT dp [ 0 ] [ i ] = 1 NEW_LINE dp [ 1 ] [ i ] = arr [ i ] 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 ] ) : NEW_LINE INDENT if ( dp [ 0 ] [ i ] < dp [ 0 ] [ j ] + 1 ) : NEW_LINE INDENT dp [ 0 ] [ i ] = dp [ 0 ] [ j ] + 1 NEW_LINE dp [ 1 ] [ i ] = dp [ 1 ] [ j ] + arr [ i ] NEW_LINE DEDENT elif ( dp [ 0 ] [ i ] == dp [ 0 ] [ j ] + 1 ) : NEW_LINE INDENT dp [ 1 ] [ i ] = min ( dp [ 1 ] [ i ] , dp [ 1 ] [ j ] + arr [ i ] ) NEW_LINE DEDENT DEDENT DEDENT DEDENT maxm = 0 NEW_LINE subtractSum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( dp [ 0 ] [ i ] > maxm ) : NEW_LINE INDENT maxm = dp [ 0 ] [ i ] NEW_LINE subtractSum = dp [ 1 ] [ i ] NEW_LINE DEDENT elif ( dp [ 0 ] [ i ] == maxm ) : NEW_LINE INDENT subtractSum = min ( subtractSum , dp [ 1 ] [ i ] ) NEW_LINE DEDENT DEDENT return totalSum - subtractSum NEW_LINE DEDENT arr = [ 4 , 6 , 1 , 2 , 3 , 8 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findSum ( arr , n ) ) NEW_LINE
Count of possible permutations of a number represented as a sum of 2 ' s , ▁ 4' s and 6 's only | Returns number of ways to reach score n ; table [ i ] will store count of solutions for value i . ; Initialize all table values as 0 ; Base case ( If given value is 0 , 1 , 2 , or 4 ) ; Driver Code
def count ( n ) : NEW_LINE INDENT if ( n == 2 ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT elif ( n == 4 ) : NEW_LINE INDENT return 2 ; NEW_LINE DEDENT elif ( n == 6 ) : NEW_LINE INDENT return 4 ; NEW_LINE DEDENT table = [ 0 ] * ( n + 1 ) ; NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT table [ i ] = 0 ; NEW_LINE DEDENT table [ 0 ] = 0 ; NEW_LINE table [ 2 ] = 1 ; NEW_LINE table [ 4 ] = 2 ; NEW_LINE table [ 6 ] = 4 ; NEW_LINE for i in range ( 8 , n + 1 , 2 ) : NEW_LINE INDENT table [ i ] = table [ i - 2 ] + table [ i - 4 ] + table [ i - 6 ] ; NEW_LINE DEDENT return table [ n ] ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 8 ; NEW_LINE print ( count ( n ) ) ; NEW_LINE DEDENT
Count of ways to split a given number into prime segments | Python3 implementation to count total number of ways to split a string to get prime numbers ; Function to check whether a number is a prime number or not ; Function to find the count of ways to split string into prime numbers ; 1 based indexing ; Consider every suffix up to 6 digits ; Number should not have a leading zero and it should be a prime number ; Return the final result ; Driver code
MOD = 1000000007 NEW_LINE def isPrime ( number ) : NEW_LINE INDENT num = int ( number ) NEW_LINE i = 2 NEW_LINE while i * i <= num : NEW_LINE INDENT if ( ( num % i ) == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT i += 1 NEW_LINE DEDENT if num > 1 : NEW_LINE return True NEW_LINE else : NEW_LINE return False NEW_LINE DEDENT def countPrimeStrings ( number , i ) : NEW_LINE INDENT if ( i == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT cnt = 0 NEW_LINE for j in range ( 1 , 7 ) : NEW_LINE INDENT if ( i - j >= 0 and number [ i - j ] != '0' and isPrime ( number [ i - j : i ] ) ) : NEW_LINE INDENT cnt += countPrimeStrings ( number , i - j ) NEW_LINE cnt %= MOD NEW_LINE DEDENT DEDENT return cnt NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s1 = "3175" NEW_LINE l = len ( s1 ) NEW_LINE print ( countPrimeStrings ( s1 , l ) ) NEW_LINE DEDENT
Count of ways to split a given number into prime segments | Python3 implementation to count total number of ways to split a String to get prime numbers ; Function to build sieve ; If p is a prime ; Update all multiples of p as non prime ; Function to check whether a number is a prime number or not ; Function to find the count of ways to split String into prime numbers ; Number should not have a leading zero and it should be a prime number ; Function to count the number of prime Strings ; Driver code
MOD = 1000000007 NEW_LINE sieve = [ 0 ] * ( 1000000 ) NEW_LINE def buildSieve ( ) : NEW_LINE INDENT for i in range ( len ( sieve ) ) : NEW_LINE INDENT sieve [ i ] = True NEW_LINE DEDENT sieve [ 0 ] = False NEW_LINE sieve [ 1 ] = False NEW_LINE p = 2 NEW_LINE while p * p <= 1000000 : NEW_LINE INDENT if sieve [ p ] == True : NEW_LINE INDENT for i in range ( p * p , 1000000 , p ) : NEW_LINE INDENT sieve [ i ] = False NEW_LINE DEDENT DEDENT p += 1 NEW_LINE DEDENT DEDENT def isPrime ( number ) : NEW_LINE INDENT num = int ( number ) NEW_LINE return sieve [ num ] NEW_LINE DEDENT def rec ( number , i , dp ) : NEW_LINE INDENT if dp [ i ] != - 1 : NEW_LINE INDENT return dp [ i ] NEW_LINE DEDENT cnt = 0 NEW_LINE for j in range ( 1 , 7 ) : NEW_LINE if ( i - j ) >= 0 and number [ i - j ] != '0' and isPrime ( number [ i - j : i ] ) : NEW_LINE INDENT cnt += rec ( number , i - j , dp ) NEW_LINE cnt %= MOD NEW_LINE DEDENT dp [ i ] = cnt NEW_LINE return dp [ i ] NEW_LINE DEDENT def countPrimeStrings ( number ) : NEW_LINE INDENT n = len ( number ) NEW_LINE dp = [ 0 ] * ( n + 1 ) NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT dp [ i ] = - 1 NEW_LINE DEDENT dp [ 0 ] = 1 NEW_LINE return rec ( number , n , dp ) NEW_LINE DEDENT buildSieve ( ) NEW_LINE s1 = "3175" NEW_LINE print ( countPrimeStrings ( s1 ) ) NEW_LINE
Check if a matrix contains a square submatrix with 0 as boundary element | Function checks if square with all 0 's in boundary exists in the matrix ; r1 is the top row , c1 is the left col r2 is the bottom row , c2 is the right ; Function checks if the boundary of the square consists of 0 's ; Driver Code
def squareOfZeroes ( ) : NEW_LINE INDENT global matrix , cache NEW_LINE lastIdx = len ( matrix ) - 1 NEW_LINE return hasSquareOfZeroes ( 0 , 0 , lastIdx , lastIdx ) NEW_LINE DEDENT def hasSquareOfZeroes ( r1 , c1 , r2 , c2 ) : NEW_LINE INDENT global matrix , cache NEW_LINE if ( r1 >= r2 or c1 >= c2 ) : NEW_LINE INDENT return False NEW_LINE DEDENT key = ( str ( r1 ) + ' - ' + str ( c1 ) + ' - ' + str ( r2 ) + ' - ' + str ( c2 ) ) NEW_LINE if ( key in cache ) : NEW_LINE INDENT return cache [ key ] NEW_LINE DEDENT cache [ key ] = ( isSquareOfZeroes ( r1 , c1 , r2 , c2 ) or hasSquareOfZeroes ( r1 + 1 , c1 + 1 , r2 - 1 , c2 - 1 ) ) NEW_LINE cache [ key ] = ( cache [ key ] or hasSquareOfZeroes ( r1 , c1 + 1 , r2 - 1 , c2 ) or hasSquareOfZeroes ( r1 + 1 , c1 , r2 , c2 - 1 ) ) NEW_LINE cache [ key ] = ( cache [ key ] or hasSquareOfZeroes ( r1 + 1 , c1 + 1 , r2 , c2 ) or hasSquareOfZeroes ( r1 , c1 , r2 - 1 , c2 - 1 ) ) NEW_LINE return cache [ key ] NEW_LINE DEDENT def isSquareOfZeroes ( r1 , c1 , r2 , c2 ) : NEW_LINE INDENT global matrix NEW_LINE for row in range ( r1 , r2 + 1 ) : NEW_LINE INDENT if ( matrix [ row ] [ c1 ] != 0 or matrix [ row ] [ c2 ] != 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT for col in range ( c1 , c2 + 1 ) : NEW_LINE INDENT if ( matrix [ r1 ] [ col ] != 0 or matrix [ r2 ] [ col ] != 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT cache = { } NEW_LINE matrix = [ [ 1 , 1 , 1 , 0 , 1 , 0 ] , [ 0 , 0 , 0 , 0 , 0 , 1 ] , [ 0 , 1 , 1 , 1 , 0 , 1 ] , [ 0 , 0 , 0 , 1 , 0 , 1 ] , [ 0 , 1 , 1 , 1 , 0 , 1 ] , [ 0 , 0 , 0 , 0 , 0 , 1 ] ] NEW_LINE ans = squareOfZeroes ( ) NEW_LINE if ( ans == 1 ) : NEW_LINE INDENT print ( " True " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " False " ) NEW_LINE DEDENT DEDENT
Number of ways to obtain each numbers in range [ 1 , b + c ] by adding any two numbers in range [ a , b ] and [ b , c ] | Python3 program to calculate the number of ways ; Initialising the array with zeros . You can do using memset too . ; Printing the array ; Driver code
def CountWays ( a , b , c ) : NEW_LINE INDENT x = b + c + 1 ; NEW_LINE arr = [ 0 ] * x ; NEW_LINE for i in range ( a , b + 1 ) : NEW_LINE INDENT for j in range ( b , c + 1 ) : NEW_LINE INDENT arr [ i + j ] += 1 ; NEW_LINE DEDENT DEDENT for i in range ( 1 , x ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = 1 ; NEW_LINE b = 2 ; NEW_LINE c = 2 ; NEW_LINE CountWays ( a , b , c ) ; NEW_LINE DEDENT
Number of ways to obtain each numbers in range [ 1 , b + c ] by adding any two numbers in range [ a , b ] and [ b , c ] | Python3 program to calculate the number of ways ; 2 is added because sometimes we will decrease the value out of bounds . ; Initialising the array with zeros . ; Printing the array ; Driver code
def CountWays ( a , b , c ) : NEW_LINE INDENT x = b + c + 2 ; NEW_LINE arr = [ 0 ] * x ; NEW_LINE for i in range ( 1 , b + 1 ) : NEW_LINE arr [ i + b ] = arr [ i + b ] + 1 ; NEW_LINE arr [ i + c + 1 ] = arr [ i + c + 1 ] - 1 ; NEW_LINE for i in range ( 1 , x - 1 ) : NEW_LINE arr [ i ] += arr [ i - 1 ] ; NEW_LINE print ( arr [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = 1 ; NEW_LINE b = 2 ; NEW_LINE c = 2 ; NEW_LINE CountWays ( a , b , c ) ; NEW_LINE DEDENT
Count of distinct possible strings after performing given operations | Function that prints the number of different strings that can be formed ; Computing the length of the given string ; Base case ; Traverse the given string ; If two consecutive 1 ' s ▁ ▁ or ▁ 2' s are present ; Otherwise take the previous value ; Driver Code
def differentStrings ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE dp = [ 0 ] * ( n + 1 ) NEW_LINE dp [ 0 ] = dp [ 1 ] = 1 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( s [ i ] == s [ i - 1 ] and ( s [ i ] == '1' or s [ i ] == '2' ) ) : NEW_LINE INDENT dp [ i + 1 ] = dp [ i ] + dp [ i - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i + 1 ] = dp [ i ] NEW_LINE DEDENT DEDENT print ( dp [ n ] ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = "0111022110" NEW_LINE differentStrings ( S ) NEW_LINE DEDENT