giulio98/xlcost-single-prompt huggingface dataset:
# minimum number of elements which are not part of increasing or decreasing subsequence in array | python3 program to return minimum number of elements which are not part of increasing or decreasing subsequences . ; return minimum number of elements which is not part of any of the sequence . ; if already calculated , return value . ; if whole array is traversed . ; calculating by considering element as part of decreasing sequence . ; calculating by considering element as part of increasing sequence . ; if cannot be calculated for decreasing sequence . ; after considering once by decreasing sequence , now try for increasing sequence . ; if element cannot be part of any of the sequence . ; after considering element as part of increasing and decreasing sequence trying as not part of any of the sequence . ; wrapper function ; adding two number at the end of array , so that increasing and decreasing sequence can be made . max - 2 index is assigned int_max for decreasing sequence because / next number of sequence must be less than it . similarly , for increasing sequence int_min is assigned to max - 1 index . ; 

MAX = 102


def countMin(arr, dp, n, dec, inc, i):
    if dp[dec][inc][i] != -1:
        return dp[dec][inc][i]
    if i == n:
        return 0
    if arr[i] < arr[dec]:
        dp[dec][inc][i] = countMin(arr, dp, n, i, inc, i + 1)
    if arr[i] > arr[inc]:
        if dp[dec][inc][i] == -1:
            dp[dec][inc][i] = countMin(arr, dp, n, dec, i, i + 1)
        else:
            dp[dec][inc][i] = min(
                countMin(
                    arr,
                    dp,
                    n,
                    dec,
                    i,
                    i + 1),
                dp[dec][inc][i])
    if dp[dec][inc][i] == -1:
        dp[dec][inc][i] = 1 + countMin(arr, dp, n, dec, inc, i + 1)
    else:
        dp[dec][inc][i] = min(
            1 +
            countMin(
                arr,
                dp,
                n,
                dec,
                inc,
                i +
                1),
            dp[dec][inc][i])
    return dp[dec][inc][i]


def wrapper(arr, n):
    arr[MAX - 2] = 1000000000
    arr[MAX - 1] = -1000000000
    dp = [[[-1 for i in range(MAX)]for i in range(MAX)]for i in range(MAX)]
    return countMin(arr, dp, n, MAX - 2, MAX - 1, 0)


if __name__ == '__main__':
    n = 12
    arr = [7, 8, 1, 2, 4, 6, 3, 5, 2, 1, 8, 7]
    for i in range(MAX):
        arr .append(0)
    print(wrapper(arr, n))

# Expected output:
2
# find all distinct subset ( or subsequence ) sums of an array | sum denotes the current sum of the subset currindex denotes the index we have reached in the given array ; this function mainly calls recursive function distsumrec ( ) to generate distinct sum subsets . and finally prints the generated subsets . ; print the result ; 
def distSumRec(arr, n, sum, currindex, s):
    if (currindex > n):
        return
    if (currindex == n):
        s .add(sum)
        return
    distSumRec(arr, n, sum + arr[currindex], currindex + 1, s)
    distSumRec(arr, n, sum, currindex + 1, s)


def printDistSum(arr, n):
    s = set()
    distSumRec(arr, n, 0, 0, s)
    for i in s:
        print(i, end=" ")


if __name__ == '__main__':
    arr = [2, 3, 4, 5, 6]
    n = len(arr)
    printDistSum(arr, n)

# Expected output:
0 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 20
# find all distinct subset ( or subsequence ) sums of an array | uses dynamic programming to find distinct subset sums ; dp [ i ] [ j ] would be true if arr [ 0. . i - 1 ] has a subset with sum equal to j . ; there is always a subset with 0 sum ; fill dp [ ] [ ] in bottom up manner ; sums that were achievable without current array element ; print last row elements ; 
def printDistSum(arr, n):
    Sum = sum(arr)
    dp = [[False for i in range(Sum + 1)]for i in range(n + 1)]
    for i in range(n + 1):
        dp[i][0] = True
    for i in range(1, n + 1):
        dp[i][arr[i - 1]] = True
        for j in range(1, Sum + 1):
            if (dp[i - 1][j]):
                dp[i][j] = True
                dp[i][j + arr[i - 1]] = True
    for j in range(Sum + 1):
        if (dp[n][j]):
            print(j, end=" ")


arr = [2, 3, 4, 5, 6]
n = len(arr)
printDistSum(arr, n)

# Expected output:
0 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 20
# super ugly number ( number whose prime factors are in given set ) | function will return the nth super ugly number ; n cannot be negative hence return - 1 if n is 0 or - ve ; declare a min heap priority queue ; push all the array elements to priority queue ; once count = n we return no ; sorted ( pq ) get the minimum value from priority_queue ; if top of pq is no then don 't increment count.  this to avoid duplicate counting of same no. ; push all the multiples of no . to priority_queue ; cnt += 1 ; return nth super ugly number ; 
def ugly(a, size, n):
    if (n <= 0):
        return -1
    if (n == 1):
        return 1
    pq = []
    for i in range(size):
        pq .append(a[i])
    count = 1
    no = 0
    pq = sorted(pq)
    while (count < n):
        no = pq[0]
        del pq[0]
        if (no != pq[0]):
            count += 1
            for i in range(size):
                pq .append(no * a[i])
        pq = sorted(pq)
    return no


if __name__ == '__main__':
    a = [2, 3, 5]
    size = len(a)
    print(ugly(a, size, 1000))

# Expected output:
51200000
# count number of ways to reach destination in a maze | python 3 program to count number of paths in a maze with obstacles . ; returns count of possible paths in a maze [ r ] [ c ] from ( 0 , 0 ) to ( r - 1 , c - 1 ) ; 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 ; 

R = 4
C = 4


def countPaths(maze):
    if (maze[0][0] == -1):
        return 0
    for i in range(R):
        if (maze[i][0] == 0):
            maze[i][0] = 1
        else:
            break
    for i in range(1, C, 1):
        if (maze[0][i] == 0):
            maze[0][i] = 1
        else:
            break
    for i in range(1, R, 1):
        for j in range(1, C, 1):
            if (maze[i][j] == -1):
                continue
            if (maze[i - 1][j] > 0):
                maze[i][j] = (maze[i][j] + maze[i - 1][j])
            if (maze[i][j - 1] > 0):
                maze[i][j] = (maze[i][j] + maze[i][j - 1])
    if (maze[R - 1][C - 1] > 0):
        return maze[R - 1][C - 1]
    else:
        return 0


if __name__ == '__main__':
    maze = [[0, 0, 0, 0], [0, -1, 0, 0], [-1, 0, 0, 0], [0, 0, 0, 0]]
    print(countPaths(maze))

# Expected output:
4
# minimum sum subsequence such that at least one of every four consecutive elements is picked | returns sum of minimum sum subsequence such that one of every four consecutive elements is picked from arr [ ] . ; dp [ i ] is going to store minimum sum subsequence of arr [ 0. . i ] such that arr [ i ] is part of the solution . note that this may not be the best solution for subarray arr [ 0. . i ] ; if there is single value , we get the minimum sum equal to arr [ 0 ] ; if there are two values , we get the minimum sum equal to the minimum of two values ; if there are three values , return minimum of the three elements of array ; if there are four values , return minimum of the four elements of array ; return the minimum of last 4 index ; 
def minSum(arr, n):
    dp = [0] * n
    if (n == 1):
        return arr[0]
    if (n == 2):
        return min(arr[0], arr[1])
    if (n == 3):
        return min(arr[0], min(arr[1], arr[2]))
    if (n == 4):
        return min(min(arr[0], arr[1]), min(arr[2], arr[3]))
    dp[0] = arr[0]
    dp[1] = arr[1]
    dp[2] = arr[2]
    dp[3] = arr[3]
    for i in range(4, n):
        dp[i] = arr[i] + min(min(dp[i - 1], dp[i - 2]),
                             min(dp[i - 3], dp[i - 4]))
    return min(min(dp[n - 1], dp[n - 2]), min(dp[n - 4], dp[n - 3]))


if __name__ == "__main__":
    arr = [1, 2, 3, 3, 4, 5, 6, 1]
    n = len(arr)
    print(minSum(arr, n))

# Expected output:
4
# maximum decimal value path in a binary matrix | python3 program to find maximum decimal value path in binary matrix ; returns maximum decimal value in binary matrix . here p indicate power of 2 ; out of matrix boundary ; if current matrix value is 1 then return result + power ( 2 , p ) else result ; 

N = 4


def maxDecimalValue(mat, i, j, p):
    if i >= N or j >= N:
        return 0
    result = max(
        maxDecimalValue(
            mat,
            i,
            j + 1,
            p + 1),
        maxDecimalValue(
            mat,
            i + 1,
            j,
            p + 1))
    if mat[i][j] == 1:
        return pow(2, p) + result
    else:
        return result


mat = [[1, 1, 0, 1], [0, 1, 1, 0], [1, 0, 0, 1], [1, 0, 1, 1]]
print(maxDecimalValue(mat, 0, 0, 0))

# Expected output:
111
# maximum decimal value path in a binary matrix | python3 program to find maximum decimal value path in binary matrix ; returns maximum decimal value in binary matrix . here p indicate power of 2 ; compute binary stream of first row of matrix and store result in dp [ 0 ] [ i ] ; indicate 1 * ( 2 ^ i ) + result of previous ; indicate 0 * ( 2 ^ i ) + result of previous ; compute binary stream of first column of matrix and store result in dp [ i ] [ 0 ] ; indicate 1 * ( 2 ^ i ) + result of previous ; indicate 0 * ( 2 ^ i ) + result of previous ; traversal rest binary matrix and compute maximum decimal value ; here ( i + j ) indicate the current power of 2 in path that is 2 ^ ( i + j ) ; return maximum decimal value in binary matrix ; 

N = 4


def MaximumDecimalValue(mat, n):
    dp = [[0 for i in range(n)] for i in range(n)]
    if (mat[0][0] == 1):
        dp[0][0] = 1
    for i in range(1, n):
        if (mat[0][i] == 1):
            dp[0][i] = dp[0][i-1] + 2**i
        else:
            dp[0][i] = dp[0][i-1]
    for i in range(1, n):
        if (mat[i][0] == 1):
            dp[i][0] = dp[i-1][0] + 2**i
    else:
        dp[i][0] = dp[i-1][0]
    for i in range(1, n):
        for j in range(1, n):
            if (mat[i][j] == 1):
                dp[i][j] = max(dp[i][j-1], dp[i-1][j])+(2**(i+j))
            else:
                dp[i][j] = max(dp[i][j-1], dp[i-1][j])
    return dp[n-1][n-1]


if __name__ == '__main__':
    mat = [[1, 1, 0, 1], [0, 1, 1, 0], [1, 0, 0, 1], [1, 0, 1, 1]]
    print(MaximumDecimalValue(mat, 4))

# Expected output:
111
# count all palindrome sub | returns total number of palindrome substring of length greater then equal to 2 ; creat empty 2 - d matrix that counts all palindrome substring . dp [ i ] [ j ] stores counts of palindromic substrings in st [ i . . j ] ; p [ i ] [ j ] = true if substring str [ i . . j ] is palindrome , else false ; palindrome of single length ; palindrome of length 2 ; palindromes of length more than 2. this loop is similar to matrix chain multiplication . we start with a gap of length 2 and fill dp table in a way that the gap between starting and ending indexes increase one by one by outer loop . ; pick a starting point for the current gap ; set ending point ; if current string is palindrome ; add current palindrome substring ( + 1 ) and rest palindrome substring ( dp [ i ] [ j - 1 ] + dp [ i + 1 ] [ j ] ) remove common palindrome substrings ( - dp [ i + 1 ] [ j - 1 ] ) ; return total palindromic substrings ; 
def CountPS(str, n):
    dp = [[0 for x in range(n)]for y in range(n)]
    P = [[False for x in range(n)]for y in range(n)]
    for i in range(n):
        P[i][i] = True
    for i in range(n - 1):
        if (str[i] == str[i + 1]):
            P[i][i + 1] = True
            dp[i][i + 1] = 1
    for gap in range(2, n):
        for i in range(n - gap):
            j = gap + i
            if (str[i] == str[j] and P[i + 1][j - 1]):
                P[i][j] = True
            if (P[i][j]):
                dp[i][j] = (dp[i][j - 1] + dp[i + 1][j] + 1 - dp[i + 1][j - 1])
            else:
                dp[i][j] = (dp[i][j - 1] + dp[i + 1][j] - dp[i + 1][j - 1])
    return dp[0][n - 1]


if __name__ == "__main__":
    str = "abaab"
    n = len(str)
    print(CountPS(str, n))

# Expected output:
3
# maximum subsequence sum such that no three are consecutive | returns maximum subsequence sum such that no three elements are consecutive ; stores result for subarray arr [ 0. . i ] , i . e . , maximum possible sum in subarray arr [ 0. . i ] such that no three elements are consecutive . ; base cases ( process first three elements ) ; process rest of the elements we have three cases 1 ) exclude arr [ i ] , i . e . , sum [ i ] = sum [ i - 1 ] 2 ) exclude arr [ i - 1 ] , i . e . , sum [ i ] = sum [ i - 2 ] + arr [ i ] 3 ) exclude arr [ i - 2 ] , i . e . , sum [ i - 3 ] + arr [ i ] + arr [ i - 1 ] ; 
def maxSumWO3Consec(arr, n):
    sum = [0 for k in range(n)]
    if n >= 1:
        sum[0] = arr[0]
    if n >= 2:
        sum[1] = arr[0] + arr[1]
    if n > 2:
        sum[2] = max(sum[1], max(arr[1] + arr[2], arr[0] + arr[2]))
    for i in range(3, n):
        sum[i] = max(max(sum[i - 1], sum[i - 2] + arr[i]),
                     arr[i] + arr[i - 1] + sum[i - 3])
    return sum[n - 1]


arr = [100, 1000, 100, 1000, 1]
n = len(arr)
print(maxSumWO3Consec(arr, n))

# Expected output:
2101
# maximum sum of pairs with specific difference | method to return maximum sum we can get by get by finding less than k difference pair ; sort input array in ascending order . ; dp [ i ] denotes the maximum disjoint pair sum we can achieve using first i elements ; if no element then dp value will be 0 ; first give previous value to dp [ i ] i . e . no pairing with ( i - 1 ) th element ; if current and previous element can form a pair ; update dp [ i ] by choosing maximum between pairing and not pairing ; last index will have the result ; 
def maxSumPairWithDifferenceLessThanK(arr, N, K):
    arr .sort()
    dp = [0] * N
    dp[0] = 0
    for i in range(1, N):
        dp[i] = dp[i - 1]
        if (arr[i] - arr[i - 1] < K):
            if (i >= 2):
                dp[i] = max(dp[i], dp[i - 2] + arr[i] + arr[i - 1])
            else:
                dp[i] = max(dp[i], arr[i] + arr[i - 1])
    return dp[N - 1]


arr = [3, 5, 10, 15, 17, 12, 9]
N = len(arr)
K = 4
print(maxSumPairWithDifferenceLessThanK(arr, N, K))

# Expected output:
62
# recursively break a number in 3 parts to get maximum sum | function to find the maximum sum ; base conditions ; recursively break the number and return what maximum you can get ; 
def breakSum(n):
    if (n == 0 or n == 1):
        return n
    return max((breakSum(n // 2) + breakSum(n // 3) + breakSum(n // 4)), n)


if __name__ == "__main__":
    n = 12
    print(breakSum(n))

# Expected output:
13
# count all palindromic subsequence in a given string | function return the total palindromic subsequence ; create a 2d array to store the count of palindromic subsequence ; palindromic subsequence of length 1 ; check subsequence of length l is palindrome or not ; return total palindromic subsequence ; 
def countPS(str):
    N = len(str)
    cps = [[0 for i in range(N + 2)]for j in range(N + 2)]
    for i in range(N):
        cps[i][i] = 1
    for L in range(2, N + 1):
        for i in range(N):
            k = L + i - 1
            if (k < N):
                if (str[i] == str[k]):
                    cps[i][k] = (cps[i][k - 1] + cps[i + 1][k] + 1)
                else:
                    cps[i][k] = (cps[i][k - 1] + cps[i + 1]
                                 [k] - cps[i + 1][k - 1])
    return cps[0][N - 1]


str = "abcb"
print("Total palindromic subsequence are : ", countPS(str))

# Expected output:
Total palindromic subsequence are :  6
# count all palindromic subsequence in a given string | python 3 program to counts palindromic subsequence in a given string using recursion ; function return the total palindromic subsequence ; 

str = "abcb"


def countPS(i, j):
    if (i > j):
        return 0
    if (dp[i][j] != -1):
        return dp[i][j]
    if (i == j):
        dp[i][j] = 1
        return dp[i][j]
    elif (str[i] == str[j]):
        dp[i][j] = (countPS(i + 1, j) + countPS(i, j - 1) + 1)
        return dp[i][j]
    else:
        dp[i][j] = (
            countPS(
                i +
                1,
                j) +
            countPS(
                i,
                j -
                1) -
            countPS(
                i +
                1,
                j -
                1))
        return dp[i][j]


if __name__ == "__main__":
    dp = [[-1 for x in range(1000)]for y in range(1000)]
    n = len(str)
    print("Total palindromic subsequence are :", countPS(0, n - 1))

# Expected output:
Total palindromic subsequence are : 6
# count all increasing subsequences | function to count all the sub - sequences possible in which digit is greater than all previous digits arr [ ] is array of n digits ; count [ ] array is used to store all sub - sequences possible using that digit count [ ] array covers all the digit from 0 to 9 ; scan each digit in arr [ ] ; count all possible sub - sequences by the digits less than arr [ i ] digit ; store sum of all sub - sequences plus 1 in count [ ] array ; now sum up the all sequences possible in count [ ] array ; 
def countSub(arr, n):
    count = [0 for i in range(10)]
    for i in range(n):
        for j in range(arr[i] - 1, -1, -1):
            count[arr[i]] += count[j]
        count[arr[i]] += 1
    result = 0
    for i in range(10):
        result += count[i]
    return result


arr = [3, 2, 4, 5, 4]
n = len(arr)
print(countSub(arr, n))

# Expected output:
14
# find minimum sum such that one of every three consecutive elements is taken | a utility function to find minimum of 3 elements ; returns minimum possible sum of elements such that an element out of every three consecutive elements is picked . ; create a dp table to store results of subproblems . sum [ i ] is going to store minimum possible sum when arr [ i ] is part of the solution . ; when there are less than or equal to 3 elements ; iterate through all other elements ; 
def minimum(a, b, c):
    return min(min(a, b), c)


def findMinSum(arr, n):
    sum = []
    sum .append(arr[0])
    sum .append(arr[1])
    sum .append(arr[2])
    for i in range(3, n):
        sum .append(arr[i] + minimum(sum[i - 3], sum[i - 2], sum[i - 1]))
    return minimum(sum[n - 1], sum[n - 2], sum[n - 3])


arr = [1, 2, 3, 20, 2, 10, 1]
n = len(arr)
print("Min Sum is ", findMinSum(arr, n))

# Expected output:
Min Sum is  4
# count distinct subsequences | python3 program to print distinct subsequences of a given string ; create an empty set to store the subsequences ; function for generating the subsequences ; base case ; insert each generated subsequence into the set ; recursive case ; when a particular character is taken ; when a particular character isn 't taken ; 

import math
sn = []
global m
m = 0


def subsequences(s, op, i, j):
    if (i == m):
        op[j] = None
        temp = "".join([i for i in op if i])
        sn .append(temp)
        return
    else:
        op[j] = s[i]
        subsequences(s, op, i + 1, j + 1)
        subsequences(s, op, i + 1, j)
        return


str = "ggg"
m = len(str)
n = int(math .pow(2, m) + 1)
op = [None for i in range(n)]
subsequences(str, op, 0, 0)
print(len(set(sn)))

# Expected output:
4
# count distinct subsequences | returns count of distinct subsequences of str . ; iterate from 0 to length of s ; iterate from 0 to length of s ; check if i equal to 0 ; replace levelcount withe allcount + 1 ; if map is less than 0 ; return answer ; 
def countSub(s):
    Map = {}
    for i in range(len(s)):
        Map[s[i]] = -1
    allCount = 0
    levelCount = 0
    for i in range(len(s)):
        c = s[i]
        if (i == 0):
            allCount = 1
            Map = 1
            levelCount = 1
            continue
        levelCount = allCount + 1
        if (Map < 0):
            allCount = allCount + levelCount
        else:
            allCount = allCount + levelCount - Map
        Map = levelCount
    return allCount


List = ["abab", "gfg"]
for s in List:
    cnt = countSub(s)
    withEmptyString = cnt + 1
    print("With empty string count for", s, "is", withEmptyString)
    print("Without empty string count for", s, "is", cnt)

# Expected output:
With empty string count for abab is 5
Without empty string count for abab is 4
With empty string count for gfg is 4
Without empty string count for gfg is 3
# minimum cost to fill given weight in a bag | python program to find minimum cost to get exactly w kg with given packets ; cost [ ] initial cost array including unavailable packet w capacity of bag ; val [ ] and wt [ ] arrays val [ ] array to store cost of ' i ' kg packet of orange wt [ ] array weight of packet of orange ; traverse the original cost [ ] array and skip unavailable packets and make val [ ] and wt [ ] array . size variable tells the available number of distinct weighted packets . ; fill 0 th row with infinity ; fill 0 th column with 0 ; now check for each weight one by one and fill the matrix according to the condition ; wt [ i - 1 ] > j means capacity of bag is less than weight of item ; here we check we get minimum cost either by including it or excluding it ; exactly weight w can not be made by given weights ; 

INF = 1000000


def MinimumCost(cost, n, W):
    val = list()
    wt = list()
    size = 0
    for i in range(n):
        if (cost[i] != -1):
            val .append(cost[i])
            wt .append(i + 1)
            size += 1
    n = size
    min_cost = [[0 for i in range(W + 1)]for j in range(n + 1)]
    for i in range(W + 1):
        min_cost[0][i] = INF
    for i in range(1, n + 1):
        min_cost[i][0] = 0
    for i in range(1, n + 1):
        for j in range(1, W + 1):
            if (wt[i - 1] > j):
                min_cost[i][j] = min_cost[i - 1][j]
            else:
                min_cost[i][j] = min(min_cost[i - 1][j],
                                     min_cost[i][j - wt[i - 1]] + val[i - 1])
    if (min_cost[n][W] == INF):
        return -1
    else:
        return min_cost[n][W]


cost = [1, 2, 3, 4, 5]
W = 5
n = len(cost)
print(MinimumCost(cost, n, W))

# Expected output:
5
# find number of times a string occurs as a subsequence in given string | recursive function to find the number of times the second string occurs in the first string , whether continuous or discontinuous ; if both first and second string is empty , or if second string is empty , return 1 ; if only first string is empty and second string is not empty , return 0 ; if last characters are same recur for remaining strings by 1. considering last characters of both strings 2. ignoring last character of first string ; if last characters are different , ignore last char of first string and recur for remaining string ; 
def count(a, b, m, n):
    if ((m == 0 and n == 0) or n == 0):
        return 1
    if (m == 0):
        return 0
    if (a[m - 1] == b[n - 1]):
        return (count(a, b, m - 1, n - 1) + count(a, b, m - 1, n))
    else:
        return count(a, b, m - 1, n)


a = "GeeksforGeeks"
b = "Gks"
print(count(a, b, len(a), len(b)))

# Expected output:
4