codeparrot/xlcost-text-to-code huggingface dataset:
Minimum Bipartite Groups | ''Function to find the height sizeof the current component with vertex s ; Visit the current Node ; Call DFS recursively to find the maximum height of current CC ; If the node is not visited then the height recursively for next element ; Function to find the minimum Groups ; Initialise with visited array ; To find the minimum groups ; Traverse all the non visited Node and calculate the height of the tree with current node as a head ; If the current is not visited therefore , we get another CC ; Return the minimum bipartite matching ; Function that adds the current edges in the given graph ; Driver code ; Adjacency List ; Adding edges to List
import sys NEW_LINE def height ( s , adj , visited ) : NEW_LINE INDENT visited [ s ] = 1 NEW_LINE h = 0 NEW_LINE for child in adj [ s ] : NEW_LINE INDENT if ( visited [ child ] == 0 ) : NEW_LINE INDENT h = max ( h , 1 + height ( child , adj , visited ) ) NEW_LINE DEDENT DEDENT return h NEW_LINE DEDENT def minimumGroups ( adj , N ) : NEW_LINE INDENT visited = [ 0 for i in range ( N + 1 ) ] NEW_LINE groups = - sys . maxsize NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT if ( visited [ i ] == 0 ) : NEW_LINE INDENT comHeight = height ( i , adj , visited ) NEW_LINE groups = max ( groups , comHeight ) NEW_LINE DEDENT DEDENT return groups NEW_LINE DEDENT def addEdge ( adj , u , v ) : NEW_LINE INDENT adj [ u ] . append ( v ) NEW_LINE adj [ v ] . append ( u ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 5 NEW_LINE adj = [ [ ] for i in range ( N + 1 ) ] NEW_LINE addEdge ( adj , 1 , 2 ) NEW_LINE addEdge ( adj , 3 , 2 ) NEW_LINE addEdge ( adj , 4 , 3 ) NEW_LINE print ( minimumGroups ( adj , N ) ) NEW_LINE DEDENT
Sum of subsets of all the subsets of an array | O ( 2 ^ N ) | store the answer ; Function to sum of all subsets of a given array ; Function to generate the subsets ; Base - case ; Finding the sum of all the subsets of the generated subset ; Recursively accepting and rejecting the current number ; Driver code
c = [ ] NEW_LINE ans = 0 NEW_LINE def subsetSum ( ) : NEW_LINE INDENT global ans NEW_LINE L = len ( c ) NEW_LINE mul = pow ( 2 , L - 1 ) NEW_LINE i = 0 NEW_LINE while ( i < len ( c ) ) : NEW_LINE INDENT ans += c [ i ] * mul NEW_LINE i += 1 NEW_LINE DEDENT DEDENT def subsetGen ( arr , i , n ) : NEW_LINE INDENT if ( i == n ) : NEW_LINE INDENT subsetSum ( ) NEW_LINE return NEW_LINE DEDENT subsetGen ( arr , i + 1 , n ) NEW_LINE c . append ( arr [ i ] ) NEW_LINE subsetGen ( arr , i + 1 , n ) NEW_LINE c . pop ( ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE subsetGen ( arr , 0 , n ) NEW_LINE print ( ans ) NEW_LINE DEDENT
Find the maximum possible value of a [ i ] % a [ j ] over all pairs of i and j | Function that returns the second largest element in the array if exists , else 0 ; There must be at least two elements ; To store the maximum and the second maximum element from the array ; If current element is greater than first then update both first and second ; If arr [ i ] is in between first and second then update second ; No second maximum found ; Driver code
def getMaxValue ( arr , arr_size ) : NEW_LINE INDENT if ( arr_size < 2 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT first = - sys . maxsize - 1 NEW_LINE second = - sys . maxsize - 1 NEW_LINE for i in range ( arr_size ) : NEW_LINE INDENT if ( arr [ i ] > first ) : NEW_LINE INDENT second = first NEW_LINE first = arr [ i ] NEW_LINE DEDENT elif ( arr [ i ] > second and arr [ i ] != first ) : NEW_LINE INDENT second = arr [ i ] NEW_LINE DEDENT DEDENT if ( second == - sys . maxsize - 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT return second NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 4 , 5 , 1 , 8 ] NEW_LINE n = len ( arr ) NEW_LINE print ( getMaxValue ( arr , n ) ) NEW_LINE DEDENT
Maximize the value of the given expression | Function to return the maximum result ; To store the count of negative integers ; Sum of all the three integers ; Product of all the three integers ; To store the smallest and the largest among all the three integers ; Calculate the count of negative integers ; When all three are positive integers ; For single negative integer ; For two negative integers ; For three negative integers ; Driver Code
def maximumResult ( a , b , c ) : NEW_LINE INDENT countOfNegative = 0 NEW_LINE Sum = a + b + c NEW_LINE product = a * b * c NEW_LINE largest = max ( a , b , c ) NEW_LINE smallest = min ( a , b , c ) NEW_LINE if a < 0 : NEW_LINE INDENT countOfNegative += 1 NEW_LINE DEDENT if b < 0 : NEW_LINE INDENT countOfNegative += 1 NEW_LINE DEDENT if c < 0 : NEW_LINE INDENT countOfNegative += 1 NEW_LINE DEDENT if countOfNegative == 0 : NEW_LINE INDENT return ( Sum - largest ) * largest NEW_LINE DEDENT elif countOfNegative == 1 : NEW_LINE INDENT return ( product // smallest ) + smallest NEW_LINE DEDENT elif countOfNegative == 2 : NEW_LINE INDENT return ( product // largest ) + largest NEW_LINE DEDENT elif countOfNegative == 3 : NEW_LINE INDENT return ( Sum - smallest ) * smallest NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a , b , c = - 2 , - 1 , - 4 NEW_LINE print ( maximumResult ( a , b , c ) ) NEW_LINE DEDENT
Maximum students to pass after giving bonus to everybody and not exceeding 100 marks | Function to return the number of students that can pass ; maximum marks ; maximum bonus marks that can be given ; counting the number of students that can pass ; Driver code
def check ( n , marks ) : NEW_LINE INDENT x = max ( marks ) NEW_LINE bonus = 100 - x NEW_LINE c = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( marks [ i ] + bonus >= 50 ) : NEW_LINE INDENT c += 1 NEW_LINE DEDENT DEDENT return c NEW_LINE DEDENT n = 5 NEW_LINE marks = [ 0 , 21 , 83 , 45 , 64 ] NEW_LINE print ( check ( n , marks ) ) NEW_LINE
Sum of first N natural numbers which are not powers of K | Function to return the sum of first n natural numbers which are not positive powers of k ; sum of first n natural numbers ; subtract all positive powers of k which are less than n ; next power of k ; Driver code
def find_sum ( n , k ) : NEW_LINE INDENT total_sum = ( n * ( n + 1 ) ) // 2 NEW_LINE power = k NEW_LINE while power <= n : NEW_LINE INDENT total_sum -= power NEW_LINE power *= k NEW_LINE DEDENT return total_sum NEW_LINE DEDENT n = 11 ; k = 2 NEW_LINE print ( find_sum ( n , k ) ) NEW_LINE
Minimum number of operations required to delete all elements of the array | Python 3 implementation of the above approach ; function to find minimum operations ; sort array ; prepare hash of array ; Driver Code
MAX = 10000 NEW_LINE hashTable = [ 0 ] * MAX NEW_LINE def minOperations ( arr , n ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT hashTable [ arr [ i ] ] += 1 NEW_LINE DEDENT res = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( hashTable [ arr [ i ] ] ) : NEW_LINE INDENT for j in range ( i , n ) : NEW_LINE INDENT if ( arr [ j ] % arr [ i ] == 0 ) : NEW_LINE INDENT hashTable [ arr [ j ] ] = 0 NEW_LINE DEDENT DEDENT res += 1 NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 4 , 6 , 2 , 8 , 7 , 21 , 24 , 49 , 44 ] NEW_LINE n = len ( arr ) NEW_LINE print ( minOperations ( arr , n ) ) NEW_LINE DEDENT
Sorting array with reverse around middle | Python 3 program to find possibility to sort by multiple subarray reverse operarion ; making the copy of the original array ; sorting the copied array ; checking mirror image of elements of sorted copy array and equivalent element of original array ; Driver code
def ifPossible ( arr , n ) : NEW_LINE INDENT cp = [ 0 ] * n NEW_LINE cp = arr NEW_LINE cp . sort ( ) NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( not ( arr [ i ] == cp [ i ] ) and not ( arr [ n - 1 - i ] == cp [ i ] ) ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT arr = [ 1 , 7 , 6 , 4 , 5 , 3 , 2 , 8 ] NEW_LINE n = len ( arr ) NEW_LINE if ( ifPossible ( arr , n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Program for Shortest Job First ( SJF ) scheduling | Set 2 ( Preemptive ) | Function to find the waiting time for all processes ; Copy the burst time into rt [ ] ; Process until all processes gets completed ; Find process with minimum remaining time among the processes that arrives till the current time ` ; Reduce remaining time by one ; Update minimum ; If a process gets completely executed ; Increment complete ; Find finish time of current process ; Calculate waiting time ; Increment time ; Function to calculate turn around time ; Calculating turnaround time ; Function to calculate average waiting and turn - around times . ; Function to find waiting time of all processes ; Function to find turn around time for all processes ; Display processes along with all details ; Driver code ; Process id 's
def findWaitingTime ( processes , n , wt ) : NEW_LINE INDENT rt = [ 0 ] * n NEW_LINE for i in range ( n ) : NEW_LINE INDENT rt [ i ] = processes [ i ] [ 1 ] NEW_LINE DEDENT complete = 0 NEW_LINE t = 0 NEW_LINE minm = 999999999 NEW_LINE short = 0 NEW_LINE check = False NEW_LINE while ( complete != n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT if ( ( processes [ j ] [ 2 ] <= t ) and ( rt [ j ] < minm ) and rt [ j ] > 0 ) : NEW_LINE INDENT minm = rt [ j ] NEW_LINE short = j NEW_LINE check = True NEW_LINE DEDENT DEDENT if ( check == False ) : NEW_LINE INDENT t += 1 NEW_LINE continue NEW_LINE DEDENT rt [ short ] -= 1 NEW_LINE minm = rt [ short ] NEW_LINE if ( minm == 0 ) : NEW_LINE INDENT minm = 999999999 NEW_LINE DEDENT if ( rt [ short ] == 0 ) : NEW_LINE INDENT complete += 1 NEW_LINE check = False NEW_LINE fint = t + 1 NEW_LINE wt [ short ] = ( fint - proc [ short ] [ 1 ] - proc [ short ] [ 2 ] ) NEW_LINE if ( wt [ short ] < 0 ) : NEW_LINE INDENT wt [ short ] = 0 NEW_LINE DEDENT DEDENT t += 1 NEW_LINE DEDENT DEDENT def findTurnAroundTime ( processes , n , wt , tat ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT tat [ i ] = processes [ i ] [ 1 ] + wt [ i ] NEW_LINE DEDENT DEDENT def findavgTime ( processes , n ) : NEW_LINE INDENT wt = [ 0 ] * n NEW_LINE tat = [ 0 ] * n NEW_LINE findWaitingTime ( processes , n , wt ) NEW_LINE findTurnAroundTime ( processes , n , wt , tat ) NEW_LINE print ( " Processes ▁ Burst ▁ Time ▁ Waiting " , " Time ▁ Turn - Around ▁ Time " ) NEW_LINE total_wt = 0 NEW_LINE total_tat = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT total_wt = total_wt + wt [ i ] NEW_LINE total_tat = total_tat + tat [ i ] NEW_LINE print ( " ▁ " , processes [ i ] [ 0 ] , " TABSYMBOL TABSYMBOL " , processes [ i ] [ 1 ] , " TABSYMBOL TABSYMBOL " , wt [ i ] , " TABSYMBOL TABSYMBOL " , tat [ i ] ) NEW_LINE DEDENT print ( " Average waiting time = % .5 f   " % ( total_wt / n ) ) NEW_LINE print ( " Average ▁ turn ▁ around ▁ time ▁ = ▁ " , total_tat / n ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT proc = [ [ 1 , 6 , 1 ] , [ 2 , 8 , 1 ] , [ 3 , 7 , 2 ] , [ 4 , 3 , 3 ] ] NEW_LINE n = 4 NEW_LINE findavgTime ( proc , n ) NEW_LINE DEDENT
Count N | Function to count N - length strings consisting of vowels only sorted lexicographically ; Initializing vector to store count of strings . ; Summing up the total number of combinations . ; Return the result ; Function Call
def findNumberOfStrings ( N ) : NEW_LINE INDENT counts = [ ] NEW_LINE for i in range ( 5 ) : NEW_LINE INDENT counts . append ( 1 ) NEW_LINE DEDENT for i in range ( 2 , N + 1 ) : NEW_LINE INDENT for j in range ( 3 , - 1 , - 1 ) : NEW_LINE INDENT counts [ j ] += counts [ j + 1 ] NEW_LINE DEDENT DEDENT ans = 0 NEW_LINE for c in counts : NEW_LINE INDENT ans += c NEW_LINE DEDENT return ans NEW_LINE DEDENT N = 2 NEW_LINE print ( findNumberOfStrings ( N ) ) NEW_LINE
Count N | Stores the value of overlapping states ; Function to check whether a number have only digits X or Y or not ; Until sum is positive ; If any digit is not X or Y then return 0 ; Return 1 ; Function to find the count of numbers that are formed by digits X and Y and whose sum of digit also have digit X or Y ; Initialize dp array ; Base Case ; Check if sum of digits formed by only X or Y ; Return the already computed ; Place the digit X at the current position ; Place the digit Y at the current position ; Update current state result ; Driver Code ; Function Call
dp = [ [ - 1 for x in range ( 9000 + 5 ) ] for y in range ( 1000 + 5 ) ] NEW_LINE mod = 1000000007 NEW_LINE def check ( sum , x , y ) : NEW_LINE INDENT while ( sum > 0 ) : NEW_LINE INDENT ln = sum % 10 NEW_LINE if ( ln != x and ln != y ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT sum //= 10 NEW_LINE DEDENT return 1 NEW_LINE DEDENT def countNumbers ( n , x , y , sum ) : NEW_LINE INDENT global dp NEW_LINE if ( n == 0 ) : NEW_LINE INDENT return check ( sum , x , y ) NEW_LINE DEDENT if ( dp [ n ] [ sum ] != - 1 ) : NEW_LINE INDENT return dp [ n ] [ sum ] % mod NEW_LINE DEDENT option1 = countNumbers ( n - 1 , x , y , sum + x ) % mod NEW_LINE option2 = countNumbers ( n - 1 , x , y , sum + y ) % mod NEW_LINE dp [ n ] [ sum ] = ( option1 + option2 ) % mod NEW_LINE return dp [ n ] [ sum ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 3 NEW_LINE X = 1 NEW_LINE Y = 5 NEW_LINE print ( countNumbers ( N , X , Y , 0 ) % mod ) NEW_LINE DEDENT
Minimum cost to generate any permutation of the given string | Function to find the minimum cost to form any permutation of string s ; Base Case ; Return the precomputed state ; Iterate over the string and check all possible characters available for current position ; Check if character can be placed at current position ; As there is no previous character so the cost for 1 st character is 0 ; Find the cost of current character and move to next position ; Store the answer for each current state ; Function that returns True if the current bit is set ; Function that generates any permutation of the given string with minimum cost ; Initialize dp table ; Set all the bits of the current character id ; Minimum cost of generating the permutation ; Driver Code ; Function Call
def solve ( a , s , n , prev , mask , dp ) : NEW_LINE INDENT if ( mask == 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( dp [ mask ] [ prev + 1 ] != - 1 ) : NEW_LINE INDENT return dp [ mask ] [ prev + 1 ] ; NEW_LINE DEDENT ans = 10000 ; NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT id = ord ( s [ i ] ) - ord ( ' a ' ) ; NEW_LINE if ( check ( mask , id ) ) : NEW_LINE INDENT if ( prev == - 1 ) : NEW_LINE INDENT ans = min ( ans , solve ( a , s , n , id , mask ^ ( 1 << id ) , dp ) ) ; NEW_LINE DEDENT else : NEW_LINE INDENT ans = min ( ans , a [ prev ] [ id ] + solve ( a , s , n , id , mask ^ ( 1 << id ) , dp ) ) ; NEW_LINE DEDENT DEDENT DEDENT dp [ mask ] [ prev + 1 ] = ans ; NEW_LINE return ans ; NEW_LINE DEDENT def check ( mask , i ) : NEW_LINE INDENT c = ( mask & ( 1 << i ) ) ; NEW_LINE return c != 0 ; NEW_LINE DEDENT def generatePermutation ( mask , n , a , s ) : NEW_LINE INDENT dp = [ [ - 1 for i in range ( n + 5 ) ] for j in range ( ( 1 << n ) + 5 ) ] NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT id = ord ( s [ i ] ) - ord ( ' a ' ) ; NEW_LINE mask |= ( 1 << id ) ; NEW_LINE DEDENT print ( solve ( a , s , n , - 1 , mask , dp ) ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 ; NEW_LINE str = " abcde " ; NEW_LINE mat = [ [ 0 , 5 , 1 , 5 , 3 ] , [ 4 , 0 , 9 , 4 , 2 ] , [ 7 , 9 , 0 , 10 , 7 ] , [ 1 , 2 , 8 , 0 , 2 ] , [ 3 , 9 , 7 , 7 , 0 ] ] ; NEW_LINE generatePermutation ( 0 , N , mat , str ) ; NEW_LINE DEDENT
Minimize Cost to reduce the Array to a single element by given operations | Python3 program for the above approach ; Function to minimize the cost to add the array elements to a single element ; Check if the value is already stored in the array ; Compute left subproblem ; Compute left subproblem ; Calculate minimum cost ; Store the answer to avoid recalculation ; Function to generate the cost using Prefix Sum Array technique ; Function to combine the sum of the two subproblems ; Driver Code ; Initialise dp array ; Preprocessing the array
inf = 10000000 NEW_LINE def minCost ( a , i , j , k , prefix , dp ) : NEW_LINE INDENT if ( i >= j ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( dp [ i ] [ j ] != - 1 ) : NEW_LINE INDENT return dp [ i ] [ j ] NEW_LINE DEDENT best_cost = inf NEW_LINE for pos in range ( i , j ) : NEW_LINE INDENT left = minCost ( a , i , pos , k , prefix , dp ) NEW_LINE right = minCost ( a , pos + 1 , j , k , prefix , dp ) NEW_LINE best_cost = min ( best_cost , left + right + ( k * Combine ( prefix , i , j ) ) ) NEW_LINE DEDENT dp [ i ] [ j ] = best_cost NEW_LINE return dp [ i ] [ j ] NEW_LINE DEDENT def preprocess ( a , n ) : NEW_LINE INDENT p = [ 0 ] * n NEW_LINE p [ 0 ] = a [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT p [ i ] = p [ i - 1 ] + a [ i ] NEW_LINE DEDENT return p NEW_LINE DEDENT def Combine ( p , i , j ) : NEW_LINE INDENT if ( i == 0 ) : NEW_LINE INDENT return p [ j ] NEW_LINE DEDENT else : NEW_LINE INDENT return p [ j ] - p [ i - 1 ] NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 4 NEW_LINE a = [ 4 , 5 , 6 , 7 ] NEW_LINE k = 3 NEW_LINE dp = [ [ - 1 for x in range ( n + 1 ) ] for y in range ( n + 1 ) ] NEW_LINE prefix = preprocess ( a , n ) NEW_LINE print ( minCost ( a , 0 , n - 1 , k , prefix , dp ) ) NEW_LINE DEDENT
Minimize operations required to obtain N | ''Function to find the minimum number of operations ; Storing the minimum operations to obtain all numbers up to N ; Initial state ; Iterate for the remaining numbers ; If the number can be obtained by multiplication by 2 ; If the number can be obtained by multiplication by 3 ; Obtaining the number by adding 1 ; Return the minimum operations to obtain n ; Driver code
import NEW_LINE def minOperations ( n ) : NEW_LINE INDENT dp = [ sys . maxsize ] * ( n + 1 ) NEW_LINE dp [ 1 ] = 0 NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT if i % 2 == 0 : NEW_LINE INDENT x = dp [ i // 2 ] NEW_LINE if x + 1 < dp [ i ] : NEW_LINE INDENT dp [ i ] = x + 1 NEW_LINE DEDENT DEDENT if i % 3 == 0 : NEW_LINE INDENT x = dp [ i // 3 ] NEW_LINE if x + 1 < dp [ i ] : NEW_LINE INDENT dp [ i ] = x + 1 NEW_LINE DEDENT DEDENT x = dp [ i - 1 ] NEW_LINE if x + 1 < dp [ i ] : NEW_LINE INDENT dp [ i ] = x + 1 NEW_LINE DEDENT DEDENT return dp [ n ] NEW_LINE DEDENT n = 15 NEW_LINE print ( minOperations ( n ) ) NEW_LINE
Longest Reverse Bitonic Sequence | Function to return the length of the Longest Reverse Bitonic Subsequence in the array ; Allocate memory for LIS [ ] and initialize LIS values as 1 for all indexes ; Compute LIS values from left to right ; Allocate memory for LDS and initialize LDS values for all indexes ; Compute LDS values from right to left Loop from n - 2 downto 0 ; Loop from n - 1 downto i - 1 ; Return the maximum value of ( lis [ i ] + lds [ i ] - 1 ) ; Driver code
def ReverseBitonic ( arr ) : NEW_LINE INDENT N = len ( arr ) NEW_LINE lds = [ 1 for i in range ( N + 1 ) ] NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT for j in range ( 0 , i ) : NEW_LINE INDENT if ( ( arr [ i ] < arr [ j ] ) and ( lds [ i ] < lds [ j ] + 1 ) ) : NEW_LINE INDENT lds [ i ] = lds [ j ] + 1 NEW_LINE DEDENT DEDENT DEDENT lis = [ 1 for i in range ( N + 1 ) ] NEW_LINE for i in reversed ( range ( N - 1 ) ) : NEW_LINE INDENT for j in reversed ( range ( i - 1 , N ) ) : NEW_LINE INDENT if ( arr [ i ] < arr [ j ] and lis [ i ] < lis [ j ] + 1 ) : NEW_LINE INDENT lis [ i ] = lis [ j ] + 1 NEW_LINE DEDENT DEDENT DEDENT maximum = lis [ 0 ] + lds [ 0 ] - 1 NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT maximum = max ( ( lis [ i ] + lds [ i ] - 1 ) , maximum ) NEW_LINE DEDENT return maximum NEW_LINE DEDENT arr = [ 0 , 8 , 4 , 12 , 2 , 10 , 6 , 14 , 1 , 9 , 5 , 13 , 3 , 11 , 7 , 15 ] NEW_LINE print ( " Length ▁ of ▁ LBS ▁ is " , ReverseBitonic ( arr ) ) NEW_LINE
Maze With N doors and 1 Key | Recursive function to check whether there is a path from the top left cell to the bottom right cell of the maze ; Check whether the current cell is within the maze ; If key is required to move further ; If the key hasn 't been used before ; If current cell is the destination ; Either go down or right ; Key has been used before ; If current cell is the destination ; Either go down or right ; Driver code ; If there is a path from the cell ( 0 , 0 )
def findPath ( maze , xpos , ypos , key ) : NEW_LINE INDENT if xpos < 0 or xpos >= len ( maze ) or ypos < 0 or ypos >= len ( maze ) : NEW_LINE INDENT return False NEW_LINE DEDENT if maze [ xpos ] [ ypos ] == '1' : NEW_LINE INDENT if key == True : NEW_LINE INDENT if xpos == len ( maze ) - 1 and ypos == len ( maze ) - 1 : NEW_LINE INDENT return True NEW_LINE DEDENT return findPath ( maze , xpos + 1 , ypos , False ) or findPath ( maze , xpos , ypos + 1 , False ) NEW_LINE DEDENT return False NEW_LINE DEDENT if xpos == len ( maze ) - 1 and ypos == len ( maze ) - 1 : NEW_LINE INDENT return True NEW_LINE DEDENT return findPath ( maze , xpos + 1 , ypos , key ) or findPath ( maze , xpos , ypos + 1 , key ) NEW_LINE DEDENT def mazeProb ( maze , xpos , ypos ) : NEW_LINE INDENT key = True NEW_LINE if findPath ( maze , xpos , ypos , key ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT maze = [ [ '0' , '0' , '1' ] , [ '1' , '0' , '1' ] , [ '1' , '1' , '0' ] ] NEW_LINE n = len ( maze ) NEW_LINE if mazeProb ( maze , 0 , 0 ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Minimum cost to reach end of array array when a maximum jump of K index is allowed | Python 3 implementation of the approach ; Function to return the minimum cost to reach the last index ; If we reach the last index ; Already visited state ; Initially maximum ; Visit all possible reachable index ; If inside range ; We cannot move any further ; Memoize ; Driver Code
import sys NEW_LINE def FindMinimumCost ( ind , a , n , k , dp ) : NEW_LINE INDENT if ( ind == ( n - 1 ) ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT elif ( dp [ ind ] != - 1 ) : NEW_LINE INDENT return dp [ ind ] NEW_LINE DEDENT else : NEW_LINE INDENT ans = sys . maxsize NEW_LINE for i in range ( 1 , k + 1 ) : NEW_LINE INDENT if ( ind + i < n ) : NEW_LINE INDENT ans = min ( ans , abs ( a [ ind + i ] - a [ ind ] ) + FindMinimumCost ( ind + i , a , n , k , dp ) ) NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT dp [ ind ] = ans NEW_LINE return ans NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = [ 10 , 30 , 40 , 50 , 20 ] NEW_LINE k = 3 NEW_LINE n = len ( a ) NEW_LINE dp = [ - 1 for i in range ( n ) ] NEW_LINE print ( FindMinimumCost ( 0 , a , n , k , dp ) ) NEW_LINE DEDENT
Sum of all even factors of numbers in the range [ l , r ] | Function to calculate the prefix sum of all the even factors ; Add i to all the multiples of i ; Update the prefix sum ; Function to return the sum of all the even factors of the numbers in the given range ; Driver code
def sieve_modified ( ) : NEW_LINE INDENT for i in range ( 2 , MAX , 2 ) : NEW_LINE INDENT for j in range ( i , MAX , i ) : NEW_LINE INDENT prefix [ j ] += i NEW_LINE DEDENT DEDENT for i in range ( 1 , MAX ) : NEW_LINE INDENT prefix [ i ] += prefix [ i - 1 ] NEW_LINE DEDENT DEDENT def sumEvenFactors ( L , R ) : NEW_LINE INDENT return ( prefix [ R ] - prefix [ L - 1 ] ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT MAX = 100000 NEW_LINE prefix = [ 0 ] * MAX NEW_LINE sieve_modified ( ) NEW_LINE l , r = 6 , 10 NEW_LINE print ( sumEvenFactors ( l , r ) ) NEW_LINE DEDENT