giulio98/xlcost-single-prompt huggingface dataset:
# number of values of b such that a = b + ( a ^ b ) | function to return the number of solutions ; check for every possible value ; 
def countSolutions(a):
    count = 0
    for i in range(a + 1):
        if (a == (i + (a ^ i))):
            count += 1
    return count


if __name__ == "__main__":
    a = 3
    print(countSolutions(a))

# Expected output:
4
# number of values of b such that a = b + ( a ^ b ) | function to return the number of solutions ; 
def countSolutions(a):
    count = bin(a).count('1')
    return 2 ** count


if __name__ == "__main__":
    a = 3
    print(countSolutions(a))

# Expected output:
4
# sum of area of all possible square inside a rectangle | function to calculate the sum of area of all possible squares that comes inside the rectangle ; square with max size possible ; calculate total square of a given size ; calculate area of squares of a particular size ; total area ; increment size ; 
def calculateAreaSum(l, b):
    size = 1
    maxSize = min(l, b)
    totalArea = 0
    for i in range(1, maxSize + 1):
        totalSquares = ((l - size + 1) * (b - size + 1))
        area = (totalSquares * size * size)
        totalArea += area
        size += 1
    return totalArea


if __name__ == "__main__":
    l = 4
    b = 3
    print(calculateAreaSum(l, b))

# Expected output:
54
# hyperfactorial of a number | function to calculate the value of hyperfactorial ; initialise the val to 1 ; returns the hyperfactorial of a number ; 
def boost_hyperfactorial(num):
    val = 1
    for i in range(1, num + 1):
        val = val * pow(i, i)
    return val


num = 5
print(boost_hyperfactorial(num))

# Expected output:
86400000
# hyperfactorial of a number | function to calculate the value of hyperfactorial ; initialise the val to 1 ; 1 ^ 1 * 2 ^ 2 * 3 ^ 3. . . . ; returns the hyperfactorial of a number ; 
def boost_hyperfactorial(num):
    val = 1
    for i in range(1, num + 1):
        for j in range(1, i + 1):
            val *= i
    return val


num = 5
print(boost_hyperfactorial(num))

# Expected output:
86400000
# subtract 1 without arithmetic operators | python 3 code to subtract one from a given number ; flip all the set bits until we find a 1 ; flip the rightmost 1 bit ; 
def subtractOne(x):
    m = 1
    while ((x & m) == False):
        x = x ^ m
        m = m << 1
    x = x ^ m
    return x


if __name__ == '__main__':
    print(subtractOne(13))

# Expected output:
12
# find the mean vector of a matrix | python3 program to find mean vector of given matrix ; function to find mean vector ; loop to traverse each column ; to calculate mean of each row ; to store sum of elements of a column ; 

rows = 3
cols = 3


def meanVector(mat):
    print("[ ", end="")
    for i in range(rows):
        mean = 0.00
        sum = 0
        for j in range(cols):
            sum = sum + mat[j][i]
            mean = int(sum / rows)
            print(mean, end=" ")
            print("]")


mat = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
meanVector(mat)

# Expected output:
[ 0 ]
1 ]
4 ]
0 ]
2 ]
5 ]
1 ]
3 ]
6 ]
# hoax number | python3 code to check if a number is a hoax number or not . ; function to find distinct prime factors of given number n ; n is odd at this point , since it is no longer divisible by 2. so we can test only for the odd numbers , whether they are factors of n ; check if i is prime factor ; this condition is to handle the case when n is a prime number greater than 2 ; function to calculate sum of digits of distinct prime factors of given number n and sum of digits of number n and compare the sums obtained ; distinct prime factors of n are being stored in vector pf ; if n is a prime number , it cannot be a hoax number ; finding sum of digits of distinct prime factors of the number n ; finding sum of digits in current prime factor pf [ i ] . ; finding sum of digits of number n ; comparing the two calculated sums ; 

import math


def primeFactors(n):
    res = []
    if (n % 2 == 0):
        while (n % 2 == 0):
            n = int(n / 2)
        res .append(2)
    for i in range(3, int(math .sqrt(n)), 2):
        if (n % i == 0):
            while (n % i == 0):
                n = int(n / i)
            res .append(i)
    if (n > 2):
        res .append(n)
    return res


def isHoax(n):
    pf = primeFactors(n)
    if (pf[0] == n):
        return False
    all_pf_sum = 0
    for i in range(0, len(pf)):
        pf_sum = 0
        while (pf[i] > 0):
            pf_sum += pf[i] % 10
            pf[i] = int(pf[i] / 10)
        all_pf_sum += pf_sum
    sum_n = 0
    while (n > 0):
        sum_n += n % 10
        n = int(n / 10)
    return sum_n == all_pf_sum


n = 84
if (isHoax(n)):
    print("AHoaxNumber")
else:
    print("NotaHoaxNumber")

# Expected output:
AHoaxNumber
# primality test | set 5 ( using lucas | function to find out first n terms ( considering 4 as 0 th term ) of lucas - lehmer series . ; the 0 th term of the series is 4. ; create an array to store the terms . ; compute each term and add it to the array . ; print out the terms one by one . ; 
def LucasLehmer(n):
    current_val = 4
    series = []
    series .append(current_val)
    for i in range(n):
        current_val = current_val * current_val - 2
        series .append(current_val)
    for i in range(n + 1):
        print("Term", i, ":", series[i])


if __name__ == '__main__':
    n = 5
    LucasLehmer(n)

# Expected output:
Term 0 : 4
Term 1 : 14
Term 2 : 194
Term 3 : 37634
Term 4 : 1416317954
Term 5 : 2005956546822746114
# modular multiplicative inverse from 1 to n | a naive method to find modular multiplicative inverse of ' a ' under modulo 'prime ; 
def modInverse(a, prime):
    a = a % prime
    for x in range(1, prime):
        if ((a * x) % prime == 1):
            return x
    return -1


def printModIverses(n, prime):
    for i in range(1, n + 1):
        print(modInverse(i, prime), end="")


n = 10
prime = 17
printModIverses(n, prime)

# Expected output:
1961373515212
# convert to number with digits as 3 and 8 only | function for minimum operation ; remainder and operations count ; count digits not equal to 3 or 8 ; 
def minOp(num):
    count = 0
    while (num):
        rem = num % 10
        if (not (rem == 3 or rem == 8)):
            count = count + 1
        num = num // 10
    return count


num = 234198
print("Minimum Operations =", minOp(num))

# Expected output:
Minimum Operations = 4
# biggest integer which has maximum digit sum in range from 1 to n | function to calculate the sum of digits of a number . ; returns the maximum number with maximum sum of digits . ; initializing b as 1 and initial max sum to be of n ; iterates from right to left in a digit ; while iterating this is the number from right to left ; calls the function to check if sum of cur is more then of ans ; reduces the number to one unit less ; 
def sumOfDigits(a):
    sm = 0
    while (a != 0):
        sm = sm + a % 10
        a = a // 10
    return sm


def findMax(x):
    b = 1
    ans = x
    while (x != 0):
        cur = (x - 1) * b + (b - 1)
        if (sumOfDigits(cur) > sumOfDigits(ans) or (
                sumOfDigits(cur) == sumOfDigits(ans) and cur > ans)):
            ans = cur
        x = x // 10
        b = b * 10
    return ans


n = 521
print(findMax(n))

# Expected output:
499
# largest palindromic number in an array | function to check if n is palindrome ; find the appropriate divisor to extract the leading digit ; if first and last digits are not same then return false ; removing the leading and trailing digits from the number ; reducing divisor by a factor of 2 as 2 digits are dropped ; function to find the largest palindromic number ; sort the array ; if number is palindrome ; if no palindromic number found ; 
def isPalindrome(n):
    divisor = 1
    while (n / divisor >= 10):
        divisor *= 10
    while (n != 0):
        leading = n // divisor
        trailing = n % 10
        if (leading != trailing):
            return False
        n = (n % divisor) // 10
        divisor = divisor // 100
    return True


def largestPalindrome(A, n):
    A .sort()
    for i in range(n - 1, -1, -1):
        if (isPalindrome(A[i])):
            return A[i]
    return -1


if __name__ == "__main__":
    A = [1, 232, 54545, 999991]
    n = len(A)
    print(largestPalindrome(A, n))

# Expected output:
54545
# sum of the multiples of two numbers below n | function to return the sum of all the integers below n which are multiples of either a or b ; if i is a multiple of a or b ; 
def findSum(n, a, b):
    sum = 0
    for i in range(0, n, 1):
        if (i % a == 0 or i % b == 0):
            sum += i
    return sum


if __name__ == '__main__':
    n = 10
    a = 3
    b = 5
    print(findSum(n, a, b))

# Expected output:
23
# subtract 1 without arithmetic operators | ''
def subtractOne(x):
    return ((x << 1) + (~x))


print(subtractOne(13))

# Expected output:
12
# pell number | calculate nth pell number ; 
def pell(n):
    if (n <= 2):
        return n
    return (2 * pell(n - 1) + pell(n - 2))


n = 4
print(pell(n))

# Expected output:
12
# finding lcm of more than two ( or array ) numbers without using gcd | returns lcm of arr [ 0. . n - 1 ] ; find the maximum value in arr [ ] ; initialize result ; find all factors that are present in two or more array elements . x = 2 ; current factor . ; to store indexes of all array elements that are divisible by x . ; if there are 2 or more array elements that are divisible by x . ; reduce all array elements divisible by x . ; then multiply all reduced array elements ; 
def LCM(arr, n):
    max_num = 0
    for i in range(n):
        if (max_num < arr[i]):
            max_num = arr[i]
    res = 1
    x = 2
    while (x <= max_num):
        indexes = []
        for j in range(n):
            if (arr[j] % x == 0):
                indexes.append(j)
        if (len(indexes) >= 2):
            for j in range(len(indexes)):
                arr[indexes[j]] = int(arr[indexes[j]] / x)

            res = res * x
        else:
            x += 1
    for i in range(n):
        res = res * arr[i]

    return res


arr = [1, 2, 3, 4, 5, 10, 20, 35]
n = len(arr)
print(LCM(arr, n))

# Expected output:
420
# find politeness of a number | python program for the above approach ; function to find politeness ; sqrt ( 2 * n ) as max length will be when the sum starts from 1 which follows the equation n ^ 2 - n - ( 2 * sum ) = 0 ; 

import math


def politness(n):
    count = 0
    for i in range(2, int(math .sqrt(2 * n)) + 1):
        if ((2 * n) % i != 0):
            continue
        a = 2 * n
        a = a / i
        a = a - (i - 1)
        if (a % 2 != 0):
            continue
        a /= 2
        if (a > 0):
            count = count + 1
    return count


n = 90
print("Politness of ", n, " = ", politness(n))
n = 15
print("Politness of ", n, " = ", politness(n))

# Expected output:
Politness of  90  =  5
Politness of  15  =  3
# k | python program to print kth prime factor ; a function to generate prime factors of a given number n and return k - th prime factor ; find the number of 2 's that divide k ; n must be odd at this point . so we can skip one element ( note i = i + 2 ) ; while i divides n , store i and divide n ; this condition is to handle the case where n is a prime number greater than 2 ; 

import math


def kPrimeFactor(n, k):
    while (n % 2 == 0):
        k = k - 1
        n = n / 2
        if (k == 0):
            return 2
    i = 3
    while i <= math .sqrt(n):
        while (n % i == 0):
            if (k == 1):
                return i
            k = k - 1
            n = n / i
        i = i + 2
    if (n > 2 and k == 1):
        return n
    return -1


n = 12
k = 3
print(kPrimeFactor(n, k))
n = 14
k = 3
print(kPrimeFactor(n, k))

# Expected output:
3.0
-1
# find sum of divisors of all the divisors of a natural number | python3 program to find sum of divisors of all the divisors of a natural number . ; returns sum of divisors of all the divisors of n ; calculating powers of prime factors and storing them in a map mp [ ] . ; if n is a prime number ; for each prime factor , calculating ( p ^ ( a + 1 ) - 1 ) / ( p - 1 ) and adding it to answer . ; 

import math as mt


def sumDivisorsOfDivisors(n):
    mp = dict()
    for j in range(2, mt .ceil(mt .sqrt(n))):
        count = 0
        while (n % j == 0):
            n //= j
            count += 1
        if (count):
            mp[j] = count
    if (n != 1):
        mp[n] = 1
    ans = 1
    for it in mp:
        pw = 1
        summ = 0
        for i in range(mp[it] + 1, 0, -1):
            summ += (i * pw)
            pw *= it
        ans *= summ
    return ans


n = 10
print(sumDivisorsOfDivisors(n))

# Expected output:
28
# find recurring sequence in a fraction | this function returns repeating sequence of a fraction . if repeating sequence doesn 't exits, then returns empty string ; initialize result ; create a map to store already seen remainders . remainder is used as key and its position in result is stored as value . note that we need position for cases like 1 / 6. in this case , the recurring sequence doesn 't start  from first remainder. ; find first remainder ; keep finding remainder until either remainder becomes 0 or repeats ; store this remainder ; multiply remainder with 10 ; append rem / denr to result ; update remainder ; 
def fractionToDecimal(numr, denr):
    res = ""
    mp = {}
    rem = numr % denr
    while ((rem != 0) and (rem not in mp)):
        mp[rem] = len(res)
        rem = rem * 10
        res_part = rem // denr
        res += str(res_part)
        rem = rem % denr
    if (rem == 0):
        return ""
    else:
        return res[mp[rem]:]


numr, denr = 50, 22
res = fractionToDecimal(numr, denr)
if (res == ""):
    print("No recurring sequence")
else:
    print("Recurring sequence is", res)

# Expected output:
Recurring sequence is 27
# count numbers having 0 as a digit | returns 1 if x has 0 , else 0 ; traverse through all digits of x to check if it has 0. ; if current digit is 0 , return true ; returns count of numbers from 1 to n with 0 as digit ; initialize count of numbers having 0 as digit . ; traverse through all numbers and for every number check if it has 0. ; 
def has0(x):
    while (x != 0):
        if (x % 10 == 0):
            return 1
        x = x // 10
    return 0


def getCount(n):
    count = 0
    for i in range(1, n + 1):
        count = count + has0(i)
    return count


n = 107
print("Count of numbers from 1", " to ", n, " is ", getCount(n))

# Expected output:
Count of numbers from 1  to  107  is  17
# euler 's criterion (check if square root under modulo p exists) | returns true if square root of n under modulo p exists ; one by one check all numbers from 2 to p - 1 ; 
def squareRootExists(n, p):
    n = n % p
    for x in range(2, p, 1):
        if ((x * x) % p == n):
            return True
    return False


if __name__ == '__main__':
    p = 7
    n = 2
    if (squareRootExists(n, p)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# legendre 's formula (given p and n, find the largest x such that p^x divides n!) | returns largest power of p that divides n ! ; initialize result ; calculate x = n / p + n / ( p ^ 2 ) + n / ( p ^ 3 ) + ... . ; 
def largestPower(n, p):
    x = 0
    while n:
        n /= p
        x += n
    return x


n = 10
p = 3
print("Thelargestpowerof%dthatdivides%d!is%d" % (p, n, largestPower(n, p)))

# Expected output:
Thelargestpowerof3thatdivides10!is4
# program for factorial of a number | python 3 program to find factorial of given number ; single line to find factorial ; 
def factorial(n):
    return 1 if (n == 1 or n == 0)else n * factorial(n - 1)


num = 5
print("Factorial of", num, "is", factorial(num))

# Expected output:
Factorial of 5 is 120
# sum of bitwise and of each array element with the elements of another array | function to compute the and sum for each element of an array ; declaring an array of size 32 for storing the count of each bit ; traverse the array arr2 [ ] and store the count of a bit in frequency array ; current bit position ; while num is greater than 0 ; checks if ith bit is set or not ; increment the count of bit by one ; increment the bit position by one ; right shift the num by one ; traverse in the arr2 [ ] ; store the ith bit value ; total required sum ; traverse in the range [ 0 , 31 ] ; checks if current bit is set ; increment the bitwise sum by frequency [ bit_position ] * value_at_that_bit ; right shift num by one ; left shift vale_at_that_bit by one ; print sum obtained for ith number in arr1 [ ] ; 
def Bitwise_AND_sum_i(arr1, arr2, M, N):
    frequency = [0] * 32
    for i in range(N):
        bit_position = 0
        num = arr1[i]
        while (num):
            if (num & 1):
                frequency[bit_position] += 1
            bit_position += 1
            num >>= 1
    for i in range(M):
        num = arr2[i]
        value_at_that_bit = 1
        bitwise_AND_sum = 0
        for bit_position in range(32):
            if (num & 1):
                bitwise_AND_sum += frequency[bit_position] * value_at_that_bit
            num >>= 1
            value_at_that_bit <<= 1
        print(bitwise_AND_sum, end=" ")
    return


if __name__ == '__main__':
    arr1 = [1, 2, 3]
    arr2 = [1, 2, 3]
    N = len(arr1)
    M = len(arr2)
    Bitwise_AND_sum_i(arr1, arr2, M, N)

# Expected output:
2 4 6
# turn off the rightmost set bit | set 2 | unsets the rightmost set bit of n and returns the result ; checking whether bit position is set or not ; if bit position is found set , we flip this bit by xoring given number and number with bit position set ; 
def FlipBits(n):
    for bit in range(32):
        if ((n >> bit) & 1):
            n = n ^ (1 << bit)
            break
    print("The number after unsetting the", end=" ")
    print("rightmost set bit", n)


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

# Expected output:
The number after unsetting the rightmost set bit 8
# bitwise and of all the odd numbers from 1 to n | function to return the bitwise and of all the odd integers from the range [ 1 , n ] ; initialize result to 1 ; starting from 3 , bitwise and all the odd integers less than or equal to n ; 
def bitwiseAndOdd(n):
    result = 1
    for i in range(3, n + 1, 2):
        result = (result & i)
    return result


if __name__ == "__main__":
    n = 10
    print(bitwiseAndOdd(n))

# Expected output:
1
# bitwise and of all the odd numbers from 1 to n | function to return the bitwise and of all the odd integers from the range [ 1 , n ] ; 
def bitwiseAndOdd(n):
    return 1


n = 10
print(bitwiseAndOdd(n))

# Expected output:
1
# ways to split array into two groups of same xor value | return the count of number of ways to split array into two groups such that each group has equal xor value . ; we can split only if xor is 0. since xor of all is 0 , we can consider all subsets as one group . ; 
def countgroup(a, n):
    xs = 0
    for i in range(n):
        xs = xs ^ a[i]
    if xs == 0:
        return (1 << (n - 1)) - 1
    return 0


a = [1, 2, 3]
n = len(a)
print(countgroup(a, n))

# Expected output:
3
# extract ' k ' bits from a 
def bitExtracted(number, k, p):
    return (((1 << k) - 1) & (number >> (p - 1)))


number = 171
k = 5
p = 2
print("The extracted number is ", bitExtracted(number, k, p))

# Expected output:
The extracted number is  21
# maximize a given unsigned number number by swapping bits at it 's extreme positions. | python 3 program to find maximum number by swapping extreme bits . ; traverse bits from both extremes ; obtaining i - th and j - th bits ; swapping the bits if lesser significant is greater than higher significant bit and accordingly modifying the number ; 
def findMax(num):
    num_copy = num
    j = 4 * 8 - 1
    i = 0
    while (i < j):
        m = (num_copy >> i) & 1
        n = (num_copy >> j) & 1
        if (m > n):
            x = (1 << i | 1 << j)
            num = num ^ x
        i += 1
        j -= 1
    return num


if __name__ == "__main__":
    num = 4
    print(findMax(num))

# Expected output:
536870912
# efficiently check whether n is a multiple of 4 or not | function to check whether ' n ' is a multiple of 4 or not ; if true , then ' n ' is a multiple of 4 ; else ' n ' is not a multiple of 4 ; 
def isAMultipleOf4(n):
    if ((n & 3) == 0):
        return "Yes"
    return "No"


if __name__ == "__main__":
    n = 16
    print(isAMultipleOf4(n))

# Expected output:
Yes
# calculate square of a number without using * , / and pow ( ) | simple solution to calculate square without using * and pow ( ) ; handle negative input ; initialize result ; add n to res n - 1 times ; 
def square(n):
    if (n < 0):
        n = -n
    res = n
    for i in range(1, n):
        res += n
    return res


for n in range(1, 6):
    print("n =", n, end=", ")
    print("n^2 =", square(n))

# Expected output:
n = 1, n^2 = 1
n = 2, n^2 = 4
n = 3, n^2 = 9
n = 4, n^2 = 16
n = 5, n^2 = 25
# find a point that lies inside exactly k given squares | python 3 implementation of the above approach ; 
def PointInKSquares(n, a, k):
    a .sort()
    return a[n - k]


if __name__ == "__main__":
    k = 2
    a = [1, 2, 3, 4]
    n = len(a)
    x = PointInKSquares(n, a, k)
    print("(", x, ",", x, ")")

# Expected output:
( 3 , 3 )
# number of n digit stepping numbers | space optimized solution | function that calculates the answer ; dp [ j ] stores count of i digit stepping numbers ending with digit j . ; to store resu1lt of length i - 1 before updating dp [ j ] for length i . ; if n is 1 then answer will be 10. ; initialize values for count of digits equal to 1. ; compute values for count of digits more than 1. ; if ending digit is 0 ; if ending digit is 9 ; for other digits . ; stores the final answer ; 
def answer(n):
    dp = [0] * 10
    prev = [0] * 10
    if (n == 1):
        return 10
    for j in range(0, 10):
        dp[j] = 1
    for i in range(2, n + 1):
        for j in range(0, 10):
            prev[j] = dp[j]
        for j in range(0, 10):
            if (j == 0):
                dp[j] = prev[j + 1]
            elif (j == 9):
                dp[j] = prev[j - 1]
            else:
                dp[j] = prev[j - 1] + prev[j + 1]
    sum = 0
    for j in range(1, 10):
        sum = sum + dp[j]
    return sum


n = 2
print(answer(n))

# Expected output:
17
# minimum changes required to make a catalan sequence | python3 implementation of the approach ; to store first n catalan numbers ; function to find first n catalan numbers ; initialize first two values in table ; fill entries in catalan [ ] using recursive formula ; function to return the minimum changes required ; find first n catalan numbers ; a and b are first two catalan sequence numbers ; insert first n catalan elements to set ; if catalan element is present in the array then remove it from set ; return the remaining number of elements in the set ; 

MAX = 100000
catalan = [0] * MAX


def catalanDP(n):
    catalan[0] = catalan[1] = 1
    for i in range(2, n + 1):
        catalan[i] = 0
        for j in range(i):
            catalan[i] += (catalan[j] * catalan[i - j - 1])


def CatalanSequence(arr, n):
    catalanDP(n)
    s = set()
    a = 1
    b = 1
    s .add(a)
    if (n >= 2):
        s .add(b)
    for i in range(2, n):
        s .add(catalan[i])
    temp = set()
    for i in range(n):
        if arr[i] in s:
            temp .add(arr[i])
    s = s - temp
    return len(s)


if __name__ == "__main__":
    arr = [1, 1, 2, 5, 41]
    n = len(arr)
    print(CatalanSequence(arr, n))

# Expected output:
1
# sum of all the composite numbers from odd indices of the given array | function to print the sum of all composite numbers in the array ; iterate for odd indices in the array ; check if the number is composite then add it to sum ; return the sum ; function to check for composite numbers ; check if the factors are greater than 2 ; check if the number is composite or not ; 
def composite(n):
    flag = 0
    c = 0
    for j in range(1, n + 1):
        if (n % j == 0):
            c += 1
    if (c >= 3):
        flag = 1
    return flag


def odd_indices(arr):
    sum = 0
    for k in range(0, len(arr), 2):
        check = composite(arr[k])
        sum += arr[k]if check == 1 else 0
    print(sum)


if __name__ == "__main__":
    arr = [13, 5, 8, 16, 25]
    odd_indices(arr)

# Expected output:
33