giulio98/xlcost-single-prompt huggingface dataset:
# maximum prefix sum possible by merging two given arrays | python3 implementation of the above approach ; stores the maximum prefix sum of the array a [ ] ; traverse the array a [ ] ; stores the maximum prefix sum of the array b [ ] ; traverse the array b [ ] ; 
def maxPresum(a, b):
    X = max(a[0], 0)
    for i in range(1, len(a)):
        a[i] += a[i - 1]
        X = max(X, a[i])
    Y = max(b[0], 0)
    for i in range(1, len(b)):
        b[i] += b[i - 1]
        Y = max(Y, b[i])
    return X + Y


A = [2, -1, 4, -5]
B = [4, -3, 12, 4, -3]
print(maxPresum(A, B))

# Expected output:
22
# check if a number can be represented as sum of two positive perfect cubes | python3 program for the above approach ; function to check if n can be represented as sum of two perfect cubes or not ; if it is same return true ; ; if the curr smaller than n increment the lo ; if the curr is greater than curr decrement the hi ; 

import math


def sumOfTwoCubes(n):
    lo = 1
    hi = round(math .pow(n, 1 / 3))
    while (lo <= hi):
        curr = (lo * lo * lo + hi * hi * hi)
        if (curr == n):
            return True
        if (curr < n):
            lo += 1
        else:
            hi -= 1
    return False


N = 28
if (sumOfTwoCubes(N)):
    print("True")
else:
    print("False")

# Expected output:
True
# generate an n | python3 program for the above approach ; function to generate all prime numbers upto 10 ^ 6 ; initialize sieve [ ] as 1 ; iterate over the range [ 2 , n ] ; if current element is non - prime ; make all multiples of i as 0 ; function to construct an array a [ ] satisfying the given conditions ; stores the resultant array ; stores all prime numbers ; sieve of erastosthenes ; append the integer i if it is a prime ; indicates current position in list of prime numbers ; traverse the array arr [ ] ; if already filled with another prime number ; if a [ i ] is not filled but a [ ind ] is filled ; store a [ i ] = a [ ind ] ; if none of them were filled ; to make sure a [ i ] does not affect other values , store next prime number ; print the resultant array ; 

sieve = [1] * (1000000 + 1)


def sieveOfPrimes():
    global sieve
    N = 1000000
    for i in range(2, N + 1):
        if i * i > N:
            break
        if (sieve[i] == 0):
            continue
        for j in range(i * i, N + 1, i):
            sieve[j] = 0


def getArray(arr, N):
    global sieve
    A = [0] * N
    v = []
    sieveOfPrimes()
    for i in range(2, int(1e5) + 1):
        if (sieve[i]):
            v .append(i)
    j = 0
    for i in range(N):
        ind = arr[i]
        if (A[i] != 0):
            continue
        elif (A[ind] != 0):
            A[i] = A[ind]
        else:
            prime = v[j]
            A[i] = prime
            A[ind] = A[i]
            j += 1
    for i in range(N):
        print(A[i], end=" ")


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

# Expected output:
2 3 5 7 2
# nth natural number after removing all numbers consisting of the digit 9 | function to find nth number in base 9 ; stores the nth number ; iterate while n is greater than 0 ; update result ; divide n by 9 ; multiply p by 10 ; return result ; 
def findNthNumber(N):
    result = 0
    p = 1
    while (N > 0):
        result += (p * (N % 9))
        N = N // 9
        p = p * 10
    return result


if __name__ == '__main__':
    N = 9
    print(findNthNumber(N))

# Expected output:
10
# check if an integer is rotation of another given integer | python3 implementation of the approach ; function to check if the integer a is a rotation of the integer b ; stores the count of digits in a ; stores the count of digits in b ; if dig1 not equal to dig2 ; stores position of first digit ; stores the first digit ; rotate the digits of the integer ; if a is equal to b ; if a is equal to the initial value of integer a ; 

import math


def check(A, B):
    if (A == B):
        return 1
    dig1 = math .floor(math .log10(A) + 1)
    dig2 = math .floor(math .log10(B) + 1)
    if (dig1 != dig2):
        return 0
    temp = A
    while (True):
        power = pow(10, dig1 - 1)
        firstdigit = A // power
        A = A - firstdigit * power
        A = A * 10 + firstdigit
        if (A == B):
            return 1
        if (A == temp):
            return 0


A, B = 967, 679
if (check(A, B)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# count of quadruples with product of a pair equal to the product of the remaining pair | function to count the number of unique quadruples from an array that satisfies the given condition ; hashmap to store the product of pairs ; store the count of required quadruples ; traverse the array arr [ ] and generate all possible pairs ; store their product ; pair ( a , b ) can be used to generate 8 unique permutations with another pair ( c , d ) ; increment umap [ prod ] by 1 ; print the result ; 
def sameProductQuadruples(nums, N):
    umap = {}
    res = 0
    for i in range(N):
        for j in range(i + 1, N):
            prod = nums[i] * nums[j]
            if prod in umap:
                res += 8 * umap[prod]
                umap[prod] += 1
            else:
                umap[prod] = 1
    print(res)


if __name__ == "__main__":
    arr = [2, 3, 4, 6]
    N = len(arr)
    sameProductQuadruples(arr, N)

# Expected output:
8
# count ways to place m objects in distinct partitions of n boxes | python3 implementation of the above approach ; iterative function to calculate ( x ^ y ) % p in o ( log y ) ; initialize result ; update x if x >= mod to avoid multiplication overflow ; if y is odd , multiply x with result ; y = y / 2 ; change x to x ^ 2 ; utility function to find the total number of ways ; number of even indexed boxes ; number of partitions of even indexed boxes ; number of ways to distribute objects ; 

MOD = 1000000007


def power(x, y, p=MOD):
    res = 1
    x = x % p
    while (y > 0):
        if (y & 1):
            res = (res * x) % p
        y = y >> 1
        x = (x * x) % p
    return res


def totalWays(N, M):
    X = N // 2
    S = (X * (X + 1)) % MOD
    print(power(S, M, MOD))


if __name__ == '__main__':
    N, M = 5, 2
    totalWays(N, M)

# Expected output:
36
# check if a graph constructed from an array based on given conditions consists of a cycle or not | function to check if the graph constructed from given array contains a cycle or not ; traverse the array ; if arr [ i ] is less than arr [ i - 1 ] and arr [ i ] ; 
def isCycleExists(arr, N):
    valley = 0
    for i in range(1, N):
        if (arr[i] < arr[i - 1] and arr[i] < arr[i + 1]):
            print("Yes")
            return
    print("No")


if __name__ == '__main__':
    arr = [1, 3, 2, 4, 5]
    N = len(arr)
    isCycleExists(arr, N)

# Expected output:
Yes
# maximize first array element by performing given operations at most k times | function to maximize the first array element ; traverse the array ; initialize cur_val to a [ i ] ; if all operations are not over yet ; if current value is greater than zero ; incrementing first element of array by 1 ; decrementing current value of array by 1 ; decrementing number of operations by i ; if current value is zero , then break ; print first array element ; 
def getMax(arr, N, K):
    for i in range(1, N, 1):
        cur_val = arr[i]
        while (K >= i):
            if (cur_val > 0):
                arr[0] = arr[0] + 1
                cur_val = cur_val - 1
                K = K - i
            else:
                break
    print(arr[0])


if __name__ == '__main__':
    arr = [1, 0, 3, 2]
    N = len(arr)
    K = 5
    getMax(arr, N, K)

# Expected output:
3
# count non | python3 program of the above approach ; function to find the gcd of the two numbers ; function to find distinct elements in the array by repeatidely inserting the absolute difference of all possible pairs ; stores largest element of the array ; traverse the array , arr [ ] ; update max_value ; stores gcd of array ; update gcdarr ; stores distinct elements in the array by repeatedely inserting absolute difference of all possible pairs ; 

import sys


def gcd(a, b):
    if a == 0:
        return b
    return gcd(b % a, a)


def DistinctValues(arr, N):
    max_value = -sys .maxsize - 1
    max_value = max(arr)
    GCDArr = arr[0]
    for i in range(1, N):
        GCDArr = gcd(GCDArr, arr[i])
    answer = max_value // GCDArr
    return answer + 1


arr = [4, 12, 16, 24]
N = len(arr)
print(DistinctValues(arr, N))

# Expected output:
7
# minimum row or column swaps required to make every pair of adjacent cell of a binary matrix distinct | function to return number of moves to convert matrix into chessboard ; size of the matrix ; traverse the matrix ; initialize rowsum to count 1 s in row ; initialize colsum to count 1 s in column ; to store no . of rows to be corrected ; to store no . of columns to be corrected ; traverse in the range [ 0 , n - 1 ] ; check if rows is either n / 2 or ( n + 1 ) / 2 and return - 1 ; check if rows is either n / 2 or ( n + 1 ) / 2 and return - 1 ; check if n is odd ; check if column required to be corrected is odd and then assign n - colswap to colswap ; check if rows required to be corrected is odd and then assign n - rowswap to rowswap ; take min of colswap and n - colswap ; take min of rowswap and n - rowswap ; finally return answer ; 
def minSwaps(b):
    n = len(b)
    for i in range(n):
        for j in range(n):
            if (b[0][0] ^ b[0][j] ^ b[i][0] ^ b[i][j]):
                return -1
    rowSum = 0
    colSum = 0
    rowSwap = 0
    colSwap = 0
    for i in range(n):
        rowSum += b[i][0]
        colSum += b[0][i]
        rowSwap += b[i][0] == i % 2
        colSwap += b[0][i] == i % 2
    if (rowSum != n // 2 and rowSum != (n + 1) // 2):
        return -1
    if (colSum != n // 2 and colSum != (n + 1) // 2):
        return -1
    if (n % 2 == 1):
        if (colSwap % 2):
            colSwap = n - colSwap
        if (rowSwap % 2):
            rowSwap = n - rowSwap
    else:
        colSwap = min(colSwap, n - colSwap)
        rowSwap = min(rowSwap, n - rowSwap)
    return (rowSwap + colSwap) // 2


if __name__ == "__main__":
    M = [[0, 1, 1, 0], [0, 1, 1, 0], [1, 0, 0, 1], [1, 0, 0, 1]]
    ans = minSwaps(M)
    print(ans)

# Expected output:
2
# minimum number of coins having value equal to powers of 2 required to obtain n | function to count of set bit in n ; stores count of set bit in n ; iterate over the range [ 0 , 31 ] ; if current bit is set ; update result ; 
def count_setbit(N):
    result = 0
    for i in range(32):
        if ((1 << i) & N):
            result = result + 1
    print(result)


if __name__ == '__main__':
    N = 43
    count_setbit(N)

# Expected output:
4
# evaluate the expression ( n1 * ( n | python 3 program to implement the above approach ; function to find the value of the expression ( n ^ 1 * ( n 1 ) ^ 2 * ... * 1 ^ n ) % ( 109 + 7 ) . ; factorial [ i ] : stores factorial of i ; base case for factorial ; precompute the factorial ; dp [ n ] : stores the value of the expression ( n ^ 1 * ( n 1 ) ^ 2 * ... * 1 ^ n ) % ( 109 + 7 ) . ; update dp [ i ] ; return the answer . ; 

mod = 1000000007


def ValOfTheExpression(n):
    global mod
    factorial = [0 for i in range(n + 1)]
    factorial[0] = 1
    factorial[1] = 1
    for i in range(2, n + 1, 1):
        factorial[i] = ((factorial[i - 1] % mod) * (i % mod)) % mod
    dp = [0 for i in range(n + 1)]
    dp[1] = 1
    for i in range(2, n + 1, 1):
        dp[i] = ((dp[i - 1] % mod) * (factorial[i] % mod)) % mod
    return dp[n]


if __name__ == '__main__':
    n = 4
    print(ValOfTheExpression(n))

# Expected output:
288
# chocolate distribution problem | set 2 | function to print minimum number of candies required ; distribute 1 chocolate to each ; traverse from left to right ; traverse from right to left ; initialize sum ; find total sum ; return sum ; 
def minChocolates(A, N):
    B = [1 for i in range(N)]
    for i in range(1, N):
        if (A[i] > A[i - 1]):
            B[i] = B[i - 1] + 1
        else:
            B[i] = 1
    for i in range(N - 2, -1, -1):
        if (A[i] > A[i + 1]):
            B[i] = max(B[i + 1] + 1, B[i])
        else:
            B[i] = max(B[i], 1)
    sum = 0
    for i in range(N):
        sum += B[i]
    print(sum)


if __name__ == '__main__':
    A = [23, 14, 15, 14, 56, 29, 14]
    N = len(A)
    minChocolates(A, N)

# Expected output:
12
# construct longest possible sequence of unique elements with given lcm | python3 program to implement the above approach ; function to construct an array of unique elements whose lcm is n ; stores array elements whose lcm is n ; iterate over the range [ 1 , sqrt ( n ) ] ; if n is divisible by i ; insert i into newarr [ ] ; if n is not perfect square ; sort the array newarr [ ] ; print array elements ; 

from math import sqrt, ceil, floor


def constructArrayWithGivenLCM(N):
    newArr = []
    for i in range(1, ceil(sqrt(N + 1))):
        if (N % i == 0):
            newArr .append(i)
            if (N // i != i):
                newArr .append(N // i)
    newArr = sorted(newArr)
    for i in newArr:
        print(i, end=" ")


if __name__ == '__main__':
    N = 12
    constructArrayWithGivenLCM(N)

# Expected output:
1 2 3 4 6 12
# count numbers from given range having odd digits at odd places and even digits at even places | function to calculate 5 ^ p ; stores the result ; multiply 5 p times ; return the result ; function to count anumbers upto n having odd digits at odd places and even digits at even places ; stores the count ; stores the digits of n ; insert the digits of n ; reverse the vector to arrange the digits from first to last ; stores count of digits of n ; stores the count of numbers with i digits ; if the last digit is reached , subtract numbers eceeding range ; iterate over athe places ; stores the digit in the pth place ; stores the count of numbers having a digit greater than x in the p - th position ; calculate the count of numbers exceeding the range if p is even ; calculate the count of numbers exceeding the range if p is odd ; subtract the count of numbers exceeding the range from total count ; if the parity of p and the parity of x are not same ; add count of numbers having i digits and satisfies the given conditions ; return the total count of numbers tin ; function to calculate the count of numbers from given range having odd digits places and even digits at even places ; count of numbers in range [ l , r ] = count of numbers tir - ; count of numbers ti ( l - 1 ) ; 
def getPower(p):
    res = 1
    while (p):
        res *= 5
        p -= 1
    return res


def countNumbersUtil(N):
    count = 0
    digits = []
    while (N):
        digits .append(N % 10)
        N //= 10
    digits .reverse()
    D = len(digits)
    for i in range(1, D + 1, 1):
        res = getPower(i)
        if (i == D):
            for p in range(1, D + 1, 1):
                x = digits[p - 1]
                tmp = 0
                if (p % 2 == 0):
                    tmp = ((5 - (x // 2 + 1)) * getPower(D - p))
                else:
                    tmp = ((5 - (x + 1) // 2) * getPower(D - p))
                res -= tmp
                if (p % 2 != x % 2):
                    break
        count += res
    return count


def countNumbers(L, R):
    print(countNumbersUtil(R) - countNumbersUtil(L - 1))


L = 128
R = 162
countNumbers(L, R)

# Expected output:
7
# sum of first n natural numbers with alternate signs | function to find the sum of first n natural numbers with alternate signs ; stores sum of alternate sign of first n natural numbers ; if is an even number ; update alternatesum ; if i is an odd number ; update alternatesum ; 
def alternatingSumOfFirst_N(N):
    alternateSum = 0
    for i in range(1, N + 1):
        if (i % 2 == 0):
            alternateSum += -i
        else:
            alternateSum += i
    return alternateSum


if __name__ == "__main__":
    N = 6
    print(alternatingSumOfFirst_N(N))

# Expected output:
-3
# sum of all numbers up to n that are co | function to return gcd of a and b ; base case ; recursive gcd ; function to calculate the sum of all numbers till n that are coprime with n ; stores the resultant sum ; iterate over [ 1 , n ] ; if gcd is 1 ; update sum ; return the final sum ; 
def gcd(a, b):
    if (a == 0):
        return b
    return gcd(b % a, a)


def findSum(N):
    sum = 0
    for i in range(1, N):
        if (gcd(i, N) == 1):
            sum += i
    return sum


if __name__ == '__main__':
    N = 5
    print(findSum(N))

# Expected output:
10
# count all distinct pairs of repeating elements from the array for every array element | function to prthe required count of pairs excluding the current element ; store the frequency ; find all the count ; delete the contribution of each element for equal pairs ; print the answer ; 
def solve(arr, n):
    mp = {}
    for i in arr:
        mp[i] = mp .get(i, 0) + 1
    cnt = 0
    for x in mp:
        cnt += ((mp[x]) * (mp[x] - 1) // 2)
    ans = [0] * n
    for i in range(n):
        ans[i] = cnt - (mp[arr[i]] - 1)
    for i in ans:
        print(i, end=" ")


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

# Expected output:
2 2 3 2 3
# mode in a stream of integers ( running integers ) | function that prints the mode values ; map used to mp integers to its frequency ; to store the maximum frequency ; to store the element with the maximum frequency ; loop used to read the elements one by one ; updates the frequency of that element ; checks for maximum number of occurrence ; updates the maximum frequency ; updates the mode ; 
def findMode(a, n):
    mp = {}
    max = 0
    mode = 0
    for i in range(n):
        if a[i] in mp:
            mp[a[i]] += 1
        else:
            mp[a[i]] = 1
        if (mp[a[i]] >= max):
            max = mp[a[i]]
            mode = a[i]
        print(mode, end=" ")


arr = [2, 7, 3, 2, 5]
n = len(arr)
findMode(arr, n)

# Expected output:
2 7 3 2 2
# count of distinct numbers formed by shuffling the digits of a large number n | recursive function to return the value of ( x ^ n ) % m ; base case ; if n is even ; else n is odd ; function to find modular inverse of a number x under modulo m ; using fermat 's little theorem ; function to count of numbers formed by shuffling the digits of a large number n ; modulo value ; array to store the factorials upto the maximum value of n ; store factorial of i at index i ; to store count of occurrence of a digit ; increment the count of digit occured ; assign the factorial of length of input ; multiplying result with the modulo multiplicative inverse of factorial of count of i ; print the result ; 
def modexp(x, n, m):
    if (n == 0):
        return 1
    else:
        if (n % 2 == 0):
            return modexp((x * x) % m, n / 2, m)
        else:
            return (x * modexp((x * x) % m, (n - 1) / 2, m) % m)


def modInverse(x, m):
    return modexp(x, m - 2, m)


def countNumbers(N):
    m = 1000000007
    factorial = [0 for x in range(100001)]
    factorial[0] = 1
    for i in range(1, 100001):
        factorial[i] = (factorial[i - 1] * i) % m
    count = [0 for x in range(10)]
    for i in range(0, 10):
        count[i] = 0
    length = len(N)
    for i in range(0, length):
        count[int(N[i])] += 1
    result = factorial[int(length)]
    for i in range(0, 10):
        result = (result * modInverse(factorial[int(count[i])], m)) % m
    print(result)


N = "0223"
countNumbers(N)

# Expected output:
12
# find prime factors of array elements whose sum of exponents is divisible by k | to store the smallest prime factor till 10 ^ 5 ; function to compute smallest prime factor array ; initialize the spf array first element ; marking smallest prime factor for every number to be itself ; separately marking smallest prime factor for every even number as 2 ; checking if i is prime ; marking spf for all numbers divisible by i ; marking spf [ j ] if it is not previously marked ; function that finds minimum operation ; create a spf [ ] array ; map created to store the unique prime numbers ; to store the result ; to store minimum operations ; to store every unique prime number ; erase 1 as a key because it is not a prime number ; first prime number ; frequency is divisible by k then insert primenum in the result [ ] ; print the elements if it exists ; 

spf = [0 for i in range(10001)]


def spf_array(spf):
    spf[1] = 1
    for i in range(2, 1000, 1):
        spf[i] = i
    for i in range(4, 1000, 2):
        spf[i] = 2
    i = 3
    while (i * i < 1000):
        if (spf[i] == i):
            j = i * i
            while (j < 1000):
                if (spf[j] == j):
                    spf[j] = i
                j += i
        i += 1


def frequent_prime(arr, N, K):
    spf_array(spf)
    Hmap = {}
    result = []
    i = 0
    c = 0
    for i in range(N):
        x = arr[i]
        while (x != 1):
            Hmap[spf[x]] = Hmap .get(spf[x], 0) + 1
            x = x // spf[x]
    if (1 in Hmap):
        Hmap .pop(1)
    for key, value in Hmap .items():
        primeNum = key
        frequency = value
        if (frequency % K == 0):
            result .append(primeNum)
    result = result[::-1]
    if (len(result) > 0):
        for it in result:
            print(it, end=" ")
    else:
        print("{}")


if __name__ == '__main__':
    arr = [1, 4, 6]
    K = 1
    N = len(arr)
    frequent_prime(arr, N, K)

# Expected output:
3 2
# generate first k multiples of n using bitwise operators | function to print the first k multiples of n ; print the value of n * i ; iterate each bit of n and add pow ( 2 , pos ) , where pos is the index of each set bit ; check if current bit at pos j is fixed or not ; for next set bit ; 
def Kmultiples(n, k):
    a = n
    for i in range(1, k + 1):
        print("{} * {} = {}".format(n, i, a))
        j = 0
        while (n >= (1 << j)):
            a += n & (1 << j)
            j += 1


N = 16
K = 7
Kmultiples(N, K)

# Expected output:
16 * 1 = 16
16 * 2 = 32
16 * 3 = 48
16 * 4 = 64
16 * 5 = 80
16 * 6 = 96
16 * 7 = 112
# Least Square Regression Line | Function to calculate b ; sum of array x ; sum of array y ; for sum of product of x and y ; sum of square of x ; Function to find the least regression line ; Finding b ; Calculating a ; Printing regression line ; Statistical data
def calculateB(x, y, n):
    sx = sum(x)
    sy = sum(y)
    sxsy = 0
    sx2 = 0
    for i in range(n):
        sxsy += x[i] * y[i]
        sx2 += x[i] * x[i]
    b = (n * sxsy - sx * sy) / (n * sx2 - sx * sx)
    return b


def leastRegLine(X, Y, n):
    b = calculateB(X, Y, n)
    meanX = int(sum(X) / n)
    meanY = int(sum(Y) / n)
    a = meanY - b * meanX
    print("Regression line:")
    print("Y = ", '%.3f' % a, " + ", '%.3f' % b, "*X", sep="")


X = [95, 85, 80, 70, 60]
Y = [90, 80, 70, 65, 60]
n = len(X)
leastRegLine(X, Y, n)

# Expected output:
Regression line:
Y = 5.685 + 0.863*X
# count of repeating digits in a given number | function that returns the count of repeating digits of the given number ; initialize a variable to store count of repeating digits ; initialize cnt array to store digit count ; iterate through the digits of n ; retrieve the last digit of n ; increase the count of digit ; remove the last digit of n ; iterate through the cnt array ; if frequency of digit is greater than 1 ; increment the count of repeating digits ; return count of repeating digit ; 
def countRepeatingDigits(N):
    res = 0
    cnt = [0] * 10
    while (N > 0):
        rem = N % 10
        cnt[rem] += 1
        N = N // 10
    for i in range(10):
        if (cnt[i] > 1):
            res += 1
    return res


N = 12
print(countRepeatingDigits(N))

# Expected output:
0
# find temperature of missing days using given sum and average | function for finding the temperature ; store day1 - day2 in diff ; remaining from s will be day1 ; print day1 and day2 ; 
def findTemperature(x, y, s):
    diff = (x - y) * 6
    Day2 = (diff + s) // 2
    Day1 = s - Day2
    print("Day1 : ", Day1)
    print("Day2 : ", Day2)


if __name__ == '__main__':
    x = 5
    y = 10
    s = 40
    findTemperature(x, y, s)

# Expected output:
Day1 :  35
Day2 :  5