giulio98/xlcost-single-prompt huggingface dataset:
# count ways to obtain given sum by repeated throws of a dice | function to calculate the total number of ways to have sum n ; base case ; return already stored result ; recur for all 6 states ; return the result ; 
def findWays(N, dp):
    if (N == 0):
        return 1
    if (dp[N] != -1):
        return dp[N]
    cnt = 0
    for i in range(1, 7):
        if (N - i >= 0):
            cnt = (cnt + findWays(N - i, dp))
    dp[N] = cnt
    return dp[N]


if __name__ == "__main__":
    N = 4
    dp = [-1] * (N + 1)
    print(findWays(N, dp))

# Expected output:
8
# count ways to obtain given sum by repeated throws of a dice | function to calculate the total number of ways to have sum n ; initialize dp array ; iterate over all the possible intermediate values to reach n ; calculate the sum for all 6 faces ; print total number of ways ; 
def findWays(N):
    dp = [0] * (N + 1)
    dp[0] = 1
    for i in range(1, N + 1):
        dp[i] = 0
        for j in range(1, 7):
            if (i - j >= 0):
                dp[i] = dp[i] + dp[i - j]
    print(dp[N])


if __name__ == '__main__':
    N = 4
    findWays(N)

# Expected output:
8
# split the string into minimum parts such that each part is in the another string | python3 implementation to split the string into minimum number of parts such that each part is also present in the another string ; node of trie ; function to insert a node in the trie data structure ; inserting every character from idx till end to string into trie ; if there is no edge corresponding to the ith character , then make a new node ; function to find the minimum number of parts such that each part is present into another string ; making a new trie ; inserting every substring of s2 in trie ; creating dp array and init it with infinity ; base case ; starting the cut from ith character taking temporary node pointer for checking whether the substring [ i , j ) is present in trie of not ; if the jth character is not in trie we 'll break ; updating the the ending of jth character with dp [ i ] + 1 ; descending the trie pointer ; answer not possible ; 

INF = 1e9 + 9


class TrieNode ():
    def __init__(self):
        self .child = [None] * 26


def insert(idx, s, root):
    temp = root
    for i in range(idx, len(s)):
        if temp .child[ord(s[i]) - ord('a')] is None:
            temp .child[ord(s[i]) - ord('a')] = TrieNode()
        temp = temp .child[ord(s[i]) - ord('a')]


def minCuts(S1, S2):
    n1 = len(S1)
    n2 = len(S2)
    root = TrieNode()
    for i in range(n2):
        insert(i, S2, root)
    dp = [INF] * (n1 + 1)
    dp[0] = 0
    for i in range(n1):
        temp = root
        for j in range(i + 1, n1 + 1):
            if temp .child[ord(S1[j - 1]) - ord('a')] is None:
                break
            dp[j] = min(dp[j], dp[i] + 1)
            temp = temp .child[ord(S1[j - 1]) - ord('a')]
    if dp[n1] >= INF:
        return -1
    else:
        return dp[n1]


S1 = "abcdab"
S2 = "dabc"
print(minCuts(S1, S2))

# Expected output:
2
# largest square in a binary matrix with at most k 1 s for multiple queries | function to find the largest square in the matrix such that it contains atmost k 1 's  ; precomputing the countdp prefix sum of the matrix ; loop to solve queries ; calculating the maximum possible distance of the centre from edge ; count total number of 1 s in the sub square considered ; if the count is less than or equals to the maximum move to right half ; 
def largestSquare(matrix, R, C, q_i, q_j, K, Q):
    countDP = [[0 for x in range(C)]for x in range(R)]
    countDP[0][0] = matrix[0][0]
    for i in range(1, R):
        countDP[i][0] = (countDP[i - 1][0] + matrix[i][0])
    for j in range(1, C):
        countDP[0][j] = (countDP[0][j - 1] + matrix[0][j])
    for i in range(1, R):
        for j in range(1, C):
            countDP[i][j] = (matrix[i][j] + countDP[i - 1]
                             [j] + countDP[i][j - 1] - countDP[i - 1][j - 1])
    for q in range(0, Q):
        i = q_i[q]
        j = q_j[q]
        min_dist = min(i, j, R - i - 1, C - j - 1)
        ans = -1
        l = 0
        u = min_dist
        while (l <= u):
            mid = int((l + u) / 2)
            x1 = i - mid
            x2 = i + mid
            y1 = j - mid
            y2 = j + mid
            count = countDP[x2][y2]
            if (x1 > 0):
                count -= countDP[x1 - 1][y2]
            if (y1 > 0):
                count -= countDP[x2][y1 - 1]
            if (x1 > 0 and y1 > 0):
                count += countDP[x1 - 1][y1 - 1]
            if (count <= K):
                ans = 2 * mid + 1
                l = mid + 1
            else:
                u = mid - 1
        print(ans)


matrix = [[1, 0, 1, 0, 0], [1, 0, 1, 1, 1], [1, 1, 1, 1, 1], [1, 0, 0, 1, 0]]
K = 9
Q = 1
q_i = [1]
q_j = [2]
largestSquare(matrix, 4, 5, q_i, q_j, K, Q)

# Expected output:
3