codeparrot/xlcost-text-to-code huggingface dataset:
Count of occurrences of each prefix in a string using modified KMP algorithm | Function to print the count of all prefix in the given string ; Iterate over string s ; Print the prefix and their frequency ; Function to implement the LPS array to store the longest prefix which is also a suffix for every substring of the string S ; Array to store LPS values Value of lps [ 0 ] is 0 by definition ; Find the values of LPS [ i ] for the rest of the string using two pointers and DP ; Initially set the value of j as the longest prefix that is also a suffix for i as LPS [ i - 1 ] ; Check if the suffix of length j + 1 is also a prefix ; If s [ i ] = s [ j ] then , assign LPS [ i ] as j + 1 ; If we reached j = 0 , assign LPS [ i ] as 0 as there was no prefix equal to suffix ; Return the calculated LPS array ; Function to count the occurrence of all the prefix in the string S ; Call the prefix_function to get LPS ; To store the occurrence of all the prefix ; Count all the suffixes that are also prefix ; Add the occurences of i to smaller prefixes ; Adding 1 to all occ [ i ] for all the orignal prefix ; Function Call to print the occurence of all the prefix ; Given String ; Function Call
def Print ( occ , s ) : NEW_LINE INDENT for i in range ( 1 , len ( s ) + 1 ) : NEW_LINE INDENT print ( s [ 0 : i ] , " occur " , occ [ i ] , " times . " ) NEW_LINE DEDENT DEDENT def prefix_function ( s ) : NEW_LINE INDENT LPS = [ 0 for i in range ( len ( s ) ) ] NEW_LINE for i in range ( 1 , len ( s ) ) : NEW_LINE INDENT j = LPS [ i - 1 ] NEW_LINE while ( j > 0 and s [ i ] != s [ j ] ) : NEW_LINE INDENT j = LPS [ j - 1 ] NEW_LINE DEDENT if ( s [ i ] == s [ j ] ) : NEW_LINE INDENT LPS [ i ] = j + 1 NEW_LINE DEDENT else : NEW_LINE INDENT LPS [ i ] = 0 NEW_LINE DEDENT DEDENT return LPS NEW_LINE DEDENT def count_occurence ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE LPS = prefix_function ( s ) NEW_LINE occ = [ 0 for i in range ( n + 1 ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT occ [ LPS [ i ] ] += 1 NEW_LINE DEDENT for i in range ( n - 1 , 0 , - 1 ) : NEW_LINE INDENT occ [ LPS [ i - 1 ] ] += occ [ i ] NEW_LINE DEDENT for i in range ( n + 1 ) : NEW_LINE INDENT occ [ i ] += 1 NEW_LINE DEDENT Print ( occ , s ) NEW_LINE DEDENT A = " ABACABA " NEW_LINE count_occurence ( A ) NEW_LINE
Maximum sum by picking elements from two arrays in order | Set 2 | Function to calculate maximum sum ; Maximum elements that can be chosen from array A ; Maximum elements that can be chosen from array B ; Stores the maximum sum possible ; Fill the dp [ ] for base case when all elements are selected from A [ ] ; Fill the dp [ ] for base case when all elements are selected from B [ ] ; Return the final answer ; Driver Program
def maximumSum ( A , B , length , X , Y ) : NEW_LINE INDENT l = length NEW_LINE l1 = min ( length , X ) NEW_LINE l2 = min ( length , Y ) NEW_LINE dp = [ [ 0 for i in range ( l2 + 1 ) ] for i in range ( l1 + 1 ) ] NEW_LINE dp [ 0 ] [ 0 ] = 0 NEW_LINE max_sum = - 10 * 9 NEW_LINE for i in range ( 1 , l1 + 1 ) : NEW_LINE INDENT dp [ i ] [ 0 ] = dp [ i - 1 ] [ 0 ] + A [ i - 1 ] NEW_LINE max_sum = max ( max_sum , dp [ i ] [ 0 ] ) NEW_LINE DEDENT for i in range ( 1 , l2 + 1 ) : NEW_LINE INDENT dp [ 0 ] [ i ] = dp [ 0 ] [ i - 1 ] + B [ i - 1 ] NEW_LINE max_sum = max ( max_sum , dp [ 0 ] [ i ] ) NEW_LINE DEDENT for i in range ( 1 , l1 + 1 ) : NEW_LINE INDENT for j in range ( 1 , l2 + 1 ) : NEW_LINE INDENT if ( i + j <= l ) : NEW_LINE INDENT dp [ i ] [ j ] = max ( dp [ i - 1 ] [ j ] + A [ i + j - 1 ] , dp [ i ] [ j - 1 ] + B [ i + j - 1 ] ) NEW_LINE DEDENT max_sum = max ( dp [ i ] [ j ] , max_sum ) NEW_LINE DEDENT DEDENT return max_sum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE B = [ 5 , 4 , 3 , 2 , 1 ] NEW_LINE X = 3 NEW_LINE Y = 2 NEW_LINE N = len ( A ) NEW_LINE print ( maximumSum ( A , B , N , X , Y ) ) NEW_LINE DEDENT
Min number of operations to reduce N to 0 by subtracting any digits from N | Function to reduce an integer N to Zero in minimum operations by removing digits from N ; Initialise dp [ ] to steps ; Iterate for all elements ; For each digit in number i ; Either select the number or do not select it ; dp [ N ] will give minimum step for N ; Given Number ; Function Call
def reduceZero ( N ) : NEW_LINE INDENT dp = [ 1e9 for i in range ( N + 1 ) ] NEW_LINE dp [ 0 ] = 0 NEW_LINE for i in range ( N + 1 ) : NEW_LINE INDENT for c in str ( i ) : NEW_LINE INDENT dp [ i ] = min ( dp [ i ] , dp [ i - ( ord ( c ) - 48 ) ] + 1 ) NEW_LINE DEDENT DEDENT return dp [ N ] NEW_LINE DEDENT N = 25 NEW_LINE print ( reduceZero ( N ) ) NEW_LINE
Pentanacci Numbers | Function to print Nth Pentanacci number ; Initialize first five numbers to base cases ; declare a current variable ; Loop to add previous five numbers for each number starting from 5 and then assign first , second , third , fourth to second , third , fourth and curr to fifth respectively ; Driver code
def printpenta ( n ) : NEW_LINE INDENT if ( n < 0 ) : NEW_LINE INDENT return NEW_LINE DEDENT first = 0 NEW_LINE second = 0 NEW_LINE third = 0 NEW_LINE fourth = 0 NEW_LINE fifth = 1 NEW_LINE curr = 0 NEW_LINE if ( n == 0 or n == 1 or \ n == 2 or n == 3 ) : NEW_LINE INDENT print ( first ) NEW_LINE DEDENT elif ( n == 5 ) : NEW_LINE INDENT print ( fifth ) NEW_LINE DEDENT else : NEW_LINE INDENT for i in range ( 5 , n ) : NEW_LINE INDENT curr = first + second + third + fourth + fifth NEW_LINE first = second NEW_LINE second = third NEW_LINE third = fourth NEW_LINE fourth = fifth NEW_LINE fifth = curr NEW_LINE DEDENT DEDENT print ( curr ) NEW_LINE DEDENT n = 10 NEW_LINE printpenta ( n ) NEW_LINE
Count of binary strings of length N with even set bit count and at most K consecutive 1 s | Python3 program for the above approach ; Table to store solution of each subproblem ; Function to calculate the possible binary strings ; If number of ones is equal to K ; pos : current position Base Case : When n length is traversed ; sum : count of 1 ' s ▁ ▁ Return ▁ the ▁ count ▁ ▁ of ▁ 1' s obtained ; If the subproblem has already been solved ; Return the answer ; Recursive call when current position is filled with 1 ; Recursive call when current position is filled with 0 ; Store the solution to this subproblem ; Driver Code ; Initialising the table with - 1
import numpy as np NEW_LINE dp = np . ones ( ( ( 100002 , 21 , 3 ) ) ) NEW_LINE dp = - 1 * dp NEW_LINE def possibleBinaries ( pos , ones , sum , k ) : NEW_LINE INDENT if ( ones == k ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( pos == 0 ) : NEW_LINE INDENT return 1 if ( sum == 0 ) else 0 NEW_LINE DEDENT if ( dp [ pos ] [ ones ] [ sum ] != - 1 ) : NEW_LINE INDENT return dp [ pos ] [ ones ] [ sum ] NEW_LINE DEDENT ret = ( possibleBinaries ( pos - 1 , ones + 1 , ( sum + 1 ) % 2 , k ) + possibleBinaries ( pos - 1 , 0 , sum , k ) ) NEW_LINE dp [ pos ] [ ones ] [ sum ] = ret NEW_LINE return dp [ pos ] [ ones ] [ sum ] NEW_LINE DEDENT N = 3 NEW_LINE K = 2 NEW_LINE print ( int ( possibleBinaries ( N , 0 , 0 , K ) ) ) NEW_LINE
Sum of absolute difference of all pairs raised to power K | Python3 program for the above approach ; Since K can be 100 max ; Initializing with - 1 ; Making vector A as 1 - Indexing ; To Calculate the value nCk ; Since nCj = ( n - 1 ) Cj + ( n - 1 ) C ( j - 1 ) ; ; Function that summation of absolute differences of all pairs raised to the power k ; Sort the given array ; Precomputation part , O ( n * k ) ; Traverse the array arr [ ] ; For each K ; Return the final answer ; Driver code ; Given N and K ; Given array ; Creation of object of class ; Function call
class Solution : NEW_LINE INDENT def __init__ ( self , N , K , B ) : NEW_LINE INDENT self . ncr = [ ] NEW_LINE for i in range ( 101 ) : NEW_LINE INDENT temp = [ ] NEW_LINE for j in range ( 101 ) : NEW_LINE INDENT temp . append ( - 1 ) NEW_LINE DEDENT self . ncr . append ( temp ) NEW_LINE DEDENT self . n = N NEW_LINE self . k = K NEW_LINE self . A = [ 0 ] + B NEW_LINE DEDENT def f ( self , n , k ) : NEW_LINE INDENT if k == 0 : NEW_LINE INDENT return 1 NEW_LINE DEDENT if n == k : NEW_LINE INDENT return 1 NEW_LINE DEDENT if n < k : NEW_LINE INDENT return 0 NEW_LINE DEDENT if self . ncr [ n ] [ k ] != - 1 : NEW_LINE INDENT return self . ncr [ n ] [ k ] NEW_LINE DEDENT self . ncr [ n ] [ k ] = ( self . f ( n - 1 , k ) + self . f ( n - 1 , k - 1 ) ) NEW_LINE return self . ncr [ n ] [ k ] NEW_LINE DEDENT def pairsPower ( self ) : NEW_LINE INDENT pre = [ ] NEW_LINE for i in range ( self . n + 1 ) : NEW_LINE INDENT temp = [ ] NEW_LINE for j in range ( self . k + 1 ) : NEW_LINE INDENT temp . append ( 0 ) NEW_LINE DEDENT pre . append ( temp ) NEW_LINE DEDENT ans = 0 NEW_LINE self . A . sort ( ) NEW_LINE for i in range ( 1 , self . n + 1 ) : NEW_LINE INDENT pre [ i ] [ 0 ] = 1 NEW_LINE for j in range ( 1 , self . k + 1 ) : NEW_LINE INDENT pre [ i ] [ j ] = ( self . A [ i ] * pre [ i ] [ j - 1 ] ) NEW_LINE DEDENT if i != 1 : NEW_LINE INDENT for j in range ( self . k + 1 ) : NEW_LINE INDENT pre [ i ] [ j ] = ( pre [ i ] [ j ] + pre [ i - 1 ] [ j ] ) NEW_LINE DEDENT DEDENT DEDENT for i in range ( self . n , 1 , - 1 ) : NEW_LINE INDENT for j in range ( self . k + 1 ) : NEW_LINE INDENT val = self . f ( self . k , j ) NEW_LINE val1 = ( pow ( self . A [ i ] , self . k - j ) * pre [ i - 1 ] [ j ] ) NEW_LINE val = val * val1 NEW_LINE if j % 2 == 0 : NEW_LINE INDENT ans = ans + val NEW_LINE DEDENT else : NEW_LINE INDENT ans = ans - val NEW_LINE DEDENT DEDENT DEDENT ans = 2 * ans NEW_LINE return ans NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 3 NEW_LINE K = 3 NEW_LINE arr = [ 1 , 2 , 3 ] NEW_LINE obj = Solution ( N , K , arr ) NEW_LINE print ( obj . pairsPower ( ) ) NEW_LINE DEDENT
Count of ways to traverse a Matrix and return to origin in K steps | Python3 program to count total number of ways to return to origin after completing given number of steps . ; Function Initialize dp [ ] [ ] [ ] array with - 1 ; Function returns the total count ; Driver code
MOD = 1000000007 NEW_LINE dp = [ [ [ 0 for i in range ( 101 ) ] for i in range ( 101 ) ] for i in range ( 101 ) ] NEW_LINE N , M , K = 0 , 0 , 0 NEW_LINE def Initialize ( ) : NEW_LINE INDENT for i in range ( 101 ) : NEW_LINE INDENT for j in range ( 101 ) : NEW_LINE INDENT for z in range ( 101 ) : NEW_LINE INDENT dp [ i ] [ j ] [ z ] = - 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT def CountWays ( i , j , k ) : NEW_LINE INDENT if ( i >= N or i < 0 or j >= M or j < 0 or k < 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( i == 0 and j == 0 and k == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( dp [ i ] [ j ] [ k ] != - 1 ) : NEW_LINE INDENT return dp [ i ] [ j ] [ k ] NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] [ j ] [ k ] = ( CountWays ( i + 1 , j , k - 1 ) % MOD + CountWays ( i - 1 , j , k - 1 ) % MOD + CountWays ( i , j - 1 , k - 1 ) % MOD + CountWays ( i , j + 1 , k - 1 ) % MOD + CountWays ( i , j , k - 1 ) % MOD ) % MOD NEW_LINE DEDENT return dp [ i ] [ j ] [ k ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 3 NEW_LINE M = 3 NEW_LINE K = 4 NEW_LINE Initialize ( ) NEW_LINE print ( CountWays ( 0 , 0 , K ) ) NEW_LINE DEDENT
Count of subsequences of length atmost K containing distinct prime elements | Python3 Program to find the count of distinct prime subsequences at most of of length K from a given array ; Initialize all indices as true ; A value in prime [ i ] will finally be false if i is not a prime , else true ; If prime [ p ] is true , then it is a prime ; Update all multiples of p as false , i . e . non - prime ; Returns number of subsequences of maximum length k and contains distinct primes ; Store the primes in the given array ; Sort the primes ; Store the frequencies of all the distinct primes ; Store the frequency of primes ; Store the sum of all frequencies ; Store the length of subsequence at every instant ; Store the frequency ; Store the previous count of updated DP ; Calculate total subsequences of current of_length ; Add the number of subsequences to the answer ; Update the value in dp [ i ] ; Store the updated dp [ i ] ; Driver Code
prime = [ True ] * 1000000 NEW_LINE def SieveOfEratosthenes ( ) : NEW_LINE INDENT global prime NEW_LINE prime [ 0 ] = prime [ 1 ] = False NEW_LINE p = 2 NEW_LINE while p * p <= 100000 : NEW_LINE INDENT if ( prime [ p ] == True ) : NEW_LINE INDENT for i in range ( p * p , 100001 , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT p += 1 NEW_LINE DEDENT DEDENT def distinctPrimeSubSeq ( a , n , k ) : NEW_LINE INDENT SieveOfEratosthenes ( ) NEW_LINE primes = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( prime [ a [ i ] ] ) : NEW_LINE INDENT primes . append ( a [ i ] ) NEW_LINE DEDENT DEDENT l = len ( primes ) NEW_LINE primes . sort ( ) NEW_LINE b = [ ] NEW_LINE dp = [ ] NEW_LINE sum = 0 NEW_LINE i = 0 NEW_LINE while i < l : NEW_LINE INDENT count = 1 NEW_LINE x = a [ i ] NEW_LINE i += 1 NEW_LINE while ( i < l and a [ i ] == x ) : NEW_LINE INDENT count += 1 NEW_LINE i += 1 NEW_LINE DEDENT b . append ( count ) NEW_LINE dp . append ( count ) NEW_LINE sum += count NEW_LINE DEDENT of_length = 2 NEW_LINE leng = len ( dp ) NEW_LINE ans = 0 NEW_LINE while ( of_length <= k ) : NEW_LINE INDENT freq = 0 NEW_LINE prev = 0 NEW_LINE for i in range ( leng - 1 ) : NEW_LINE INDENT freq += dp [ i ] NEW_LINE j = sum - freq NEW_LINE subseq = b [ i ] * j NEW_LINE ans += subseq NEW_LINE dp [ i ] = subseq NEW_LINE prev += dp [ i ] NEW_LINE DEDENT leng -= 1 NEW_LINE sum = prev NEW_LINE of_length += 1 NEW_LINE DEDENT ans += ( l + 1 ) NEW_LINE return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 1 , 2 , 2 , 3 , 3 , 4 , 5 ] NEW_LINE n = len ( a ) NEW_LINE k = 3 NEW_LINE print ( distinctPrimeSubSeq ( a , n , k ) ) NEW_LINE DEDENT
Minimize prize count required such that smaller value gets less prize in an adjacent pair | Function to find the minimum number of required such that adjacent smaller elements gets less number of prizes ; Loop to iterate over every elements of the array ; Loop to find the consecutive smaller elements at left ; Loop to find the consecutive smaller elements at right ; Driver code
def findMinPrizes ( arr , n ) : NEW_LINE INDENT totalPrizes = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT x = 1 NEW_LINE j = i NEW_LINE while ( j > 0 and arr [ j ] > arr [ j - 1 ] ) : NEW_LINE INDENT x += 1 NEW_LINE j -= 1 NEW_LINE DEDENT j = i NEW_LINE y = 1 NEW_LINE while ( j < n - 1 and arr [ j ] > arr [ j + 1 ] ) : NEW_LINE INDENT y += 1 NEW_LINE j += 1 NEW_LINE DEDENT totalPrizes += max ( x , y ) NEW_LINE DEDENT print ( totalPrizes ) NEW_LINE DEDENT arr = [ 1 , 2 , 2 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE findMinPrizes ( arr , n ) NEW_LINE
Number of ways to split N as sum of K numbers from the given range | Python3 implementation to count the number of ways to divide N in K groups such that each group has elements in range [ L , R ] ; DP Table ; Function to count the number of ways to divide the number N in K groups such that each group has number of elements in range [ L , R ] ; Base Case ; if N is divides completely into less than k groups ; If the subproblem has been solved , use the value ; put all possible values greater equal to prev ; Function to count the number of ways to divide the number N ; Initialize DP Table as - 1 ; Driver code
mod = 1000000007 NEW_LINE dp = [ [ - 1 for j in range ( 1000 ) ] for i in range ( 1000 ) ] NEW_LINE def calculate ( pos , left , k , L , R ) : NEW_LINE INDENT if ( pos == k ) : NEW_LINE INDENT if ( left == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT if ( left == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( dp [ pos ] [ left ] != - 1 ) : NEW_LINE INDENT return dp [ pos ] [ left ] NEW_LINE DEDENT answer = 0 NEW_LINE for i in range ( L , R + 1 ) : NEW_LINE INDENT if ( i > left ) : NEW_LINE INDENT break NEW_LINE DEDENT answer = ( answer + calculate ( pos + 1 , left - i , k , L , R ) ) % mod NEW_LINE DEDENT dp [ pos ] [ left ] = answer NEW_LINE return answer NEW_LINE DEDENT def countWaystoDivide ( n , k , L , R ) : NEW_LINE INDENT return calculate ( 0 , n , k , L , R ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 12 NEW_LINE K = 3 NEW_LINE L = 1 NEW_LINE R = 5 NEW_LINE print ( countWaystoDivide ( N , K , L , R ) ) NEW_LINE DEDENT
Minimum number of squares whose sum equals to given number N | set 2 | Function for finding minimum square numbers ; arr [ i ] of array arr store minimum count of square number to get i ; sqrNum [ i ] store last square number to get i ; Find minimum count of square number for all value 1 to n ; In worst case it will be arr [ i - 1 ] + 1 we use all combination of a [ i - 1 ] and add 1 ; Check for all square number less or equal to i ; If it gives less count then update it ; v stores optimum square number whose sum give N ; Driver code ; Calling function ; Printing vector
def minSqrNum ( n ) : NEW_LINE INDENT arr = [ 0 ] * ( n + 1 ) NEW_LINE sqrNum = [ 0 ] * ( n + 1 ) NEW_LINE v = [ ] NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT arr [ i ] = arr [ i - 1 ] + 1 NEW_LINE sqrNum [ i ] = 1 NEW_LINE k = 1 ; NEW_LINE while ( k * k <= i ) : NEW_LINE INDENT if ( arr [ i ] > arr [ i - k * k ] + 1 ) : NEW_LINE INDENT arr [ i ] = arr [ i - k * k ] + 1 NEW_LINE sqrNum [ i ] = k * k NEW_LINE DEDENT k += 1 NEW_LINE DEDENT DEDENT while ( n > 0 ) : NEW_LINE INDENT v . append ( sqrNum [ n ] ) NEW_LINE n -= sqrNum [ n ] ; NEW_LINE DEDENT return v NEW_LINE DEDENT n = 10 NEW_LINE v = minSqrNum ( n ) NEW_LINE for i in range ( len ( v ) ) : NEW_LINE INDENT print ( v [ i ] , end = " " ) NEW_LINE if ( i < len ( v ) - 1 ) : NEW_LINE INDENT print ( " ▁ + ▁ " , end = " " ) NEW_LINE DEDENT DEDENT