codeparrot/xlcost-text-to-code huggingface dataset:
Sum of XOR of all subarrays | Function to calculate the Sum of XOR of all subarrays ; variable to store the final Sum ; multiplier ; variable to store number of sub - arrays with odd number of elements with ith bits starting from the first element to the end of the array ; variable to check the status of the odd - even count while calculating c_odd ; loop to calculate initial value of c_odd ; loop to iterate through all the elements of the array and update Sum ; updating the multiplier ; returning the Sum ; Driver Code
def findXorSum ( arr , n ) : NEW_LINE INDENT Sum = 0 NEW_LINE mul = 1 NEW_LINE for i in range ( 30 ) : NEW_LINE INDENT c_odd = 0 NEW_LINE odd = 0 NEW_LINE for j in range ( n ) : NEW_LINE INDENT if ( ( arr [ j ] & ( 1 << i ) ) > 0 ) : NEW_LINE INDENT odd = ( ~ odd ) NEW_LINE DEDENT if ( odd ) : NEW_LINE INDENT c_odd += 1 NEW_LINE DEDENT DEDENT for j in range ( n ) : NEW_LINE INDENT Sum += ( mul * c_odd ) NEW_LINE if ( ( arr [ j ] & ( 1 << i ) ) > 0 ) : NEW_LINE INDENT c_odd = ( n - j - c_odd ) NEW_LINE DEDENT DEDENT mul *= 2 NEW_LINE DEDENT return Sum NEW_LINE DEDENT arr = [ 3 , 8 , 13 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findXorSum ( arr , n ) ) NEW_LINE
Minimum replacements to make adjacent characters unequal in a ternary string | Set | Python 3 program to count the minimal replacements such that adjacent characters are unequal ; function to return integer value of i - th character in the string ; Function to count the number of minimal replacements ; If the string has reached the end ; If the state has been visited previously ; Get the current value of character ; If it is equal then change it ; All possible changes ; Change done ; If same no change ; Driver Code ; Length of string ; Create a DP array ; First character ; Function to find minimal replacements
import sys NEW_LINE def charVal ( s , i ) : NEW_LINE INDENT if ( s [ i ] == '0' ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT elif ( s [ i ] == '1' ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT return 2 NEW_LINE DEDENT DEDENT def countMinimalReplacements ( s , i , prev , dp , n ) : NEW_LINE INDENT if ( i == n ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( dp [ i ] [ prev ] != - 1 ) : NEW_LINE INDENT return dp [ i ] [ prev ] NEW_LINE DEDENT val = charVal ( s , i ) NEW_LINE ans = sys . maxsize NEW_LINE if ( val == prev ) : NEW_LINE INDENT val = 0 NEW_LINE for cur in range ( 3 ) : NEW_LINE INDENT if ( cur == prev ) : NEW_LINE INDENT continue NEW_LINE DEDENT val = 1 + countMinimalReplacements ( s , i + 1 , cur , dp , n ) NEW_LINE ans = min ( ans , val ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT ans = countMinimalReplacements ( s , i + 1 , val , dp , n ) NEW_LINE DEDENT dp [ i ] [ val ] = ans NEW_LINE return dp [ i ] [ val ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = "201220211" NEW_LINE n = len ( s ) NEW_LINE dp = [ [ - 1 for i in range ( 3 ) ] for i in range ( n ) ] NEW_LINE val = charVal ( s , 0 ) NEW_LINE print ( countMinimalReplacements ( s , 1 , val , dp , n ) ) NEW_LINE DEDENT
Find probability of selecting element from kth column after N iterations | Python3 implementation of the above approach ; Function to calculate probability ; declare dp [ ] [ ] and sum [ ] ; precalculate the first row ; calculate the probability for each element and update dp table ; return result ; Driver code
n = 4 NEW_LINE m = 4 NEW_LINE def calcProbability ( M , k ) : NEW_LINE INDENT dp = [ [ 0 for i in range ( n ) ] for i in range ( m ) ] NEW_LINE Sum = [ 0 for i in range ( n ) ] NEW_LINE for j in range ( n ) : NEW_LINE INDENT dp [ 0 ] [ j ] = M [ 0 ] [ j ] NEW_LINE Sum [ 0 ] += dp [ 0 ] [ j ] NEW_LINE DEDENT for i in range ( 1 , m ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT dp [ i ] [ j ] += ( dp [ i - 1 ] [ j ] / Sum [ i - 1 ] + M [ i ] [ j ] ) NEW_LINE Sum [ i ] += dp [ i ] [ j ] NEW_LINE DEDENT DEDENT return dp [ n - 1 ] [ k - 1 ] / Sum [ n - 1 ] NEW_LINE DEDENT M = [ [ 1 , 1 , 0 , 3 ] , [ 2 , 3 , 2 , 3 ] , [ 9 , 3 , 0 , 2 ] , [ 2 , 3 , 2 , 2 ] ] NEW_LINE k = 3 NEW_LINE print ( calcProbability ( M , k ) ) NEW_LINE
Possible cuts of a number such that maximum parts are divisible by 3 | Python3 program to find the maximum number of numbers divisible by 3 in a large number ; Function to find the maximum number of numbers divisible by 3 in a large number ; store size of the string ; Stores last index of a remainder ; last visited place of remainder zero is at 0. ; To store result from 0 to i ; get the remainder ; Get maximum res [ i ] value ; Driver Code
import math as mt NEW_LINE def MaximumNumbers ( string ) : NEW_LINE INDENT n = len ( string ) NEW_LINE remIndex = [ - 1 for i in range ( 3 ) ] NEW_LINE remIndex [ 0 ] = 0 NEW_LINE res = [ - 1 for i in range ( n + 1 ) ] NEW_LINE r = 0 NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT r = ( r + ord ( string [ i - 1 ] ) - ord ( '0' ) ) % 3 NEW_LINE res [ i ] = res [ i - 1 ] NEW_LINE if ( remIndex [ r ] != - 1 ) : NEW_LINE INDENT res [ i ] = max ( res [ i ] , res [ remIndex [ r ] ] + 1 ) NEW_LINE DEDENT remIndex [ r ] = i + 1 NEW_LINE DEDENT return res [ n ] NEW_LINE DEDENT s = "12345" NEW_LINE print ( MaximumNumbers ( s ) ) NEW_LINE
Minimum steps required to convert X to Y where a binary matrix represents the possible conversions | Pyton3 implementation of the above approach ; dist [ ] [ ] will be the output matrix that will finally have the shortest distances between every pair of numbers ; Initially same as mat ; Add all numbers one by one to the set of intermediate numbers . Before start of an iteration , we have shortest distances between all pairs of numbers such that the shortest distances consider only the numbers in set { 0 , 1 , 2 , . . k - 1 } as intermediate numbers . After the end of an iteration , vertex no . k is added to the set of intermediate numbers and the set becomes { 0 , 1 , 2 , . . k } ; Pick all numbers as source one by one ; Pick all numbers as destination for the above picked source ; If number k is on the shortest path from i to j , then update the value of dist [ i ] [ j ] ; If no path ; Driver Code
INF = 99999 NEW_LINE size = 10 NEW_LINE def findMinimumSteps ( mat , x , y , n ) : NEW_LINE INDENT dist = [ [ 0 for i in range ( n ) ] for i in range ( n ) ] NEW_LINE i , j , k = 0 , 0 , 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT if ( mat [ i ] [ j ] == 0 ) : NEW_LINE INDENT dist [ i ] [ j ] = INF NEW_LINE DEDENT else : NEW_LINE INDENT dist [ i ] [ j ] = 1 NEW_LINE DEDENT if ( i == j ) : NEW_LINE INDENT dist [ i ] [ j ] = 1 NEW_LINE DEDENT DEDENT DEDENT for k in range ( n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT if ( dist [ i ] [ k ] + dist [ k ] [ j ] < dist [ i ] [ j ] ) : NEW_LINE INDENT dist [ i ] [ j ] = dist [ i ] [ k ] + dist [ k ] [ j ] NEW_LINE DEDENT DEDENT DEDENT DEDENT if ( dist [ x ] [ y ] < INF ) : NEW_LINE INDENT return dist [ x ] [ y ] NEW_LINE DEDENT else : NEW_LINE INDENT return - 1 NEW_LINE DEDENT DEDENT mat = [ [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 ] , [ 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] , [ 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 ] ] NEW_LINE x , y = 2 , 3 NEW_LINE print ( findMinimumSteps ( mat , x , y , size ) ) NEW_LINE
Count number of paths whose weight is exactly X and has at | Python3 program to count the number of paths ; Function to find the number of paths ; If the Summation is more than X ; If exactly X weights have reached ; Already visited ; Count paths ; Traverse in all paths ; If the edge weight is M ; else : Edge 's weight is not M ; Driver Code ; Initialized the DP array with - 1 ; Function to count paths
Max = 4 NEW_LINE c = 2 NEW_LINE def countPaths ( Sum , get , m , n , dp ) : NEW_LINE INDENT if ( Sum < 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( Sum == 0 ) : NEW_LINE INDENT return get NEW_LINE DEDENT if ( dp [ Sum ] [ get ] != - 1 ) : NEW_LINE INDENT return dp [ Sum ] [ get ] NEW_LINE DEDENT res = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT if ( i == m ) : NEW_LINE INDENT res += countPaths ( Sum - i , 1 , m , n , dp ) NEW_LINE res += countPaths ( Sum - i , get , m , n , dp ) NEW_LINE DEDENT DEDENT dp [ Sum ] [ get ] = res NEW_LINE return dp [ Sum ] [ get ] NEW_LINE DEDENT n = 3 NEW_LINE m = 2 NEW_LINE x = 3 NEW_LINE dp = [ [ - 1 for i in range ( 2 ) ] for i in range ( Max + 1 ) ] NEW_LINE for i in range ( Max + 1 ) : NEW_LINE INDENT for j in range ( 2 ) : NEW_LINE INDENT dp [ i ] [ j ] = - 1 NEW_LINE DEDENT DEDENT print ( countPaths ( x , 0 , m , n , dp ) ) NEW_LINE
Minimum Operations to make value of all vertices of the tree Zero | A utility function to add an edge in an undirected graph ; A utility function to print the adjacency list representation of graph ; Utility Function for findMinOperation ( ) ; Base Case for current node ; Iterate over the adjacency list for src ; calculate DP table for each child V ; Number of Increase Type operations for node src is equal to maximum of number of increase operations required by each of its child ; Number of Decrease Type operations for node src is equal to maximum of number of decrease operations required by each of its child ; After performing operations for subtree rooted at src A [ src ] changes by the net difference of increase and decrease type operations ; for negative value of node src ; Returns the minimum operations required to make value of all vertices equal to zero , uses findMinOperationUtil ( ) ; Initialise DP table ; find dp [ 1 ] [ 0 ] and dp [ 1 ] [ 1 ] ; Driver code ; Build the Graph / Tree
def addEdge ( adj , u , v ) : NEW_LINE INDENT adj [ u ] . append ( v ) NEW_LINE adj [ v ] . append ( u ) NEW_LINE DEDENT def printGraph ( adj , V ) : NEW_LINE INDENT for v in range ( 0 , V ) : NEW_LINE INDENT print ( " Adjacency ▁ list ▁ of ▁ vertex " , v ) NEW_LINE print ( " head " , end = " ▁ " ) NEW_LINE for x in adj [ v ] : NEW_LINE INDENT print ( " - > " , x , end = " " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT def findMinOperationUtil ( dp , adj , A , src , parent ) : NEW_LINE INDENT dp [ src ] [ 0 ] = dp [ src ] [ 1 ] = 0 NEW_LINE for V in adj [ src ] : NEW_LINE INDENT if V == parent : NEW_LINE INDENT continue NEW_LINE DEDENT findMinOperationUtil ( dp , adj , A , V , src ) NEW_LINE dp [ src ] [ 0 ] = max ( dp [ src ] [ 0 ] , dp [ V ] [ 0 ] ) NEW_LINE dp [ src ] [ 1 ] = max ( dp [ src ] [ 1 ] , dp [ V ] [ 1 ] ) NEW_LINE DEDENT A [ src - 1 ] += dp [ src ] [ 0 ] - dp [ src ] [ 1 ] NEW_LINE if A [ src - 1 ] > 0 : NEW_LINE INDENT dp [ src ] [ 1 ] += A [ src - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT dp [ src ] [ 0 ] += abs ( A [ src - 1 ] ) NEW_LINE DEDENT DEDENT def findMinOperation ( adj , A , V ) : NEW_LINE INDENT dp = [ [ 0 , 0 ] for i in range ( V + 1 ) ] NEW_LINE findMinOperationUtil ( dp , adj , A , 1 , 0 ) NEW_LINE minOperations = dp [ 1 ] [ 0 ] + dp [ 1 ] [ 1 ] NEW_LINE return minOperations NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT V = 5 NEW_LINE adj = [ [ ] for i in range ( V + 1 ) ] NEW_LINE addEdge ( adj , 1 , 2 ) NEW_LINE addEdge ( adj , 1 , 3 ) NEW_LINE A = [ 1 , - 1 , 1 ] NEW_LINE minOperations = findMinOperation ( adj , A , V ) NEW_LINE print ( minOperations ) NEW_LINE DEDENT
Sum of kth powers of first n natural numbers | global array to store factorials ; function to calculate the factorials of all the numbers upto k ; function to return the binomial coeff ; nCr = ( n ! * ( n - r ) ! ) / r ! ; function to return the sum of the kth powers of n natural numbers ; when j is 1 ; calculating sum ( n ^ 1 ) of unity powers of n storing sum ( n ^ 1 ) for sum ( n ^ 2 ) ; if k == 1 then temp is the result ; for finding sum ( n ^ k ) removing 1 and n * KCk from ( n + 1 ) ^ k ; Removing all kC2 * sum ( n ^ ( k - 2 ) ) + ... + kCk - 1 * ( sum ( n ^ ( k - ( k - 1 ) ) ; storing the result for next sum of next powers of k ; Driver code
MAX_K = 15 NEW_LINE fac = [ 1 for i in range ( MAX_K ) ] NEW_LINE def factorial ( k ) : NEW_LINE INDENT fac [ 0 ] = 1 NEW_LINE for i in range ( 1 , k + 2 ) : NEW_LINE INDENT fac [ i ] = ( i * fac [ i - 1 ] ) NEW_LINE DEDENT DEDENT def bin ( a , b ) : NEW_LINE INDENT ans = fac [ a ] // ( fac [ a - b ] * fac [ b ] ) NEW_LINE return ans NEW_LINE DEDENT def sumofn ( n , k ) : NEW_LINE INDENT p = 0 NEW_LINE num1 , temp = 1 , 1 NEW_LINE arr = [ 1 for i in range ( 1000 ) ] NEW_LINE for j in range ( 1 , k + 1 ) : NEW_LINE INDENT if j == 1 : NEW_LINE INDENT num1 = ( n * ( n + 1 ) ) // 2 NEW_LINE arr [ p ] = num1 NEW_LINE p += 1 NEW_LINE DEDENT else : NEW_LINE INDENT temp = pow ( n + 1 , j + 1 ) - 1 - n NEW_LINE for s in range ( 1 , j ) : NEW_LINE INDENT temp = temp - ( arr [ j - s - 1 ] * bin ( j + 1 , s + 1 ) ) NEW_LINE DEDENT temp = temp // ( j + 1 ) NEW_LINE arr [ p ] = temp NEW_LINE p += 1 NEW_LINE DEDENT DEDENT temp = arr [ p - 1 ] NEW_LINE return temp NEW_LINE DEDENT n , k = 5 , 2 NEW_LINE factorial ( k ) NEW_LINE print ( sumofn ( n , k ) ) NEW_LINE
Ways to fill N positions using M colors such that there are exactly K pairs of adjacent different colors | Python 3 implementation of the approach ; Recursive function to find the required number of ways ; When all positions are filled ; If adjacent pairs are exactly K ; If already calculated ; Next position filled with same color ; Next position filled with different color So there can be m - 1 different colors ; Driver Code
max = 4 NEW_LINE def countWays ( index , cnt , dp , n , m , k ) : NEW_LINE INDENT if ( index == n ) : NEW_LINE INDENT if ( cnt == k ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT if ( dp [ index ] [ cnt ] != - 1 ) : NEW_LINE INDENT return dp [ index ] [ cnt ] NEW_LINE DEDENT ans = 0 NEW_LINE ans += countWays ( index + 1 , cnt , dp , n , m , k ) NEW_LINE ans += ( m - 1 ) * countWays ( index + 1 , cnt + 1 , dp , n , m , k ) NEW_LINE dp [ index ] [ cnt ] = ans NEW_LINE return dp [ index ] [ cnt ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 3 NEW_LINE m = 3 NEW_LINE k = 2 NEW_LINE dp = [ [ - 1 for x in range ( n + 1 ) ] for y in range ( max ) ] NEW_LINE print ( m * countWays ( 1 , 0 , dp , n , m , k ) ) NEW_LINE DEDENT
Number of balanced bracket expressions that can be formed from a string | Max string length ; Function to check whether index start and end can form a bracket pair or not ; Check for brackets ( ) ; Check for brackets [ ] ; Check for brackets { } ; Function to find number of proper bracket expressions ; If starting index is greater than ending index ; If dp [ start ] [ end ] has already been computed ; Search for the bracket in from next index ; If bracket pair is formed , add number of combination ; If ? comes then all three bracket expressions are possible ; Return answer ; If n is odd , string cannot be balanced ; Driver Code
MAX = 300 NEW_LINE def checkFunc ( i , j , st ) : NEW_LINE INDENT if ( st [ i ] == ' ( ' and st [ j ] == ' ) ' ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( st [ i ] == ' ( ' and st [ j ] == ' ? ' ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( st [ i ] == ' ? ' and st [ j ] == ' ) ' ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( st [ i ] == ' [ ' and st [ j ] == ' ] ' ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( st [ i ] == ' [ ' and st [ j ] == ' ? ' ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( st [ i ] == ' ? ' and st [ j ] == ' ] ' ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( st [ i ] == ' { ' and st [ j ] == ' } ' ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( st [ i ] == ' { ' and st [ j ] == ' ? ' ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( st [ i ] == ' ? ' and st [ j ] == ' } ' ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT return 0 NEW_LINE DEDENT def countRec ( start , end , dp , st ) : NEW_LINE INDENT sum = 0 NEW_LINE if ( start > end ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( dp [ start ] [ end ] != - 1 ) : NEW_LINE INDENT return dp [ start ] [ end ] NEW_LINE DEDENT r = 0 NEW_LINE for i in range ( start + 1 , end + 1 , 2 ) : NEW_LINE INDENT if ( checkFunc ( start , i , st ) ) : NEW_LINE INDENT sum = ( sum + countRec ( start + 1 , i - 1 , dp , st ) * countRec ( i + 1 , end , dp , st ) ) NEW_LINE DEDENT elif ( st [ start ] == ' ? ' and st [ i ] == ' ? ' ) : NEW_LINE INDENT sum = ( sum + countRec ( start + 1 , i - 1 , dp , st ) * countRec ( i + 1 , end , dp , st ) * 3 ) NEW_LINE DEDENT DEDENT dp [ start ] [ end ] = sum NEW_LINE return dp [ start ] [ end ] NEW_LINE DEDENT def countWays ( st ) : NEW_LINE INDENT n = len ( st ) NEW_LINE if ( n % 2 == 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT dp = [ [ - 1 for i in range ( MAX ) ] for i in range ( MAX ) ] NEW_LINE return countRec ( 0 , n - 1 , dp , st ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT st = " ( ? ( [ ? ) ] ? } ? " NEW_LINE print ( countWays ( st ) ) NEW_LINE DEDENT
Count pairs ( A , B ) such that A has X and B has Y number of set bits and A + B = C | Initial DP array ; Recursive function to generate all combinations of bits ; if the state has already been visited ; find if C has no more set bits on left ; if no set bits are left for C and there are no set bits for A and B and the carry is 0 , then this combination is possible ; if no set bits are left for C and requirement of set bits for A and B have exceeded ; Find if the bit is 1 or 0 at third index to the left ; carry = 1 and bit set = 1 ; since carry is 1 , and we need 1 at C 's bit position we can use 0 and 0  or 1 and 1 at A and B bit position ; carry = 0 and bit set = 1 ; since carry is 0 , and we need 1 at C 's  bit position we can use 1 and 0  or 0 and 1 at A and B bit position ; carry = 1 and bit set = 0 ; since carry is 1 , and we need 0 at C 's  bit position we can use 1 and 0  or 0 and 1 at A and B bit position ; carry = 0 and bit set = 0 ; since carry is 0 , and we need 0 at C 's  bit position we can use 0 and 0  or 1 and 1 at A and B bit position ; Function to count ways ; function call that returns the answer ; Driver Code
dp = [ [ [ [ - 1 , - 1 ] for i in range ( 64 ) ] for j in range ( 64 ) ] for k in range ( 64 ) ] NEW_LINE def func ( third , seta , setb , carry , number ) : NEW_LINE INDENT if dp [ third ] [ seta ] [ setb ] [ carry ] != - 1 : NEW_LINE INDENT return dp [ third ] [ seta ] [ setb ] [ carry ] NEW_LINE DEDENT shift = number >> third NEW_LINE if ( shift == 0 and seta == 0 and setb == 0 and carry == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( shift == 0 or seta < 0 or setb < 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT mask = shift & 1 NEW_LINE dp [ third ] [ seta ] [ setb ] [ carry ] = 0 NEW_LINE if ( mask ) and carry : NEW_LINE INDENT dp [ third ] [ seta ] [ setb ] [ carry ] += func ( third + 1 , seta , setb , 0 , number ) + func ( third + 1 , seta - 1 , setb - 1 , 1 , number ) NEW_LINE DEDENT elif mask and not carry : NEW_LINE INDENT dp [ third ] [ seta ] [ setb ] [ carry ] += func ( third + 1 , seta - 1 , setb , 0 , number ) + func ( third + 1 , seta , setb - 1 , 0 , number ) NEW_LINE DEDENT elif not mask and carry : NEW_LINE INDENT dp [ third ] [ seta ] [ setb ] [ carry ] += func ( third + 1 , seta - 1 , setb , 1 , number ) + func ( third + 1 , seta , setb - 1 , 1 , number ) NEW_LINE DEDENT elif not mask and not carry : NEW_LINE INDENT dp [ third ] [ seta ] [ setb ] [ carry ] += func ( third + 1 , seta , setb , 0 , number ) + func ( third + 1 , seta - 1 , setb - 1 , 1 , number ) NEW_LINE DEDENT return dp [ third ] [ seta ] [ setb ] [ carry ] NEW_LINE DEDENT def possibleSwaps ( a , b , c ) : NEW_LINE INDENT ans = func ( 0 , a , b , 0 , c ) NEW_LINE return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT x , y , c = 2 , 2 , 20 NEW_LINE print ( possibleSwaps ( x , y , c ) ) NEW_LINE DEDENT
Sum of Fibonacci numbers at even indexes upto N terms | Computes value of first fibonacci numbers and stores the even - indexed sum ; Initialize result ; Add remaining terms ; For even indices ; Return the alternting sum ; Driver code ; Get n ; Find the even - indiced sum
def calculateEvenSum ( n ) : NEW_LINE INDENT if n <= 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT fibo = [ 0 ] * ( 2 * n + 1 ) NEW_LINE fibo [ 0 ] , fibo [ 1 ] = 0 , 1 NEW_LINE sum = 0 NEW_LINE for i in range ( 2 , 2 * n + 1 ) : NEW_LINE INDENT fibo [ i ] = fibo [ i - 1 ] + fibo [ i - 2 ] NEW_LINE if i % 2 == 0 : NEW_LINE INDENT sum += fibo [ i ] NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 8 NEW_LINE print ( " Even ▁ indexed ▁ Fibonacci ▁ Sum ▁ upto " , n , " terms : " , calculateEvenSum ( n ) ) NEW_LINE DEDENT
Sum of Fibonacci numbers at even indexes upto N terms | Python3 Program to find even indexed Fibonacci Sum in O ( Log n ) time . ; Create an array for memoization ; Returns n 'th Fibonacci number using table f[] ; Base cases ; If fib ( n ) is already computed ; Applying above formula [ Note value n & 1 is 1 if n is odd , else 0 ] . ; Computes value of even - indexed Fibonacci Sum ; Driver Code ; Get n ; Find the alternating sum
MAX = 1000 ; NEW_LINE f = [ 0 ] * MAX ; NEW_LINE def fib ( n ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( n == 1 or n == 2 ) : NEW_LINE INDENT f [ n ] = 1 ; NEW_LINE return f [ n ] ; NEW_LINE DEDENT if ( f [ n ] ) : NEW_LINE INDENT return f [ n ] ; NEW_LINE DEDENT k = ( n + 1 ) // 2 if ( n % 2 == 1 ) else n // 2 ; NEW_LINE f [ n ] = ( fib ( k ) * fib ( k ) + fib ( k - 1 ) * fib ( k - 1 ) ) if ( n % 2 == 1 ) else ( 2 * fib ( k - 1 ) + fib ( k ) ) * fib ( k ) ; NEW_LINE return f [ n ] ; NEW_LINE DEDENT def calculateEvenSum ( n ) : NEW_LINE INDENT return ( fib ( 2 * n + 1 ) - 1 ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 8 ; NEW_LINE print ( " Even ▁ indexed ▁ Fibonacci ▁ Sum ▁ upto " , n , " terms : " , calculateEvenSum ( n ) ) ; NEW_LINE DEDENT
Gould 's Sequence | 32768 = 2 ^ 15 ; Array to store Sequence up to 2 ^ 16 = 65536 ; Utility function to pre - compute odd numbers in ith row of Pascals 's triangle ; First term of the Sequence is 1 ; Initialize i to 1 ; Initialize p to 1 ( i . e 2 ^ i ) in each iteration i will be pth power of 2 ; loop to generate gould 's Sequence ; i is pth power of 2 traverse the array from j = 0 to i i . e ( 2 ^ p ) ; double the value of arr [ j ] and store to arr [ i + j ] ; update i to next power of 2 ; increment p ; Function to print gould 's Sequence ; loop to generate gould 's Sequence  up to n ; Driver code ; Get n ; Function call
MAX = 32768 NEW_LINE arr = [ None ] * ( 2 * MAX ) NEW_LINE def gouldSequence ( ) : NEW_LINE INDENT arr [ 0 ] = 1 NEW_LINE i = 1 NEW_LINE p = 1 NEW_LINE while i <= MAX : NEW_LINE INDENT j = 0 NEW_LINE while j < i : NEW_LINE INDENT arr [ i + j ] = 2 * arr [ j ] NEW_LINE j += 1 NEW_LINE DEDENT i = ( 1 << p ) NEW_LINE p += 1 NEW_LINE DEDENT DEDENT def printSequence ( n ) : NEW_LINE INDENT for i in range ( 0 , n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT gouldSequence ( ) NEW_LINE n = 16 NEW_LINE printSequence ( n ) NEW_LINE DEDENT
Count the number of ways to traverse a Matrix | Find factorial ; Find number of ways to reach mat [ m - 1 ] [ n - 1 ] from mat [ 0 ] [ 0 ] in a matrix mat [ ] [ ] ] ; Driver code ; Function call
def factorial ( n ) : NEW_LINE INDENT res = 1 NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT res *= i NEW_LINE DEDENT return res NEW_LINE DEDENT def countWays ( m , n ) : NEW_LINE INDENT m = m - 1 NEW_LINE n = n - 1 NEW_LINE return ( factorial ( m + n ) // ( factorial ( m ) * factorial ( n ) ) ) NEW_LINE DEDENT m = 5 NEW_LINE n = 5 NEW_LINE result = countWays ( m , n ) NEW_LINE print ( result ) NEW_LINE