giulio98/xlcost-single-prompt huggingface dataset:
# count of substrings whose decimal equivalent is greater than or equal to k | function to count number of substring whose decimal equivalent is greater than or equal to k ; left pointer of the substring ; right pointer of the substring ; loop to maintain the last occurrence of the 1 in the string ; variable to count the substring ; loop to maintain the every possible end index of the substring ; loop to find the substring whose decimal equivalent is greater than or equal to k ; condition to check no of bits is out of bound ; 
def countSubstr(s, k):
    n = len(s)
    l = n - 1
    r = n - 1
    arr = [0] * n
    last_indexof1 = -1
    for i in range(n):
        if (s[i] == '1'):
            arr[i] = i
            last_indexof1 = i
        else:
            arr[i] = last_indexof1
    no_of_substr = 0
    for r in range(n - 1, -1, -1):
        l = r
        while (l >= 0 and (r - l + 1) <= 64 and int(s[l:r + 1], 2) < k):
            l -= 1
        if (r - l + 1 <= 64):
            no_of_substr += l + 1
        else:
            no_of_substr += arr[l + 1] + 1
    return no_of_substr


s = "11100"
k = 3
print(countSubstr(s, k))

# Expected output:
8
# perfect cube string | python3 program to find if str1ing is a perfect cube or not . ; finding ascii values of each character and finding its sum ; find the cube root of sum ; check if sum is a perfect cube ; 

from math import ceil


def isPerfectCubeString(str1):
    sum = 0
    for i in range(len(str1)):
        sum += ord(str1[i])
    cr = ceil((sum)**(1 / 3))
    return (cr * cr * cr == sum)


str1 = "ll"
if (isPerfectCubeString(str1)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# program to find the xor of ascii values of characters in a string | function to find the xor of ascii value of characters in str1ing ; store value of first character ; traverse str1ing to find the xor ; return the xor ; 
def XorAscii(str1, len1):
    ans = ord(str1[0])
    for i in range(1, len1):
        ans = (ans ^ (ord(str1[i])))
    return ans


str1 = "geeksforgeeks"
len1 = len(str1)
print(XorAscii(str1, len1))
str1 = "GfG"
len1 = len(str1)
print(XorAscii(str1, len1))

# Expected output:
123
102
# camelcase pattern matching | function that prints the camel case pattern matching ; map to store the hashing of each words with every uppercase letter found ; traverse the words array that contains all the string ; initialise str as empty ; length of string words [ i ] ; for every uppercase letter found map that uppercase to original words ; traverse the map for pattern matching ; if pattern matches then print the corresponding mapped words ; if word not found print " no ▁ match ▁ found " ; 
def CamelCase(words, pattern):
    map = dict .fromkeys(words, None)
    for i in range(len(words)):
        string = ""
        l = len(words[i])
        for j in range(l):
            if (words[i][j] >= 'A' and words[i][j] <= 'Z'):
                string += words[i][j]
                if string not in map:
                    map[string] = [words[i]]
                elif map[string] is None:
                    map[string] = [words[i]]
                else:
                    map[string].append(words[i])
    wordFound = False
    for key, value in map .items():
        if (key == pattern):
            wordFound = True
            for itt in value:
                print(itt)
    if (not wordFound):
        print("No match found")


if __name__ == "__main__":
    words = [
        "Hi",
        "Hello",
        "HelloWorld",
        "HiTech",
        "HiGeek",
        "HiTechWorld",
        "HiTechCity",
        "HiTechLab"]
    pattern = "HT"
    CamelCase(words, pattern)

# Expected output:
HiTech
HiTechWorld
HiTechCity
HiTechLab
# program to accept string starting with capital letter | function to check if first character is capital ; function to check ; 
def checkIfStartsWithCapital(string):
    if (string[0] >= 'A' and string[0] <= 'Z'):
        return 1
    else:
        return 0


def check(string):
    if (checkIfStartsWithCapital(string)):
        print("Accepted")
    else:
        print("Not Accepted")


if __name__ == "__main__":
    string = "GeeksforGeeks"
    check(string)
    string = "geeksforgeeks"
    check(string)

# Expected output:
Accepted
Not Accepted
# program to accept a strings which contains all the vowels | function to to check that a string contains all vowels ; hash array of size 5 such that the index 0 , 1 , 2 , 3 and 4 represent the vowels a , e , i , o and u ; loop the string to mark the vowels which are present ; loop to check if there is any vowel which is not present in the string ; function to to check that a string contains all vowels ; 
def checkIfAllVowels(string):
    hash = [0] * 5
    for i in range(len(string)):
        if (string[i] == 'A' or string[i] == 'a'):
            hash[0] = 1
        elif (string[i] == 'E' or string[i] == 'e'):
            hash[1] = 1
        elif (string[i] == 'I' or string[i] == 'i'):
            hash[2] = 1
        elif (string[i] == 'O' or string[i] == 'o'):
            hash[3] = 1
        elif (string[i] == 'U' or string[i] == 'u'):
            hash[4] = 1
    for i in range(5):
        if (hash[i] == 0):
            return 1
    return 0


def checkIfAllVowelsArePresent(string):
    if (checkIfAllVowels(string)):
        print("Not Accepted")
    else:
        print("Accepted")


if __name__ == "__main__":
    string = "aeioubc"
    checkIfAllVowelsArePresent(string)

# Expected output:
Accepted
# smallest odd number with even sum of digits from the given number n | function to find the smallest odd number whose sum of digits is even from the given string ; converting the given string to a list of digits ; an empty array to store the digits ; for loop to iterate through each digit ; if the given digit is odd then the digit is appended to the array b ; sorting the list of digits ; if the size of the list is greater than 1 then a 2 digit smallest odd number is returned since the sum of two odd digits is always even ; else , - 1 is returned ; 
def smallest(s):
    a = list(s)
    b = []
    for i in range(len(a)):
        if (int(a[i]) % 2 != 0):
            b .append(a[i])
    b = sorted(b)
    if (len(b) > 1):
        return int(b[0]) * 10 + int(b[1])
    return -1


if __name__ == "__main__":
    print(smallest("15470"))

# Expected output:
15
# number formed by deleting digits such that sum of the digits becomes even and the number odd | function to convert a number into odd number such that digit - sum is odd ; loop to find any first two odd number such that their sum is even and number is odd ; print the result ; 
def converthenumber(n):
    s = str(n)
    res = ""
    for i in range(len(s)):
        if (s[i] == '1' or s[i] == '3' or s[i] ==
                '5' or s[i] == '7' or s[i] == '9'):
            res += s[i]
        if (len(res) == 2):
            break
    if (len(res) == 2):
        print(res)
    else:
        print("-1")


if __name__ == "__main__":
    n = 18720
    converthenumber(n)

# Expected output:
17
# longest palindromic string formed using concatenation of given strings in any order | function to find the longest palindromic from given array of strings ; loop to find the pair of strings which are reverse of each other ; loop to find if any palindromic is still left in the array ; update the answer with all strings of pair1 ; update the answer with palindromic s1 ; update the answer with all strings of pair2 ; 
def longestPalindrome(a, n):
    pair1 = [0] * n
    pair2 = [0] * n
    r = 0
    for i in range(n):
        s = a[i]
        s = s[::-1]
        for j in range(i + 1, n):
            if (a[i] != "" and a[j] != ""):
                if (s == a[j]):
                    pair1[r] = a[i]
                    pair2[r] = a[j]
                    r += 1
                    a[i] = ""
                    a[j] = ""
                    break
    s1 = ""
    for i in range(n):
        s = a[i]
        a[i] = a[i][::-1]
        if (a[i] != ""):
            if (a[i] == s):
                s1 = a[i]
                break
    ans = ""
    for i in range(r):
        ans = ans + pair1[i]
    if (s1 != ""):
        ans = ans + s1
    for j in range(r - 1, -1, -1):
        ans = ans + pair2[j]
    print(ans)


a1 = ["aba", "aba"]
n1 = len(a1)
longestPalindrome(a1, n1)
a2 = ["abc", "dba", "kop", "abd", "cba"]
n2 = len(a2)
longestPalindrome(a2, n2)

# Expected output:
abaaba
abcdbaabdcba
# program to print the given digit in words | function to return the word of the corresponding digit ; for digit 0 ; for digit 1 ; for digit 2 ; for digit 3 ; for digit 4 ; for digit 5 ; for digit 6 ; for digit 7 ; for digit 8 ; for digit 9 ; function to iterate through every digit in the given number ; finding each digit of the number ; print the digit in words ; 
def printValue(digit):
    if digit == '0':
        print("Zero ", end=" ")
    elif digit == '1':
        print("One ", end=" ")
    elif digit == '2':
        print("Two ", end=" ")
    elif digit == '3':
        print("Three", end=" ")
    elif digit == '4':
        print("Four ", end=" ")
    elif digit == '5':
        print("Five ", end=" ")
    elif digit == '6':
        print("Six ", end=" ")
    elif digit == '7':
        print("Seven", end=" ")
    elif digit == '8':
        print("Eight", end=" ")
    elif digit == '9':
        print("Nine ", end=" ")


def printWord(N):
    i = 0
    length = len(N)
    while i < length:
        printValue(N[i])
        i += 1


N = "123"
printWord(N)

# Expected output:
One  Two  Three
# jaro and jaro | python3 implementation of above approach ; function to calculate the jaro similarity of two s ; if the s are equal ; length of two s ; maximum distance upto which matching is allowed ; count of matches ; hash for matches ; traverse through the first ; check if there is any matches ; if there is a match ; if there is no match ; number of transpositions ; count number of occurrences where two characters match but there is a third matched character in between the indices ; find the next matched character in second ; return the jaro similarity ; 

from math import floor, ceil


def jaro_distance(s1, s2):
    if (s1 == s2):
        return 1.0
    len1 = len(s1)
    len2 = len(s2)
    max_dist = floor(max(len1, len2) / 2) - 1
    match = 0
    hash_s1 = [0] * len(s1)
    hash_s2 = [0] * len(s2)
    for i in range(len1):
        for j in range(max(0, i - max_dist), min(len2, i + max_dist + 1)):
            if (s1[i] == s2[j] and hash_s2[j] == 0):
                hash_s1[i] = 1
                hash_s2[j] = 1
                match += 1
                break
    if (match == 0):
        return 0.0
    t = 0
    point = 0
    for i in range(len1):
        if (hash_s1[i]):
            while (hash_s2[point] == 0):
                point += 1
            if (s1[i] != s2[point]):
                point += 1
                t += 1
    t = t // 2
    return (match / len1 + match / len2 + (match - t + 1) / match) / 3.0


s1 = "CRATE"
s2 = "TRACE"
print(round(jaro_distance(s1, s2), 6))

# Expected output:
0.733333
# find the resultant string after replacing x with y and removing z | function to replace and remove ; two pointer start and end points to beginning and end position in the string ; if start is having z find x pos in end and replace z with another character ; find location for having different character instead of z ; if found swap character at start and end ; else increment start also checkin for x at start position ; 
def replaceRemove(s, X, Y, Z):
    s = list(s)
    start = 0
    end = len(s) - 1
    while (start <= end):
        if (s[start] == Z):
            while (end >= 0 and s[end] == Z):
                end -= 1
            if (end > start):
                s[start], s[end] = s[end], s[start]
                if (s[start] == X):
                    s[start] = Y
                start += 1
        else:
            if (s[start] == X):
                s[start] = Y
            start += 1
    while (len(s) > 0 and s[len(s) - 1] == Z):
        s .pop()
    return "".join(s)


if __name__ == "__main__":
    string = "batman"
    X = 'a'
    Y = 'd'
    Z = 'b'
    string = replaceRemove(string, X, Y, Z)
    if (len(string) == 0):
        print(-1)
    else:
        print(string)

# Expected output:
ndtmd
# check if all bits can be made same by flipping two consecutive bits | function to check if binary string can be made equal ; counting occurence of zero and one in binary string ; from above observation ; 
def canMake(s):
    o = 0
    z = 0
    for i in range(len(s)):
        if (ord(s[i]) - ord('0') == 1):
            o += 1
        else:
            z += 1
    if (o % 2 == 1 and z % 2 == 1):
        return "NO"
    else:
        return "YES"


if __name__ == "__main__":
    s = "01011"
    print(canMake(s))

# Expected output:
YES
# count of non | python3 implementation of the above approach ; function to find the count of the number of strings ; loop to iterate over the frequency of character of string ; reduce the frequency of current element ; recursive call ; backtrack ; function to count the number of non - empty sequences ; store the frequency of each character ; maintain the frequency ; 

count = 0


def countNumberOfStringsUtil(freq, Count):
    global count
    count = Count
    for i in range(26):
        if (freq[i] > 0):
            freq[i] -= 1
            count += 1
            countNumberOfStringsUtil(freq, count)
            freq[i] += 1


def countNumberOfStrings(s):
    global count
    global freq
    freq = [0 for i in range(26)]
    for i in range(len(s)):
        freq[ord(s[i]) - ord('A')] += 1
    countNumberOfStringsUtil(freq, count)
    return count


s = "AAABBC"
print(countNumberOfStrings(s))

# Expected output:
188
# minimum swaps required to make a binary string divisible by 2 ^ k | function to return the minimum swaps required ; to store the final answer ; to store the count of one and zero ; loop from end of the string ; if s [ i ] = 1 ; if s [ i ] = 0 ; if c_zero = k ; if the result can 't  be achieved ; return the final answer ; 
def findMinSwaps(s, k):
    ans = 0
    c_one = 0
    c_zero = 0
    for i in range(len(s) - 1, -1, -1):
        if (s[i] == '1'):
            c_one += 1
        if (s[i] == '0'):
            c_zero += 1
            ans += c_one
        if (c_zero == k):
            break
    if (c_zero < k):
        return -1
    return ans


if __name__ == "__main__":
    s = "100111"
    k = 2
    print(findMinSwaps(s, k))

# Expected output:
6
# number of sub | function to return the count of the required substrings ; to store the final answer ; loop to find the answer ; condition to update the answer ; 
def countSubStr(strr, lenn):
    ans = 0
    for i in range(lenn):
        if (strr[i] == '0'):
            ans += (i + 1)
    return ans


strr = "10010"
lenn = len(strr)
print(countSubStr(strr, lenn))

# Expected output:
10
# smallest non | function to return the length of the smallest subdivisible by 2 ^ k ; to store the final answer ; left pointer ; right pointer ; count of the number of zeros and ones in the current substring ; loop for two pointers ; condition satisfied ; updated the answer ; update the pointer and count ; update the pointer and count ; 
def findLength(s, k):
    ans = 10 ** 9
    l = 0
    r = 0
    cnt_zero = 0
    cnt_one = 0
    while (l < len(s) and r <= len(s)):
        if (cnt_zero >= k and cnt_one >= 1):
            ans = min(ans, r - l)
            l += 1
            if (s[l - 1] == '0'):
                cnt_zero -= 1
            else:
                cnt_one -= 1
        else:
            if (r == len(s)):
                break
            if (s[r] == '0'):
                cnt_zero += 1
            else:
                cnt_one += 1
            r += 1
    if (ans == 10 ** 9):
        return -1
    return ans


s = "100"
k = 2
print(findLength(s, k))

# Expected output:
3
# largest sub | function to return the largest substring divisible by 2 ; while the last character of the string is '1' , pop it ; if the original string had no '0 ; 
def findMaxPairs(a, b, n, k):

    a.sort()
    b.sort()

    flag = [False] * n
    result = 0
    for i in range(n):
        for j in range(n):
            if (abs(a[i] - b[j]) <= k and
                    flag[j] == False):
                result += 1
                flag[j] = True
                break
    return result


if __name__ == "__main__":
    a = [10, 15, 20]
    b = [17, 12, 24]
    n = len(a)
    k = 3
    print(findMaxPairs(a, b, n, k))

# Expected output:
2
# find whether x exists in y after jumbling x | python3 implementation of the approach ; function that returns true if both the arrays have equal values ; test the equality ; function that returns true if any permutation of x exists as a sub in y ; base case ; to store cumulative frequency ; finding the frequency of characters in x ; finding the frequency of characters in y upto the length of x ; equality check ; two pointer approach to generate the entire cumulative frequency ; remove the first character of the previous window ; add the last character of the current window ; equality check ; 

MAX = 26


def areEqual(a, b):
    for i in range(MAX):
        if (a[i] != b[i]):
            return False
    return True


def xExistsInY(x, y):
    if (len(x) > len(y)):
        return False
    cnt_x = [0] * MAX
    cnt = [0] * MAX
    for i in range(len(x)):
        cnt_x[ord(x[i]) - ord('a')] += 1
    for i in range(len(x)):
        cnt[ord(y[i]) - ord('a')] += 1
    if (areEqual(cnt_x, cnt)):
        return True
    for i in range(1, len(y) - len(x) + 1):
        cnt[ord(y[i - 1]) - ord('a')] -= 1
        cnt[ord(y[i + len(x) - 1]) - ord('a')] += 1
        if (areEqual(cnt, cnt_x)):
            return True
    return False


if __name__ == '__main__':
    x = "skege"
    y = "geeksforgeeks"
    if (xExistsInY(x, y)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# reverse substrings between each pair of parenthesis | function to return the modified string ; push the index of the current opening bracket ; reverse the substarting after the last encountered opening bracket till the current character ; to store the modified string ; 
def reverseParentheses(strr, lenn):
    st = []
    for i in range(lenn):
        if (strr[i] == '('):
            st .append(i)
        elif (strr[i] == ')'):
            temp = strr[st[-1]:i + 1]
            strr = strr[:st[-1]] + temp[::-1] + strr[i + 1:]
            del st[-1]
    res = ""
    for i in range(lenn):
        if (strr[i] != ')' and strr[i] != '('):
            res += (strr[i])
    return res


if __name__ == '__main__':
    strr = "(skeeg(for)skeeg)"
    lenn = len(strr)
    st = [i for i in strr]
    print(reverseParentheses(strr, lenn))

# Expected output:
geeksforgeeks
# count of times second string can be formed from the characters of first string | python3 implementation of the approach ; function to update the freq [ ] array to store the frequencies of all the characters of strr ; update the frequency of the characters ; function to return the maximum count of times patt can be formed using the characters of strr ; to store the frequencies of all the characters of strr ; to store the frequencies of all the characters of patt ; to store the result ; for every character ; if the current character doesn 't appear in patt ; update the result ; 

MAX = 26


def updateFreq(strr, freq):
    lenn = len(strr)
    for i in range(lenn):
        freq[ord(strr[i]) - ord('a')] += 1


def maxCount(strr, patt):
    strrFreq = [0 for i in range(MAX)]
    updateFreq(strr, strrFreq)
    pattFreq = [0 for i in range(MAX)]
    updateFreq(patt, pattFreq)
    ans = 10 ** 9
    for i in range(MAX):
        if (pattFreq[i] == 0):
            continue
        ans = min(ans, strrFreq[i] // pattFreq[i])
    return ans


strr = "geeksforgeeks"
patt = "geeks"
print(maxCount(strr, patt))

# Expected output:
2
# reduce the number to minimum multiple of 4 after removing the digits | python3 implementation of the approach ; function to return the minimum number that can be formed after removing the digits which is a multiple of 4 ; for every digit of the number ; check if the current digit is divisible by 4 ; if any subsequence of two digits is divisible by 4 ; 

import sys
TEN = 10


def minNum(str, len1):
    res = sys .maxsize
    for i in range(len1):
        if (str[i] == '4' or str[i] == '8'):
            res = min(res, ord(str[i]) - ord('0'))
    for i in range(len1 - 1):
        for j in range(i + 1, len1, 1):
            num = (ord(str[i]) - ord('0')) * TEN + (ord(str[j]) - ord('0'))
            if (num % 4 == 0):
                res = min(res, num)
    if (res == sys .maxsize):
        return -1
    else:
        return res


if __name__ == '__main__':
    str = "17"
    len1 = len(str)
    print(minNum(str, len1))

# Expected output:
-1
# check if two strings can be made equal by swapping one character among each other | function that returns true if the string can be made equal after one swap ; a and b are new a and b after we omit the same elements ; take only the characters which are different in both the strings for every pair of indices ; if the current characters differ ; the strings were already equal ; if the lengths of the strings are two ; if swapping these characters can make the strings equal ; 
def canBeEqual(a, b, n):
    A = []
    B = []
    for i in range(n):
        if a[i] != b[i]:
            A .append(a[i])
            B .append(b[i])
    if len(A) == len(B) == 0:
        return True
    if len(A) == len(B) == 2:
        if A[0] == A[1] and B[0] == B[1]:
            return True
    return False


A = 'SEEKSFORGEEKS'
B = 'GEEKSFORGEEKG'
if (canBeEqual(A, B, len(A))):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# find the mid | function to find the mid alphabets ; for every character pair ; get the average of the characters ; 
def findMidAlphabet(s1, s2, n):
    for i in range(n):
        mid = (ord(s1[i]) + ord(s2[i])) // 2
        print(chr(mid), end="")


s1 = "akzbqzgw"
s2 = "efhctcsz"
n = len(s1)
findMidAlphabet(s1, s2, n)

# Expected output:
chqbrnmx
# queries to find the count of vowels in the substrings of the given string | python 3 implementation of the approach ; function that returns true if ch is a vowel ; pre [ i ] will store the count of vowels in the substring str [ 0. . . i ] ; fill the pre [ ] array ; if current character is a vowel ; if its a consonant ; for every query ; find the count of vowels for the current query ; 

N = 2


def isVowel(ch):
    return (ch == 'a' or ch == 'e' or ch == 'i' or ch == 'o' or ch == 'u')


def performQueries(str1, len1, queries, q):
    pre = [0 for i in range(len1)]
    if (isVowel(str1[0])):
        pre[0] = 1
    else:
        pre[0] = 0
    for i in range(0, len1, 1):
        if (isVowel(str1[i])):
            pre[i] = 1 + pre[i - 1]
        else:
            pre[i] = pre[i - 1]
    for i in range(q):
        if (queries[i][0] == 0):
            print(pre[queries[i][1]])
        else:
            print(pre[queries[i][1]] - pre[queries[i][0] - 1])


if __name__ == '__main__':
    str1 = "geeksforgeeks"
    len1 = len(str1)
    queries = [[1, 3], [2, 4], [1, 9]]
    q = len(queries)
    performQueries(str1, len1, queries, q)

# Expected output:
2
1
4
# check whether the given decoded string is divisible by 6 | function to return the sum of the digits of n ; function that return true if the decoded string is divisible by 6 ; to store the sum of the digits ; for each character , get the sum of the digits ; if the sum of digits is not divisible by 3 ; get the last digit of the number formed ; if the last digit is not divisible by 2 ; 
def sumDigits(n):
    sum = 0
    while (n > 0):
        digit = n % 10
        sum += digit
        n //= 10
    return sum


def isDivBySix(string, n):
    sum = 0
    for i in range(n):
        sum += (ord(string[i]) - ord('a') + 1)
    if (sum % 3 != 0):
        return False
    lastDigit = (ord(string[n - 1]) - ord('a') + 1) % 10
    if (lastDigit % 2 != 0):
        return False
    return True


if __name__ == "__main__":
    string = "ab"
    n = len(string)
    if (isDivBySix(string, n)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes