giulio98/xlcost-single-prompt huggingface dataset:
# number of ways to reach nth floor by taking at | python3 program to reach n - th stair by taking a maximum of k leap ; elements of combo [ ] stores the no of possible ways to reach it by all combinations of k leaps or less ; assuming leap 0 exist and assigning its value to 1 for calculation ; loop to iterate over all possible leaps upto k ; ; in this loop we count all possible leaps to reach the jth stair with the help of ith leap or less ; if the leap is not more than the i - j ; calculate the value and store in combo [ j ] to reuse it for next leap calculation for the jth stair ; returns the no of possible number of leaps to reach the top of building of n stairs ; 
def solve(N, K):
    combo = [0] * (N + 1)
    combo[0] = 1
    for i in range(1, K + 1):
        for j in range(0, N + 1):
            if j >= i:
                combo[j] += combo[j - i]
    return combo[N]


if __name__ == "__main__":
    N, K = 29, 5
    print(solve(N, K))

# Expected output:
603
# find the longest increasing subsequence in circular manner | utility method to find lis using dynamic programming ; initialize lis values for all indexes ; compute optimized lis values in bottom up manner ; set j on the basis of current window i . e . first element of the current window ; pick maximum of all lis values ; function to find longest increasing subsequence in circular manner ; make a copy of given array by appending same array elements to itself ; perform lis for each window of size n ; 
def computeLIS(circBuff, start, end, n):
    LIS = [0 for i in range(end)]
    for i in range(start, end):
        LIS[i] = 1
    for i in range(start + 1, end):
        for j in range(start, i):
            if (circBuff[i] > circBuff[j] and LIS[i] < LIS[j] + 1):
                LIS[i] = LIS[j] + 1
    res = -100000
    for i in range(start, end):
        res = max(res, LIS[i])
    return res


def LICS(arr, n):
    circBuff = [0 for i in range(2 * n)]
    for i in range(n):
        circBuff[i] = arr[i]
    for i in range(n, 2 * n):
        circBuff[i] = arr[i - n]
    res = -100000
    for i in range(n):
        res = max(computeLIS(circBuff, i, i + n, n), res)
    return res


arr = [1, 4, 6, 2, 3]
n = len(arr)
print("Length of LICS is", LICS(arr, n))

# Expected output:
Length of LICS is 4
# counts paths from a point to reach origin | function to find binomial coefficient ; constructing pascal 's triangle ; 
def binomialCoeff(n, k):
    C = [0] * (k + 1)
    C[0] = 1
    for i in range(1, n + 1):
        j = min(i, k)
        while (j > 0):
            C[j] = C[j] + C[j - 1]
            j -= 1
    return C[k]


n = 3
m = 2
print("Number of Paths:", binomialCoeff(n + m, n))

# Expected output:
Number of Paths: 10
# longest common increasing subsequence ( lcs + lis ) | returns the length and the lcis of two arrays arr1 [ 0. . n - 1 ] and arr2 [ 0. . m - 1 ] ; table [ j ] is going to store length of lcis ending with arr2 [ j ] . we initialize it as 0 , ; traverse all elements of arr1 [ ] ; initialize current length of lcis ; for each element of arr1 [ ] , traverse all elements of arr2 [ ] . ; if both the array have same elements . note that we don 't break the loop here. ; now seek for previous smaller common element for current element of arr1 ; the maximum value in table [ ] is out result ; 
def LCIS(arr1, n, arr2, m):
    table = [0] * m
    for j in range(m):
        table[j] = 0
    for i in range(n):
        current = 0
        for j in range(m):
            if (arr1[i] == arr2[j]):
                if (current + 1 > table[j]):
                    table[j] = current + 1
            if (arr1[i] > arr2[j]):
                if (table[j] > current):
                    current = table[j]
    result = 0
    for i in range(m):
        if (table[i] > result):
            result = table[i]
    return result


if __name__ == "__main__":
    arr1 = [3, 4, 9, 1]
    arr2 = [5, 3, 8, 9, 10, 2, 1]
    n = len(arr1)
    m = len(arr2)
    print("Length of LCIS is", LCIS(arr1, n, arr2, m))

# Expected output:
Length of LCIS is 2
# length of longest common prefix possible by rearranging strings in a given array | python3 program to implement the above approach ; function to get the length of the longest common prefix by rearranging the strings ; freq [ i ] [ j ] : stores the frequency of a character ( = j ) in a arr [ i ] ; traverse the given array ; stores length of current string ; traverse current string of the given array ; update the value of freq [ i ] [ arr [ i ] [ j ] ] ; stores the length of longest common prefix ; count the minimum frequency of each character in in all the strings of arr [ ] ; stores minimum value in each row of freq [ ] [ ] ; calculate minimum frequency of current character in all the strings . ; update minrowval ; update maxlen ; 

import sys


def longComPre(arr, N):
    freq = [[0 for i in range(256)]for i in range(N)]
    for i in range(N):
        M = len(arr[i])
        for j in range(M):
            freq[i][ord(arr[i][j])] += 1
    maxLen = 0
    for j in range(256):
        minRowVal = sys .maxsize
        for i in range(N):
            minRowVal = min(minRowVal, freq[i][j])
        maxLen += minRowVal
    return maxLen


if __name__ == '__main__':
    arr = ["aabdc", "abcd", "aacd"]
    N = 3
    print(longComPre(arr, N))

# Expected output:
3
# remove characters from a string that appears exactly k times | python 3 program to remove characters from a string that appears exactly k times ; function to reduce the string by removing the characters which appears exactly k times ; hash table initialised to 0 ; increment the frequency of the character ; to store answer ; next index in reduced string ; append the characters which appears exactly k times ; 

MAX_CHAR = 26


def removeChars(arr, k):
    hash = [0] * MAX_CHAR
    n = len(arr)
    for i in range(n):
        hash[ord(arr[i]) - ord('a')] += 1
    ans = ""
    index = 0
    for i in range(n):
        if (hash[ord(arr[i]) - ord('a')] != k):
            ans += arr[i]
    return ans


if __name__ == "__main__":
    str = "geeksforgeeks"
    k = 2
    print(removeChars(str, k))

# Expected output:
eeforee
# contiguous subsegments of a string having distinct subsequent characters | function that prints the segments ; new array for every iteration ; check if the character is in the array ; 
def sub_segments(string, n):
    l = len(string)
    for x in range(0, l, n):
        newlist = string[x:x + n]
        arr = []
        for y in newlist:
            if y not in arr:
                arr .append(y)
        print(''.join(arr))


string = "geeksforgeeksgfg"
n = 4
sub_segments(string, n)

# Expected output:
gek
sfor
gek
sgf
# program to find the encrypted word | function to find the encrypted string ; to store the encrypted string ; after ' z ' , it should go to a . ; 
def findWord(c, n):
    co = 0
    s = [0] * n
    for i in range(n):
        if (i < n / 2):
            co += 1
        else:
            co = n - i
        if (ord(c[i]) + co <= 122):
            s[i] = chr(ord(c[i]) + co)
        else:
            s[i] = chr(ord(c[i]) + co - 26)
    print(*s, sep="")


s = "abcd"
findWord(s, len(s))

# Expected output:
bdee
# check if two strings are same ignoring their cases | function to compare two strings ignoring their cases ; length of first string ; length of second string ; if length is not same simply return false since both string can not be same if length is not equal ; loop to match one by one all characters of both string ; if current characters of both string are same , increase value of i to compare next character ; if any character of first string is some special character or numeric character and not same as corresponding character of second string then return false ; do the same for second string ; this block of code will be executed if characters of both strings are of different cases ; compare characters by ascii value ; if characters matched , increase the value of i to compare next char ; if all characters of the first string are matched with corresponding characters of the second string , then return true ; function to print the same or not same if strings are equal or not equal ; 
def equalIgnoreCase(str1, str2):
    i = 0
    len1 = len(str1)
    len2 = len(str2)
    if (len1 != len2):
        return False
    while (i < len1):
        if (str1[i] == str2[i]):
            i += 1
        elif (((str1[i] >= 'a' and str1[i] <= 'z') or (str1[i] >= 'A' and str1[i] <= 'Z')) == False):
            return False
        elif (((str2[i] >= 'a' and str2[i] <= 'z') or (str2[i] >= 'A' and str2[i] <= 'Z')) == False):
            return False
        else:
            if (str1[i] >= 'a' and str1[i] <= 'z'):
                if (ord(str1[i]) - 32 != ord(str2[i])):
                    return False
            elif (str1[i] >= 'A' and str1[i] <= 'Z'):
                if (ord(str1[i]) + 32 != ord(str2[i])):
                    return False
            i += 1
    return True


def equalIgnoreCaseUtil(str1, str2):
    res = equalIgnoreCase(str1, str2)
    if (res):
        print("Same")
    else:
        print("Not Same")


if __name__ == '__main__':
    str1 = "Geeks"
    str2 = "geeks"
    equalIgnoreCaseUtil(str1, str2)
    str1 = "Geek"
    str2 = "geeksforgeeks"
    equalIgnoreCaseUtil(str1, str2)

# Expected output:
Same
Not Same
# maximize the value of a by replacing some of its digits with digits of b | function to return the maximized value of a ; sort digits in ascending order ; j points to largest digit in b ; if all the digits of b have been used ; current digit has been used ; return the maximized value ; 
def maxValue(a, b):
    b = sorted(b)
    bi = [i for i in b]
    ai = [i for i in a]
    n = len(a)
    m = len(b)
    j = m - 1
    for i in range(n):
        if (j < 0):
            break
        if (bi[j] > ai[i]):
            ai[i] = bi[j]
            j -= 1
    x = "".join(ai)
    return x


a = "1234"
b = "4321"
print(maxValue(a, b))

# Expected output:
4334
# count numbers in range such that digits in it and it 's product with q are unequal | function to check if all of the digits in a number and it 's product with q are unequal or not ; convert first number into string ; insert elements from 1 st number to hash ; calculate corresponding product ; convert the product to string ; using the hash check if any digit of product matches with the digits of input number ; if yes , return false ; return true ; function to count numbers in the range [ l , r ] such that all of the digits of the number and it 's product with q are unequal ; check for every number between l and r ; 
def checkIfUnequal(n, q):
    s1 = str(n)
    a = [0 for i in range(26)]
    for i in range(0, len(s1), 1):
        a[ord(s1[i]) - ord('0')] += 1
    prod = n * q
    s2 = str(prod)
    for i in range(0, len(s2), 1):
        if (a[ord(s2[i]) - ord('0')]):
            return False
    return True


def countInRange(l, r, q):
    count = 0
    for i in range(l, r + 1, 1):
        if (checkIfUnequal(i, q)):
            count += 1
    return count


if __name__ == '__main__':
    l = 10
    r = 12
    q = 2
    print(countInRange(l, r, q))

# Expected output:
1
# check if it is possible to rearrange a binary string with alternate 0 s and 1 s | function to check the binary string ; length of string ; count zero 's ; count one 's ; if length is even ; if length is odd ; 
def is_possible(s):
    l = len(s)
    one = 0
    zero = 0
    for i in range(0, l):
        if (s[i] == '0'):
            zero += 1
        else:
            one += 1
    if (l % 2 == 0):
        return (one == zero)
    else:
        return (abs(one - zero) == 1)


if __name__ == "__main__":
    s = "100110"
    if (is_possible(s)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# Frequency Measuring Techniques for Competitive Programming | Python3 program to count frequencies of array items ; Create an array to store counts . The size of array is limit + 1 and all values are initially 0 ; Traverse through string characters and count frequencies ;

limit = 255


def countFreq(Str):
    count = [0] * (limit + 1)
    for i in range(len(Str)):
        count[ord(Str[i])] += 1
    for i in range(limit + 1):
        if (count[i] > 0):
            print(chr(i), count[i])


Str = "GeeksforGeeks"
countFreq(Str)

# Expected output:
G 2
e 4
f 1
k 2
o 1
r 1
s 2
# google case of a given sentence | python program to convert given sentence to camel case . ; function to remove spaces and convert into camel case ; check for spaces in the sentence ; conversion into upper case ; if not space , copy character ; return string to main ; 

import math


def convert(s):
    n = len(s)
    s1 = ""
    s1 = s1 + s[0].lower()
    i = 1
    while i < n:
        if (s[i] == ' ' and i <= n):
            s1 = s1 + " " + (s[i + 1]).lower()
            i = i + 1
        else:
            s1 = s1 + (s[i]).upper()
        i = i + 1
    return s1


str = "I get intern at geeksforgeeks"
print(convert(str))

# Expected output:
i gET iNTERN aT gEEKSFORGEEKS
# program to find the n | python3 program to find n - th number containing only 3 and 5. ; if n is odd , append 3 and move to parent ; if n is even , append 5 and move to parent ; reverse res and return . ; 
def reverse(s):
    if len(s) == 0:
        return s
    else:
        return reverse(s[1:]) + s[0]


def findNthNo(n):
    res = ""
    while (n >= 1):
        if (n & 1):
            res = res + "3"
            n = (int)((n - 1) / 2)
        else:
            res = res + "5"
            n = (int)((n - 2) / 2)
    return reverse(res)


n = 5
print(findNthNo(n))

# Expected output:
53
# nth non | python3 program to find n - th non - square number . ; function to find the nth non - square number ; conversion from int to long double is necessary in order to preserve decimal places after square root . ; calculating the result ; 

import math


def findNthNonSquare(n):
    x = n
    ans = x + math .floor(0.5 + math .sqrt(x))
    return int(ans)


n = 16
print("The", n, "th Non-Square number is", findNthNonSquare(n))

# Expected output:
The 16 th Non-Square number is 20
# sum of series with alternate signed squares of ap | function to calculate series sum ; 
def seiresSum(n, a):
    return (n * (a[0] * a[0] - a[2 * n - 1] * a[2 * n - 1]) / (2 * n - 1))


n = 2
a = [1, 2, 3, 4]
print(int(seiresSum(n, a)))

# Expected output:
-10
# find nth number that contains the digit k or divisible by k . | function for checking if digit k is in n or not ; finding remainder ; if digit found ; function for finding nth number ; since k is the first which satisfy th criteria , so consider it in count making count = 1 and starting from i = k + 1 ; checking that the number contain k digit or divisible by k ; 
def checkdigit(n, k):
    while (n):
        rem = n % 10
        if (rem == k):
            return 1
        n = n / 10
    return 0


def findNthNumber(n, k):
    i = k + 1
    count = 1
    while (count < n):
        if (checkdigit(i, k) or (i % k == 0)):
            count += 1
        if (count == n):
            return i
        i += 1
    return -1


n = 10
k = 2
print(findNthNumber(n, k))

# Expected output:
20
# count of subarrays of size k which is a permutation of numbers from 1 to k | python3 program to implement the above approach ; save index of numbers of the array ; update min and max index with the current index and check if it 's a  valid permutation ; 
def find_permutations(arr):
    cnt = 0
    max_ind = -1
    min_ind = 10000000
    n = len(arr)
    index_of = {}
    for i in range(n):
        index_of[arr[i]] = i + 1
    for i in range(1, n + 1):
        max_ind = max(max_ind, index_of[i])
        min_ind = min(min_ind, index_of[i])
        if (max_ind - min_ind + 1 == i):
            cnt += 1
    return cnt


if __name__ == "__main__":
    nums = []
    nums .append(2)
    nums .append(3)
    nums .append(1)
    nums .append(5)
    nums .append(4)
    print(find_permutations(nums))

# Expected output:
3
# count of integers that divide all the elements of the given array | function to return the count of the required integers ; to store the gcd of the array elements ; to store the count of factors of the found gcd ; if g is a perfect square ; factors appear in pairs ; 

from math import gcd as __gcd


def getCount(a, n):
    gcd = 0
    for i in range(n):
        gcd = __gcd(gcd, a[i])
    cnt = 0
    for i in range(1, gcd + 1):
        if i * i > gcd:
            break
        if (gcd % i == 0):
            if (i * i == gcd):
                cnt += 1
            else:
                cnt += 2
    return cnt


a = [4, 16, 1024, 48]
n = len(a)
print(getCount(a, n))

# Expected output:
3
# minimize cost of removals required to make all remaining characters of the string unique | function to find the minimum cost of removing characters to make the string unique ; stores the visited characters ; stores the answer ; traverse the string ; if already visited ; stores the maximum cost of removing a particular character ; store the total deletion cost of a particular character ; mark the current character visited ; traverse the indices of the string [ i , n - 1 ] ; if any duplicate is found ; update the maximum cost and total cost ; mark the current character visited ; keep the character with maximum cost and delete the rest ; return the minimum cost ; 
def delCost(s, cost):
    visited = [False] * len(s)
    ans = 0
    for i in range(len(s)):
        if visited[i]:
            continue
        maxDel = 0
        totCost = 0
        visited[i] = True
        for j in range(i, len(s)):
            if s[i] == s[j]:
                maxDel = max(maxDel, cost[j])
                totCost += cost[j]
                visited[j] = True
        ans += totCost - maxDel
    return ans


string = "AAABBB"
cost = [1, 2, 3, 4, 5, 6]
string = "AAABBB"
print(delCost(string, cost))

# Expected output:
12
# make all array elements equal by replacing triplets with their bitwise xor | function to find triplets such that replacing them with their xor make all array elements equal ; if n is even ; calculate xor of array elements ; traverse the array ; update xor ; if xor is not equal to 0 ; selecting the triplets such that elements of the pairs ( arr [ 0 ] , arr [ 1 ] ) , ( arr [ 2 ] , arr [ 3 ] ) . . . can be made equal ; selecting the triplets such that all array elements can be made equal to arr [ n - 1 ] ; selecting the triplets such that elements of the pairs ( arr [ 0 ] , arr [ 1 ] ) , ( arr [ 2 ] , arr [ 3 ] ) . . . can be made equal ; selecting the triplets such that all array elements can be made equal to arr [ n - 1 ] ; 
def checkXOR(arr, N):
    if (N % 2 == 0):
        xro = 0
        for i in range(N):
            xro ^= arr[i]
        if (xro != 0):
            print(-1)
            return
        for i in range(0, N - 3, 2):
            print(i, " ", (i + 1), " ", (i + 2), end=" ")
        for i in range(0, N - 3, 2):
            print(i, " ", (i + 1), " ", (N - 1), end=" ")
    else:
        for i in range(0, N - 2, 2):
            print(i, " ", (i + 1), " ", (i + 2))
        for i in range(0, N - 2, 2):
            print(i, " ", (i + 1), " ", (N - 1))


if __name__ == '__main__':
    arr = [4, 2, 1, 7, 2]
    N = len(arr)
    checkXOR(arr, N)

# Expected output:
0   1   2
2   3   4
0   1   4
2   3   4
# make all array elements even by replacing adjacent pair of array elements with their sum | function to find minimum count of operations required to make all array elements even ; stores minimum count of replacements to make all array elements even ; stores the count of odd continuous numbers ; traverse the array ; if arr [ i ] is an odd number ; update odd_cont_seg ; if odd_cont_seg is even ; update res ; update res ; reset odd_cont_seg = 0 ; if odd_cont_seg exceeds 0 ; if odd_cont_seg is even ; update res ; update res ; prthe result ; 
def make_array_element_even(arr, N):
    res = 0
    odd_cont_seg = 0
    for i in range(0, N):
        if (arr[i] % 2 == 1):
            odd_cont_seg += 1
        else:
            if (odd_cont_seg > 0):
                if (odd_cont_seg % 2 == 0):
                    res += odd_cont_seg // 2
                else:
                    res += (odd_cont_seg // 2) + 2
                odd_cont_seg = 0
    if (odd_cont_seg > 0):
        if (odd_cont_seg % 2 == 0):
            res += odd_cont_seg // 2
        else:
            res += odd_cont_seg // 2 + 2
    return res


arr = [2, 4, 5, 11, 6]
N = len(arr)
print(make_array_element_even(arr, N))

# Expected output:
1
# find a number k such that exactly k array elements are greater than or equal to k | function to find k for which there are exactly k array elements greater than or equal to k ; finding the largest array element ; possible values of k ; traverse the array ; if current array element is greater than or equal to i ; if i array elements are greater than or equal to i ; otherwise ; 
def zvalue(nums):
    m = max(nums)
    cnt = 0
    for i in range(0, m + 1, 1):
        cnt = 0
        for j in range(0, len(nums), 1):
            if (nums[j] >= i):
                cnt += 1
        if (cnt == i):
            return i
    return -1


if __name__ == '__main__':
    nums = [7, 8, 9, 0, 0, 1]
    print(zvalue(nums))

# Expected output:
3
# count of permutations of an array having each element as a multiple or a factor of its index | function to find the count of desired permutations ; base case ; if i has not been inserted ; backtrack ; insert i ; recur to find valid permutations ; remove i ; return the final count ; 
def findPermutation(arr, N):
    pos = len(arr) + 1
    if (pos > N):
        return 1
    res = 0
    for i in range(1, N + 1):
        if (i not in arr):
            if (i % pos == 0 or pos % i == 0):
                arr .add(i)
                res += findPermutation(arr, N)
                arr .remove(i)
    return res


N = 5
arr = set()
print(findPermutation(arr, N))

# Expected output:
10
# check if sum y can be obtained from the array by the given operations | function to check if it is possible to obtain sum y from a sequence of sum x from the array arr [ ] ; store the difference ; iterate over the array ; if diff reduced to 0 ; 
def solve(arr, n, X, Y):
    diff = Y - X
    for i in range(n):
        if (arr[i] != 1):
            diff = diff % (arr[i] - 1)
    if (diff == 0):
        print("Yes")
    else:
        print("No")


arr = [1, 2, 7, 9, 10]
n = len(arr)
X, Y = 11, 13
solve(arr, n, X, Y)

# Expected output:
Yes
# middle of three using minimum comparisons | python3 program to find middle of three distinct numbers ; function to find the middle of three number ; checking for b ; checking for a ; 
def middleOfThree(a, b, c):
    def middleOfThree(a, b, c):
        if ((a < b and b < c) or (c < b and b < a)):
            return b
        if ((b < a and a < c) or (c < a and a < b)):
            return a
        else:
            return c


a = 20
b = 30
c = 40
print(middleOfThree(a, b, c))

# Expected output:
None