giulio98/xlcost-single-prompt huggingface dataset:
# check if a number n can be expressed as the sum of powers of x or not | function to check if the number n can be expressed as the sum of different powers of x or not ; while n is a positive number ; find the remainder ; if rem is at least 2 , then representation is impossible ; divide the value of n by x ; 
def ToCheckPowerofX(n, x):
    while (n > 0):
        rem = n % x
        if (rem >= 2):
            return False
        n = n // x
    return True


if __name__ == '__main__':
    N = 10
    X = 3
    if (ToCheckPowerofX(N, X)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# find the maximum between n and the number formed by reversing 32 | function that obtains the number using said operations from n ; stores the binary representation of the number n ; find the binary representation of the number n ; check for the set bits ; reverse the string s ; stores the obtained number ; calculating the decimal value ; check for set bits ; function to find the maximum value between n and the obtained number ; 
def reverseBin(N):
    S = ""
    i = 0
    for i in range(32):
        if (N & (1 << i)):
            S += '1'
        else:
            S += '0'
    S = list(S)
    S = S[::-1]
    S = ''.join(S)
    M = 0
    for i in range(32):
        if (S[i] == '1'):
            M += (1 << i)
    return M


def maximumOfTwo(N):
    M = reverseBin(N)
    return max(N, M)


if __name__ == '__main__':
    N = 6
    print(maximumOfTwo(N))

# Expected output:
1610612736
# sum of euler totient functions obtained for each divisor of n | function to find the sum of euler totient function of divisors of n ; return the value of n ; 
def sumOfDivisors(N):
    return N


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

# Expected output:
5
# square root of two complex numbers | python3 program for the above approach ; function to find the square root of a complex number ; stores all the square roots ; stores the first square root ; push the square root in the ans ; stores the second square root ; if x2 is not 0 ; push the square root in the array ans [ ] ; stores the third square root ; if x3 is greater than 0 ; push the square root in the array ans [ ] ; stores the fourth square root ; push the square root in the array ans [ ] ; prints the square roots ; 

from math import sqrt


def complexRoot(A, B):
    ans = []
    X1 = abs(sqrt((A + sqrt(A * A + B * B)) / 2))
    Y1 = B / (2 * X1)
    ans .append([X1, Y1])
    X2 = -1 * X1
    Y2 = B / (2 * X2)
    if (X2 != 0):
        ans .append([X2, Y2])
    X3 = (A - sqrt(A * A + B * B)) / 2
    if (X3 > 0):
        X3 = abs(sqrt(X3))
        Y3 = B / (2 * X3)
        ans .append([X3, Y3])
        X4 = -1 * X3
        Y4 = B / (2 * X4)
        if (X4 != 0):
            ans .append([X4, Y4])
    print("The Square roots are: ")
    for p in ans:
        print(round(p[0], 6), end="")
        if (p[1] > 0):
            print("+", end="")
        if (p[1]):
            print(str(round(p[1], 6)) + "*i")
        else:
            print()


if __name__ == '__main__':
    A, B = 0, 1
    complexRoot(A, B)

# Expected output:
The Square roots are: 
0.707107+0.707107*i
-0.707107-0.707107*i
# minimum time required to schedule k processes | function to find minimum required time to schedule all process ; stores max element from a [ ] ; find the maximum element ; stores frequency of each element ; stores minimum time required to schedule all process ; count frequencies of elements ; find the minimum time ; decrease the value of k ; increment tmp [ i / 2 ] ; increment the count ; return count , if all process are scheduled ; increment count ; return the count ; if it is not possible to schedule all process ; 
def minTime(A, n, K):
    max_ability = A[0]
    for i in range(1, n):
        max_ability = max(max_ability, A[i])
    tmp = [0 for i in range(max_ability + 1)]
    count = 0
    for i in range(n):
        tmp[A[i]] += 1
    i = max_ability
    while (i >= 0):
        if (tmp[i] != 0):
            if (tmp[i] * i < K):
                K -= (i * tmp[i])
                tmp[i // 2] += tmp[i]
                count += tmp[i]
                if (K <= 0):
                    return count
            else:
                if (K % i != 0):
                    count += (K // i) + 1
                else:
                    count += (K // i)
                return count
        i -= 1
    return -1


if __name__ == '__main__':
    arr = [3, 1, 7, 2, 4]
    N = 5
    K = 15
    print(minTime(arr, N, K))

# Expected output:
4
# minimum length of a rod that can be split into n equal parts that can further be split into given number of equal parts | function to find gcd of two numbers a and b ; base case ; find gcd recursively ; function to find the lcm of the resultant array ; initialize a variable ans as the first element ; traverse the array ; update lcm ; return the minimum length of the rod ; function to find the minimum length of the rod that can be divided into n equals parts and each part can be further divided into arr [ i ] equal parts ; print the result ; 
def gcd(a, b):
    if (b == 0):
        return a
    return gcd(b, a % b)


def findlcm(arr, n):
    ans = arr[0]
    for i in range(n):
        ans = (((arr[i] * ans)) / (gcd(arr[i], ans)))
    return ans


def minimumRod(A, N):
    print(int(N * findlcm(A, N)))


arr = [1, 2]
N = len(arr)
minimumRod(arr, N)

# Expected output:
4
# check if n can be represented as sum of positive integers containing digit d at least once | function to check if n contains digit d in it ; iterate until n is positive ; find the last digit ; if the last digit is the same as digit d ; return false ; function to check if the value of n can be represented as sum of integers having digit d in it ; iterate until n is positive ; check if n contains digit d or not ; subtracting d from n ; return false ; 
def findDigit(N, D):
    while (N > 0):
        a = N % 10
        if (a == D):
            return True
        N /= 10
    return False


def check(N, D):
    while (N > 0):
        if (findDigit(N, D)):
            return True
        N -= D
    return False


if __name__ == '__main__':
    N = 24
    D = 7
    if (check(N, D)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# number of relations that are neither reflexive nor irreflexive on a set | python program for the above approach ; function to calculate x ^ y modulo 10 ^ 9 + 7 in o ( log y ) ; stores the result of ( x ^ y ) ; update x , if it exceeds mod ; if x is divisible by mod ; if y is odd , then multiply x with res ; divide y by 2 ; update the value of x ; return the value of x ^ y ; function to count the number of relations that are neither reflexive nor irreflexive ; return the resultant count ; 

mod = 1000000007


def power(x, y):
    res = 1
    x = x % mod
    if (x == 0):
        return 0
    while (y > 0):
        if (y % 2 == 1):
            res = (res * x) % mod
        y = y >> 1
        x = (x * x) % mod
    return res


def countRelations(N):
    print((power(2, N) - 2) * power(2, N * N - N))


N = 2
countRelations(N)

# Expected output:
8
# minimum operations required to make all elements in an array of first n odd numbers equal | function to find the minimum number of operations required to make the array elements equal ; stores the array elements ; stores the sum of the array ; iterate over the range [ 0 , n ] ; update the value arr [ i ] ; increment the sum by the value arr [ i ] ; stores the middle element ; if n is even ; otherwise ; stores the result ; traverse the range [ 0 , n / 2 ] ; update the value of ans ; return the result ; 
def minOperations(N):
    arr = [0] * N
    sum = 0
    for i in range(N):
        arr[i] = (2 * i) + 1
        sum = sum + arr[i]
    mid = 0
    if N % 2 == 0:
        mid = sum / N
    else:
        mid = arr[int(N / 2)]
    ans = 0
    for i in range(int(N / 2)):
        ans += mid - arr[i]
    return int(ans)


N = 6
print(minOperations(N))

# Expected output:
9
# minimum operations required to make all elements in an array of first n odd numbers equal | function to find the minimum number of operations required to make the array elements equal ; if the value of n is even ; return the value ; otherwise , n is odd ; return the value ; 
def minOperation(N):
    if (N % 2 == 0):
        return (N / 2) * (N / 2)
    k = (N - 1) / 2
    return (k * (k + 1))


N = 6
print(int(minOperation(N)))

# Expected output:
9
# bitwise xor of bitwise and of all pairs from two given arrays | function to find the bitwise xor of bitwise and of all pairs from the arrays arr1 [ ] and arr2 [ ] ; stores the result ; iterate over the range [ 0 , n - 1 ] ; iterate over the range [ 0 , m - 1 ] ; stores bitwise and of the pair { arr1 [ i ] , arr2 [ j ] } ; update res ; return the res ; 
def findXORS(arr1, arr2, N, M):
    res = 0
    for i in range(N):
        for j in range(M):
            temp = arr1[i] & arr2[j]
            res ^= temp
    return res


if __name__ == '__main__':
    arr1 = [1, 2, 3]
    arr2 = [6, 5]
    N = len(arr1)
    M = len(arr2)
    print(findXORS(arr1, arr2, N, M))

# Expected output:
0
# bitwise xor of bitwise and of all pairs from two given arrays | function to find the bitwise xor of bitwise and of all pairs from the arrays arr1 [ ] and arr2 [ ] ; stores xor of array arr1 [ ] ; stores xor of array arr2 [ ] ; traverse the array arr1 [ ] ; traverse the array arr2 [ ] ; return the result ; 
def findXORS(arr1, arr2, N, M):
    XORS1 = 0
    XORS2 = 0
    for i in range(N):
        XORS1 ^= arr1[i]
    for i in range(M):
        XORS2 ^= arr2[i]
    return XORS1 and XORS2


if __name__ == '__main__':
    arr1 = [1, 2, 3]
    arr2 = [6, 5]
    N = len(arr1)
    M = len(arr2)
    print(findXORS(arr1, arr2, N, M))

# Expected output:
0
# count numbers up to n that cannot be expressed as sum of at least two consecutive positive integers | function to check if a number can be expressed as a power of 2 ; if n is power of two ; function to count numbers that cannot be expressed as sum of two or more consecutive + ve integers ; stores the resultant count of integers ; iterate over the range [ 1 , n ] ; check if i is power of 2 ; increment the count if i is not power of 2 ; print the value of count ; 
def isPowerof2(n):
    return ((n & (n - 1)) and n)


def countNum(N):
    count = 0
    for i in range(1, N + 1):
        flag = isPowerof2(i)
        if (not flag):
            count += 1
    print(count)


if __name__ == '__main__':
    N = 100
    countNum(N)

# Expected output:
7
# count numbers up to n that cannot be expressed as sum of at least two consecutive positive integers | python 3 program for the above approach ; function to count numbers that cannot be expressed as sum of two or more consecutive + ve integers ; stores the count of such numbers ; 

import math


def countNum(N):
    ans = int(math .log2(N)) + 1
    print(ans)


if __name__ == "__main__":
    N = 100
    countNum(N)

# Expected output:
7
# replace array elements that contains k as a digit with the nearest power of k | python3 program for the above approach ; function to calculate the power of base nearest to x ; stores logx to the base k ; function to replace array elements with nearest power of k ; traverse the array ; convert integer into a string ; if k is found , then replace with the nearest power of k ; print the array ; given array ; 

import math


def nearestPow(x, base):
    k = int(math .log(x, base))
    if abs(base ** k - x) < abs(base ** (k + 1) - x):
        return base ** k
    else:
        return base ** (k + 1)


def replaceWithNearestPowerOfK(arr, K):
    for i in range(len(arr)):
        strEle = str(arr[i])
        for c in strEle:
            if int(c) == K:
                arr[i] = nearestPow(arr[i], K)
                break
    print(arr)


arr = [432, 953, 232, 333]
K = 3
replaceWithNearestPowerOfK(arr, K)

# Expected output:
[243, 729, 243, 243]
# difference between ceil of array sum divided by k and sum of ceil of array elements divided by k | python3 program for the above approach ; function to find absolute difference between array sum divided by x and sum of ceil of array elements divided by x ; stores the total sum ; stores the sum of ceil of array elements divided by x ; traverse the array ; adding each array element ; add the value ceil of arr [ i ] / x ; find the ceil of the total sum divided by x ; return absolute difference ; 

from math import ceil


def ceilDifference(arr, n, x):
    totalSum = 0
    perElementSum = 0
    for i in range(n):
        totalSum += arr[i]
        perElementSum += ceil(arr[i] / x)
    totalCeilSum = ceil(totalSum / x)
    return abs(perElementSum - totalCeilSum)


if __name__ == '__main__':
    arr = [1, 2, 3, 4, 5, 6]
    K = 4
    N = len(arr)
    print(ceilDifference(arr, N, K))

# Expected output:
2
# remove trailing zeros from the sum of two numbers ( using stack ) | function to remove trailing zeros from the sum of two numbers ; stores the sum of a and b ; stores the digits ; stores the equivalent string of integer n ; traverse the string ; push the digit at i in the stack ; while top element is '0 ; pop the top element ; stores the resultant number without tailing 0 's ; while s is not empty ; append top element of s in res ; pop the top element of s ; reverse the string res ; 
def removeTailing(A, B):
    N = A + B
    s = []
    strsum = str(N)
    for i in range(len(strsum)):
        s .append(strsum[i])

    while (s[-1] == '0'):
        s .pop()
    res = ""
    while (len(s) != 0):
        res = res + (s[-1])
        s .pop()
    res = list(res)
    res .reverse()
    res = ''.join(res)
    return res


if __name__ == "__main__":
    A = 130246
    B = 450164
    print(removeTailing(A, B))

# Expected output:
58041
# count number of triplets with product not exceeding a given number | function to count the number of triplets whose product is at most n ; stores the count of triplets ; iterate over the range [ 0 , n ] ; iterate over the range [ 0 , n ] ; if the product of pairs exceeds n ; increment the count of possible triplets ; return the total count ; 
def countTriplets(N):
    ans = 0
    for i in range(1, N + 1):
        for j in range(1, N + 1):
            if (i * j > N):
                break
            ans += N // (i * j)
    return ans


if __name__ == "__main__":
    N = 10
    print(countTriplets(N))

# Expected output:
53
# sum of an infinite geometric progression ( gp ) | function to calculate the sum of an infinite geometric progression ; case for infinite sum ; store the sum of gp series ; print the value of sum ; 
def findSumOfGP(a, r):
    if (abs(r) >= 1):
        print("Infinite")
        return
    sum = a / (1 - r)
    print(int(sum))


if __name__ == '__main__':
    A, R = 1, 0.5
    findSumOfGP(A, R)

# Expected output:
2
# number of relations that are both irreflexive and antisymmetric on a set | python 3 program for the above approach ; function to calculate x ^ y % mod in o ( log y ) ; stores the result of x ^ y ; update x if it exceeds mod ; if y is odd , then multiply x with result ; divide y by 2 ; update the value of x ; return the value of x ^ y ; function to count relations that are irreflexive and antisymmetric in a set consisting of n elements ; return the resultant count ; 

mod = 1000000007


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


def numberOfRelations(N):
    return power(3, (N * N - N) // 2)


if __name__ == "__main__":
    N = 2
    print(numberOfRelations(N))

# Expected output:
3
# count of numbers up to n having at least one prime factor common with n | python 3 program for the above approach ; function to count all the numbers in the range [ 1 , n ] having common factor with n other than 1 ; stores the count of numbers having more than 1 factor with n ; iterate over the range [ 1 , n ] ; if gcd is not 1 then increment the count ; print the resultant count ; 

import math


def countNumbers(N):
    count = 0
    for i in range(1, N + 1):
        if (math .gcd(i, N) != 1):
            count += 1
    print(count)


if __name__ == "__main__":
    N = 5
    countNumbers(N)

# Expected output:
1
# count of numbers up to n having at least one prime factor common with n | function to calculate the value of euler 's totient function ; initialize result with n ; find all prime factors of n and subtract their multiples ; check if p is a prime factor ; if found to be true , then update n and result ; if n has a prime factor greater than sqrt ( n ) , then there can be at - most one such prime factor ; function to count all the numbers in the range [ 1 , n ] having common factor with n other than 1 ; stores the resultant count ; print the count ; 
def phi(N):
    result = N
    for p in range(2, int(pow(N, 1 / 2)) + 1):
        if (N % p == 0):
            while (N % p == 0):
                N = N / p
            result -= result // p
    if (N > 1):
        result -= result // N
    return result


def countNumbers(N):
    count = N - phi(N)
    print(count)


N = 5
countNumbers(N)

# Expected output:
1
# queries to update array by adding or multiplying array elements and print the element present at specified index | function to modify the array by performing given queries ; stores the multiplication of all integers of type 1 ; stores the value obtained after performing queries of type 1 & 2 ; iterate over the queries ; query of type 0 ; update the value of add ; query of type 1 ; update the value of mul ; update the value of add ; otherwise ; store the element at index q [ i ] [ 1 ] ; prthe result for the current query ; 
def Query(arr, N, Q):
    mul = 1
    add = 0
    for i in range(len(Q)):
        if (Q[i][0] == 0):
            add = add + Q[i][1]
        elif (Q[i][0] == 1):
            mul = mul * Q[i][1]
            add = add * Q[i][1]
        else:
            ans = arr[Q[i][1]] * mul + add
            print(ans, end=" ")


if __name__ == '__main__':
    arr = [3, 1, 23, 45, 100]
    N = len(arr)
    Q = [[1, 2], [0, 10], [2, 3], [1, 5], [2, 4]]
    Query(arr, N, Q)

# Expected output:
100 1050
# minimize cost of converting all array elements to fibonacci numbers | python program for the above approach ; function to find the n - th fibonacci number ; find the value of a , b , and r ; find the n - th fibonacci ; return the result ; function to find the fibonacci number which is nearest to x ; calculate the value of n for x ; return the nearest fibonacci number ; function to find the minimum cost to convert all array elements to fibonacci numbers ; stores the total minimum cost ; traverse the given array arr [ ] ; find the nearest fibonacci number ; add the cost ; return the final cost ; 

import math


def nthFibo(n):
    a = (5 ** (1 / 2) + 1) / 2
    b = (-5 ** (1 / 2) + 1) / 2
    r = 5 ** (1 / 2)
    ans = (a ** n - b ** n) / r
    return int(ans)


def nearFibo(X):
    a = (5 ** (1 / 2) + 1) / 2
    n = int(math .log((5 ** (1 / 2)) * X) / math .log(a))
    nth = nthFibo(n)
    nplus = nthFibo(n + 1)
    if abs(X - nth) < abs(X - nplus):
        return nth
    else:
        return nplus


def getCost(arr):
    cost = 0
    for i in arr:
        fibo = nearFibo(i)
        cost += abs(i - fibo)
    return cost


arr = [56, 34, 23, 98, 7]
print(getCost(arr))

# Expected output:
13
# maximum sum of pairs that are at least k distance apart in an array | function to find the largest sum pair that are k distant apart ; stores the prefix maximum array ; base case ; traverse the array and update the maximum value upto index i ; stores the maximum sum of pairs ; iterate over the range [ k , n ] ; find the maximum value of the sum of valid pairs ; return the resultant sum ; 
def getMaxPairSum(arr, N, K):
    preMax = [0] * N
    preMax[0] = arr[0]
    for i in range(1, N):
        preMax[i] = max(preMax[i - 1], arr[i])
    res = -10 ** 8
    for i in range(K, N):
        res = max(res, arr[i] + preMax[i - K])
    return res


if __name__ == '__main__':
    arr = [1, 2, 4, 8, 6, 3]
    K = 3
    N = len(arr)
    print(getMaxPairSum(arr, N, K))

# Expected output:
9
# program to calculate sum of an infinite arithmetic | function to find the sum of the infinite agp ; stores the sum of infinite agp ; print the required sum ; 
def sumOfInfiniteAGP(a, d, r):
    ans = a / (1 - r) + (d * r) / (1 - r * r)
    print(round(ans, 6))


if __name__ == '__main__':
    a, d, r = 0, 1, 0.5
    sumOfInfiniteAGP(a, d, r)

# Expected output:
0.666667
# count pairs from an array having gcd equal to the minimum element in the pair | function to count pairs from an array having gcd equal to minimum element of that pair ; stores the resultant count ; iterate over the range [ 0 , n - 2 ] ; iterate over the range [ i + 1 , n ] ; if arr [ i ] % arr [ j ] is 0 or arr [ j ] % arr [ i ] is 0 ; increment count by 1 ; return the resultant count ; 
def countPairs(arr, N):
    count = 0
    for i in range(N - 1):
        for j in range(i + 1, N):
            if (arr[i] % arr[j] == 0 or arr[j] % arr[i] == 0):
                count += 1
    return count


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

# Expected output:
4
# count pairs from an array having gcd equal to the minimum element in the pair | python3 program for the above approach ; function to count pairs from an array having gcd equal to minimum element of that pair ; stores the resultant count ; stores the frequency of each array element ; traverse the array arr [ ] ; iterate over the map mp ; stores the array element ; stores the count of array element x ; if x is 1 ; increment res by n - 1 ; increment res by yc2 ; iterate over the range [ 2 , sqrt ( x ) ] ; if x is divisible by j ; increment the value of res by mp [ j ] ; if j is not equal to x / j ; increment res by mp [ x / j ] ; return the resultant count ; 

from math import sqrt


def CountPairs(arr, N):
    res = 0
    mp = {}
    for i in range(N):
        if (arr[i] in mp):
            mp[arr[i]] += 1
        else:
            mp[arr[i]] = 1
    for key, value in mp .items():
        x = key
        y = value
        if (x == 1):
            res += N - 1
            continue
        res += (y * (y - 1)) // 2
        for j in range(2, int(sqrt(x)) + 1, 1):
            if (x % j == 0):
                res += mp[j]
                if (j != x // j):
                    res += mp[x // j]
    return res


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

# Expected output:
4
# product of count of set bits present in binary representations of elements in an array | function to count the set bits in an integer ; stores the count of set bits ; iterate while n is not equal to 0 ; increment count by 1 ; divide n by 2 ; return the total count obtained ; function to find the product of count of set bits present in each element of an array ; stores the resultant product ; traverse the array arr [ ] ; stores the count of set bits of arr [ i ] ; update the product ; return the resultant product ; 
def countbits(n):
    count = 0
    while (n != 0):
        if (n & 1):
            count += 1
        n = n // 2
    return count


def BitProduct(arr, N):
    product = 1
    for i in range(N):
        bits = countbits(arr[i])
        product *= bits
    return product


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

# Expected output:
4