codeparrot/xlcost-text-to-code huggingface dataset:
Number of subsequences with negative product | Python 3 implementation of the approach ; Function to return the count of all the subsequences with negative product ; To store the count of positive elements in the array ; To store the count of negative elements in the array ; If the current element is positive ; If the current element is negative ; For all the positive elements of the array ; For all the negative elements of the array ; Driver code
import math NEW_LINE def cntSubSeq ( arr , n ) : NEW_LINE INDENT pos_count = 0 ; NEW_LINE neg_count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] > 0 ) : NEW_LINE INDENT pos_count += 1 NEW_LINE DEDENT if ( arr [ i ] < 0 ) : NEW_LINE INDENT neg_count += 1 NEW_LINE DEDENT DEDENT result = int ( math . pow ( 2 , pos_count ) ) NEW_LINE if ( neg_count > 0 ) : NEW_LINE INDENT result *= int ( math . pow ( 2 , neg_count - 1 ) ) NEW_LINE DEDENT else : NEW_LINE INDENT result = 0 NEW_LINE DEDENT return result NEW_LINE DEDENT arr = [ 2 , - 3 , - 1 , 4 ] NEW_LINE n = len ( arr ) ; NEW_LINE print ( cntSubSeq ( arr , n ) ) NEW_LINE
Find minimum number of steps to reach the end of String | Python3 implementation of the approach ; Function to return the minimum number of steps to reach the end ; If the end can 't be reached ; Already at the end ; If the length is 2 or 3 then the end can be reached in a single step ; For the other cases , solve the problem using dynamic programming ; It requires no move from the end to reach the end ; From the 2 nd last and the 3 rd last index , only a single move is required ; Update the answer for every index ; If the current index is not reachable ; To store the minimum steps required from the current index ; If it is a valid move then update the minimum steps required ; Update the minimum steps required starting from the current index ; Cannot reach the end starting from str [ 0 ] ; Return the minimum steps required ; Driver code
import sys NEW_LINE INT_MAX = sys . maxsize ; NEW_LINE def minSteps ( string , n , k ) : NEW_LINE INDENT if ( string [ n - 1 ] == '0' ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT if ( n == 1 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( n < 4 ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT dp = [ 0 ] * n ; NEW_LINE dp [ n - 1 ] = 0 ; NEW_LINE dp [ n - 2 ] = 1 ; NEW_LINE dp [ n - 3 ] = 1 ; NEW_LINE for i in range ( n - 4 , - 1 , - 1 ) : NEW_LINE INDENT if ( string [ i ] == '0' ) : NEW_LINE INDENT continue ; NEW_LINE DEDENT steps = INT_MAX ; NEW_LINE if ( i + k < n and string [ i + k ] == '1' ) : NEW_LINE INDENT steps = min ( steps , dp [ i + k ] ) ; NEW_LINE DEDENT if ( string [ i + 1 ] == '1' ) : NEW_LINE INDENT steps = min ( steps , dp [ i + 1 ] ) ; NEW_LINE DEDENT if ( string [ i + 2 ] == '1' ) : NEW_LINE INDENT steps = min ( steps , dp [ i + 2 ] ) ; NEW_LINE DEDENT dp [ i ] = steps if ( steps == INT_MAX ) else ( 1 + steps ) ; NEW_LINE DEDENT if ( dp [ 0 ] == INT_MAX ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT return dp [ 0 ] ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = "101000011" ; NEW_LINE n = len ( string ) ; NEW_LINE k = 5 ; NEW_LINE print ( minSteps ( string , n , k ) ) ; NEW_LINE DEDENT
Length of longest Palindromic Subsequence of even length with no two adjacent characters same | To store the values of subproblems ; Function to find the Longest Palindromic subsequence of even length with no two adjacent characters same ; Base cases If the string length is 1 return 0 ; If the string length is 2 ; Check if the characters match ; If the value with given parameters is previously calculated ; If the first and last character of the string matches with the given character ; Remove the first and last character and call the function for all characters ; If it does not match ; Then find the first and last index of given character in the given string ; Take the substring from i to j and call the function with substring and the given character ; Store the answer for future use ; Driver code ; Check for all starting characters
dp = { } NEW_LINE def solve ( s , c ) : NEW_LINE INDENT if ( len ( s ) == 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( len ( s ) == 2 ) : NEW_LINE INDENT if ( s [ 0 ] == s [ 1 ] and s [ 0 ] == c ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT if ( s + " ▁ " + c ) in dp : NEW_LINE INDENT return dp [ s + " ▁ " + c ] NEW_LINE DEDENT ans = 0 NEW_LINE if ( s [ 0 ] == s [ len ( s ) - 1 ] and s [ 0 ] == c ) : NEW_LINE INDENT for c1 in range ( 97 , 123 ) : NEW_LINE INDENT if ( chr ( c1 ) != c ) : NEW_LINE INDENT ans = max ( ans , 1 + solve ( s [ 1 : len ( s ) - 1 ] , chr ( c1 ) ) ) NEW_LINE DEDENT DEDENT DEDENT else : NEW_LINE INDENT for i in range ( len ( s ) ) : NEW_LINE INDENT if ( s [ i ] == c ) : NEW_LINE INDENT for j in range ( len ( s ) - 1 , i , - 1 ) : NEW_LINE INDENT if ( s [ j ] == c ) : NEW_LINE INDENT if ( j == i ) : NEW_LINE INDENT break NEW_LINE DEDENT ans = solve ( s [ i : j - i + 2 ] , c ) NEW_LINE break NEW_LINE DEDENT DEDENT break NEW_LINE DEDENT DEDENT DEDENT dp [ s + " ▁ " + c ] = ans NEW_LINE return dp [ s + " ▁ " + c ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " abscrcdba " NEW_LINE ma = 0 NEW_LINE for c1 in range ( 97 , 123 ) : NEW_LINE INDENT ma = max ( ma , solve ( s , chr ( c1 ) ) * 2 ) NEW_LINE DEDENT print ( ma ) NEW_LINE DEDENT
Eggs dropping puzzle | Set 2 | Function to return the minimum number of trials needed in the worst case with n eggs and k floors ; Fill all the entries in table using optimal substructure property ; Return the minimum number of moves ; Driver code
def eggDrop ( n , k ) : NEW_LINE INDENT dp = [ [ 0 for i in range ( n + 1 ) ] for j in range ( k + 1 ) ] NEW_LINE x = 0 ; NEW_LINE while ( dp [ x ] [ n ] < k ) : NEW_LINE INDENT x += 1 ; NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT dp [ x ] [ i ] = dp [ x - 1 ] [ i - 1 ] + dp [ x - 1 ] [ i ] + 1 ; NEW_LINE DEDENT DEDENT return x ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 2 ; k = 36 ; NEW_LINE print ( eggDrop ( n , k ) ) ; NEW_LINE DEDENT
Maximum length of Strictly Increasing Sub | Function to return the maximum length of strictly increasing subarray after removing atmost one element ; Create two arrays pre and pos ; Find out the contribution of the current element in array [ 0 , i ] and update pre [ i ] ; Find out the contribution of the current element in array [ N - 1 , i ] and update pos [ i ] ; Calculate the maximum length of the stricly increasing subarray without removing any element ; Calculate the maximum length of the strictly increasing subarray after removing the current element ; Driver code
def maxIncSubarr ( a , n ) : NEW_LINE INDENT pre = [ 0 ] * n ; NEW_LINE pos = [ 0 ] * n ; NEW_LINE pre [ 0 ] = 1 ; NEW_LINE pos [ n - 1 ] = 1 ; NEW_LINE l = 0 ; NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( a [ i ] > a [ i - 1 ] ) : NEW_LINE INDENT pre [ i ] = pre [ i - 1 ] + 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT pre [ i ] = 1 ; NEW_LINE DEDENT DEDENT l = 1 ; NEW_LINE for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT if ( a [ i ] < a [ i + 1 ] ) : NEW_LINE INDENT pos [ i ] = pos [ i + 1 ] + 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT pos [ i ] = 1 ; NEW_LINE DEDENT DEDENT ans = 0 ; NEW_LINE l = 1 ; NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( a [ i ] > a [ i - 1 ] ) : NEW_LINE INDENT l += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT l = 1 ; NEW_LINE DEDENT ans = max ( ans , l ) ; NEW_LINE DEDENT for i in range ( 1 , n - 1 ) : NEW_LINE INDENT if ( a [ i - 1 ] < a [ i + 1 ] ) : NEW_LINE INDENT ans = max ( pre [ i - 1 ] + pos [ i + 1 ] , ans ) ; NEW_LINE DEDENT DEDENT return ans ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( maxIncSubarr ( arr , n ) ) ; NEW_LINE DEDENT
Number of square matrices with all 1 s | Python3 program to return the number of square submatrices with all 1 s ; Function to return the number of square submatrices with all 1 s ; Initialize count variable ; If a [ i ] [ j ] is equal to 0 ; Calculate number of square submatrices ending at ( i , j ) ; Calculate the sum of the array ; Driver code
n = 3 NEW_LINE m = 3 NEW_LINE def countSquareMatrices ( a , N , M ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT for j in range ( 1 , M ) : NEW_LINE INDENT if ( a [ i ] [ j ] == 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT a [ i ] [ j ] = min ( [ a [ i - 1 ] [ j ] , a [ i ] [ j - 1 ] , a [ i - 1 ] [ j - 1 ] ] ) + 1 NEW_LINE DEDENT DEDENT for i in range ( N ) : NEW_LINE INDENT for j in range ( M ) : NEW_LINE INDENT count += a [ i ] [ j ] NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT arr = [ [ 1 , 0 , 1 ] , [ 1 , 1 , 0 ] , [ 1 , 1 , 0 ] ] NEW_LINE print ( countSquareMatrices ( arr , n , m ) ) NEW_LINE
Count number of increasing sub | Python3 implementation of the approach ; Segment tree array ; Function for point update in segment tree ; Base case ; If l == r == up ; Mid element ; Updating the segment tree ; Function for the range sum - query ; Base case ; Mid element ; CallIng for the left and the right subtree ; Function to return the count ; Copying array arr to sort it ; Sorting array brr ; Map to store the rank of each element ; dp array ; To store the final answer ; Updating the dp array ; Rank of the element ; Solving the dp - states using segment tree ; UpdatIng the final answer ; Updating the segment tree ; Returning the final answer ; Driver code
N = 10000 NEW_LINE seg = [ 0 ] * ( 3 * N ) NEW_LINE def update ( In , l , r , up_In , val ) : NEW_LINE INDENT if ( r < up_In or l > up_In ) : NEW_LINE INDENT return seg [ In ] NEW_LINE DEDENT if ( l == up_In and r == up_In ) : NEW_LINE INDENT seg [ In ] = val NEW_LINE return val NEW_LINE DEDENT m = ( l + r ) // 2 NEW_LINE seg [ In ] = update ( 2 * In + 1 , l , m , up_In , val ) + update ( 2 * In + 2 , m + 1 , r , up_In , val ) NEW_LINE return seg [ In ] NEW_LINE DEDENT def query ( In , l , r , l1 , r1 ) : NEW_LINE INDENT if ( l > r ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( r < l1 or l > r1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( l1 <= l and r <= r1 ) : NEW_LINE INDENT return seg [ In ] NEW_LINE DEDENT m = ( l + r ) // 2 NEW_LINE return query ( 2 * In + 1 , l , m , l1 , r1 ) + query ( 2 * In + 2 , m + 1 , r , l1 , r1 ) NEW_LINE DEDENT def fIndCnt ( arr , n ) : NEW_LINE INDENT brr = [ 0 ] * n NEW_LINE for i in range ( n ) : NEW_LINE INDENT brr [ i ] = arr [ i ] NEW_LINE DEDENT brr = sorted ( brr ) NEW_LINE r = dict ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT r [ brr [ i ] ] = i + 1 NEW_LINE DEDENT dp = [ 0 ] * n NEW_LINE ans = 0 NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT rank = r [ arr [ i ] ] NEW_LINE dp [ i ] = 1 + query ( 0 , 0 , n - 1 , rank , n - 1 ) NEW_LINE ans += dp [ i ] NEW_LINE update ( 0 , 0 , n - 1 , rank - 1 , dp [ i ] + query ( 0 , 0 , n - 1 , rank - 1 , rank - 1 ) ) NEW_LINE DEDENT return ans NEW_LINE DEDENT arr = [ 1 , 2 , 10 , 9 ] NEW_LINE n = len ( arr ) NEW_LINE print ( fIndCnt ( arr , n ) ) NEW_LINE
Minimum cost to build N blocks from one block | Function to calculate min cost to build N blocks ; Initialize base case ; Recurence when i is odd ; Recurence when i is even ; Driver code
def minCost ( n , x , y , z ) : NEW_LINE INDENT dp = [ 0 ] * ( n + 1 ) NEW_LINE dp [ 0 ] = dp [ 1 ] = 0 NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT if ( i % 2 == 1 ) : NEW_LINE INDENT dp [ i ] = min ( dp [ ( i + 1 ) // 2 ] + x + z , dp [ i - 1 ] + y ) NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] = min ( dp [ i // 2 ] + x , dp [ i - 1 ] + y ) NEW_LINE DEDENT DEDENT return dp [ n ] NEW_LINE DEDENT n = 5 NEW_LINE x = 2 NEW_LINE y = 1 NEW_LINE z = 3 NEW_LINE print ( minCost ( n , x , y , z ) ) NEW_LINE
Size of the largest divisible subset in an Array | Python3 implementation of the above approach ; Function to find the size of the largest divisible subarray ; Mark all elements of the array ; Traverse reverse ; For all multiples of i ; Return the required answer ; Driver code ; Function call
N = 1000005 NEW_LINE def maximum_set ( a , n ) : NEW_LINE INDENT dp = [ 0 for i in range ( N ) ] NEW_LINE for i in a : NEW_LINE INDENT dp [ i ] = 1 NEW_LINE DEDENT ans = 1 NEW_LINE for i in range ( N - 1 , 0 , - 1 ) : NEW_LINE INDENT if ( dp [ i ] != 0 ) : NEW_LINE INDENT for j in range ( 2 * i , N , i ) : NEW_LINE INDENT dp [ i ] = max ( dp [ i ] , 1 + dp [ j ] ) NEW_LINE ans = max ( ans , dp [ i ] ) NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT arr = [ 3 , 4 , 6 , 8 , 10 , 18 , 21 , 24 ] NEW_LINE n = len ( arr ) NEW_LINE print ( maximum_set ( arr , n ) ) NEW_LINE
Longest sub | Python3 implementation of the approach ; Function to return the length of the largest sub - string divisible by 3 ; Base - case ; If the state has been solved before then return its value ; Marking the state as solved ; Recurrence relation ; Driver code
import numpy as np NEW_LINE import sys NEW_LINE N = 100 NEW_LINE INT_MIN = - ( sys . maxsize - 1 ) NEW_LINE dp = np . zeros ( ( N , 3 ) ) ; NEW_LINE v = np . zeros ( ( N , 3 ) ) ; NEW_LINE def findLargestString ( s , i , r ) : NEW_LINE INDENT if ( i == len ( s ) ) : NEW_LINE INDENT if ( r == 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT else : NEW_LINE INDENT return INT_MIN ; NEW_LINE DEDENT DEDENT if ( v [ i ] [ r ] ) : NEW_LINE INDENT return dp [ i ] [ r ] ; NEW_LINE DEDENT v [ i ] [ r ] = 1 ; NEW_LINE dp [ i ] [ r ] = max ( 1 + findLargestString ( s , i + 1 , ( r * 2 + ( ord ( s [ i ] ) - ord ( '0' ) ) ) % 3 ) , findLargestString ( s , i + 1 , r ) ) ; NEW_LINE return dp [ i ] [ r ] ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = "101" ; NEW_LINE print ( findLargestString ( s , 0 , 0 ) ) ; NEW_LINE DEDENT
Number of sub | Python3 implementation of th approach ; Function to return the number of sub - sequences divisible by 3 ; Base - cases ; If the state has been solved before then return its value ; Marking the state as solved ; Recurrence relation ; Driver code
import numpy as np NEW_LINE N = 100 NEW_LINE dp = np . zeros ( ( N , 3 ) ) ; NEW_LINE v = np . zeros ( ( N , 3 ) ) ; NEW_LINE def findCnt ( s , i , r ) : NEW_LINE INDENT if ( i == len ( s ) ) : NEW_LINE INDENT if ( r == 0 ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT DEDENT if ( v [ i ] [ r ] ) : NEW_LINE INDENT return dp [ i ] [ r ] ; NEW_LINE DEDENT v [ i ] [ r ] = 1 ; NEW_LINE dp [ i ] [ r ] = findCnt ( s , i + 1 , ( r * 2 + ( ord ( s [ i ] ) - ord ( '0' ) ) ) % 3 ) + findCnt ( s , i + 1 , r ) ; NEW_LINE return dp [ i ] [ r ] ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = "11" ; NEW_LINE print ( findCnt ( s , 0 , 0 ) - 1 ) ; NEW_LINE DEDENT
Remove minimum elements from ends of array so that sum decreases by at least K | O ( N ) | Function to return the count of minimum elements to be removed from the ends of the array such that the sum of the array decrease by at least K ; To store the final answer ; Maximum possible sum required ; Left point ; Right pointer ; Total current sum ; Two pointer loop ; If the sum fits ; Update the answer ; Update the total sum ; Increment the left pointer ; Driver code
def minCount ( arr , n , k ) : NEW_LINE INDENT ans = 0 ; NEW_LINE sum = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += arr [ i ] ; NEW_LINE DEDENT sum -= k ; NEW_LINE l = 0 ; NEW_LINE r = 0 ; NEW_LINE tot = 0 ; NEW_LINE while ( l < n ) : NEW_LINE INDENT if ( tot <= sum ) : NEW_LINE INDENT ans = max ( ans , r - l ) ; NEW_LINE if ( r == n ) : NEW_LINE INDENT break ; NEW_LINE DEDENT tot += arr [ r ] ; NEW_LINE r += 1 NEW_LINE DEDENT else : NEW_LINE INDENT tot -= arr [ l ] ; NEW_LINE l += 1 NEW_LINE DEDENT DEDENT return ( n - ans ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 11 , 5 , 5 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE k = 11 ; NEW_LINE print ( minCount ( arr , n , k ) ) ; NEW_LINE DEDENT
Minimum cost to merge all elements of List | Python3 implementation of the approach ; To store the states of DP ; Function to return the minimum merge cost ; Base case ; If the state has been solved before ; Marking the state as solved ; Reference to dp [ i ] [ j ] ; To store the sum of all the elements in the subarray arr [ i ... j ] ; Loop to iterate the recurrence ; Returning the solved value ; Driver code
import sys NEW_LINE N = 401 ; NEW_LINE dp = [ [ 0 for i in range ( N ) ] for j in range ( N ) ] ; NEW_LINE v = [ [ False for i in range ( N ) ] for j in range ( N ) ] ; NEW_LINE def minMergeCost ( i , j , arr ) : NEW_LINE INDENT if ( i == j ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( v [ i ] [ j ] ) : NEW_LINE INDENT return dp [ i ] [ j ] ; NEW_LINE DEDENT v [ i ] [ j ] = True ; NEW_LINE x = dp [ i ] [ j ] ; NEW_LINE x = sys . maxsize ; NEW_LINE tot = 0 ; NEW_LINE for k in range ( i , j + 1 ) : NEW_LINE INDENT tot += arr [ k ] ; NEW_LINE DEDENT for k in range ( i + 1 , j + 1 ) : NEW_LINE INDENT x = min ( x , tot + minMergeCost ( i , k - 1 , arr ) + minMergeCost ( k , j , arr ) ) ; NEW_LINE DEDENT return x ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 3 , 7 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( minMergeCost ( 0 , n - 1 , arr ) ) ; NEW_LINE DEDENT
Expected number of moves to reach the end of a board | Dynamic programming | Python3 implementation of the approach ; To store the states of dp ; To determine whether a state has been solved before ; Function to return the count ; Base cases ; If a state has been solved before it won 't be evaluated again ; Recurrence relation ; Driver code
maxSize = 50 NEW_LINE dp = [ 0 ] * maxSize NEW_LINE v = [ 0 ] * maxSize NEW_LINE def expectedSteps ( x ) : NEW_LINE INDENT if ( x == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( x <= 5 ) : NEW_LINE INDENT return 6 NEW_LINE DEDENT if ( v [ x ] ) : NEW_LINE INDENT return dp [ x ] NEW_LINE DEDENT v [ x ] = 1 NEW_LINE dp [ x ] = 1 + ( expectedSteps ( x - 1 ) + expectedSteps ( x - 2 ) + expectedSteps ( x - 3 ) + expectedSteps ( x - 4 ) + expectedSteps ( x - 5 ) + expectedSteps ( x - 6 ) ) / 6 NEW_LINE return dp [ x ] NEW_LINE DEDENT n = 10 NEW_LINE print ( round ( expectedSteps ( n - 1 ) , 5 ) ) NEW_LINE
Number of subsequences in a given binary string divisible by 2 | Function to return the count of the required subsequences ; To store the final answer ; Multiplier ; Loop to find the answer ; Condition to update the answer ; updating multiplier ; Driver code
def countSubSeq ( strr , lenn ) : NEW_LINE INDENT ans = 0 NEW_LINE mul = 1 NEW_LINE for i in range ( lenn ) : NEW_LINE INDENT if ( strr [ i ] == '0' ) : NEW_LINE INDENT ans += mul NEW_LINE DEDENT mul *= 2 NEW_LINE DEDENT return ans NEW_LINE DEDENT strr = "10010" NEW_LINE lenn = len ( strr ) NEW_LINE print ( countSubSeq ( strr , lenn ) ) NEW_LINE
Knapsack with large Weights | Python3 implementation of the approach ; To store the states of DP ; Function to solve the recurrence relation ; Base cases ; Marking state as solved ; Recurrence relation ; Function to return the maximum weight ; Iterating through all possible values to find the the largest value that can be represented by the given weights ; Driver code
V_SUM_MAX = 1000 NEW_LINE N_MAX = 100 NEW_LINE W_MAX = 10000000 NEW_LINE dp = [ [ 0 for i in range ( N_MAX ) ] for i in range ( V_SUM_MAX + 1 ) ] NEW_LINE v = [ [ 0 for i in range ( N_MAX ) ] for i in range ( V_SUM_MAX + 1 ) ] NEW_LINE def solveDp ( r , i , w , val , n ) : NEW_LINE INDENT if ( r <= 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( i == n ) : NEW_LINE INDENT return W_MAX NEW_LINE DEDENT if ( v [ r ] [ i ] ) : NEW_LINE INDENT return dp [ r ] [ i ] NEW_LINE DEDENT v [ r ] [ i ] = 1 NEW_LINE dp [ r ] [ i ] = min ( solveDp ( r , i + 1 , w , val , n ) , w [ i ] + solveDp ( r - val [ i ] , i + 1 , w , val , n ) ) NEW_LINE return dp [ r ] [ i ] NEW_LINE DEDENT def maxWeight ( w , val , n , c ) : NEW_LINE INDENT for i in range ( V_SUM_MAX , - 1 , - 1 ) : NEW_LINE INDENT if ( solveDp ( i , 0 , w , val , n ) <= c ) : NEW_LINE INDENT return i NEW_LINE DEDENT DEDENT return 0 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT w = [ 3 , 4 , 5 ] NEW_LINE val = [ 30 , 50 , 60 ] NEW_LINE n = len ( w ) NEW_LINE C = 8 NEW_LINE print ( maxWeight ( w , val , n , C ) ) NEW_LINE DEDENT
Sum of lengths of all paths possible in a given tree | Python3 implementation of the approach ; Number of vertices ; Adjacency list representation of the tree ; Array that stores the subtree size ; Array to mark all the vertices which are visited ; Utility function to create an edge between two vertices ; Add a to b 's list ; Add b to a 's list ; Function to calculate the subtree size ; Mark visited ; For every adjacent node ; If not already visited ; Recursive call for the child ; Function to calculate the contribution of each edge ; Mark current node as visited ; For every adjacent node ; If it is not already visisted ; Function to return the required sum ; First pass of the dfs ; Second pass ; Driver code
sz = 10 ** 5 NEW_LINE n = 5 NEW_LINE an = 0 NEW_LINE tree = [ [ ] for i in range ( sz ) ] NEW_LINE subtree_size = [ 0 ] * sz NEW_LINE vis = [ 0 ] * sz NEW_LINE def addEdge ( a , b ) : NEW_LINE INDENT tree [ a ] . append ( b ) NEW_LINE tree [ b ] . append ( a ) NEW_LINE DEDENT def dfs ( node ) : NEW_LINE INDENT leaf = True NEW_LINE vis [ node ] = 1 NEW_LINE for child in tree [ node ] : NEW_LINE INDENT if ( vis [ child ] == 0 ) : NEW_LINE INDENT leaf = False NEW_LINE dfs ( child ) NEW_LINE subtree_size [ node ] += subtree_size [ child ] NEW_LINE DEDENT DEDENT if leaf : NEW_LINE INDENT subtree_size [ node ] = 1 NEW_LINE DEDENT DEDENT def contribution ( node , ans ) : NEW_LINE INDENT global an NEW_LINE vis [ node ] = 1 NEW_LINE for child in tree [ node ] : NEW_LINE INDENT if ( vis [ child ] == 0 ) : NEW_LINE INDENT an += ( subtree_size [ child ] * ( n - subtree_size [ child ] ) ) NEW_LINE contribution ( child , ans ) NEW_LINE DEDENT DEDENT DEDENT def getSum ( ) : NEW_LINE INDENT for i in range ( sz ) : NEW_LINE INDENT vis [ i ] = 0 NEW_LINE DEDENT dfs ( 0 ) NEW_LINE ans = 0 NEW_LINE for i in range ( sz ) : NEW_LINE INDENT vis [ i ] = 0 NEW_LINE DEDENT contribution ( 0 , ans ) NEW_LINE return an NEW_LINE DEDENT n = 5 NEW_LINE addEdge ( 0 , 1 ) NEW_LINE addEdge ( 0 , 2 ) NEW_LINE addEdge ( 1 , 3 ) NEW_LINE addEdge ( 1 , 4 ) NEW_LINE print ( getSum ( ) ) NEW_LINE