codeparrot/xlcost-text-to-code huggingface dataset:
Check if a path exists from a given cell to any boundary element of the Matrix with sum of elements not exceeding K | Python3 program for the above approach ; Function to check if it is valid to move to the given index or not ; Otherwise return false ; Function to check if there exists a path from the cell ( X , Y ) of the matrix to any boundary cell with sum of elements at most K or not ; Base Case ; If K >= board [ X ] [ Y ] ; Stores the current element ; Mark the current cell as visited ; Visit all the adjacent cells ; Mark the cell unvisited ; Return false ; Driver Code
import sys NEW_LINE INT_MAX = sys . maxsize NEW_LINE def isValid ( board , i , j , K ) : NEW_LINE INDENT if ( board [ i ] [ j ] <= K ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def findPath ( board , X , Y , M , N , K ) : NEW_LINE INDENT if ( X < 0 or X == M or Y < 0 or Y == N ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( isValid ( board , X , Y , K ) ) : NEW_LINE INDENT board_XY = board [ X ] [ Y ] NEW_LINE board [ X ] [ Y ] = INT_MAX NEW_LINE if ( findPath ( board , X + 1 , Y , M , N , K - board_XY ) or findPath ( board , X - 1 , Y , M , N , K - board_XY ) or findPath ( board , X , Y + 1 , M , N , K - board_XY ) or findPath ( board , X , Y - 1 , M , N , K - board_XY ) ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT board [ X ] [ Y ] = board_XY NEW_LINE DEDENT return False NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT grid = [ [ 25 , 5 , 25 , 25 , 25 , 25 ] , [ 25 , 1 , 1 , 5 , 12 , 25 ] , [ 25 , 1 , 12 , 0 , 15 , 25 ] , [ 22 , 1 , 11 , 2 , 19 , 15 ] , [ 25 , 2 , 2 , 1 , 12 , 15 ] , [ 25 , 9 , 10 , 1 , 11 , 25 ] , [ 25 , 25 , 25 , 25 , 25 , 25 ] ] NEW_LINE K = 17 NEW_LINE M = len ( grid ) NEW_LINE N = len ( grid [ 0 ] ) NEW_LINE X = 2 NEW_LINE Y = 3 NEW_LINE if ( findPath ( grid , X , Y , M , N , K ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Sum of all subsets whose sum is a Perfect Number from a given array | Function to check is a given number is a perfect number or not ; Stores sum of divisors ; Add all divisors of x to sum_div ; If the sum of divisors is equal to the given number , return true ; Otherwise , return false ; Function to find the sum of all the subsets from an array whose sum is a perfect number ; Stores the total number of subsets , i . e . 2 ^ n ; Consider all numbers from 0 to 2 ^ n - 1 ; Consider array elements from positions of set bits in the binary representation of n ; If sum of chosen elements is a perfect number ; Driver Code
def isPerfect ( x ) : NEW_LINE INDENT sum_div = 1 NEW_LINE for i in range ( 2 , int ( x / 2 + 1 ) ) : NEW_LINE INDENT if ( x % i == 0 ) : NEW_LINE INDENT sum_div = sum_div + i NEW_LINE DEDENT DEDENT if ( sum_div == x ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT def subsetSum ( arr , n ) : NEW_LINE INDENT total = 1 << n NEW_LINE for i in range ( total ) : NEW_LINE INDENT sum = 0 NEW_LINE for j in range ( n ) : NEW_LINE INDENT if ( i & ( 1 << j ) != 0 ) : NEW_LINE INDENT sum = sum + arr [ j ] NEW_LINE DEDENT DEDENT if ( isPerfect ( sum ) ) : NEW_LINE INDENT print ( sum , " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT arr = [ 5 , 4 , 6 ] NEW_LINE N = len ( arr ) NEW_LINE subsetSum ( arr , N ) NEW_LINE
Pen Distribution Problem | Recursive function to play Game ; Box is empty , Game Over ! or Both have quit , Game Over ! ; P1 moves ; P2 moves ; Increment X ; Switch moves between P1 and P2 ; Function to find the number of pens remaining in the box and calculate score for each player ; Score of P1 ; Score of P2 ; Initialized to zero ; Move = 0 , P1 ' s ▁ turn ▁ ▁ Move ▁ = ▁ 1 , ▁ P2' s turn ; Has P1 quit ; Has P2 quit ; Recursively continue the game ; Driver Code
def solve ( N , P1 , P2 , X , Move , QuitP1 , QuitP2 ) : NEW_LINE INDENT if ( N == 0 or ( QuitP1 and QuitP2 ) ) : NEW_LINE INDENT print ( " Number ▁ of ▁ pens ▁ remaining ▁ in ▁ the ▁ box : ▁ " , N ) NEW_LINE print ( " Number ▁ of ▁ pens ▁ collected ▁ by ▁ P1 : ▁ " , P1 ) NEW_LINE print ( " Number ▁ of ▁ pens ▁ collected ▁ by ▁ P2 : ▁ " , P2 ) NEW_LINE return NEW_LINE DEDENT if ( Move == 0 and QuitP1 == False ) : NEW_LINE INDENT req_P1 = int ( pow ( 2 , X ) ) NEW_LINE if ( req_P1 <= N ) : NEW_LINE INDENT P1 += req_P1 NEW_LINE N -= req_P1 NEW_LINE DEDENT else : NEW_LINE INDENT QuitP1 = True NEW_LINE DEDENT DEDENT elif ( Move == 1 and QuitP2 == False ) : NEW_LINE INDENT req_P2 = int ( pow ( 3 , X ) ) NEW_LINE if ( req_P2 <= N ) : NEW_LINE INDENT P2 += req_P2 NEW_LINE N -= req_P2 NEW_LINE DEDENT else : NEW_LINE INDENT QuitP2 = True NEW_LINE DEDENT DEDENT X += 1 NEW_LINE if ( Move == 1 ) : NEW_LINE INDENT Move = 0 NEW_LINE DEDENT else : NEW_LINE INDENT Move = 1 NEW_LINE DEDENT solve ( N , P1 , P2 , X , Move , QuitP1 , QuitP2 ) NEW_LINE DEDENT def PenGame ( N ) : NEW_LINE INDENT P1 = 0 NEW_LINE P2 = 0 NEW_LINE X = 0 NEW_LINE Move = False NEW_LINE QuitP1 = False NEW_LINE QuitP2 = False NEW_LINE solve ( N , P1 , P2 , X , Move , QuitP1 , QuitP2 ) NEW_LINE DEDENT N = 22 NEW_LINE PenGame ( N ) NEW_LINE
Generate all N digit numbers having absolute difference as K between adjacent digits | Function that recursively finds the possible numbers and append into ans ; Base Case ; Check the sum of last digit and k less than or equal to 9 or not ; If k == 0 , then subtraction and addition does not make any difference Hence , subtraction is skipped ; Function to call checkUntil function for every integer from 1 to 9 ; check_util function recursively store all numbers starting from i ; Function to print the all numbers which satisfy the conditions ; Driver Code ; Given N and K ; To store the result ; Function call ; Print resultant numbers
def checkUntil ( num , K , N , ans ) : NEW_LINE INDENT if ( N == 1 ) : NEW_LINE INDENT ans . append ( num ) NEW_LINE return NEW_LINE DEDENT if ( ( num % 10 + K ) <= 9 ) : NEW_LINE INDENT checkUntil ( 10 * num + ( num % 10 + K ) , K , N - 1 , ans ) NEW_LINE DEDENT if ( K ) : NEW_LINE INDENT if ( ( num % 10 - K ) >= 0 ) : NEW_LINE INDENT checkUntil ( 10 * num + num % 10 - K , K , N - 1 , ans ) NEW_LINE DEDENT DEDENT DEDENT def check ( K , N , ans ) : NEW_LINE INDENT for i in range ( 1 , 10 ) : NEW_LINE INDENT checkUntil ( i , K , N , ans ) NEW_LINE DEDENT DEDENT def print_list ( ans ) : NEW_LINE INDENT for i in range ( len ( ans ) ) : NEW_LINE INDENT print ( ans [ i ] , end = " , ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 4 NEW_LINE K = 8 ; NEW_LINE ans = [ ] NEW_LINE check ( K , N , ans ) NEW_LINE print_list ( ans ) NEW_LINE DEDENT
Minimum Cost Path in a directed graph via given set of intermediate nodes | Stores minimum - cost of path from source ; Function to Perform BFS on graph g starting from vertex v ; If destination is reached ; Set flag to true ; Visit all the intermediate nodes ; If any intermediate node is not visited ; If all intermediate nodes are visited ; Update the minSum ; Mark the current node visited ; Traverse adjacent nodes ; Mark the neighbour visited ; Find minimum cost path considering the neighbour as the source ; Mark the neighbour unvisited ; Mark the source unvisited ; Driver Code ; Stores the graph ; Number of nodes ; Source ; Destination ; Keeps a check on visited and unvisited nodes ; Stores intemediate nodes ; If no path is found
minSum = 1000000000 NEW_LINE def getMinPathSum ( graph , visited , necessary , source , dest , currSum ) : NEW_LINE INDENT global minSum NEW_LINE if ( src == dest ) : NEW_LINE INDENT flag = True ; NEW_LINE for i in necessary : NEW_LINE INDENT if ( not visited [ i ] ) : NEW_LINE INDENT flag = False ; NEW_LINE break ; NEW_LINE DEDENT DEDENT if ( flag ) : NEW_LINE INDENT minSum = min ( minSum , currSum ) ; NEW_LINE DEDENT return ; NEW_LINE DEDENT else : NEW_LINE INDENT visited [ src ] = True ; NEW_LINE for node in graph [ src ] : NEW_LINE INDENT if not visited [ node [ 0 ] ] : NEW_LINE INDENT visited [ node [ 0 ] ] = True ; NEW_LINE getMinPathSum ( graph , visited , necessary , node [ 0 ] , dest , currSum + node [ 1 ] ) ; NEW_LINE visited [ node [ 0 ] ] = False ; NEW_LINE DEDENT DEDENT visited [ src ] = False ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT graph = dict ( ) NEW_LINE graph [ 0 ] = [ [ 1 , 2 ] , [ 2 , 3 ] , [ 3 , 2 ] ] ; NEW_LINE graph [ 1 ] = [ [ 4 , 4 ] , [ 0 , 1 ] ] ; NEW_LINE graph [ 2 ] = [ [ 4 , 5 ] , [ 5 , 6 ] ] ; NEW_LINE graph [ 3 ] = [ [ 5 , 7 ] , [ 0 , 1 ] ] ; NEW_LINE graph [ 4 ] = [ [ 6 , 4 ] ] ; NEW_LINE graph [ 5 ] = [ [ 6 , 2 ] ] ; NEW_LINE graph [ 6 ] = [ [ 7 , 11 ] ] ; NEW_LINE n = 7 ; NEW_LINE source = 0 ; NEW_LINE dest = 6 ; NEW_LINE visited = [ False for i in range ( n + 1 ) ] NEW_LINE necessary = [ 2 , 4 ] ; NEW_LINE getMinPathSum ( graph , visited , necessary , source , dest , 0 ) ; NEW_LINE if ( minSum == 1000000000 ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( minSum ) NEW_LINE DEDENT DEDENT
Unique subsequences of length K with given sum | Function to find all the subsequences of a given length and having sum S ; Termination condition ; Add value to sum ; Check if the resultant sum equals to target sum ; If true ; Print resultant array ; End this recursion stack ; Check all the combinations using backtracking ; Check all the combinations using backtracking ; Driver code ; Given array ; To store the subsequence ; Function call
def comb ( arr , Len , r , ipos , op , opos , Sum ) : NEW_LINE INDENT if ( opos == r ) : NEW_LINE INDENT sum2 = 0 NEW_LINE for i in range ( opos ) : NEW_LINE INDENT sum2 = sum2 + op [ i ] NEW_LINE DEDENT if ( Sum == sum2 ) : NEW_LINE INDENT for i in range ( opos ) : NEW_LINE INDENT print ( op [ i ] , end = " , ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT return NEW_LINE DEDENT if ( ipos < Len ) : NEW_LINE INDENT comb ( arr , Len , r , ipos + 1 , op , opos , Sum ) NEW_LINE op [ opos ] = arr [ ipos ] NEW_LINE comb ( arr , Len , r , ipos + 1 , op , opos + 1 , Sum ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 4 , 6 , 8 , 2 , 12 ] NEW_LINE K = 3 NEW_LINE S = 20 NEW_LINE N = len ( arr ) NEW_LINE op = [ 0 ] * N NEW_LINE comb ( arr , N , K , 0 , op , 0 , S ) NEW_LINE DEDENT
Traverse the matrix in Diagonally Bottum | Static variable for changing Row and column ; Flag variable for handling Bottum up diagonal traversing ; Recursive function to traverse the matrix Diagonally Bottom - up ; Base Condition ; Condition when to traverse Bottom Diagonal of the matrix ; Print matrix cell value ; Recursive function to traverse The matrix diagonally ; Recursive function to change diagonal ; Initialize the 5 x 5 matrix ; Function call for traversing matrix
k1 = 0 NEW_LINE k2 = 0 NEW_LINE flag = True NEW_LINE def traverseMatrixDiagonally ( m , i , j , row , col ) : NEW_LINE INDENT global k1 NEW_LINE global k2 NEW_LINE global flag NEW_LINE if ( i >= row or j >= col ) : NEW_LINE INDENT if ( flag ) : NEW_LINE INDENT a = k1 NEW_LINE k1 = k2 NEW_LINE k2 = a NEW_LINE if ( flag ) : NEW_LINE INDENT flag = False NEW_LINE DEDENT else : NEW_LINE INDENT flag = True NEW_LINE DEDENT k1 += 1 NEW_LINE DEDENT else : NEW_LINE INDENT a = k1 NEW_LINE k1 = k2 NEW_LINE k2 = a NEW_LINE if ( flag ) : NEW_LINE INDENT flag = False NEW_LINE DEDENT else : NEW_LINE INDENT flag = True NEW_LINE DEDENT DEDENT print ( ) NEW_LINE return False NEW_LINE DEDENT print ( m [ i ] [ j ] , end = " ▁ " ) NEW_LINE if ( traverseMatrixDiagonally ( m , i + 1 , j + 1 , row , col ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( traverseMatrixDiagonally ( m , k1 , k2 , row , col ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT return True NEW_LINE DEDENT mtrx = [ [ 10 , 11 , 12 , 13 , 14 ] , [ 15 , 16 , 17 , 18 , 19 ] , [ 20 , 21 , 22 , 23 , 24 ] , [ 25 , 26 , 27 , 28 , 29 ] , [ 30 , 31 , 32 , 33 , 34 ] ] NEW_LINE traverseMatrixDiagonally ( mtrx , 0 , 0 , 5 , 5 ) NEW_LINE
Minimum colors required such that edges forming cycle do not have same color | Python3 implementation to find the minimum colors required to such that edges forming cycle don 't have same color ; Variable to store the graph ; To store that the vertex is visited or not ; Boolean Value to store that graph contains cycle or not ; Variable to store the color of the edges of the graph ; Function to traverse graph using DFS Traversal ; Loop to iterate for all edges from the source vertex ; If the vertex is not visited ; Condition to check cross and forward edges of the graph ; Presence of Back Edge ; Driver Code ; Loop to run DFS Traversal on vertex which is not visited ; Loop to print the colors of the edges
n = 5 NEW_LINE m = 7 ; NEW_LINE g = [ [ ] for i in range ( m ) ] NEW_LINE col = [ 0 for i in range ( n ) ] ; NEW_LINE cyc = True NEW_LINE res = [ 0 for i in range ( m ) ] ; NEW_LINE def dfs ( v ) : NEW_LINE INDENT col [ v ] = 1 ; NEW_LINE for p in g [ v ] : NEW_LINE INDENT to = p [ 0 ] NEW_LINE id = p [ 1 ] ; NEW_LINE if ( col [ to ] == 0 ) : NEW_LINE INDENT dfs ( to ) ; NEW_LINE res [ id ] = 1 ; NEW_LINE DEDENT elif ( col [ to ] == 2 ) : NEW_LINE INDENT res [ id ] = 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT res [ id ] = 2 ; NEW_LINE cyc = True ; NEW_LINE DEDENT DEDENT col [ v ] = 2 ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT g [ 0 ] . append ( [ 1 , 0 ] ) ; NEW_LINE g [ 0 ] . append ( [ 2 , 1 ] ) ; NEW_LINE g [ 1 ] . append ( [ 2 , 2 ] ) ; NEW_LINE g [ 1 ] . append ( [ 3 , 3 ] ) ; NEW_LINE g [ 2 ] . append ( [ 3 , 4 ] ) ; NEW_LINE g [ 3 ] . append ( [ 4 , 5 ] ) ; NEW_LINE g [ 4 ] . append ( [ 2 , 6 ] ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( col [ i ] == 0 ) : NEW_LINE INDENT dfs ( i ) ; NEW_LINE DEDENT DEDENT print ( 2 if cyc else 1 ) NEW_LINE for i in range ( m ) : NEW_LINE INDENT print ( res [ i ] , end = ' ▁ ' ) NEW_LINE DEDENT DEDENT
Perfect Sum Problem | Function to print the subsets whose sum is equal to the given target K ; Create the new array with size equal to array set [ ] to create binary array as per n ( decimal number ) ; Convert the array into binary array ; Calculate the sum of this subset ; Check whether sum is equal to target if it is equal , then print the subset ; Function to find the subsets with sum K ; Calculate the total no . of subsets ; Run loop till total no . of subsets and call the function for each subset ; Driver code
def sumSubsets ( sets , n , target ) : NEW_LINE INDENT x = [ 0 ] * len ( sets ) ; NEW_LINE j = len ( sets ) - 1 ; NEW_LINE while ( n > 0 ) : NEW_LINE INDENT x [ j ] = n % 2 ; NEW_LINE n = n // 2 ; NEW_LINE j -= 1 ; NEW_LINE DEDENT sum = 0 ; NEW_LINE for i in range ( len ( sets ) ) : NEW_LINE INDENT if ( x [ i ] == 1 ) : NEW_LINE INDENT sum += sets [ i ] ; NEW_LINE DEDENT DEDENT if ( sum == target ) : NEW_LINE INDENT print ( " { " , end = " " ) ; NEW_LINE for i in range ( len ( sets ) ) : NEW_LINE INDENT if ( x [ i ] == 1 ) : NEW_LINE INDENT print ( sets [ i ] , end = " , ▁ " ) ; NEW_LINE DEDENT DEDENT print ( " } , ▁ " , end = " " ) ; NEW_LINE DEDENT DEDENT def findSubsets ( arr , K ) : NEW_LINE INDENT x = pow ( 2 , len ( arr ) ) ; NEW_LINE for i in range ( 1 , x ) : NEW_LINE INDENT sumSubsets ( arr , i , K ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 5 , 10 , 12 , 13 , 15 , 18 ] ; NEW_LINE K = 30 ; NEW_LINE findSubsets ( arr , K ) ; NEW_LINE DEDENT
Sum of subsets of all the subsets of an array | O ( N ) | Python3 implementation of the approach ; To store factorial values ; Function to return ncr ; Function to return the required sum ; Initialising factorial ; Multiplier ; Finding the value of multipler according to the formula ; To store the final answer ; Calculate the final answer ; Driver code
maxN = 10 NEW_LINE fact = [ 0 ] * maxN ; NEW_LINE def ncr ( n , r ) : NEW_LINE INDENT return ( fact [ n ] // fact [ r ] ) // fact [ n - r ] ; NEW_LINE DEDENT def findSum ( arr , n ) : NEW_LINE INDENT fact [ 0 ] = 1 ; NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT fact [ i ] = i * fact [ i - 1 ] ; NEW_LINE DEDENT mul = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT mul += ( 2 ** i ) * ncr ( n - 1 , i ) ; NEW_LINE DEDENT ans = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT ans += mul * arr [ i ] ; NEW_LINE DEDENT return ans ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 1 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( findSum ( arr , n ) ) ; NEW_LINE DEDENT
Count number of ways to reach destination in a maze | Python3 implementation of the approach ; Function to return the count of possible paths in a maze [ R ] [ C ] from ( 0 , 0 ) to ( R - 1 , C - 1 ) that do not pass through any of the marked cells ; If the initial cell is blocked , there is no way of moving anywhere ; Initializing the leftmost column ; If we encounter a blocked cell in leftmost row , there is no way of visiting any cell directly below it . ; Similarly initialize the topmost row ; If we encounter a blocked cell in bottommost row , there is no way of visiting any cell directly below it . ; The only difference is that if a cell is - 1 , simply ignore it else recursively compute count value maze [ i ] [ j ] ; If blockage is found , ignore this cell ; If we can reach maze [ i ] [ j ] from maze [ i - 1 ] [ j ] then increment count . ; If we can reach maze [ i ] [ j ] from maze [ i ] [ j - 1 ] then increment count . ; If the final cell is blocked , output 0 , otherwise the answer ; Function to return the count of all possible paths from ( 0 , 0 ) to ( n - 1 , m - 1 ) ; We have to calculate m + n - 2 C n - 1 here which will be ( m + n - 2 ) ! / ( n - 1 ) ! ( m - 1 ) ! ; Function to return the total count of paths from ( 0 , 0 ) to ( n - 1 , m - 1 ) that pass through at least one of the marked cells ; Total count of paths - Total paths that do not pass through any of the marked cell ; return answer ; Driver code
R = 4 NEW_LINE C = 4 NEW_LINE def countPaths ( maze ) : NEW_LINE INDENT if ( maze [ 0 ] [ 0 ] == - 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT for i in range ( R ) : NEW_LINE INDENT if ( maze [ i ] [ 0 ] == 0 ) : NEW_LINE INDENT maze [ i ] [ 0 ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT for i in range ( 1 , C ) : NEW_LINE INDENT if ( maze [ 0 ] [ i ] == 0 ) : NEW_LINE INDENT maze [ 0 ] [ i ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT for i in range ( 1 , R ) : NEW_LINE INDENT for j in range ( 1 , C ) : NEW_LINE INDENT if ( maze [ i ] [ j ] == - 1 ) : NEW_LINE INDENT continue NEW_LINE DEDENT if ( maze [ i - 1 ] [ j ] > 0 ) : NEW_LINE INDENT maze [ i ] [ j ] = ( maze [ i ] [ j ] + maze [ i - 1 ] [ j ] ) NEW_LINE DEDENT if ( maze [ i ] [ j - 1 ] > 0 ) : NEW_LINE INDENT maze [ i ] [ j ] = ( maze [ i ] [ j ] + maze [ i ] [ j - 1 ] ) NEW_LINE DEDENT DEDENT DEDENT if ( maze [ R - 1 ] [ C - 1 ] > 0 ) : NEW_LINE INDENT return maze [ R - 1 ] [ C - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT def numberOfPaths ( m , n ) : NEW_LINE INDENT path = 1 NEW_LINE for i in range ( n , m + n - 1 ) : NEW_LINE INDENT path *= i NEW_LINE path //= ( i - n + 1 ) NEW_LINE DEDENT return path NEW_LINE DEDENT def solve ( maze ) : NEW_LINE INDENT ans = ( numberOfPaths ( R , C ) - countPaths ( maze ) ) NEW_LINE return ans NEW_LINE DEDENT maze = [ [ 0 , 0 , 0 , 0 ] , [ 0 , - 1 , 0 , 0 ] , [ - 1 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 ] ] NEW_LINE print ( solve ( maze ) ) NEW_LINE
Rat in a Maze with multiple steps or jump allowed | Maze size ; A utility function to prsolution matrix sol ; A utility function to check if x , y is valid index for N * N maze ; if ( x , y outside maze ) return false ; This function solves the Maze problem using Backtracking . It mainly uses solveMazeUtil ( ) to solve the problem . It returns false if no path is possible , otherwise return True and prints the path in the form of 1 s . Please note that there may be more than one solutions , this function prints one of the feasible solutions . ; A recursive utility function to solve Maze problem ; if ( x , y is goal ) return True ; Check if maze [ x ] [ y ] is valid ; mark x , y as part of solution path ; Move forward in x direction ; Move forward in x direction ; If moving in x direction doesn 't give  solution then Move down in y direction  ; If none of the above movements work then BACKTRACK : unmark x , y as part of solution path ; Driver Code
N = 4 NEW_LINE def printSolution ( sol ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT for j in range ( N ) : NEW_LINE INDENT print ( sol [ i ] [ j ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT def isSafe ( maze , x , y ) : NEW_LINE INDENT if ( x >= 0 and x < N and y >= 0 and y < N and maze [ x ] [ y ] != 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def solveMaze ( maze ) : NEW_LINE INDENT sol = [ [ 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 ] ] NEW_LINE if ( solveMazeUtil ( maze , 0 , 0 , sol ) == False ) : NEW_LINE INDENT print ( " Solution ▁ doesn ' t ▁ exist " ) NEW_LINE return False NEW_LINE DEDENT printSolution ( sol ) NEW_LINE return True NEW_LINE DEDENT def solveMazeUtil ( maze , x , y , sol ) : NEW_LINE INDENT if ( x == N - 1 and y == N - 1 ) : NEW_LINE INDENT sol [ x ] [ y ] = 1 NEW_LINE return True NEW_LINE DEDENT if ( isSafe ( maze , x , y ) == True ) : NEW_LINE INDENT sol [ x ] [ y ] = 1 NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT if ( i <= maze [ x ] [ y ] ) : NEW_LINE INDENT if ( solveMazeUtil ( maze , x + i , y , sol ) == True ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( solveMazeUtil ( maze , x , y + i , sol ) == True ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT DEDENT sol [ x ] [ y ] = 0 NEW_LINE return False NEW_LINE DEDENT return False NEW_LINE DEDENT maze = [ [ 2 , 1 , 0 , 0 ] , [ 3 , 0 , 0 , 1 ] , [ 0 , 1 , 0 , 1 ] , [ 0 , 0 , 0 , 1 ] ] NEW_LINE solveMaze ( maze ) NEW_LINE
Prime numbers after prime P with sum S | Python Program to print all N primes after prime P whose sum equals S ; vector to store prime and N primes whose sum equals given S ; function to check prime number ; square root of x ; since 1 is not prime number ; if any factor is found return false ; no factor found ; function to display N primes whose sum equals S ; function to evaluate all possible N primes whose sum equals S ; if total equals S And total is reached using N primes ; display the N primes ; if total is greater than S or if index has reached last element ; add prime [ index ] to set vector ; include the ( index ) th prime to total ; remove element from set vector ; exclude ( index ) th prime ; function to generate all primes ; all primes less than S itself ; if i is prime add it to prime vector ; if primes are less than N ; Driver Code
import math NEW_LINE set = [ ] NEW_LINE prime = [ ] NEW_LINE def isPrime ( x ) : NEW_LINE INDENT sqroot = int ( math . sqrt ( x ) ) NEW_LINE flag = True NEW_LINE if ( x == 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( 2 , sqroot + 1 ) : NEW_LINE INDENT if ( x % i == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def display ( ) : NEW_LINE INDENT global set , prime NEW_LINE length = len ( set ) NEW_LINE for i in range ( 0 , length ) : NEW_LINE INDENT print ( set [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT def primeSum ( total , N , S , index ) : NEW_LINE INDENT global set , prime NEW_LINE if ( total == S and len ( set ) == N ) : NEW_LINE INDENT display ( ) NEW_LINE return NEW_LINE DEDENT if ( total > S or index == len ( prime ) ) : NEW_LINE INDENT return NEW_LINE DEDENT set . append ( prime [ index ] ) NEW_LINE primeSum ( total + prime [ index ] , N , S , index + 1 ) NEW_LINE set . pop ( ) NEW_LINE primeSum ( total , N , S , index + 1 ) NEW_LINE DEDENT def allPrime ( N , S , P ) : NEW_LINE INDENT global set , prime NEW_LINE for i in range ( P + 1 , S + 1 ) : NEW_LINE INDENT if ( isPrime ( i ) ) : NEW_LINE INDENT prime . append ( i ) NEW_LINE DEDENT DEDENT if ( len ( prime ) < N ) : NEW_LINE INDENT return NEW_LINE DEDENT primeSum ( 0 , N , S , 0 ) NEW_LINE DEDENT S = 54 NEW_LINE N = 2 NEW_LINE P = 3 NEW_LINE allPrime ( N , S , P ) NEW_LINE