giulio98/xlcost-single-prompt huggingface dataset:
# prefixes with more a than b | function to count prefixes ; calculating for string s ; count == 0 or when n == 1 ; when all characters are a or a - b == 0 ; checking for saturation of string after repetitive addition ; 
def prefix(k, n):
    a = 0
    b = 0
    count = 0
    i = 0
    Len = len(k)
    for i in range(Len):
        if (k[i] == "a"):
            a += 1
        if (k[i] == "b"):
            b += 1
        if (a > b):
            count += 1
    if (count == 0 or n == 1):
        print(count)
        return 0
    if (count == Len or a - b == 0):
        print(count * n)
        return 0
    n2 = n - 1
    count2 = 0
    while (n2 != 0):
        for i in range(Len):
            if (k[i] == "a"):
                a += 1
            if (k[i] == "b"):
                b += 1
            if (a > b):
                count2 += 1
        count += count2
        n2 -= 1
        if (count2 == 0):
            break
        if (count2 == Len):
            count += (n2 * count2)
            break
        count2 = 0
    return count


S = "aba"
N = 2
print(prefix(S, N))
S = "baa"
N = 3
print(prefix(S, N))

# Expected output:
5
6
# pairs whose concatenation contain all digits | python3 program to find number of pairs whose concatenation contains all digits from 0 to 9. ; function to return number of pairs whose concatenation contain all digits from 0 to 9 ; making the mask for each of the number . ; for each of the possible pair which can make or value equal to 1023 ; finding the count of pair from the given numbers . ; 

N = 20


def countPair(st, n):
    cnt = [0] * (1 << 10)
    for i in range(n):
        mask = 0
        for j in range(len(st[i])):
            mask |= (1 << (ord(st[i][j]) - ord('0')))
        cnt[mask] += 1
    ans = 0
    for m1 in range(1024):
        for m2 in range(1024):
            if ((m1 | m2) == 1023):
                if (m1 == m2):
                    ans += (cnt[m1] * (cnt[m1] - 1))
                else:
                    ans += (cnt[m1] * cnt[m2])
    return ans // 2


if __name__ == "__main__":
    n = 5
    st = ["129300455", "5559948277", "012334556", "56789", "123456879"]
    print(countPair(st, n))

# Expected output:
5
# count of ' gfg ' subsequences in the given string | python 3 program to find the " gfg " subsequence in the given string ; print the count of " gfg " subsequence in the string ; traversing the given string ; if the character is ' g ' , increment the count of ' g ' , increase the result and update the array . ; if the character is ' f ' , increment the count of ' f ' and update the array . ; ignore other character . ; 

MAX = 100


def countSubsequence(s, n):
    cntG = 0
    cntF = 0
    result = 0
    C = 0
    for i in range(n):
        if (s[i] == 'G'):
            cntG += 1
            result += C
            continue
        if (s[i] == 'F'):
            cntF += 1
            C += cntG
            continue
        else:
            continue
    print(result)


if __name__ == '__main__':
    s = "GFGFG"
    n = len(s)
    countSubsequence(s, n)

# Expected output:
4
# lexicographical maximum substring of string | python 3 program to find the lexicographically maximum substring . ; loop to find the max leicographic substring in the substring array ; 
def LexicographicalMaxString(str):
    mx = ""
    for i in range(len(str)):
        mx = max(mx, str[i:])
    return mx


if __name__ == '__main__':
    str = "ababaa"
    print(LexicographicalMaxString(str))

# Expected output:
babaa
# lexicographical maximum substring of string | python 3 program to find the lexicographically maximum substring . ; we store all the indexes of maximum characters we have in the string ; we form a substring from that maximum character index till end and check if its greater that maxstring ; 
def LexicographicalMaxString(st):
    maxchar = 'a'
    index = []
    for i in range(len(st)):
        if (st[i] >= maxchar):
            maxchar = st[i]
            index .append(i)
    maxstring = ""
    for i in range(len(index)):
        if (st[index[i]:len(st)] > maxstring):
            maxstring = st[index[i]:len(st)]
    return maxstring


if __name__ == "__main__":
    st = "acbacbc"
    print(LexicographicalMaxString(st))

# Expected output:
cbc
# print longest palindrome word in a sentence | function to check if a word is palindrome ; making the check case case insensitive ; loop to check palindrome ; function to find longest palindrome word ; to check last word for palindrome ; to store each word ; extracting each word ; 
def checkPalin(word):
    n = len(word)
    word = word .lower()
    for i in range(n):
        if (word[i] != word[n - 1]):
            return False
        n -= 1
    return True


def longestPalin(str):
    str = str + " "
    longestword = ""
    word = ""
    length1 = 0
    for i in range(len(str)):
        ch = str[i]
        if (ch != ' '):
            word = word + ch
        else:
            length = len(word)
            if (checkPalin(word) and length > length1):
                length1 = length
                longestword = word
            word = ""
    return longestword


if __name__ == "__main__":
    s = "My name is ava and i love Geeksforgeeks"
    if (longestPalin(s) == ""):
        print("No Palindrome Word")
    else:
        print(longestPalin(s))

# Expected output:
ava
# number of common base strings for two strings | function for finding common divisor . ; checking if ' base ' is base string of 's1 ; checking if ' base ' is base string of 's2 ; 
def isCommonBase(base, s1, s2):
    for j in range(len(s1)):
        if (base[j % len(base)] != s1[j]):
            return False
    for j in range(len(s2)):
        if (base[j % len(base)] != s2[j]):
            return False

    return True


def countCommonBases(s1, s2):
    n1 = len(s1)
    n2 = len(s2)
    count = 0
    for i in range(1, min(n1, n2) + 1):
        base = s1[0: i]
        if (isCommonBase(base, s1, s2)):
            count += 1

    return count


if __name__ == "__main__":

    s1 = "pqrspqrs"
    s2 = "pqrspqrspqrspqrs"
    print(countCommonBases(s1, s2))

# Expected output:
2
# removing elements between the two zeros | function to find the string after operation ; travesing through string ; checking for character between two zeros ; deleting the character at specific position ; updating the length of the string ; 
def findstring(s):
    n = len(s)
    s = list(s)
    i = 1
    while i < n - 1:
        if (s[i - 1] == '0' and s[i + 1] == '0'):
            s .pop(i)
            i -= 1
            if i > 0 and s[i - 1] == '0':
                i -= 1
            n = len(s)
        i += 1
    return ''.join(s)


if __name__ == '__main__':
    print(findstring('100100'))

# Expected output:
100
# perfect square string | python3 program to find if string is a perfect square or not . ; calculating the length of the string ; calculating the ascii value of the string ; find floating point value of square root of x . ; if square root is an integer ; 

import math


def isPerfectSquareString(str):
    sum = 0
    l = len(str)
    for i in range(l):
        sum = sum + ord(str[i])
    squareRoot = math .sqrt(sum)
    return ((squareRoot - math .floor(squareRoot)) == 0)


str = "d"
if (isPerfectSquareString(str)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# remove consecutive vowels from string | function which returns true or false for occurrence of a vowel ; this compares vowel with character 'c ; function to print resultant string ; print 1 st character ; loop to check for each character ; comparison of consecutive characters ; 
def is_vow(c):

    return ((c == 'a') or (c == 'e') or (c == 'i') or (c == 'o') or (c == 'u'))


def removeVowels(str):
    print(str[0], end="")
    for i in range(1, len(str)):
        if ((is_vow(str[i - 1]) != True) or (is_vow(str[i]) != True)):
            print(str[i], end="")


str = " geeks for geeks"
removeVowels(str)

# Expected output:
 geks for geks
# find the number which contain the digit d | python 3 program to print the number which contain the digit d from 0 to n ; function to display the values ; converting d to character ; loop to check each digit one by one . ; initialize the string ; checking for digit ; 
def index(st, ch):
    for i in range(len(st)):
        if (st[i] == ch):
            return i
    return -1


def printNumbers(n, d):
    st = "" + str(d)
    ch = st[0]
    for i in range(0, n + 1, 1):
        st = ""
        st = st + str(i)
        if (i == d or index(st, ch) >= 0):
            print(i, end=" ")


if __name__ == '__main__':
    n = 100
    d = 5
    printNumbers(n, d)

# Expected output:
5 15 25 35 45 50 51 52 53 54 55 56 57 58 59 65 75 85 95
# transform a string such that it has abcd . . z as a subsequence | function to transform string with string passed as reference ; initializing the variable ch to 'a ; if the length of string is less than 26 , we can 't obtain the required  subsequence ; if ch has reached ' z ' , it means we have transformed our string such that required subsequence can be obtained ; current character is not greater than ch , then replace it with ch and increment ch ; 
def transformString(s):

    ch = 'a'
    if (len(s) < 26):
        return False
    for i in range(0, len(s)):
        if (ord(ch) > ord('z')):
            break
        if (s[i] <= ch):
            s[i] = ch
            ch = chr(ord(ch) + 1)
    if (ch <= 'z'):
        print("Not Possible")
    print("".join(s))


s = list("aaaaaaaaaaaaaaaaaaaaaaaaaa")
transformString(s)

# Expected output:
abcdefghijklmnopqrstuvwxyz
# number of pairs with pandigital concatenation | checks if a given is pandigital ; digit i is not present thus not pandigital ; returns number of pairs of strings resulting in pandigital concatenations ; iterate over all pair of strings ; 
def isPanDigital(s):
    digits = [False] * 10
    for i in range(0, len(s)):
        digits[int(s[i]) - int('0')] = True
    for i in range(0, 10):
        if (digits[i] == False):
            return False
    return True


def countPandigitalPairs(v):
    pairs = 0
    for i in range(0, len(v)):
        for j in range(i + 1, len(v)):
            if (isPanDigital(v[i] + v[j])):
                pairs = pairs + 1
    return pairs


v = ["123567", "098234", "14765", "19804"]
print(countPandigitalPairs(v))

# Expected output:
3
# minimum changes to a string to make all substrings distinct | python3 program to count number of changes to make all substrings distinct . ; returns minimum changes to str so that no substring is repeated . ; if length is more than maximum allowed characters , we cannot get the required string . ; variable to store count of distinct characters ; to store counts of different characters ; answer is , n - number of distinct char ; 

MAX_CHAR = [26]


def minChanges(str):
    n = len(str)
    if (n > MAX_CHAR[0]):
        return -1
    dist_count = 0
    count = [0] * MAX_CHAR[0]
    for i in range(n):
        if (count[ord(str[i]) - ord('a')] == 0):
            dist_count += 1
        count[(ord(str[i]) - ord('a'))] += 1
    return (n - dist_count)


if __name__ == '__main__':
    str = "aebaecedabbee"
    print(minChanges(str))

# Expected output:
8
# number of strings of length n with no palindromic sub string | return the count of strings with no palindromic substring . ; 
def numofstring(n, m):
    if n == 1:
        return m
    if n == 2:
        return m * (m - 1)
    return m * (m - 1) * pow(m - 2, n - 2)


n = 2
m = 3
print(numofstring(n, m))

# Expected output:
6
# count special palindromes in a string | function to count special palindromic susbstring ; store count of special palindromic substring ; it will store the count of continues same char ; traverse string character from left to right ; store same character count ; count smiler character ; case : 1 so total number of substring that we can generate are : k * ( k + 1 ) / 2 here k is samecharcount ; store current same char count in samechar [ ] array ; increment i ; case 2 : count all odd length special palindromic substring ; if current character is equal to previous one then we assign previous same character count to current one ; case 2 : odd length ; subtract all single length substring ; 
def CountSpecialPalindrome(str):
    n = len(str)
    result = 0
    sameChar = [0] * n
    i = 0
    while (i < n):
        sameCharCount = 1
        j = i + 1
        while (j < n):
            if (str[i] != str[j]):
                break
            sameCharCount += 1
            j += 1
        result += int(sameCharCount * (sameCharCount + 1) / 2)
        sameChar[i] = sameCharCount
        i = j
    for j in range(1, n):
        if (str[j] == str[j - 1]):
            sameChar[j] = sameChar[j - 1]
        if (j > 0 and j < (n - 1)
                and (str[j - 1] == str[j + 1] and str[j] != str[j - 1])):
            result += (sameChar[j - 1]if (sameChar[j - 1] <
                       sameChar[j + 1])else sameChar[j + 1])
    return result - n


str = "abccba"
print(CountSpecialPalindrome(str))

# Expected output:
1
# find if a string starts and ends with another given string | python program to find if a given corner string is present at corners . ; if length of corner string is more , it cannot be present at corners . ; return true if corner string is present at both corners of given string . ; 
def isCornerPresent(str, corner):
    n = len(str)
    cl = len(corner)
    if (n < cl):
        return False
    return ((str[:cl] == corner) and (str[n - cl:] == corner))


str = "geeksforgeeks"
corner = "geeks"
if (isCornerPresent(str, corner)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# find i 'th index character in a binary string obtained after n iterations | function to store binary representation ; function to find ith character ; function to change decimal to binary ; assign s1 string in s string ; 
def binary_conversion(s, m):
    while (m):
        temp = m % 2
        s += str(temp)
        m = m // 2
    return s[::-1]


def find_character(n, m, i):
    s = ""
    s = binary_conversion(s, m)
    s1 = ""
    for x in range(n):
        for j in range(len(s)):
            if s[j] == "1":
                s1 += "10"
            else:
                s1 += "01"
        s = s1
        s1 = ""
    e = ord(s[i])
    r = ord('0')
    return e - r


m, n, i = 5, 2, 8
print(find_character(n, m, i))

# Expected output:
1
# maximum segment value after putting k breakpoints in a number | function to find maximum number ; maximum segment length ; find value of first segment of seg_len ; find value of remaining segments using sliding window ; to find value of current segment , first remove leading digit from previous value ; then add trailing digit ; 
def findMaxSegment(s, k):
    seg_len = len(s) - k
    res = 0
    for i in range(seg_len):
        res = res * 10 + (ord(s[i]) - ord('0'))
    seg_len_pow = pow(10, seg_len - 1)
    curr_val = res
    for i in range(1, len(s) - seg_len):
        curr_val = curr_val - (ord(s[i - 1]) - ord('0')) * seg_len_pow
        curr_val = (curr_val * 10 + (ord(s[i + seg_len - 1]) - ord('0')))
        res = max(res, curr_val)
    return res


if __name__ == '__main__':
    s = "8754"
    k = 2
    print("Maximum number = ", findMaxSegment(s, k))

# Expected output:
Maximum number =  87
# converting one string to other using append and delete last operations | returns true if it is possible to convert str1 to str2 using k operations . ; case a ( i ) ; finding common length of both string ; case a ( ii ) - ; case b - ; 
def isConvertible(str1, str2, k):
    if ((len(str1) + len(str2)) < k):
        return True
    commonLength = 0
    for i in range(0, min(len(str1), len(str2)), 1):
        if (str1[i] == str2[i]):
            commonLength += 1
        else:
            break
    if ((k - len(str1) - len(str2) + 2 * commonLength) % 2 == 0):
        return True
    return False


if __name__ == '__main__':
    str1 = "geek"
    str2 = "geek"
    k = 7
    if (isConvertible(str1, str2, k)):
        print("Yes")
    else:
        print("No")
    str1 = "geeks"
    str2 = "geek"
    k = 5
    if (isConvertible(str1, str2, k)):
        print("Yes")
    else:
        print("No")

# Expected output:
No
Yes
# maximum distinct lowercase alphabets between two uppercase | python3 program to find maximum lowercase alphabets present between two uppercase alphabets ; function which computes the maximum number of distinct lowercase alphabets between two uppercase alphabets ; ignoring lowercase characters in the beginning . ; we start from next of first capital letter and traverse through remaining character . ; if character is in uppercase , ; count all distinct lower case characters ; update maximum count ; reset count array ; if character is in lowercase ; 

MAX_CHAR = 26


def maxLower(str):
    n = len(str)
    i = 0
    for i in range(n):
        if str[i] >= 'A' and str[i] <= 'Z':
            i += 1
            break
    maxCount = 0
    count = []
    for j in range(MAX_CHAR):
        count .append(0)
    for j in range(i, n):
        if str[j] >= 'A' and str[j] <= 'Z':
            currCount = 0
            for k in range(MAX_CHAR):
                if count[k] > 0:
                    currCount += 1
            maxCount = max(maxCount, currCount)
            for y in count:
                y = 0
        if str[j] >= 'a' and str[j] <= 'z':
            count[ord(str[j]) - ord('a')] += 1
    return maxCount


str = "zACaAbbaazzC"
print(maxLower(str))

# Expected output:
3
# maximum distinct lowercase alphabets between two uppercase | function which computes the maximum number of distinct lowercase alphabets between two uppercase alphabets ; ignoring lowercase characters in the beginning . ; we start from next of first capital letter and traverse through remaining character . ; if character is in uppercase , ; update maximum count if lowercase character before this is more . ; clear the set ; if character is in lowercase ; 
def maxLower(str):
    n = len(str)
    i = 0
    for i in range(n):
        if (str[i] >= 'A' and str[i] <= 'Z'):
            i += 1
            break
    maxCount = 3
    s = set()
    for i in range(n):
        if (str[i] >= 'A' and str[i] <= 'Z'):
            maxCount = max(maxCount, len(s))
            s .clear()
        if (str[i] >= 'a' and str[i] <= 'z'):
            s .add(str[i])
            return maxCount


if __name__ == '__main__':
    str = "zACaAbbaazzC"
    print(maxLower(str))

# Expected output:
3
# first uppercase letter in a string ( iterative and recursive ) | function to find string which has first character of each word . ; 
def first(str):
    for i in range(0, len(str)):
        if (str[i].istitle()):
            return str[i]
    return 0


str = "geeksforGeeKS"
res = first(str)
if (res == 0):
    print("No uppercase letter")
else:
    print(res)

# Expected output:
G
# count maximum | python3 implementation for counting maximum length palindromes ; factorial of a number ; function to count maximum length palindromes . ; count number of occurrence of a charterer in the string ; k = 0 count of singles num = 0 numerator of result den = 1 denominator of result ; if frequency is even fi = ci / 2 ; if frequency is odd fi = ci - 1 / 2. ; sum of all frequencies ; product of factorial of every frequency ; if all character are unique so there will be no pallindrome , so if num != 0 then only we are finding the factorial ; k are the single elements that can be placed in middle ; 

import math as mt


def fact(n):
    ans = 1
    for i in range(1, n + 1):
        ans = ans * i
    return (ans)


def numberOfPossiblePallindrome(string, n):
    mp = dict()
    for i in range(n):
        if string[i] in mp .keys():
            mp[string[i]] += 1
        else:
            mp[string[i]] = 1

    k = 0
    num = 0
    den = 1
    fi = 0
    for it in mp:
        if (mp[it] % 2 == 0):
            fi = mp[it] // 2
        else:
            fi = (mp[it] - 1) // 2
            k += 1
        num = num + fi
        den = den * fact(fi)
    if (num != 0):
        num = fact(num)
    ans = num // den
    if (k != 0):
        ans = ans * k
    return (ans)


string = "ababab"
n = len(string)
print(numberOfPossiblePallindrome(string, n))

# Expected output:
4
# counting even decimal value substrings in a binary string | python3 program to count all even decimal value substring ; generate all substring in arr [ 0. . n - 1 ] ; store the count ; pick starting point ; pick ending point ; substring between current starting and ending points ; increment power of 2 by one ; 

import math


def evenDecimalValue(str, n):
    result = 0
    for i in range(0, n):
        for j in range(i, n):
            decimalValue = 0
            powerOf2 = 1
            for k in range(i, j + 1):
                decimalValue += ((int(str[k]) - 0) * powerOf2)
                powerOf2 *= 2
            if (decimalValue % 2 == 0):
                result += 1
    return result


str = "10010"
n = 5
print(evenDecimalValue(str, n))

# Expected output:
8
# create a new string by alternately combining the characters of two halves of the string in reverse | function performing calculations ; calculating the two halves of string s as first and second . the final string p ; it joins the characters to final string in reverse order ; it joins the characters to final string in reverse order ; 
def solve(s):
    l = len(s)
    x = l // 2
    y = l
    p = ""
    while (x > 0 and y > l / 2):
        p = p + s[x - 1]
        x = x - 1
        p = p + s[y - 1]
        y = y - 1
    if (y > l // 2):
        p = p + s[y - 1]
        y = y - 1
    print(p)


s = "sunshine"
solve(s)

# Expected output:
sennuish
# make a lexicographically smallest palindrome with minimal changes | function to create a palindrome ; count the occurrences of every character in the string ; create a string of characters with odd occurrences ; change the created string upto middle element and update count to make sure that only one odd character exists . ; decrease the count of character updated ; create three strings to make first half second half and middle one . ; characters with even occurrences ; fill the first half . ; character with odd occurrence ; fill the first half with half of occurrence except one ; for middle element ; create the second half by reversing the first half . ; 
def palindrome(s: str, n: int) -> int:
    cnt = dict()
    R = []
    for i in range(n):
        a = s[i]
        if a in cnt:
            cnt[a] += 1
        else:
            cnt[a] = 1
    i = 'a'
    while i <= 'z':
        if i in cnt and cnt[i] % 2 != 0:
            R += i
        i = chr(ord(i) + 1)
    l = len(R)
    j = 0
    for i in range(l - 1, (l // 2) - 1, -1):
        if R[i] in cnt:
            cnt[R[i]] -= 1
        else:
            cnt[R[i]] = -1
        R[i] = R[j]
        if R[j] in cnt:
            cnt[R[j]] += 1
        else:
            cnt[R[j]] = 1
        j += 1
    first, middle, second = "", "", ""
    i = 'a'
    while i <= 'z':
        if i in cnt:
            if cnt[i] % 2 == 0:
                j = 0
                while j < cnt[i] // 2:
                    first += i
                    j += 1
            else:
                j = 0
                while j < (cnt[i] - 1) // 2:
                    first += i
                    j += 1
                middle += i
        i = chr(ord(i) + 1)
    second = first
    second = ''.join(reversed(second))
    resultant = first + middle + second
    print(resultant)


if __name__ == "__main__":
    S = "geeksforgeeks"
    n = len(S)
    palindrome(S, n)

# Expected output:
eefgksoskgfee
# program for length of a string using recursion | python program to calculate length of a string using recursion ; function to calculate length ; if we reach at the end of the string ; 

str = "GeeksforGeeks"


def string_length(str):
    if str == '':
        return 0
    else:
        return 1 + string_length(str[1:])


print(string_length(str))

# Expected output:
13