giulio98/xlcost-single-prompt huggingface dataset:
# check if a given number can be represented in given a no . of digits in any base | returns true if ' num ' can be represented using ' dig ' digits in 'base ; base case ; if there are more than 1 digits left and number is more than base , then remove last digit by doing num / base , reduce the number of digits and recur ; return true of num can be represented in ' dig ' digits in any base from 2 to 32 ; check for all bases one by one ; 
def checkUtil(num, dig, base):
    if (dig == 1 and num < base):
        return True
    if (dig > 1 and num >= base):
        return checkUtil(num / base, --dig, base)
    return False


def check(num, dig):
    for base in range(2, 33):
        if (checkUtil(num, dig, base)):
            return True
    return False


num = 8
dig = 3
if (check(num, dig)):
    print("Yes")
else:
    print("No")

# Expected output:
No
# how to compute mod of a big number ? | function to compute num ( mod a ) ; initialize result ; one by one process all digits of 'num ; 
def mod(num, a):
    res = 0
    for i in range(0, len(num)):
        res = (res * 10 + int(num[i])) % a

    return res


num = "12316767678678"
print(mod(num, 10))

# Expected output:
8
# modular multiplicative inverse | a naive method to find modulor multiplicative inverse of ' a ' under modulo 'm ; 
def modInverse(a, m):
    for x in range(1, m):
        if (((a % m) * (x % m)) % m == 1):
            return x
    return -1


a = 3
m = 11
print(modInverse(a, m))

# Expected output:
4
# modular multiplicative inverse | returns modulo inverse of a with respect to m using extended euclid algorithm assumption : a and m are coprimes , i . e . , gcd ( a , m ) = 1 ; q is quotient ; m is remainder now , process same as euclid 's algo ; update x and y ; make x positive ; 
def modInverse(a, m):
    m0 = m
    y = 0
    x = 1
    if (m == 1):
        return 0
    while (a > 1):
        q = a // m
        t = m
        m = a % m
        a = t
        t = y
        y = x - q * y
        x = t
    if (x < 0):
        x = x + m0
    return x


a = 3
m = 11
print("Modular multiplicative inverse is", modInverse(a, m))

# Expected output:
Modular multiplicative inverse is 4
# euler 's totient function | function to return gcd of a and b ; a simple method to evaluate euler totient function ; 
def gcd(a, b):
    if (a == 0):
        return b
    return gcd(b % a, a)


def phi(n):
    result = 1
    for i in range(2, n):
        if (gcd(i, n) == 1):
            result += 1
    return result


for n in range(1, 11):
    print("phi(", n, ") = ", phi(n), sep="")

# Expected output:
phi(1) = 1
phi(2) = 1
phi(3) = 2
phi(4) = 2
phi(5) = 4
phi(6) = 2
phi(7) = 6
phi(8) = 4
phi(9) = 6
phi(10) = 4
# euler 's totient function | python 3 program to calculate euler ' s ▁ totient ▁ function ▁ using ▁ euler ' s product formula ; consider all prime factors of n and for every prime factor p , multiply result with ( 1 - 1 / p ) ; check if p is a prime factor . ; if yes , then update n and result ; if n has a prime factor greater than sqrt ( n ) ( there can be at - most one such prime factor ) ; 
def phi(n):
    result = n
    p = 2
    while p * p <= n:
        if n % p == 0:
            while n % p == 0:
                n = n // p
            result = result * (1.0 - (1.0 / float(p)))
        p = p + 1
    if n > 1:
        result = result * (1.0 - (1.0 / float(n)))
    return int(result)


for n in range(1, 11):
    print("phi(", n, ") = ", phi(n))

# Expected output:
phi( 1 ) =  1
phi( 2 ) =  1
phi( 3 ) =  2
phi( 4 ) =  2
phi( 5 ) =  4
phi( 6 ) =  2
phi( 7 ) =  6
phi( 8 ) =  4
phi( 9 ) =  6
phi( 10 ) =  4
# program to find remainder without using modulo or % operator | function to return num % divisor without using % ( modulo ) operator ; while divisor is smaller than n , keep subtracting it from num ; 
def getRemainder(num, divisor):
    while (num >= divisor):
        num -= divisor
    return num


if __name__ == '__main__':
    num = 100
    divisor = 7
    print(getRemainder(num, divisor))

# Expected output:
2
# efficient program to compute sum of series 1 / 1 ! + 1 / 2 ! + 1 / 3 ! + 1 / 4 ! + . . + 1 / n ! | function to find factorial of a number ; a simple function to return value of 1 / 1 ! + 1 / 2 ! + . . + 1 / n ! ; 
def factorial(n):
    res = 1
    for i in range(2, n + 1):
        res *= i
    return res


def sum(n):
    s = 0.0
    for i in range(1, n + 1):
        s += 1.0 / factorial(i)
    print(s)


n = 5
sum(n)

# Expected output:
1.7166666666666668
# find the number of valid parentheses expressions of given length | returns value of binomial coefficient c ( n , k ) ; since c ( n , k ) = c ( n , n - k ) ; calculate value of [ n * ( n - 1 ) * -- - * ( n - k + 1 ) ] / [ k * ( k - 1 ) * -- - * 1 ] ; a binomial coefficient based function to find nth catalan number in o ( n ) time ; calculate value of 2 ncn ; return 2 ncn / ( n + 1 ) ; function to find possible ways to put balanced parenthesis in an expression of length n ; if n is odd , not possible to create any valid parentheses ; otherwise return n / 2 'th  catalan number ; 
def binomialCoeff(n, k):
    res = 1
    if (k > n - k):
        k = n - k
    for i in range(k):
        res *= (n - i)
        res /= (i + 1)
    return int(res)


def catalan(n):
    c = binomialCoeff(2 * n, n)
    return int(c / (n + 1))


def findWays(n):
    if (n & 1):
        return 0
    return catalan(int(n / 2))


n = 6
print("Total possible expressions of length", n, "is", findWays(6))

# Expected output:
Total possible expressions of length 6 is 5
# program to evaluate simple expressions | a utility function to check if a given character is operand ; utility function to find value of and operand ; this function evaluates simple expressions . it returns - 1 if the given expression is invalid . ; base case : given expression is empty ; the first character must be an operand , find its value ; traverse the remaining characters in pairs ; the next character must be an operator , and next to next an operand ; if next to next character is not an operand ; update result according to the operator ; if not a valid operator ; 
def isOperand(c):
    return (c >= '0' and c <= '9')


def value(c):
    return ord(c) - ord('0')


def evaluate(exp):
    len1 = len(exp)
    if (len1 == 0):
        return -1
    res = value(exp[0])
    for i in range(1, len1, 2):
        opr = exp[i]
        opd = exp[i + 1]
        if (isOperand(opd) == False):
            return -1
        if (opr == '+'):
            res += value(opd)
        elif (opr == '-'):
            res -= int(value(opd))
        elif (opr == '*'):
            res *= int(value(opd))
        elif (opr == '/'):
            res /= int(value(opd))
        else:
            return -1
    return res


expr1 = "1+2*5+3"
res = evaluate(expr1)
print(
    expr1,
    "is Invalid")if (
        res == -
        1)else print(
            "Value of",
            expr1,
            "is",
    res)
expr2 = "1+2*3"
res = evaluate(expr2)
print(
    expr2,
    "is Invalid")if (
        res == -
        1)else print(
            "Value of",
            expr2,
            "is",
    res)
expr3 = "4-2+6*3"
res = evaluate(expr3)
print(
    expr3,
    "is Invalid")if (
        res == -
        1)else print(
            "Value of",
            expr3,
            "is",
    res)
expr4 = "1++2"
res = evaluate(expr4)
print(
    expr4,
    "is Invalid")if (
        res == -
        1)else print(
            "Value of",
            expr4,
            "is",
    res)

# Expected output:
Value of 1+2*5+3 is 18
Value of 1+2*3 is 9
Value of 4-2+6*3 is 24
1++2 is Invalid
# program to print first n fibonacci numbers | set 1 | function to print first n fibonacci numbers ; 
def printFibonacciNumbers(n):
    f1 = 0
    f2 = 1
    if (n < 1):
        return
    print(f1, end=" ")
    for x in range(1, n):
        print(f2, end=" ")
        next = f1 + f2
        f1 = f2
        f2 = next


printFibonacciNumbers(7)

# Expected output:
0 1 1 2 3 5 8
# program to find lcm of two numbers | recursive function to return gcd of a and b ; function to return lcm of two numbers ; 
def gcd(a, b):
    if a == 0:
        return b
    return gcd(b % a, a)


def lcm(a, b):
    return (a / gcd(a, b)) * b


a = 15
b = 20
print('LCM of', a, 'and', b, 'is', lcm(a, b))

# Expected output:
LCM of 15 and 20 is 60.0
# program to convert a given number to words | a function that prints given number in words ; get number of digits in 
def convert_to_words(num):
    l = len(num)
    if (l == 0):
        print("empty string")
        return

    if (l > 4):
        print("Length more than 4 is not supported")
        return
    single_digits = ["zero", "one", "two", "three",
                     "four", "five", "six", "seven",
                     "eight", "nine"]
    two_digits = ["", "ten", "eleven", "twelve",
                  "thirteen", "fourteen", "fifteen",
                  "sixteen", "seventeen", "eighteen",
                  "nineteen"]
    tens_multiple = ["", "", "twenty", "thirty", "forty",
                     "fifty", "sixty", "seventy", "eighty",
                     "ninety"]

    tens_power = ["hundred", "thousand"]
    print(num, ":", end=" ")

    if (l == 1):
        print(single_digits[ord(num[0]) - 48])
        return

    x = 0
    while (x < len(num)):
        if (l >= 3):
            if (ord(num[x]) - 48 != 0):
                print(single_digits[ord(num[x]) - 48],
                      end=" ")
                print(tens_power[l - 3], end=" ")

            l -= 1
        else:
            if (ord(num[x]) - 48 == 1):
                sum = (ord(num[x]) - 48 +
                       ord(num[x+1]) - 48)
                print(two_digits[sum])
                return
            elif (ord(num[x]) - 48 == 2 and
                  ord(num[x + 1]) - 48 == 0):
                print("twenty")
                return

            else:
                i = ord(num[x]) - 48
                if (i > 0):
                    print(tens_multiple[i], end=" ")
                else:
                    print("", end="")
                x += 1
                if (ord(num[x]) - 48 != 0):
                    print(single_digits[ord(num[x]) - 48])
        x += 1


convert_to_words("9923")
convert_to_words("523")
convert_to_words("89")
convert_to_words("8")

# Expected output:
9923 : nine thousand nine hundred twenty three
523 : five hundred twenty three
89 : eighty nine
8 : eight
# check if a number is multiple of 5 without using / and % operators | assuming that integer takes 4 bytes , there can be maximum 10 digits in a integer ; check the last character of string ; 

MAX = 11


def isMultipleof5(n):
    s = str(n)
    l = len(s)
    if (s[l - 1] == '5' or s[l - 1] == '0'):
        return True
    return False


n = 19
if (isMultipleof5(n)):
    print(n, "is multiple of 5")
else:
    print(n, "is not a multiple of 5")

# Expected output:
19 is not a multiple of 5
# check if there are t number of continuous of blocks of 0 s or not in given binary matrix | python program for the above approach ; stores the moves in the matrix ; function to find if the current cell lies in the matrix or not ; function to perform the dfs traversal ; iterate over the direction vector ; dfs call ; function to check if it satisfy the given criteria or not ; keeps the count of cell having value as 0 ; if condition doesn 't satisfy ; keeps the track of unvisted cell having values 0 ; increasing count of black_spot ; find the gcd of n and m ; print the result ; 

import math
M = 1000000000 + 7
directions = [[0, 1], [-1, 0], [0, -1],
              [1, 0], [1, 1], [-1, -1], [-1, 1], [1, -1]]


def isInside(i, j, N, M):
    if (i >= 0 and i < N and j >= 0 and j < M):
        return True
    return False


def DFS(mat, N, M, i, j, visited):
    if (visited[i][j]):
        return
    visited[i][j] = True
    for it in directions:
        I = i + it[0]
        J = j + it[1]
        if (isInside(I, J, N, M)):
            if (mat[I][J] == 0):
                DFS(mat, N, M, I, J, visited)


def check(N, M, mat):
    black = 0
    for i in range(0, N):
        for j in range(0, M):
            if (mat[i][j] == 0):
                black += 1
    if (black < 2 * (max(N, M))):
        print("NO")
        return
    visited = []
    for i in range(0, N):
        temp = []
        for j in range(0, M):
            temp .append(False)
        visited .append(temp)
    black_spots = 0
    for i in range(0, N):
        for j in range(0, M):
            if (visited[i][j] == False and mat[i][j] == 0):
                black_spots += 1
                DFS(mat, N, M, i, j, visited)
    T = math .gcd(N, M)
    if black_spots >= T:
        print("Yes")
    else:
        print("No")


if __name__ == "__main__":
    N = 3
    M = 3
    mat = [[0, 0, 1], [1, 1, 1], [0, 0, 1]]
    check(M, N, mat)

# Expected output:
NO
# count subsequences having odd bitwise or values in an array | function to count the subsequences having odd bitwise or value ; stores count of odd elements ; stores count of even elements ; traverse the array arr [ ] ; if element is odd ; return the final answer ; 
def countSubsequences(arr):
    odd = 0
    even = 0
    for x in arr:
        if (x & 1):
            odd += 1
        else:
            even += 1
    return ((1 << odd) - 1) * (1 << even)


if __name__ == "__main__":
    arr = [2, 4, 1]
    print(countSubsequences(arr))

# Expected output:
4
# compress a binary tree from top to bottom with overlapping condition | structure of a node of th tree ; function to compress all the nodes on the same vertical line ; stores node by compressing all nodes on the current vertical line ; check if i - th bit of current bit set or not ; iterate over the range [ 0 , 31 ] ; stores count of set bits at i - th positions ; stores count of clear bits at i - th positions ; traverse the array ; if i - th bit of current element is set ; update s ; update ns ; if count of set bits at i - th position is greater than count of clear bits ; update ans ; update getbit ; function to compress all the nodes on the same vertical line with a single node that satisfies the condition ; map all the nodes on the same vertical line ; function to traverse the tree and map all the nodes of same vertical line to vertical distance ; storing the values in the map ; recursive calls on left and right subtree ; getting the range of horizontal distances ; 

class TreeNode:
    def __init__(self, val='', left=None, right=None):
        self .val = val
        self .left = left
        self .right = right


def evalComp(arr):
    ans = 0
    getBit = 1
    for i in range(32):
        S = 0
        NS = 0
        for j in arr:
            if getBit & j:
                S += 1
            else:
                NS += 1
        if S > NS:
            ans += 2 ** i
        getBit <<= 1
    print(ans, end=" ")


def compressTree(root):
    mp = {}

    def Trav(root, hd):
        if not root:
            return
        if hd not in mp:
            mp[hd] = [root .val]
        else:
            mp[hd].append(root .val)
        Trav(root .left, hd - 1)
        Trav(root .right, hd + 1)
    Trav(root, 0)
    lower = min(mp .keys())
    upper = max(mp .keys())
    for i in range(lower, upper + 1):
        evalComp(mp[i])


if __name__ == '__main__':
    root = TreeNode(5)
    root .left = TreeNode(3)
    root .right = TreeNode(2)
    root .left .left = TreeNode(1)
    root .left .right = TreeNode(4)
    root .right .left = TreeNode(1)
    root .right .right = TreeNode(2)
    compressTree(root)

# Expected output:
1 3 5 2 2
# bitwise or of bitwise and of all subsets of an array for q queries | function to find the or of and of all subsets of the array for each query ; an array to store the bits ; itearte for all the bits ; iterate over all the numbers and store the bits in bits [ ] array ; itearte over all the queries ; replace the bits of the value at arr [ queries [ p ] [ 0 ] ] with the bits of queries [ p ] [ 1 ] ; substitute the value in the array ; find or of the bits [ ] array ; print the answer ; 
def Or_of_Ands_for_each_query(arr, n, queries, q):
    bits = [0 for x in range(32)]
    for i in range(0, 32):
        for j in range(0, n):
            if ((1 << i) & arr[j]):
                bits[i] += 1
    for p in range(0, q):
        for i in range(0, 32):
            if ((1 << i) & arr[queries[p][0]]):
                bits[i] -= 1
            if (queries[p][1] & (1 << i)):
                bits[i] += 1
        arr[queries[p][0]] = queries[p][1]
        ans = 0
        for i in range(0, 32):
            if (bits[i] != 0):
                ans |= (1 << i)
        print(ans)


n = 3
q = 2
arr = [3, 5, 7]
queries = [[1, 2], [2, 1]]
Or_of_Ands_for_each_query(arr, n, queries, q)

# Expected output:
7
3
# find xor sum of bitwise and of all pairs from given two arrays | function to calculate the xor sum of all ands of all pairs on a and b ; variable to store anshu ; when there has been no and of pairs before this ; 
def XorSum(A, B, N, M):
    ans = -1
    for i in range(N):
        for j in range(M):
            if (ans == -1):
                ans = (A[i] & B[j])
            else:
                ans ^= (A[i] & B[j])
    return ans


if __name__ == '__main__':
    A = [3, 5]
    B = [2, 3]
    N = len(A)
    M = len(B)
    print(XorSum(A, B, N, M))

# Expected output:
0
# shortest path length between two given nodes such that adjacent nodes are at bit difference 2 | function to count set bits in a number ; stores count of set bits in xo ; iterate over each bits of xo ; if current bit of xo is 1 ; update count ; update xo ; function to find length of shortest path between the nodes a and b ; stores xor of a and b ; stores the count of set bits in xorval ; if cnt is an even number ; 
def countbitdiff(xo):
    count = 0
    while (xo):
        if (xo % 2 == 1):
            count += 1
        xo = xo // 2
    return count


def shortestPath(n, a, b):
    xorVal = a ^ b
    cnt = countbitdiff(xorVal)
    if (cnt % 2 == 0):
        print(cnt // 2)
    else:
        print("-1")


if __name__ == '__main__':
    n = 15
    a, b = 15, 3
    shortestPath(n, a, b)

# Expected output:
1
# modify a matrix by converting each element to xor of its digits | python3 program for the above approach ; function to calculate bitwise xor of digits present in x ; stores the bitwise xor ; while x is true ; update bitwise xor of its digits ; return the result ; function to print matrix after converting each matrix element to xor of its digits ; traverse each row of arr [ ] [ ] ; traverse each column of arr [ ] [ ] ; function to convert the given matrix to required xor matrix ; traverse each row of arr [ ] [ ] ; traverse each column of arr [ ] [ ] ; store the current matrix element ; find the xor of digits present in x ; stores the xor value ; print resultant matrix ; 

M = 3
N = 3


def findXOR(X):
    ans = 0
    while (X):
        ans ^= (X % 10)
        X //= 10
    return ans


def printXORmatrix(arr):
    for i in range(3):
        for j in range(3):
            print(arr[i][j], end=" ")
        print()


def convertXOR(arr):
    for i in range(3):
        for j in range(3):
            X = arr[i][j]
            temp = findXOR(X)
            arr[i][j] = temp
    printXORmatrix(arr)


if __name__ == '__main__':
    arr = [[27, 173, 5], [21, 6, 624], [5, 321, 49]]
    convertXOR(arr)

# Expected output:
5 5 5 
3 6 0 
5 0 13
# sum of bitwise and of sum of pairs and their bitwise and from a given array | python 3 program for the above approach ; function to find the sum of bitwise and of sum of pairs and their bitwise and from a given array ; stores the total sum ; check if jth bit is set ; stores the right shifted element by ( i + 1 ) ; update the value of x ; push in vector vec ; sort the vector in ascending order ; traverse the vector vec ; stores the value 2 ^ ( i + 1 ) - 2 ^ ( i ) - vec [ j ] ; stores count of numbers whose value > y ; update the ans ; return the ans ; 

from bisect import bisect_left
M = 1000000007


def findSum(A, N):
    ans = 0
    for i in range(30):
        vec = []
        for j in range(N):
            if ((A[j] >> i) & 1):
                X = (A[j] >> (i + 1))
                X = X * (1 << (i + 1))
                X %= M
                vec .append(A[j] - X)
        vec .sort(reverse=False)
        for j in range(len(vec)):
            Y = (1 << (i + 1)) + (1 << i) - vec[j]
            temp = vec[j + 1:]
            idx = int(bisect_left(temp, Y))
            ans += ((len(vec) - idx) * (1 << i))
            ans %= M
    ans /= 7
    print(int(ans % M))


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

# Expected output:
2
# count subarrays made up of elements having exactly k set bits | function to count the number of set bits in an integer n ; stores the count of set bits ; while n is non - zero ; if the lsb is 1 , then increment ans by 1 ; return the total set bits ; function to count the number of subarrays having made up of elements having k set bits ; stores the total count of resultant subarrays ; traverse the given array ; if the current element has k set bits ; otherwise ; increment count of subarrays ; return total count of subarrays ; 
def countSet(N):
    ans = 0
    while N:
        ans += N & 1
        N >>= 1
    return ans


def countSub(arr, k):
    ans = 0
    setK = 0
    for i in arr:
        if countSet(i) == k:
            setK += 1
        else:
            setK = 0
        ans += setK
    return ans


arr = [4, 2, 1, 5, 6]
K = 2
print(countSub(arr, K))

# Expected output:
3
# count subarrays having odd bitwise xor | function to count the number of subarrays of the given array having odd bitwise xor ; stores number of odd numbers upto i - th index ; stores number of required subarrays starting from i - th index ; store the required result ; find the number of subarrays having odd bitwise xor values starting at 0 - th index ; check if current element is odd ; if the current value of odd is not zero , increment c_odd by 1 ; find the number of subarrays having odd bitwise xor value starting at ith index and add to result ; add c_odd to result ; print the result ; 
def oddXorSubarray(a, n):
    odd = 0
    c_odd = 0
    result = 0
    for i in range(n):
        if (a[i] & 1):
            odd = not odd
        if (odd):
            c_odd += 1
    for i in range(n):
        result += c_odd
        if (a[i] & 1):
            c_odd = (n - i - c_odd)
    print(result)


if __name__ == '__main__':
    arr = [1, 4, 7, 9, 10]
    N = len(arr)
    oddXorSubarray(arr, N)

# Expected output:
8
# sum of bitwise or of every array element paired with all other array elements | function to print required sum for every valid index i ; store the required sum for current array element ; generate all possible pairs ( arr [ i ] , arr [ j ] ) ; update the value of req_sum ; print required sum ; 
def printORSumforEachElement(arr, N):
    for i in range(0, N):
        req_sum = 0
        for j in range(0, N):
            req_sum += (arr[i] | arr[j])
        print(req_sum, end=" ")


arr = [1, 2, 3, 4]
N = len(arr)
printORSumforEachElement(arr, N)

# Expected output:
12 14 16 22