giulio98/xlcost-single-prompt huggingface dataset:
# split array into two equal length subsets such that all repetitions of a number lies in a single subset | python3 program for the above approach ; function to create the frequency array of the given array arr [ ] ; hashmap to store the frequencies ; store freq for each element ; get the total frequencies ; store frequencies in subset [ ] array ; return frequency array ; function to check is sum n / 2 can be formed using some subset ; dp [ i ] [ j ] store the answer to form sum j using 1 st i elements ; initialize dp [ ] [ ] with true ; fill the subset table in the bottom up manner ; if current element is less than j ; update current state ; return the result ; function to check if the given array can be split into required sets ; store frequencies of arr [ ] ; if size of arr [ ] is odd then print " yes " ; check if answer is true or not ; print the result ; 

from collections import defaultdict


def findSubsets(arr):
    M = defaultdict(int)
    for i in range(len(arr)):
        M[arr[i]] += 1
    subsets = [0] * len(M)
    i = 0
    for j in M:
        subsets[i] = M[j]
        i += 1
    return subsets


def subsetSum(subsets, target):
    dp = [[0 for x in range(target + 1)]for y in range(len(subsets) + 1)]
    for i in range(len(dp)):
        dp[i][0] = True
    for i in range(1, len(subsets) + 1):
        for j in range(1, target + 1):
            dp[i][j] = dp[i - 1][j]
            if (j >= subsets[i - 1]):
                dp[i][j] |= (dp[i - 1][j - subsets[i - 1]])
    return dp[len(subsets)][target]


def divideInto2Subset(arr):
    subsets = findSubsets(arr)
    if (len(arr) % 2 == 1):
        print("No")
        return
    isPossible = subsetSum(subsets, len(arr) // 2)
    if (isPossible):
        print("Yes")
    else:
        print("No")


if __name__ == "__main__":
    arr = [2, 1, 2, 3]
    divideInto2Subset(arr)

# Expected output:
Yes
# maximum product cutting | dp | the main function that returns maximum product obtainable from a rope of length n ; base cases ; make a cut at different places and take the maximum of all ; return the maximum of all values ; 
def maxProd(n):
    if (n == 0 or n == 1):
        return 0
    max_val = 0
    for i in range(1, n - 1):
        max_val = max(max_val, max(i * (n - i), maxProd(n - i) * i))
    return max_val


print("Maximum Product is ", maxProd(10))

# Expected output:
Maximum Product is  36
# assembly line scheduling | dp | python program to find minimum possible time by the car chassis to complete ; time taken to leave ; first station in line 1 time taken to leave ; fill tables t1 [ ] and t2 [ ] using above given recursive relations ; consider exit times and return minimum ; 
def carAssembly(a, t, e, x):
    NUM_STATION = len(a[0])
    T1 = [0 for i in range(NUM_STATION)]
    T2 = [0 for i in range(NUM_STATION)]

    T1[0] = e[0] + a[0][0]
    T2[0] = e[1] + a[1][0]
    for i in range(1, NUM_STATION):
        T1[i] = min(T1[i - 1] + a[0][i], T2[i - 1] + t[1][i] + a[0][i])
        T2[i] = min(T2[i - 1] + a[1][i], T1[i - 1] + t[0][i] + a[1][i])
    return min(T1[NUM_STATION - 1]+x[0], T2[NUM_STATION - 1]+x[1])


a = [[4, 5, 3, 2], [2, 10, 1, 4]]
t = [[0, 7, 4, 5], [0, 9, 2, 8]]
e = [10, 12]
x = [18, 7]
print((carAssembly(a, t, e, x)))

# Expected output:
35
# longest common substring | dp | returns length of longest common substring of x [ 0. . m - 1 ] and y [ 0. . n - 1 ] ; create a table to store lengths of longest common suffixes of substrings . note that lcsuff [ i ] [ j ] contains the length of longest common suffix of x [ 0. . . i - 1 ] and y [ 0. . . j - 1 ] . the first row and first column entries have no logical meaning , they are used only for simplicity of the program . ; to store the length of longest common substring ; following steps to build lcsuff [ m + 1 ] [ n + 1 ] in bottom up fashion ; 
def LCSubStr(X, Y, m, n):
    LCSuff = [[0 for k in range(n + 1)]for l in range(m + 1)]
    result = 0
    for i in range(m + 1):
        for j in range(n + 1):
            if (i == 0 or j == 0):
                LCSuff[i][j] = 0
            elif (X[i - 1] == Y[j - 1]):
                LCSuff[i][j] = LCSuff[i - 1][j - 1] + 1
                result = max(result, LCSuff[i][j])
            else:
                LCSuff[i][j] = 0
    return result


X = 'OldSite:GeeksforGeeks.org'
Y = 'NewSite:GeeksQuiz.com'
m = len(X)
n = len(Y)
print('Length of Longest Common Substring is', LCSubStr(X, Y, m, n))

# Expected output:
Length of Longest Common Substring is 10
# minimum insertions to form a palindrome | dp | a utility function to find minimum of two integers ; a dp function to find minimum number of insertions ; create a table of size n * n . table [ i ] [ j ] will store minimum number of insertions needed to convert str1 [ i . . j ] to a palindrome . ; fill the table ; return minimum number of insertions for str1 [ 0. . n - 1 ] ; 
def Min(a, b):
    return min(a, b)


def findMinInsertionsDP(str1, n):
    table = [[0 for i in range(n)]for i in range(n)]
    l, h, gap = 0, 0, 0
    for gap in range(1, n):
        l = 0
        for h in range(gap, n):
            if str1[l] == str1[h]:
                table[l][h] = table[l + 1][h - 1]
            else:
                table[l][h] = (Min(table[l][h - 1], table[l + 1][h]) + 1)
            l += 1
    return table[0][n - 1]


str1 = "geeks"
print(findMinInsertionsDP(str1, len(str1)))

# Expected output:
3
# maximum subarray sum using divide and conquer algorithm | find the maximum possible sum in arr [ ] auch that arr [ m ] is part of it ; include elements on left of mid . ; include elements on right of mid ; return sum of elements on left and right of mid returning only left_sum + right_sum will fail for [ - 2 , 1 ] ; returns sum of maximum sum subarray in aa [ l . . h ] ; base case : only one element ; find middle point ; return maximum of following three possible cases a ) maximum subarray sum in left half b ) maximum subarray sum in right half c ) maximum subarray sum such that the subarray crosses the midpoint ; 
def maxCrossingSum(arr, l, m, h):
    sm = 0
    left_sum = -10000
    for i in range(m, l - 1, -1):
        sm = sm + arr[i]
        if (sm > left_sum):
            left_sum = sm
    sm = 0
    right_sum = -1000
    for i in range(m + 1, h + 1):
        sm = sm + arr[i]
        if (sm > right_sum):
            right_sum = sm
    return max(left_sum + right_sum, left_sum, right_sum)


def maxSubArraySum(arr, l, h):
    if (l == h):
        return arr[l]
    m = (l + h) // 2
    return max(
        maxSubArraySum(
            arr,
            l,
            m),
        maxSubArraySum(
            arr,
            m + 1,
            h),
        maxCrossingSum(
            arr,
            l,
            m,
            h))


arr = [2, 3, 4, 5, 7]
n = len(arr)
max_sum = maxSubArraySum(arr, 0, n - 1)
print("Maximum contiguous sum is ", max_sum)

# Expected output:
Maximum contiguous sum is  21
# program to find amount of water in a given glass | returns the amount of water in jth glass of ith row ; a row number i has maximum i columns . so input column number must be less than i ; there will be i * ( i + 1 ) / 2 glasses till ith row ( including ith row ) and initialize all glasses as empty ; put all water in first glass ; now let the water flow to the downward glasses till the row number is less than or / equal to i ( given row ) correction : x can be zero for side glasses as they have lower rate to fill ; fill glasses in a given row . number of columns in a row is equal to row number ; get the water from current glass ; keep the amount less than or equal to capacity in current glass ; get the remaining amount ; distribute the remaining amount to the down two glasses ; the index of jth glass in ith row will be i * ( i - 1 ) / 2 + j - 1 ; 
def findWater(i, j, X):
    if (j > i):
        print("Incorrect Input")
        return
    glass = [0] * int(i * (i + 1) / 2)
    index = 0
    glass[index] = X
    for row in range(1, i):
        for col in range(1, row + 1):
            X = glass[index]
            glass[index] = 1.0 if (X >= 1.0)else X
            X = (X - 1)if (X >= 1.0)else 0.0
            glass[index + row] += (X / 2)
            glass[index + row + 1] += (X / 2)
            index += 1
    return glass[int(i * (i - 1) / 2 + j - 1)]


if __name__ == "__main__":
    i = 2
    j = 2
    X = 2.0
    res = repr(findWater(i, j, X))
    print("Amount of water in jth glass of ith row is:", res .ljust(8, '0'))

# Expected output:
Amount of water in jth glass of ith row is: 0.500000
# maximum length chain of pairs | dp | python program for above approach ; this function assumes that arr [ ] is sorted in increasing order according the first ( or smaller ) values in pairs . ; initialize mcl ( max chain length ) values for all indices ; compute optimized chain length values in bottom up manner ; pick maximum of all mcl values ; 

class Pair (object):
    def __init__(self, a, b):
        self .a = a
        self .b = b


def maxChainLength(arr, n):
    max = 0
    mcl = [1 for i in range(n)]
    for i in range(1, n):
        for j in range(0, i):
            if (arr[i].a > arr[j].b and mcl[i] < mcl[j] + 1):
                mcl[i] = mcl[j] + 1
    for i in range(n):
        if (max < mcl[i]):
            max = mcl[i]
    return max


arr = [Pair(5, 24), Pair(15, 25), Pair(27, 40), Pair(50, 60)]
print('Length of maximum size chain is', maxChainLength(arr, len(arr)))

# Expected output:
Length of maximum size chain is 3
# palindrome partitioning | dp | returns the minimum number of cuts needed to partition a string such that every part is a palindrome ; get the length of the string ; create two arrays to build the solution in bottom up manner c [ i ] [ j ] = minimum number of cuts needed for palindrome partitioning of substring str [ i . . j ] p [ i ] [ j ] = true if substring str [ i . . j ] is palindrome , else false . note that c [ i ] [ j ] is 0 if p [ i ] [ j ] is true ; different looping variables ; every substring of length 1 is a palindrome ; l is substring length . build the solution in bottom - up manner by considering all substrings of length starting from 2 to n . the loop structure is the same as matrix chain multiplication problem ( see https : www . geeksforgeeks . org / matrix - chain - multiplication - dp - 8 / ) ; for substring of length l , set different possible starting indexes ; set ending index ; if l is 2 , then we just need to compare two characters . else need to check two corner characters and value of p [ i + 1 ] [ j - 1 ] ; if str [ i . . j ] is palindrome , then c [ i ] [ j ] is 0 ; make a cut at every possible location starting from i to j , and get the minimum cost cut . ; return the min cut value for complete string . i . e . , str [ 0. . n - 1 ] ; 
def minPalPartion(str):
    n = len(str)
    C = [[0 for i in range(n)]for i in range(n)]
    P = [[False for i in range(n)]for i in range(n)]
    j = 0
    k = 0
    L = 0
    for i in range(n):
        P[i][i] = True
        C[i][i] = 0
    for L in range(2, n + 1):
        for i in range(n - L + 1):
            j = i + L - 1
            if L == 2:
                P[i][j] = (str[i] == str[j])
            else:
                P[i][j] = ((str[i] == str[j]) and P[i + 1][j - 1])
            if P[i][j]:
                C[i][j] = 0
            else:
                C[i][j] = 100000000
                for k in range(i, j):
                    C[i][j] = min(C[i][j], C[i][k] + C[k + 1][j] + 1)
    return C[0][n - 1]


str = "ababbbabbababa"
print('Min cuts needed for Palindrome Partitioning is', minPalPartion(str))

# Expected output:
Min cuts needed for Palindrome Partitioning is 3
# minimum replacements required to make given matrix palindromic | function to count minimum changes to make the matrix palindromic ; rows in the matrix ; columns in the matrix ; traverse the given matrix ; store the frequency of the four cells ; iterate over the map ; min changes to do to make all ; four elements equal ; make the middle row palindromic ; make the middle column palindromic ; prminimum changes ; 
def minchanges(mat):
    N = len(mat)
    M = len(mat[0])
    ans = 0
    mp = {}
    for i in range(N // 2):
        for j in range(M // 2):
            mp[mat[i][M - 1 - j]] = mp .get(mat[i][M - 1 - j], 0) + 1
            mp[mat[i][j]] = mp .get(mat[i][j], 0) + 1
            mp[mat[N - 1 - i][M - 1 - j]
               ] = mp .get(mat[N - 1 - i][M - 1 - j], 0) + 1
            mp[mat[N - 1 - i][j]] = mp .get(mat[N - 1 - i][j], 0) + 1
            x = 0
            for it in mp:
                x = max(x, mp[it])
            ans = ans + 4 - x
            mp .clear()
    if (N % 2 == 1):
        for i in range(M // 2):
            if (mat[N // 2][i] != mat[N // 2][M - 1 - i]):
                ans += 1
    if (M % 2 == 1):
        for i in range(N // 2):
            if (mat[i][M // 2] != mat[N - 1 - i][M // 2]):
                ans += 1
    print(ans)


mat = [[1, 2, 3], [4, 5, 3], [1, 2, 1]]
minchanges(mat)

# Expected output:
2
# check if a number starts with another number or not | function to check if b is a prefix of a or not ; convert numbers into strings ; check if s2 is a prefix of s1 or not using startswith ( ) function ; if result is true print yes ; 
def checkprefix(A, B):
    s1 = str(A)
    s2 = str(B)
    result = s1 .startswith(s2)
    if result:
        print("Yes")
    else:
        print("No")


if __name__ == '__main__':
    A = 12345
    B = 12
    checkprefix(A, B)

# Expected output:
Yes
# check three or more consecutive identical characters or numbers | python3 program to check three or more consecutiveidentical characters or numbers using regular expression ; function to check three or more consecutiveidentical characters or numbers using regular expression ; regex to check three or more consecutive identical characters or numbers ; compile the regex ; if the string is empty return false ; return if the string matched the regex ; test case 1 : ; test case 2 : ; test case 3 : ; test case 4 : ; 

import re


def isValidGUID(str):
    regex = "\\b([a-zA-Z0-9])\\1\\1+\\b"
    p = re .compile(regex)
    if (str is None):
        return False
    if (re .search(p, str)):
        return True
    else:
        return False


str1 = "aaa"
print(isValidGUID(str1))
str2 = "11111"
print(isValidGUID(str2))
str3 = "aaab"
print(isValidGUID(str3))
str4 = "abc"
print(isValidGUID(str4))
str5 = "aa"
print(isValidGUID(str5))

# Expected output:
True
True
False
False
False
# how to validate mac address using regular expression | python3 program to validate mac address using using regular expression ; function to validate mac address . ; regex to check valid mac address ; compile the regex ; if the string is empty return false ; return if the string matched the regex ; test case 1 : ; test case 2 : ; test case 3 : ; test case 4 : ; 

import re


def isValidMACAddress(str):
    regex = (
        "^([0-9A-Fa-f]{2}[:-])" +
        "{5}([0-9A-Fa-f]{2})|" +
        "([0-9a-fA-F]{4}\\." +
        "[0-9a-fA-F]{4}\\." +
        "[0-9a-fA-F]{4})$")
    p = re .compile(regex)
    if (str is None):
        return False
    if (re .search(p, str)):
        return True
    else:
        return False


str1 = "01-23-45-67-89-AB"
print(isValidMACAddress(str1))
str2 = "01:23:45:67:89:AB"
print(isValidMACAddress(str2))
str3 = "0123.4567.89AB"
print(isValidMACAddress(str3))
str4 = "01-23-45-67-89-AH"
print(isValidMACAddress(str4))
str5 = "01-23-45-67-AH"
print(isValidMACAddress(str5))

# Expected output:
True
True
True
False
False
# how to validate guid ( globally unique identifier ) using regular expression | python3 program to validate guid ( globally unique identifier ) using regular expression ; function to validate guid ( globally unique identifier ) ; regex to check valid guid ( globally unique identifier ) ; compile the regex ; if the string is empty return false ; return if the string matched the regex ; test case 1 : ; test case 2 : ; test case 3 : ; 

import re


def isValidGUID(str):
    regex = "^[{]?[0-9a-fA-F]{8}" + \
        "-([0-9a-fA-F]{4}-)" + "{3}[0-9a-fA-F]{12}[}]?$"
    p = re .compile(regex)
    if (str is None):
        return False
    if (re .search(p, str)):
        return True
    else:
        return False


str1 = "123e4567-e89b-12d3" + "-a456-9AC7CBDCEE52"
print(isValidGUID(str1))
str2 = "{123e4567-e89b-12d3-" + "a456-9AC7CBDCEE52}"
print(isValidGUID(str2))
str3 = "123e4567-h89b-12d3-a456" + "-9AC7CBDCEE52"
print(isValidGUID(str3))
str4 = "123e4567-h89b-12d3-a456"
print(isValidGUID(str4))

# Expected output:
True
True
False
False
# how to validate indian driving license number using regular expression | python program to validate indian driving license number using regular expression ; function to validate indian driving license number . ; regex to check valid indian driving license number ; compile the regex ; if the string is empty return false ; return if the string matched the regex ; test case 1 : ; test case 2 : ; test case 3 : ; test case 4 : ; 

import re


def isValidLicenseNo(str):
    regex = (
        "^(([A-Z]{2}[0-9]{2})" +
        "( )|([A-Z]{2}-[0-9]" +
        "{2}))((19 20)[0-9]" +
        "[0-9])[0-9]{7}$")
    p = re .compile(regex)
    if (str is None):
        return False
    if (re .search(p, str)):
        return True
    else:
        return False


str1 = "HR-0619850034761"
print(isValidLicenseNo(str1))
str2 = "UP14 20160034761"
print(isValidLicenseNo(str2))
str3 = "12HR-37200602347"
print(isValidLicenseNo(str3))
str4 = "MH27 30123476102"
print(isValidLicenseNo(str4))
str5 = "GJ-2420180"
print(isValidLicenseNo(str5))

# Expected output:
False
False
False
False
False
# how to validate indian passport number using regular expression | python3 program to validate passport number of india using regular expression ; function to validate the pin code of india . ; regex to check valid pin code of india . ; compile the regex ; if the string is empty return false ; pattern class contains matcher ( ) method to find matching between given string and regular expression . ; return true if the string matched the regex else false ; 

import re


def isValidPassportNo(string):
    regex = "^[A-PR-WYa-pr-wy][1-9]\d" + "\s?\d{4}[1-9]$"
    p = re.compile(regex)
    if (string == ''):
        return False
    m = re.match(p, string)
    if m is None:
        return False
    else:
        return True


if __name__ == "__main__":
    str1 = "A21 90457"
    print(isValidPassportNo(str1))
    str2 = "A0296457"
    print(isValidPassportNo(str2))
    str3 = "Q2096453"
    print(isValidPassportNo(str3))
    str4 = "12096457"
    print(isValidPassportNo(str4))
    str5 = "A209645704"
    print(isValidPassportNo(str5))

# Expected output:
True
False
False
False
False
# how to validate visa card number using regular expression | python3 program to validate visa card number using regular expression ; function to validate visa card number using regular expression . ; regex to check valid visa card number ; compile the regex ; if the string is empty return false ; pattern class contains matcher ( ) method to find matching between given string and regular expression . ; return true if the string matched the regex else false ; 

import re


def isValidVisaCardNo(string):
    regex = "^4[0-9]{12}(?:[0-9]{3})?$"
    p = re .compile(regex)
    if (string == ''):
        return False
    m = re .match(p, string)
    if m is None:
        return False
    else:
        return True


if __name__ == "__main__":
    str1 = "4155279860457"
    print(isValidVisaCardNo(str1))
    str2 = "4155279860457201"
    print(isValidVisaCardNo(str2))
    str3 = "4155279"
    print(isValidVisaCardNo(str3))
    str4 = "6155279860457"
    print(isValidVisaCardNo(str4))

# Expected output:
True
True
False
False
# how to validate mastercard number using regular expression | python3 program to validate master card number using regular expression ; function to validate master card number using regular expression . ; regex to check valid master card number . ; compile the regex ; if the string is empty return false ; return if the string matched the regex ; test case 1 : ; test case 2 : ; test case 3 : ; test case 4 : ; 

import re


def isValidMasterCardNo(str):
    regex = "^5[1-5][0-9]{14}|" + "^(222[1-9] 22[3-9]\d " + \
        "2[3-6]\d{2} 27[0-1]\d " + "2720)[0-9]{12}$"
    p = re .compile(regex)
    if (str is None):
        return False
    if (re .search(p, str)):
        return True
    else:
        return False


str1 = "5114496353984312"
print(isValidMasterCardNo(str1))
str2 = "2720822463109651"
print(isValidMasterCardNo(str2))
str3 = "5582822410"
print(isValidMasterCardNo(str3))
str4 = "6082822463100051"
print(isValidMasterCardNo(str4))

# Expected output:
True
False
False
False
# how to validate cvv number using regular expression | python3 program to validate cvv ( card verification value ) number using regex . ; function to validate cvv ( card verification value ) number . using regular expression . ; regex to check valid cvv number . ; compile the regex ; if the string is empty return false ; return if the string matched the regex ; test case 1 : ; test case 2 : ; test case 3 : ; 

import re


def isValidCVVNumber(str):
    regex = "^[0-9]{3,4}$"
    p = re .compile(regex)
    if (str is None):
        return False
    if (re .search(p, str)):
        return True
    else:
        return False


str1 = "561"
print(isValidCVVNumber(str1))
str2 = "5061"
print(isValidCVVNumber(str2))
str3 = "50614"
print(isValidCVVNumber(str3))

# Expected output:
True
True
False
# how to validate ifsc code using regular expression | python3 program to validate ifsc ( indian financial system ) code using regular expression ; function to validate ifsc ( indian financial system ) code using regular expression . ; regex to check valid ifsc code . ; compile the regex ; if the string is empty return false ; return if the string matched the regex ; test case 1 : ; test case 2 : ; test case 3 : ; 

import re


def isValidIFSCode(str):
    regex = "^[A-Z]{4}0[A-Z0-9]{6}$"
    p = re .compile(regex)
    if (str is None):
        return False
    if (re .search(p, str)):
        return True
    else:
        return False


str1 = "SBIN0125620"
print(isValidIFSCode(str1))
str2 = "SBIN0125"
print(isValidIFSCode(str2))
str3 = "1234SBIN012"
print(isValidIFSCode(str3))
str4 = "SBIN7125620"
print(isValidIFSCode(str4))

# Expected output:
True
False
False
False
# how to validate gst ( goods and services tax ) number using regular expression | python3 program to validate gst ( goods and services tax ) number using regular expression ; function to validate gst ( goods and services tax ) number . ; regex to check valid gst ( goods and services tax ) number ; compile the regex ; if the string is empty return false ; return if the string matched the regex ; test case 1 : ; test case 2 : ; test case 3 : ; test case 4 : ; 

import re


def isValidMasterCardNo(str):
    regex = "^[0-9]{2}[A-Z]{5}[0-9]{4}"+"[A-Z]{1}[1-9A-Z]{1}"
    p = re .compile(regex)
    if (str is None):
        return False
    if (re .search(p, str)):
        return True
    else:
        return False


str1 = "06BZAHM6385P6Z2"
print(isValidMasterCardNo(str1))
str2 = "06BZAF67"
print(isValidMasterCardNo(str2))
str3 = "AZBZAHM6385P6Z2"
print(isValidMasterCardNo(str3))
str4 = "06BZ63AHM85P6Z2"
print(isValidMasterCardNo(str4))
str5 = "06BZAHM6385P6F2"
print(isValidMasterCardNo(str5))

# Expected output:
True
False
False
False
True
# how to validate html tag using regular expression | python3 program to validate html tag using regex . using regular expression ; function to validate html tag using regex . ; regex to check valid html tag using regex . ; compile the regex ; if the string is empty return false ; return if the string matched the regex ; test case 1 : ; test case 2 : ; test case 3 : ; 

import re


def isValidHTMLTag(str):
    regex = "<(\"[^\"]*\" '[^']*' [^'\">])*>"
    p = re .compile(regex)
    if (str is None):
        return False
    if (re .search(p, str)):
        return True
    else:
        return False


str1 = "<input value = '>'>"
print(isValidHTMLTag(str1))
str2 = "<br/>"
print(isValidHTMLTag(str2))
str3 = "br/>"
print(isValidHTMLTag(str3))
str4 = "<'br/>"
print(isValidHTMLTag(str4))

# Expected output:
False
False
False
False