giulio98/xlcost-single-prompt huggingface dataset:
# find whether a given integer is a power of 3 or not | returns true if n is power of 3 , else false ; the maximum power of 3 value that integer can hold is 1162261467 ( 3 ^ 19 ) . ; 
def check(n):
    return 1162261467 % n == 0


n = 9
if (check(n)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# demlo number ( square of 11. . .1 ) | to return demlo number length of s is smaller than 10 ; add numbers to res upto size of s then add in reverse ; 
def printDemlo(s):
    l = len(s)
    res = ""
    for i in range(1, l + 1):
        res = res + str(i)
    for i in range(l - 1, 0, -1):
        res = res + str(i)
    return res


s = "111111"
print(printDemlo(s))

# Expected output:
12345654321
# number of times a number can be replaced by the sum of its digits until it only contains one digit | python 3 program to count number of times we need to add digits to get a single digit . ; here the count variable store how many times we do sum of digits and temporary_sum always store the temporary sum we get at each iteration . ; in this loop we always compute the sum of digits in temporary_ sum variable and convert it into string str till its length become 1 and increase the count in each iteration . ; computing sum of its digits ; converting temporary_sum into string str again . ; increase the count ; 
def NumberofTimes(s):
    temporary_sum = 0
    count = 0
    while (len(s) > 1):
        temporary_sum = 0
        for i in range(len(s)):
            temporary_sum += (ord(s[i]) - ord('0'))
        s = str(temporary_sum)
        count += 1
    return count


if __name__ == "__main__":
    s = "991"
    print(NumberofTimes(s))

# Expected output:
3
# climb n | function to calculate leaps ; 
def calculateLeaps(n):
    if n == 0 or n == 1:
        return 1
    else:
        leaps = 0
        for i in range(0, n):
            leaps = leaps + calculateLeaps(i)
        return leaps


print(calculateLeaps(4))

# Expected output:
8
# print last k digits of a ^ b ( a raised to power b ) | iterative function to calculate ( x ^ y ) % p in o ( log y ) ; initialize result ; x = x % p update x if it is more than or equal to p ; if y is odd , multiply x with result ; y must be even now y = y >> 1 y = y / 2 ; function to calculate number of digits in x ; function to print last k digits of a ^ b ; generating 10 ^ k ; calling modular exponentiation ; printing leftmost zeros . since ( a ^ b ) % k can have digits less then k . in that case we need to print zeros ; if temp is not zero then print temp if temp is zero then already printed ; 
def power(x, y, p):

    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 numberOfDigits(x):

    i = 0
    while (x):
        x //= 10
        i += 1

    return i


def printLastKDigits(a, b, k):

    print("Last " + str(k)+" digits of " +
          str(a) + "^" + str(b), end=" = ")
    temp = 1
    for i in range(1, k + 1):
        temp *= 10
    temp = power(a, b, temp)
    for i in range(k - numberOfDigits(temp)):
        print("0")
    if (temp):
        print(temp)


if __name__ == "__main__":
    a = 11
    b = 3
    k = 2
    printLastKDigits(a, b, k)

# Expected output:
Last 2 digits of 11^3 = 31
# adam number | to reverse digits of numbers ; to square number ; to check adam number ; square first number and square reverse digits of second number ; if reverse of b equals a then given number is adam number ; 
def reverseDigits(num):
    rev = 0
    while (num > 0):
        rev = rev * 10 + num % 10
        num //= 10

    return rev


def square(num):
    return (num * num)


def checkAdamNumber(num):
    a = square(num)
    b = square(reverseDigits(num))
    if (a == reverseDigits(b)):
        return True
    else:
        return False


num = 13
if checkAdamNumber(num):
    print("Adam Number")
else:
    print("Not a Adam Number")

# Expected output:
Adam Number
# compute the parity of a number using xor and table look | generating the look - up table while pre - processing ; look_up is the macro expansion to generate the table ; function to find the parity ; number is considered to be of 32 bits ; dividing the number o 8 - bit chunks while performing x - or ; masking the number with 0xff ( 11111111 ) to produce valid 8 - bit result ; 
def P2(n, table):
    table .extend([n, n ^ 1, n ^ 1, n])


def P4(n, table):
    return (P2(n, table), P2(n ^ 1, table), P2(n ^ 1, table), P2(n, table))


def P6(n, table):
    return (P4(n, table), P4(n ^ 1, table), P4(n ^ 1, table), P4(n, table))


def LOOK_UP(table):
    return (P6(0, table), P6(1, table), P6(1, table), P6(0, table))


table = [0] * 256
LOOK_UP(table)


def Parity(num):
    max = 16
    while (max >= 8):
        num = num ^ (num >> max)
        max = max // 2
    return table[num & 0xff]


if __name__ == "__main__":
    num = 1742346774
    result = Parity(num)
    print("Odd Parity")if result else print("Even Parity")

# Expected output:
Even Parity
# count total number of digits from 1 to n | python3 program to count total number of digits we have to write from 1 to n ; number_of_digits store total digits we have to write ; in the loop we are decreasing 0 , 9 , 99 ... from n till ( n - i + 1 ) is greater than 0 and sum them to number_of_digits to get the required sum ; 
def totalDigits(n):
    number_of_digits = 0
    for i in range(1, n, 10):
        number_of_digits = (number_of_digits + (n - i + 1))
    return number_of_digits


n = 13
s = totalDigits(n) + 1
print(s)

# Expected output:
17
# numbers with exactly 3 divisors | generates all primes upto n and prints their squares ; if prime [ p ] is not changed , then it is a prime ; update all multiples of p ; print squares of primes upto n . ; 
def numbersWith3Divisors(n):
    prime = [True] * (n + 1)
    prime[0] = prime[1] = False
    p = 2
    while (p * p <= n):
        if (prime[p]):
            for i in range(p * 2, n + 1, p):
                prime[i] = False
        p += 1
    print("Numbers with 3 divisors :")
    i = 0
    while (i * i <= n):
        if (prime[i]):
            print(i * i, end=" ")
        i += 1


n = 96
numbersWith3Divisors(n)

# Expected output:
Numbers with 3 divisors :
4 9 25 49
# program for decimal to hexadecimal conversion | function to convert decimal to hexadecimal ; char array to store hexadecimal number ; counter for hexadecimal number array ; temporary variable to store remainder ; storing remainder in temp variable . ; check if temp < 10 ; printing hexadecimal number array in reverse order ; 
def decToHexa(n):
    hexaDeciNum = ['0'] * 100
    i = 0
    while (n != 0):
        temp = 0
        temp = n % 16
        if (temp < 10):
            hexaDeciNum[i] = chr(temp + 48)
            i = i + 1
        else:
            hexaDeciNum[i] = chr(temp + 55)
            i = i + 1
        n = int(n / 16)
    j = i - 1
    while (j >= 0):
        print((hexaDeciNum[j]), end="")
        j = j - 1


n = 2545
decToHexa(n)

# Expected output:
9F1
# program for decimal to binary conversion | function to convert decimal to binary ; array to store binary number ; counter for binary array ; storing remainder in binary array ; printing binary array in reverse order ; 
def decToBinary(n):
    binaryNum = [0] * n
    i = 0
    while (n > 0):
        binaryNum[i] = n % 2
        n = int(n / 2)
        i += 1
    for j in range(i - 1, -1, -1):
        print(binaryNum[j], end="")


n = 17
decToBinary(n)

# Expected output:
10001
# break the number into three parts | function to count number of ways to make the given number n ; 
def count_of_ways(n):
    count = 0
    count = (n + 1) * (n + 2) // 2
    return count


n = 3
print(count_of_ways(n))

# Expected output:
10
# number of groups of sizes two or three divisible by 3 | program to find groups of 2 or 3 whose sum is divisible by 3 ; initialize groups to 0 ; increment group with specified remainder ; return groups using the formula ; 
def numOfCombinations(arr, N):
    C = [0, 0, 0]
    for i in range(N):
        C[arr[i] % 3] = C[arr[i] % 3] + 1
    return (C[1] * C[2] + C[0] * (C[0] - 1) / 2 + C[0] * (C[0] - 1) * (C[0] - 2) / 6 + C[1]
            * (C[1] - 1) * (C[1] - 2) / 6 + C[2] * (C[2] - 1) * (C[2] - 2) / 6 + C[0] * C[1] * C[2])


arr1 = [1, 5, 7, 2, 9, 14]
print(int(numOfCombinations(arr1, 6)))
arr2 = [3, 6, 9, 12]
print(int(numOfCombinations(arr2, 4)))

# Expected output:
13
10
# check if a number can be written as a sum of ' k ' prime numbers | checking if a number is prime or not ; check for numbers from 2 to sqrt ( x ) if it is divisible return false ; returns true if n can be written as sum of k primes ; n < 2 k directly return false ; if k = 1 return value depends on primality of n ; if n is even directly return true ; ; if n is odd , then one prime must be 2. all other primes are odd and cannot have a pair sum as even . ; if k >= 3 return true ; ; 
def isprime(x):
    i = 2
    while (i * i <= x):
        if (x % i == 0):
            return 0
        i += 1
    return 1


def isSumOfKprimes(N, K):
    if (N < 2 * K):
        return 0
    if (K == 1):
        return isprime(N)
    if (K == 2):
        if (N % 2 == 0):
            return 1
        return isprime(N - 2)
    return 1


n = 15
k = 2
if (isSumOfKprimes(n, k)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# count total divisors of a or b in a given range | utility function to find gcd of two numbers ; utility function to find lcm of two numbers ; function to calculate all divisors in given range ; find lcm of a and b ; find common divisor by using lcm ; 
def __gcd(x, y):
    if x > y:
        small = y
    else:
        small = x
    for i in range(1, small + 1):
        if ((x % i == 0) and (y % i == 0)):
            gcd = i
    return gcd


def FindLCM(a, b):
    return (a * b) / __gcd(a, b)


def rangeDivisor(m, n, a, b):
    lcm = FindLCM(a, b)
    a_divisor = int(n / a - (m - 1) / a)
    b_divisor = int(n / b - (m - 1) / b)
    common_divisor = int(n / lcm - (m - 1) / lcm)
    ans = a_divisor + b_divisor - common_divisor
    return ans


m = 3
n = 11
a = 2
b = 3
print(rangeDivisor(m, n, a, b))
m = 11
n = 1000000
a = 6
b = 35
print(rangeDivisor(m, n, a, b))

# Expected output:
6
190475
# numbers having unique ( or distinct ) digits | function to print unique digit numbers in range from l to r . ; start traversing the numbers ; find digits and maintain its hash ; if a digit occurs more than 1 time then break ; num will be 0 only when above loop doesn 't get  break that means the  number is unique so  print it. ; 
def printUnique(l, r):
    for i in range(l, r + 1):
        num = i
        visited = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
        while (num):
            if visited[num % 10] == 1:
                break
            visited[num % 10] = 1
            num = (int)(num / 10)
        if num == 0:
            print(i, end=" ")


l = 1
r = 20
printUnique(l, r)

# Expected output:
1 2 3 4 5 6 7 8 9 10 12 13 14 15 16 17 18 19 20
# fibonacci modulo p | returns position of first fibonacci number whose modulo p is 0. ; add previous two remainders and then take its modulo p . ; 
def findMinZero(p):
    first = 1
    second = 1
    number = 2
    next = 1
    while (next):
        next = (first + second) % p
        first = second
        second = next
        number = number + 1
    return number


if __name__ == '__main__':
    p = 7
    print("Minimal zero is:", findMinZero(p))

# Expected output:
Minimal zero is: 8
# perfect cubes in a range | a simple method to count cubes between a and b ; traverse through all numbers in given range and one by one check if number is prime ; check if current number ' i ' is perfect cube ; 
def printCubes(a, b):
    for i in range(a, b + 1):
        j = 1
        for j in range(j ** 3, i + 1):
            if (j ** 3 == i):
                print(j ** 3, end=" ")
                break


a = 1
b = 100
print("Perfect cubes in given range: ")
printCubes(a, b)

# Expected output:
Perfect cubes in given range: 
1 8 27 64
# converting a real number ( between 0 and 1 ) to binary string | function to convert binary real number to string ; check if the number is between 0 to 1 or not ; setting a limit on length : 32 characters . ; compare the number to .5 ; now it become 0.25 ; 
def toBinary(n):
    if (n >= 1 or n <= 0):
        return "ERROR"
    frac = 0.5
    answer = "."
    while (n > 0):
        if (len(answer) >= 32):
            return "ERROR"
        if (n >= frac):
            answer += "1"
            n = n - frac
        else:
            answer += "0"
        frac = (frac / 2)
    return answer


n = 0.625
result = toBinary(n)
print("( 0", result, ") in base 2")
m = 0.72
result = toBinary(m)
print("(", result, ")")

# Expected output:
( 0 .101 ) in base 2
( ERROR )
# given a number n , find the first k digits of n ^ n | function that manually calculates n ^ n and then removes digits until k digits remain ; loop will terminate when there are only k digits left ; 
def firstkdigits(n, k):
    product = 1
    for i in range(n):
        product *= n
    while ((product // pow(10, k)) != 0):
        product = product // 10
    return product


n = 15
k = 4
print(firstkdigits(n, k))

# Expected output:
4378
# check if a large number is divisible by 9 or not | function to find that number divisible by 9 or not ; compute sum of digits ; check if sum of digits is divisible by 9. ; 
def check(st):
    n = len(st)
    digitSum = 0
    for i in range(0, n):
        digitSum = digitSum + (int)(st[i])
    return (digitSum % 9 == 0)


st = "99333"
if (check(st)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# xor of all subarray xors | set 1 | returns xor of all subarray xors ; initialize result by 0 as ( a xor 0 = a ) ; select the starting element ; select the ending element ; do xor of elements in current subarray ; 
def getTotalXorOfSubarrayXors(arr, N):
    res = 0
    for i in range(0, N):
        for j in range(i, N):
            for k in range(i, j + 1):
                res = res ^ arr[k]
    return res


arr = [3, 5, 2, 4, 6]
N = len(arr)
print(getTotalXorOfSubarrayXors(arr, N))

# Expected output:
7
# xor of all subarray xors | set 1 | returns xor of all subarray xors ; initialize result by 0 as ( a xor 0 = a ) ; loop over all elements once ; get the frequency of current element ; if frequency is odd , then include it in the result ; return the result ; 
def getTotalXorOfSubarrayXors(arr, N):
    res = 0
    for i in range(0, N):
        freq = (i + 1) * (N - i)
        if (freq % 2 == 1):
            res = res ^ arr[i]
    return res


arr = [3, 5, 2, 4, 6]
N = len(arr)
print(getTotalXorOfSubarrayXors(arr, N))

# Expected output:
7
# refactorable number | function to count all divisors ; initialize result ; if divisors are equal , count only one ; else : otherwise count both ; 

import math


def isRefactorableNumber(n):
    divCount = 0

    for i in range(1, int(math.sqrt(n))+1):

        if n % i == 0:
            if n/i == i:
                divCount += 1

            else:
                divCount += 2

    return n % divCount == 0


n = 8
if isRefactorableNumber(n):
    print("yes")
else:
    print("no")
n = 14
if (isRefactorableNumber(n)):
    print("yes")
else:
    print("no")

# Expected output:
yes
no
# count all perfect divisors of a number | python3 implementation of naive method to count all perfect divisors ; utility function to check perfect square number ; function to count all perfect divisors ; initialize result ; consider every number that can be a divisor of n ; if i is a divisor ; 

import math


def isPerfectSquare(x):
    sq = (int)(math .sqrt(x))
    return (x == sq * sq)


def countPerfectDivisors(n):
    cnt = 0
    for i in range(1, (int)(math .sqrt(n)) + 1):
        if (n % i == 0):
            if isPerfectSquare(i):
                cnt = cnt + 1
            if n / i != i and isPerfectSquare(n / i):
                cnt = cnt + 1
    return cnt


print("Total perfect divisor of 16 = ", countPerfectDivisors(16))
print("Total perfect divisor of 12 = ", countPerfectDivisors(12))

# Expected output:
Total perfect divisor of 16 =  3
Total perfect divisor of 12 =  2
# nearest element with at | python 3 program to print nearest element with at least one common prime factor . ; loop covers the every element of arr [ ] ; loop that covers from 0 to i - 1 and i + 1 to n - 1 indexes simultaneously ; print position of closest element ; 

import math


def nearestGcd(arr, n):
    for i in range(n):
        closest = -1
        j = i - 1
        k = i + 1
        while j > 0 or k <= n:
            if (j >= 0 and math .gcd(arr[i], arr[j]) > 1):
                closest = j + 1
                break
            if (k < n and math .gcd(arr[i], arr[k]) > 1):
                closest = k + 1
                break
            k += 1
            j -= 1
        print(closest, end=" ")


if __name__ == "__main__":
    arr = [2, 9, 4, 3, 13]
    n = len(arr)
    nearestGcd(arr, n)

# Expected output:
3 4 1 2 -1
# count of binary digit numbers smaller than n | python3 program to count all binary digit numbers smaller than n ; method returns count of binary digit numbers smaller than n ; queue to store all intermediate binary digit numbers ; binary digits start with 1 ; loop until we have element in queue ; push next binary digit numbers only if current popped element is n ; uncomment below line to print actual number in sorted order ; 

from collections import deque


def countOfBinaryNumberLessThanN(N):
    q = deque()
    q .append(1)
    cnt = 0
    while (q):
        t = q .popleft()
        if (t <= N):
            cnt = cnt + 1
            q .append(t * 10)
            q .append(t * 10 + 1)
    return cnt


if __name__ == '__main__':
    N = 200
    print(countOfBinaryNumberLessThanN(N))

# Expected output:
7
# sum of product of x and y such that floor ( n / x ) = y | return the sum of product x * y ; iterating x from 1 to n ; finding y = n / x . ; adding product of x and y to answer . ; 
def sumofproduct(n):
    ans = 0
    for x in range(1, n + 1):
        y = int(n / x)
        ans += (y * x)
    return ans


n = 10
print(sumofproduct(n))

# Expected output:
87
# find the first natural number whose factorial is divisible by x | function for calculating factorial ; function for check special_factorial_number ; call fact function and the modulo with k and check if condition is true then return i ; 
def fact(n):
    num = 1
    for i in range(1, n + 1):
        num = num * i
    return num


def Special_Factorial_Number(k):
    for i in range(1, k + 1):
        if (fact(i) % k == 0):
            return i
    return 0


if __name__ == '__main__':
    k = 16
    print(Special_Factorial_Number(k))

# Expected output:
6
# program for chocolate and wrapper puzzle | returns maximum number of chocolates we can eat with given money , price of chocolate and number of wrapprices required to get a chocolate . ; corner case ; first find number of chocolates that can be purchased with the 
def countMaxChoco(money, price, wrap):
    if (money < price):
        return 0
    choc = int(money / price)
    choc = choc + (choc - 1) / (wrap - 1)
    return int(choc)


money = 15
price = 1
wrap = 3
print(countMaxChoco(money, price, wrap))

# Expected output:
22
# check if possible to move from given coordinate to desired coordinate | python program to check if it is possible to reach ( a , b ) from ( x , y ) . returns gcd of i and j ; returns true if it is possible to go to ( a , b ) from ( x , y ) ; find absolute values of all as sign doesn 't  matter. ; if gcd is equal then it is possible to reach . else not possible . ; 
def gcd(i, j):
    if (i == j):
        return i
    if (i > j):
        return gcd(i - j, j)
    return gcd(i, j - i)


def ispossible(x, y, a, b):
    x, y, a, b = abs(x), abs(y), abs(a), abs(b)
    return (gcd(x, y) == gcd(a, b))


x, y = 35, 15
a, b = 20, 25
if (ispossible(x, y, a, b)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# lcm of first n natural numbers | to calculate hcf ; to calculate lcm ; lcm ( a , b ) = ( a * b ) hcf ( a , b ) ; assign a = lcm of n , n - 1 ; b = b - 1 ; 
def hcf(a, b):
    if (b == 0):
        return a
    return hcf(b, a % b)


def findlcm(a, b):
    if (b == 1):
        return a
    a = (a * b) // hcf(a, b)
    b -= 1
    return findlcm(a, b)


n = 7
if (n < 3):
    print(n)
else:
    print(findlcm(n, n - 1))

# Expected output:
420
# smallest number divisible by first n numbers | python program to find the smallest number evenly divisible by all number 1 to n ; returns the lcm of first n numbers ; 

import math


def lcm(n):
    ans = 1
    for i in range(1, n + 1):
        ans = int((ans * i) / math .gcd(ans, i))
    return ans


n = 20
print(lcm(n))

# Expected output:
232792560
# Trapezoidal Rule for Approximate Value of Definite Integral | A sample function whose definite integral 's approximate value is computed using Trapezoidal rule ; Declaring the function f ( x ) = 1 / ( 1 + x * x ) ; Function to evaluate the value of integral ; Grid spacing ; Computing sum of first and last terms in above formula ; Adding middle terms in above formula ; h / 2 indicates ( b - a ) / 2 n . Multiplying h / 2 with s . ; Range of definite integral ; Number of grids . Higher value means more accuracy
def y(x):
    return (1 / (1 + x * x))


def trapezoidal(a, b, n):
    h = (b - a) / n
    s = (y(a) + y(b))
    i = 1
    while i < n:
        s += 2 * y(a + i * h)
        i += 1
    return ((h / 2) * s)


x0 = 0
xn = 1
n = 6
print("Value of integral is ", "%.4f" % trapezoidal(x0, xn, n))

# Expected output:
Value of integral is  0.7842
# breaking an integer to get maximum product | the main function that returns the max possible product ; n equals to 2 or 3 must be handled explicitly ; keep removing parts of size 3 while n is greater than 4 ; keep multiplying 3 to res ; the last part multiplied by previous parts ; 
def maxProd(n):
    if (n == 2 or n == 3):
        return (n - 1)
    res = 1
    while (n > 4):
        n -= 3
        res *= 3
    return (n * res)


if __name__ == '__main__':
    print("Maximum Product is", maxProd(45))

# Expected output:
Maximum Product is 14348907
# number of elements with odd factors in given range | function to count odd squares ; 
def countOddSquares(n, m):
    return int(m ** 0.5) - int((n - 1)**0.5)


n = 5
m = 100
print("Count is", countOddSquares(n, m))

# Expected output:
Count is 8
# check if a number exists having exactly n factors and k prime factors | function to compute the number of factors of the given number ; vector to store the prime factors ; while there are no two multiples in the number , divide it by 2 ; if the size is already greater than k , then return true ; computing the remaining divisors of the number ; if n is divisible by i , then it is a divisor ; if the size is already greater than k , then return true ; if the size is already greater than k , then return true ; if none of the above conditions satisfies , then return false ; function to check if it is possible to make a number having total n factors and k prime factors ; if total divisors are less than the number of prime divisors , then print no ; find the number of factors of n ; 
def factors(n, k):
    v = []
    while (n % 2 == 0):
        v .append(2)
        n //= 2
    if (len(v) >= k):
        return True
    for i in range(3, int(n ** (1 / 2)), 2):
        while (n % i == 0):
            n = n // i
            v .append(i)
        if (len(v) >= k):
            return True
    if (n > 2):
        v .append(n)
    if (len(v) >= k):
        return True
    return False


def operation(n, k):
    answered = False
    if (n < k):
        answered = True
        print("No")
    ok = factors(n, k)
    if (not ok and not answered):
        answered = True
        print("No")
    if (ok and not answered):
        print("Yes")


if __name__ == "__main__":
    n = 4
    k = 2
    operation(n, k)

# Expected output:
Yes