giulio98/xlcost-single-prompt huggingface dataset:
# find n numbers such that a number and its reverse are divisible by sum of its digits | function to calculate the sum of digits ; loop to iterate through every digit of the number ; returning the sum of digits ; function to calculate the reverse of a number ; loop to calculate the reverse of the number ; return the reverse of the number ; function to print the first n numbers such that every number and the reverse of the number is divisible by its sum of digits ; loop to continuously check and generate number until there are n outputs ; variable to hold the sum of the digit of the number ; computing the reverse of the number ; checking if the condition satisfies . increment the count and print the number if it satisfies . ; 
def digit_sum(n):
    sum = 0
    while (n > 0):
        m = n % 10
        sum = sum + m
        n = n // 10
    return (sum)


def reverse(n):
    r = 0
    while (n != 0):
        r = r * 10
        r = r + n % 10
        n = n // 10
    return (r)


def operation(n):
    i = 1
    count = 0
    while (count < n):
        a = digit_sum(i)
        r = reverse(i)
        if (i % a == 0 and r % a == 0):
            print(i, end=" ")
            count += 1
            i += 1
        else:
            i += 1


if __name__ == '__main__':
    n = 10
    operation(n)

# Expected output:
1 2 3 4 5 6 7 8 9 10
# split n natural numbers into two sets having gcd of their sums greater than 1 | function to create and print the two sets ; no such split possible for n <= 2 ; print the first set consisting of even elements ; print the second set consisting of odd ones ; 
def createSets(N):
    if (N <= 2):
        print("-1")
        return
    for i in range(2, N + 1, 2):
        print(i, end=" ")
    print("")
    for i in range(1, N + 1, 2):
        print(i, end=" ")


if __name__ == '__main__':
    N = 6
    createSets(N)

# Expected output:
2 4 6 
1 3 5
# count the nodes in the given tree whose weight is a powerful number | python3 implementation to count the nodes in the given tree whose weight is a powerful number ; function to check if the number is powerful ; first divide the number repeatedly by 2 ; check if only 2 ^ 1 divides n , then return false ; check if n is not a power of 2 then this loop will execute ; find highest power of " factor " that divides n ; check if only factor ^ 1 divides n , then return false ; n must be 1 now if it is not a prime number . since prime numbers are not powerful , we return false if n is not 1. ; function to perform dfs ; check if weight of the current node is a powerful number ; 

graph = [[]for i in range(100)]
weight = [0] * 100
ans = 0


def isPowerful(n):
    while (n % 2 == 0):
        power = 0
        while (n % 2 == 0):
            n /= 2
            power += 1
        if (power == 1):
            return False
    factor = 3
    while (factor * factor <= n):
        power = 0
        while (n % factor == 0):
            n = n / factor
            power += 1
        if (power == 1):
            return False
        factor += 2
    return (n == 1)


def dfs(Node, parent):
    global ans
    if (isPowerful(weight[Node])):
        ans += 1
    for to in graph[Node]:
        if (to == parent):
            continue
        dfs(to, Node)


if __name__ == '__main__':
    weight[1] = 5
    weight[2] = 10
    weight[3] = 11
    weight[4] = 8
    weight[5] = 6
    graph[1].append(2)
    graph[2].append(3)
    graph[2].append(4)
    graph[1].append(5)
    dfs(1, 1)
    print(ans)

# Expected output:
1
# Number of ways to color boundary of each block of M * N table | Function to compute all way to fill the boundary of all sides of the unit square ; Count possible ways to fill all upper and left side of the rectangle M * N ; Count possible ways to fill all side of the all squares unit size ; Number of rows ; Number of columns
def CountWays(N, M):
    count = 1
    count = pow(3, M + N)
    count *= pow(2, M * N)
    return count


N = 3
M = 2
print(CountWays(N, M))

# Expected output:
15552
# nth positive number whose absolute difference of adjacent digits is at most 1 | return nth number with absolute difference between all adjacent digits at most 1. ; to store all such numbers ; enqueue all integers from 1 to 9 in increasing order . ; perform the operation n times so that we can get all such n numbers . ; store the front element of queue , in array and pop it from queue . ; if the last digit of dequeued integer is not 0 , then enqueue the next such number . ; enqueue the next such number ; if the last digit of dequeued integer is not 9 , then enqueue the next such number . ; 
def findNthNumber(N):
    arr = [0 for i in range(N + 1)]
    q = []
    for i in range(1, 10, 1):
        q .append(i)
    for i in range(1, N + 1, 1):
        arr[i] = q[0]
        q .remove(q[0])
        if (arr[i] % 10 != 0):
            q .append(arr[i] * 10 + arr[i] % 10 - 1)
        q .append(arr[i] * 10 + arr[i] % 10)
        if (arr[i] % 10 != 9):
            q .append(arr[i] * 10 + arr[i] % 10 + 1)
    print(arr[N])


if __name__ == '__main__':
    N = 21
    findNthNumber(N)

# Expected output:
45
# unique element in an array where all elements occur k times except one | set 2 | function that find the unique element in the array arr [ ] ; store all unique element in set ; sum of all element of the array ; sum of element in the set ; print the unique element using formula ; 
def findUniqueElements(arr, N, K):
    s = set()
    for x in arr:
        s .add(x)
    arr_sum = sum(arr)
    set_sum = 0
    for x in s:
        set_sum += x
    print((K * set_sum - arr_sum) // (K - 1))


if __name__ == '__main__':
    arr = [12, 1, 12, 3, 12, 1, 1, 2, 3, 2, 2, 3, 7]
    N = len(arr)
    K = 3
    findUniqueElements(arr, N, K)

# Expected output:
7
# form the cubic equation from the given roots | function to find the cubic equation whose roots are a , b and c ; find the value of coefficient ; print the equation as per the above coefficients ; 
def findEquation(a, b, c):
    X = (a + b + c)
    Y = (a * b) + (b * c) + (c * a)
    Z = (a * b * c)
    print("x^3 - ", X, "x^2 + ", Y, "x - ", Z, " = 0")


if __name__ == '__main__':
    a = 5
    b = 2
    c = 3
    findEquation(a, b, c)

# Expected output:
x^3 -  10 x^2 +  31 x -  30  = 0
# gill 's 4th order method to solve differential equations | python3 program to implement gill 's method ; a sample differential equation " dy / dx ▁ = ▁ ( x ▁ - ▁ y ) /2" ; finds value of y for a given x using step size h and initial value y0 at x0 ; count number of iterations using step size or height h ; initial value of y ( 0 ) ; iterate for number of iteration ; value of k1 ; value of k2 ; value of k3 ; value of k4 ; find the next value of y ( n + 1 ) using y ( n ) and values of k in the above steps ; update next value of x ; return the final value of dy / dx ; 

from math import sqrt


def dydx(x, y):
    return (x - y) / 2


def Gill(x0, y0, x, h):
    n = ((x - x0) / h)
    y = y0
    for i in range(1, int(n + 1), 1):
        k1 = h * dydx(x0, y)
        k2 = h * dydx(x0 + 0.5 * h, y + 0.5 * k1)
        k3 = h * dydx(x0 + 0.5 * h, y + 0.5 * (-1 + sqrt(2))
                      * k1 + k2 * (1 - 0.5 * sqrt(2)))
        k4 = h * dydx(x0 + h, y - (0.5 * sqrt(2)) *
                      k2 + k3 * (1 + 0.5 * sqrt(2)))
        y = y + (1 / 6) * (k1 + (2 - sqrt(2)) * k2 + (2 + sqrt(2)) * k3 + k4)
        x0 = x0 + h
    return y


if __name__ == '__main__':
    x0 = 0
    y = 3.0
    x = 5.0
    h = 0.2
    print("y(x) =", round(Gill(x0, y, x, h), 6))

# Expected output:
y(x) = 3.410426
# program to print numbers from n to 1 in reverse order | recursive function to print from n to 1 ; 
def PrintReverseOrder(N):
    for i in range(N, 0, -1):
        print(i, end=" ")


if __name__ == '__main__':
    N = 5
    PrintReverseOrder(N)

# Expected output:
5 4 3 2 1
# find count of numbers from 0 to n which satisfies the given equation for a value k | function to find the values ; calculate the lcm ; calculate the multiples of lcm ; find the values which satisfies the given condition ; subtract the extra values ; return the final result ; 
def __gcd(a, b):
    if (b == 0):
        return a
    else:
        return __gcd(b, a % b)

def findAns(a, b, n):
    lcm = (a * b) // __gcd(a, b)
    multiples = (n // lcm) + 1
    answer = max(a, b) * multiples
    lastvalue = lcm * (n // lcm) + max(a, b)
    if (lastvalue > n):
        answer = answer - (lastvalue - n - 1)
    return answer


if __name__ == '__main__':
    a = 1
    b = 13
    n = 500
    print(findAns(a, b, n))

# Expected output:
501
# program to find if two numbers and their am and hm are present in an array using stl | python3 program to check if two numbers are present in an array then their am and hm are also present . finally , find the gm of the numbers ; function to find the arithmetic mean of 2 numbers ; function to find the harmonic mean of 2 numbers ; following function checks and computes the desired results based on the means ; calculate means ; hash container ( set ) to store elements ; insertion of array elements in the set ; conditionals to check if numbers are present in array by hashing ; conditionals to check if the am and hm of the numbers are present in array ; if all conditions are satisfied , the geometric mean is calculated ; if numbers are found but the respective am and hm are not found in the array ; if none of the conditions are satisfied ; 

from math import sqrt


def ArithmeticMean(A, B):
    return (A + B) / 2


def HarmonicMean(A, B):
    return (2 * A * B) / (A + B)


def CheckArithmeticHarmonic(arr, A, B, N):
    AM = ArithmeticMean(A, B)
    HM = HarmonicMean(A, B)
    Hash = set()
    for i in range(N):
        Hash .add(arr[i])
    if (A in Hash and B in Hash):
        if (AM in Hash and HM in Hash):
            print("GM =", round(sqrt(AM * HM), 2))
        else:
            print("AM and HM not found")
    else:
        print("Numbers not found")


if __name__ == '__main__':
    arr = [1.0, 2.0, 2.5, 3.0, 4.0, 4.5, 5.0, 6.0]
    N = len(arr)
    A = 3.0
    B = 6.0
    CheckArithmeticHarmonic(arr, A, B, N)

# Expected output:
GM = 4.24
# minimum decrements to make integer a divisible by integer b | function that print number of moves required ; calculate modulo ; print the required answer ; 
def movesRequired(a, b):
    total_moves = a % b
    print(total_moves)


if __name__ == '__main__':
    A = 10
    B = 3
    movesRequired(A, B)

# Expected output:
1
# pythagorean triplet with given sum using single loop | function to calculate the pythagorean triplet in o ( n ) ; iterate a from 1 to n - 1. ; calculate value of b ; the value of c = n - a - b ; 
def PythagoreanTriplet(n):
    flag = 0
    for a in range(1, n, 1):
        b = (n * n - 2 * n * a) // (2 * n - 2 * a)
        c = n - a - b
        if (a * a + b * b == c * c and b > 0 and c > 0):
            print(a, b, c)
            flag = 1
            break
    if (flag == 0):
        print("-1")
    return


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

# Expected output:
3 4 5
# check if there exists a number with x factors out of which exactly k are prime | python3 program to check if there exists a number with x factors out of which exactly k are prime ; function to check if such number exists ; to store the sum of powers of prime factors of x which determines the maximum count of numbers whose product can form x ; determining the prime factors of x ; to check if the number is prime ; if x is 1 , then we cannot form a number with 1 factor and k prime factor ( as k is atleast 1 ) ; if x itself is prime then it can be represented as a power of only 1 prime factor w0hich is x itself so we return true ; if sum of the powers of prime factors of x is greater than or equal to k , which means x can be represented as a product of k numbers , we return true ; in any other case , we return false as we cannot form a number with x factors and k prime factors ; 

from math import sqrt


def check(X, K):
    prime = 0
    temp = X
    sqr = int(sqrt(X))
    for i in range(2, sqr + 1, 1):
        while (temp % i == 0):
            temp = temp // i
            prime += 1
    if (temp > 2):
        prime += 1
    if (X == 1):
        return False
    if (prime == 1 and K == 1):
        return True
    elif (prime >= K):
        return True
    else:
        return False


if __name__ == '__main__':
    X = 4
    K = 2
    if (check(X, K)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# number of subsets with same and , or and xor values in an array | python3 implementation to find the number of subsets with equal bitwise and , or and xor values ; function to find the number of subsets with equal bitwise and , or and xor values ; traverse through all the subsets ; finding the subsets with the bits of ' i ' which are set ; computing the bitwise and ; computing the bitwise or ; computing the bitwise xor ; comparing all the three values ; 

mod = 1000000007


def countSubsets(a, n):
    answer = 0
    for i in range(1 << n):
        bitwiseAND = -1
        bitwiseOR = 0
        bitwiseXOR = 0
        for j in range(n):
            if (i & (1 << j)):
                if (bitwiseAND == -1):
                    bitwiseAND = a[j]
                else:
                    bitwiseAND &= a[j]
                bitwiseOR |= a[j]
                bitwiseXOR ^= a[j]
        if (bitwiseAND == bitwiseOR and bitwiseOR == bitwiseXOR):
            answer = (answer + 1) % mod
    return answer


if __name__ == "__main__":
    N = 6
    A = [1, 3, 2, 1, 2, 1]
    print(countSubsets(A, N))

# Expected output:
7
# count of subsets containing only the given value k | function to find the number of subsets formed by the given value k ; count is used to maintain the number of continuous k 's ; iterating through the array ; if the element in the array is equal to k ; count * ( count + 1 ) / 2 is the total number of subsets with only k as their element ; change count to 0 because other element apart from k has been found ; to handle the last set of k 's ; 
def count(arr, N, K):
    count = 0
    ans = 0
    for i in range(N):
        if (arr[i] == K):
            count = count + 1
        else:
            ans += (count * (count + 1)) // 2
            count = 0
    ans = ans + (count * (count + 1)) // 2
    return ans


if __name__ == '__main__':
    arr = [1, 0, 0, 1, 1, 0, 0]
    N = len(arr)
    K = 0
    print(count(arr, N, K))

# Expected output:
6
# ternary number system or base 3 numbers | function to convert a decimal number to a ternary number ; base case ; finding the remainder when n is divided by 3 ; recursive function to call the function for the integer division of the value n / 3 ; handling the negative cases ; function to convert the decimal to ternary ; if the number is greater than 0 , compute the ternary representation of the number ; 
def convertToTernary(N):
    if (N == 0):
        return
    x = N % 3
    N //= 3
    if (x < 0):
        N += 1
    convertToTernary(N)
    if (x < 0):
        print(x + (3 * -1), end="")
    else:
        print(x, end="")


def convert(Decimal):
    print("Ternary number of ", Decimal, " is: ", end="")
    if (Decimal != 0):
        convertToTernary(Decimal)
    else:
        print("0", end="")


if __name__ == '__main__':
    Decimal = 2747
    convert(Decimal)

# Expected output:
Ternary number of  2747  is: 10202202
# largest number less than or equal to z that leaves a remainder x when divided by y | function to get the number ; remainder can ' t ▁ be ▁ larger ▁ ▁ than ▁ the ▁ largest ▁ number , ▁ ▁ if ▁ so ▁ then ▁ answer ▁ doesn ' t exist . ; reduce number by x ; finding the possible number that is divisible by y ; this number is always <= x as we calculated over z - x ; 
def get(x, y, z):
    if (x > z):
        return -1
    val = z - x
    div = (z - x) // y
    ans = div * y + x
    return ans


x = 1
y = 5
z = 8
print(get(x, y, z))

# Expected output:
6
# count of greater elements for each element in the array | python 3 implementation of the above approach ; store the frequency of the array elements ; store the sum of frequency of elements greater than the current element ; 
def countOfGreaterElements(arr, n):
    mp = {i: 0 for i in range(1000)}
    for i in range(n):
        mp[arr[i]] += 1
    x = 0
    p = []
    q = []
    m = []
    for key, value in mp .items():
        m .append([key, value])
    m = m[::-1]
    for p in m:
        temp = p[1]
        mp[p[0]] = x
        x += temp
    for i in range(n):
        print(mp[arr[i]], end=" ")


if __name__ == '__main__':
    arr = [7, 9, 5, 2, 1, 3, 4, 8, 6]
    n = len(arr)
    countOfGreaterElements(arr, n)

# Expected output:
2 0 4 7 8 6 5 1 3
# minimum operations required to make two numbers equal | python program to find minimum operations required to make two numbers equal ; function to return the minimum operations required ; keeping b always greater ; reduce b such that gcd ( a , b ) becomes 1. ; 

import math


def minOperations(A, B):
    if (A > B):
        swap(A, B)
    B = B // math .gcd(A, B)
    return B - 1


A = 7
B = 15
print(minOperations(A, B))

# Expected output:
14
# program to determine the quadrant of a complex number | function to determine the quadrant of a complex number ; storing the index of '+ ; storing the index of '- ; finding the real part of the complex number ; finding the imaginary part of the complex number ; 
def SumofInverseDivisors(N, Sum):
    ans = float(Sum) * 1.0 / float(N)
    return round(ans, 2)


N = 9
Sum = 13
print(SumofInverseDivisors(N, Sum))

# Expected output:
1.44
# product of all subarrays of an array | function to find product of all subarrays ; variable to store the product ; compute the product while traversing for subarrays ; printing product of all subarray ; 
def product_subarrays(arr, n):
    product = 1
    for i in range(0, n):
        for j in range(i, n):
            for k in range(i, j + 1):
                product *= arr[k]
    print(product, "")


arr = [10, 3, 7]
n = len(arr)
product_subarrays(arr, n)

# Expected output:
27783000
# check if a n base number is even or odd | to return value of a char . ; function to convert a number from n base to decimal ; power of base ; decimal equivalent is str [ len - 1 ] * 1 + str [ len - 1 ] * base + str [ len - 1 ] * ( base ^ 2 ) + ... ; a digit in input number must be less than number 's base ; returns true if n is even , else odd ; 
def val(c):
    if (ord(c) >= ord('0') and ord(c) <= ord('9')):
        return ord(c) - ord('0')
    else:
        return ord(c) - ord('A') + 10


def toDeci(str, base):
    Len = len(str)
    power = 1
    num = 0
    for i in range(Len - 1, -1, -1):
        if (val(str[i]) >= base):
            print("Invalid Number")
            return -1
        num += val(str[i]) * power
        power = power * base
    return num


def isEven(num, N):
    deci = toDeci(num, N)
    return (deci % 2 == 0)


if __name__ == '__main__':
    num = "11A"
    N = 16
    if (isEven(num, N)):
        print("Even")
    else:
        print("Odd")

# Expected output:
Even
# find k distinct positive odd integers with sum n | function to find k odd positive integers such that their summ is n ; condition to check if there are enough values to check ; 
def findDistinctOddsumm(n, k):
    if ((k * k) <= n and (n + k) % 2 == 0):
        val = 1
        summ = 0
        for i in range(1, k):
            print(val, end=" ")
            summ += val
            val += 2
        print(n - summ)
    else:
        print("NO")


n = 100
k = 4
findDistinctOddsumm(n, k)

# Expected output:
1 3 5 91
# minimum number of operations to convert array a to array b by adding an integer into a subarray | function to find the minimum number of operations in which array a can be converted to array b ; loop to iterate over the array ; if both elements are equal then move to next element ; calculate the difference between two elements ; loop while the next pair of elements have same difference ; increase the number of operations by 1 ; print the number of operations required ; 
def checkArray(a, b, n):
    operations = 0
    i = 0
    while (i < n):
        if (a[i] - b[i] == 0):
            i += 1
            continue
        diff = a[i] - b[i]
        i += 1
        while (i < n and a[i] - b[i] == diff):
            i += 1
        operations += 1
    print(operations)


if __name__ == "__main__":
    a = [3, 7, 1, 4, 1, 2]
    b = [3, 7, 3, 6, 3, 2]
    size = len(a)
    checkArray(a, b, size)

# Expected output:
1
# perfect cube | python3 program to check if a number is a perfect cube using prime factors ; inserts the prime factor in hashmap if not present if present updates it 's frequency ; a utility function to find all prime factors of a given number n ; insert the number of 2 s that divide n ; n must be odd at this point so we can skip one element ; while i divides n , insert i and divide n ; this condition is to handle the case when n is a prime number greater than 2 ; function to check if a number is perfect cube ; iteration in map ; 

import math


def insertPF(primeFact, fact):
    if (fact in primeFact):
        primeFact[fact] += 1
    else:
        primeFact[fact] = 1
    return primeFact


def primeFactors(n):
    primeFact = {}
    while (n % 2 == 0):
        primeFact = insertPF(primeFact, 2)
        n = n // 2
    for i in range(3, int(math .sqrt(n)) + 1, 2):
        while (n % i == 0):
            primeFact = insertPF(primeFact, i)
            n = n // i
    if (n > 2):
        primeFact = insertPF(primeFact, n)
    return primeFact


def perfectCube(n):
    primeFact = {}
    primeFact = primeFactors(n)
    for x in primeFact:
        if (primeFact[x] % 3 != 0):
            return "No"
    return "Yes"


N = 216
print(perfectCube(N))

# Expected output:
Yes
# count ways to reach the nth stair using multiple 1 or 2 steps and a single step 3 | python3 implementation to find the number the number of ways to reach nth stair by taking 1 or 2 steps at a time and 3 rd step exactly once ; function to find the number of ways ; base case ; count of 2 - steps ; count of 1 - steps ; initial length of sequence ; expected count of 2 - steps ; loop to find the ways for every possible sequence ; 

import math


def ways(n):
    if n < 3:
        return 0
    c2 = 0
    c1 = n - 3
    l = c1 + 1
    s = 0
    exp_c2 = c1 / 2
    while exp_c2 >= c2:
        f1 = math .factorial(l)
        f2 = math .factorial(c1)
        f3 = math .factorial(c2)
        s += f1 // (f2 * f3)
        c2 += 1
        c1 -= 2
        l -= 1
    return s


if __name__ == "__main__":
    N = 7
    print(ways(N))

# Expected output:
20
# find n from the value of n ! | map to precompute and store the factorials of the numbers ; function to precompute factorial ; calculating the factorial for each i and storing in a map ; 

m = {}


def precompute():
    fact = 1
    for i in range(1, 19):
        fact = fact * i
        m[fact] = i


if __name__ == "__main__":
    precompute()
    K = 120
    print(m[K])
    K = 6
    print(m[K])

# Expected output:
5
3
# count of leap years in a given year range | function to calculate the number of leap years in range of ( 1 , year ) ; function to calculate the number of leap years in given range ; 
def calNum(year):
    return (year // 4) - (year // 100) + (year // 400)


def leapNum(l, r):
    l -= 1
    num1 = calNum(r)
    num2 = calNum(l)
    print(num1 - num2)


if __name__ == "__main__":
    l1 = 1
    r1 = 400
    leapNum(l1, r1)
    l2 = 400
    r2 = 2000
    leapNum(l2, r2)

# Expected output:
97
389