giulio98/xlcost-single-prompt huggingface dataset:
# program for weighted mean of natural numbers . | returns weighted mean assuming for numbers 1 , 2 , . . n and weights 1 , 2 , . . n ; 
def weightedMean(n):
    return (2 * n + 1) / 3


n = 10
print(int(weightedMean(n)))

# Expected output:
7
# divide every element of one array by other array elements | python3 program to find quotient of arrayelements ; function to calculate the quotient of every element of the array ; calculate the product of all elements ; to calculate the quotient of every array element ; 

import math


def calculate(a, b, n, m):
    mul = 1
    for i in range(m):
        if (b[i] != 0):
            mul = mul * b[i]
    for i in range(n):
        x = math .floor(a[i] / mul)
        print(x, end=" ")


a = [5, 100, 8]
b = [2, 3]
n = len(a)
m = len(b)
calculate(a, b, n, m)

# Expected output:
0 16 1
# largest power of k in n ! ( factorial ) where k may not be prime | python3 program to find the largest power of k that divides n ! ; to find the power of a prime p in factorial n ; calculating floor ( n / r ) and adding to the count ; increasing the power of p from 1 to 2 to 3 and so on ; returns all the prime factors of k ; vector to store all the prime factors along with their number of occurrence in factorization of k ; returns largest power of k that divides n ! ; calculating minimum power of all the prime factors of k ; 

import sys


def findPowerOfP(n, p):
    count = 0
    r = p
    while (r <= n):
        count += (n // r)
        r = r * p
    return count


def primeFactorsofK(k):
    ans = []
    i = 2
    while k != 1:
        if k % i == 0:
            count = 0
            while k % i == 0:
                k = k // i
                count += 1
            ans .append([i, count])
        i += 1
    return ans


def largestPowerOfK(n, k):
    vec = primeFactorsofK(k)
    ans = sys .maxsize
    for i in range(len(vec)):
        ans = min(ans, findPowerOfP(n, vec[i][0]) // vec[i][1])
    return ans


print(largestPowerOfK(7, 2))
print(largestPowerOfK(10, 9))

# Expected output:
4
2
# minimum number of bombs | function to print where to shoot ; no . of bombs required ; bomb all the even positions ; bomb all the odd positions ; bomb all the even positions again ; 
def bomb_required(n):
    print(n + n // 2)
    for i in range(2, n + 1, 2):
        print(i, end=" ")
    for i in range(1, n + 1, 2):
        print(i, end=" ")
    for i in range(2, n, 2):
        print(i, end=" ")


bomb_required(3)

# Expected output:
4
2 1 3 2
# lcm of digits of a given number | define lcm function ; if at any point lcm become 0. return it ; 
def lcm_fun(a, b):
    if (b == 0):
        return a
    return lcm_fun(b, a % b)


def digitLCM(n):
    lcm = 1
    while (n > 0):
        lcm = int((n % 10 * lcm) / lcm_fun(n % 10, lcm))
        if (lcm == 0):
            return 0
        n = int(n / 10)
    return lcm


n = 397
print(digitLCM(n))

# Expected output:
63
# happy numbers | returns sum of squares of digits of a number n . for example for n = 12 it returns 1 + 4 = 5 ; returns true if n is happy number else returns false . ; keep replacing n with sum of squares of digits until we either reach 1 or we end up in a cycle ; number is happy if we reach 1 ; replace n with sum of squares of digits ; number is not happy if we reach 4 ; 
def sumDigitSquare(n):
    sq = 0
    while (n):
        digit = n % 10
        sq = sq + digit * digit
        n = n // 10
    return sq


def isHappy(n):
    while (1):
        if (n == 1):
            return True
        n = sumDigitSquare(n)
        if (n == 4):
            return False
    return False


n = 23
if (isHappy(n)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# sum of all subsets of a set formed by first n natural numbers | python program to find sum of all subsets of a set . ; sum of subsets is ( n * ( n + 1 ) / 2 ) * pow ( 2 , n - 1 ) ; 
def findSumSubsets(n):
    return (n * (n + 1) / 2) * (1 << (n - 1))


n = 3
print(findSumSubsets(n))

# Expected output:
24.0
# minimum element whose n | python3 program to find minimum element whose n - th power is greater than product of an array of size n ; function to find the minimum element ; loop to traverse and store the sum of log ; calculates the elements according to formula . ; returns the minimal element ; 

import math as m


def findMin(a, n):
    _sum = 0
    for i in range(n):
        x = m .exp(_sum / n)
    return int(x + 1)


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

# Expected output:
2
# generate all cyclic permutations of a number | python3 program to generate all cyclic permutations of number ; function to count the total number of digits in a number . ; function to generate all cyclic permutations of a number ; following three lines generates a circular permutation of a number . ; if all the permutations are checked and we obtain original number exit from loop . ; 

import math


def countdigits(N):
    count = 0
    while (N):
        count = count + 1
        N = int(math .floor(N / 10))
    return count


def cyclic(N):
    num = N
    n = countdigits(N)
    while (1):
        print(int(num))
        rem = num % 10
        div = math .floor(num / 10)
        num = ((math .pow(10, n - 1)) * rem + div)
        if (num == N):
            break


N = 5674
cyclic(N)

# Expected output:
5674
4567
7456
6745
# find if two people ever meet after same number of jumps | function to find if any one of them can overtake the other ; since starting points are always different , they will meet if following conditions are met . ( 1 ) speeds are not same ( 2 ) difference between speeds divide the total distance between initial points . ; 
def sackRace(p1, s1, p2, s2):
    return ((s1 > s2 and (p2 - p1) %
             (s1 - s2) == 0) or (s2 > s1 and (p1 - p2) %
                                 (s2 - s1) == 0))


p1 = 4
s1 = 4
p2 = 8
s2 = 2
if (sackRace(p1, s1, p2, s2)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# largest proper fraction with sum of numerator and denominator equal to a given number | python3 program to find the largest fraction a / b such that a + b is equal to given number and a < b . ; calculate n / 2 ; ; check if n is odd or even ; if n is odd answer will be ceil ( n / 2 ) - 1 and floor ( n / 2 ) + 1 ; if n is even check if n / 2 i . e a is even or odd ; if n / 2 is even apply the previous formula ; if n / 2 is odd answer will be ceil ( n / 2 ) - 2 and floor ( n / 2 ) + 2 ; 

import math


def solve(n):
    a = float(n / 2)
    if (n % 2 != 0):
        print((math .ceil(a) - 1), (math .floor(a) + 1))
    else:
        if (a % 2 == 0):
            print((math .ceil(a) - 1), (math .floor(a) + 1))
        else:
            print((math .ceil(a) - 2), (math .floor(a) + 2))


n = 34
solve(n)

# Expected output:
15 19
# number of digits in the product of two numbers | function to count number of digits in the product of two numbers ; absolute value of the product of two numbers ; if product is 0 ; count number of digits in the product 'p ; required count of digits ; 
def countDigits(a, b):
    count = 0
    p = abs(a * b)
    if (p == 0):
        return 1
    while (p > 0):
        count = count + 1
        p = p // 10
    return count


a = 33
b = -24
print(("Number of digits = ", countDigits(a, b)))

# Expected output:
('Number of digits = ', 3)
# find multiple of x closest to or a ^ b ( a raised to power b ) | python3 program to find closest multiple of x to a ^ b ; function to find closest multiple of x to a ^ b ; calculate a ^ b / x ; answer is either ( ans * x ) or ( ans + 1 ) * x ; printing nearest answer ; 

import math


def multiple(a, b, x):
    if (b < 0):
        if (a == 1 and x == 1):
            print("1")
        else:
            print("0")
    mul = int(pow(a, b))
    ans = int(mul / x)
    ans1 = x * ans
    ans2 = x * (ans + 1)
    if ((mul - ans1) <= (ans2 - mul)):
        print(ans1)
    else:
        print(ans2)


a = 349
b = 1
x = 4
multiple(a, b, x)

# Expected output:
348
# maximum sum of difference of adjacent elements | to find max sum of permutation ; base case ; otherwise max sum will be ( n * ( n - 1 ) / 2 ) - 1 + n / 2 ; 
def maxSum(n):
    if (n == 1):
        return 1
    else:
        return int((n * (n - 1) / 2) - 1 + n / 2)


n = 3
print(maxSum(n))

# Expected output:
3
# compute average of two numbers without overflow | python 3 code to compute average of two numbers ; function to compute average of two numbers ; 

import sys
from math import floor
INT_MAX = 2147483647


def compute_average(a, b):
    return floor((a + b) / 2)


if __name__ == '__main__':
    a = INT_MAX
    b = -INT_MAX - 1
    print("Actual average : ", INT_MAX)
    print("Computed average : ", compute_average(a, b))

# Expected output:
Actual average :  2147483647
Computed average :  -1
# add minimum number to an array so that the sum becomes even | function to find out minimum number ; count odd number of terms in array ; 
def minNum(arr, n):
    odd = False
    for i in range(n):
        if (arr[i] % 2):
            odd = not odd
    if (odd):
        return 1
    return 2


arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
n = len(arr)
print(minNum(arr, n))

# Expected output:
1
# Check if a number is jumbled or not | Function to check if a number is jumbled or not ; Single digit number ; Checking every digit through a loop ; All digits were checked ; Digit at index i ; Digit at index i - 1 ; If difference is greater than 1 ; Number checked ; - 1234 to be checked ; - 1247 to be checked
def checkJumbled(num):
    if (num / 10 == 0):
        return True
    while (num != 0):
        if (num / 10 == 0):
            return True
        digit1 = num % 10
        digit2 = (num / 10) % 10
        if (abs(digit2 - digit1) > 1):
            return False
        num = num / 10
    return True


num = -1234
if (checkJumbled(abs(num))):
    print("True")
else:
    print("False")
num = -1247
if (checkJumbled(abs(num))):
    print("True")
else:
    print("False")

# Expected output:
False
False
# josephus problem using bit magic | function to find the position of the most significant bit ; keeps shifting bits to the right until we are left with 0 ; function to return at which place josephus should sit to avoid being killed ; getting the position of the most significant bit ( msb ) . the leftmost '1' . if the number is '41' then its binary is '101001' . so msbpos ( 41 ) = 6 ; ' j ' stores the number with which to xor the number ' n ' . since we need '100000' we will do 1 << 6 - 1 to get '100000 ; toggling the most significant bit . changing the leftmost '1' to '0' . 101001 ^ 100000 = 001001 ( 9 ) ; left - shifting once to add an extra '0' to the right end of the binary number 001001 = 010010 ( 18 ) ; toggling the '0' at the end to '1' which is essentially the same as putting the msb at the rightmost place . 010010 | 1 = 010011 ( 19 ) ; 
def msbPos(n):
    pos = 0
    while n != 0:
        pos += 1
        n = n >> 1
    return pos


def josephify(n):
    position = msbPos(n)
    j = 1 << (position - 1)
    n = n ^ j
    n = n << 1
    n = n | 1
    return n


n = 41
print(josephify(n))

# Expected output:
19
# count pairs with odd xor | a function will return number of pair whose xor is odd ; to store count of xor pair ; if xor is odd increase count ; return count ; 
def countXorPair(arr, n):
    count = 0
    for i in range(n):
        for j in range(i + 1, n):
            if ((arr[i] ^ arr[j]) % 2 == 1):
                count += 1
    return count


if __name__ == "__main__":
    arr = [1, 2, 3]
    n = len(arr)
    print(countXorPair(arr, n))

# Expected output:
2
# discrete logarithm ( find an integer k such that a ^ k is congruent modulo b ) | python3 program to calculate discrete logarithm ; iterative function to calculate ( x ^ y ) % p in o ( log y ) ; 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 k for given a , b , m ; store all values of a ^ ( n * i ) of lhs ; calculate ( a ^ j ) * b and check for collision ; if collision occurs i . e . , lhs = rhs ; check whether ans lies below m or not ; 

import math


def powmod(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 discreteLogarithm(a, b, m):
    n = int(math.sqrt(m) + 1)

    value = [0] * m
    for i in range(n, 0, -1):
        value[powmod(a, i * n, m)] = i

    for j in range(n):
        cur = (powmod(a, j, m) * b) % m
        if (value[cur]):
            ans = value[cur] * n - j
            if (ans < m):
                return ans

    return -1


a = 2
b = 3
m = 5
print(discreteLogarithm(a, b, m))

a = 3
b = 7
m = 11
print(discreteLogarithm(a, b, m))

# Expected output:
3
-1
# discrete logarithm ( find an integer k such that a ^ k is congruent modulo b ) | python3 program to calculate discrete logarithm ; calculate a ^ n ; store all values of a ^ ( n * i ) of lhs ; calculate ( a ^ j ) * b and check for collision ; 

import math


def discreteLogarithm(a, b, m):
    n = int(math .sqrt(m) + 1)
    an = 1
    for i in range(n):
        an = (an * a) % m
    value = [0] * m
    cur = an
    for i in range(1, n + 1):
        if (value[cur] == 0):
            value[cur] = i
        cur = (cur * an) % m
    cur = b
    for i in range(n + 1):
        if (value[cur] > 0):
            ans = value[cur] * n - i
            if (ans < m):
                return ans
        cur = (cur * a) % m
    return -1


a = 2
b = 3
m = 5
print(discreteLogarithm(a, b, m))
a = 3
b = 7
m = 11
print(discreteLogarithm(a, b, m))

# Expected output:
3
-1
# finding n | python3 program to find n - th number with prime digits 2 , 3 and 7 ; remainder for check element position ; if number is 1 st position in tree ; if number is 2 nd position in tree ; if number is 3 rd position in tree ; if number is 4 th position in tree ; 
def nthprimedigitsnumber(number):
    num = ""
    while (number > 0):
        rem = number % 4
        if (rem == 1):
            num += '2'
        if (rem == 2):
            num += '3'
        if (rem == 3):
            num += '5'
        if (rem == 0):
            num += '7'
        if (number % 4 == 0):
            number = number - 1
        number = number // 4
    return num[::-1]


number = 21
print(nthprimedigitsnumber(10))
print(nthprimedigitsnumber(number))

# Expected output:
33
222
# count pairs ( a , b ) whose sum of cubes is n ( a ^ 3 + b ^ 3 = n ) | python 3 program to count pairs whose sum cubes is n ; function to count the pairs satisfying a ^ 3 + b ^ 3 = n ; check for each number 1 to cbrt ( n ) ; store cube of a number ; subtract the cube from 

import math


def countPairs(N):
    count = 0
    for i in range(1, int(math .pow(N, 1 / 3) + 1)):
        cb = i * i * i
        diff = N - cb
        cbrtDiff = int(math .pow(diff, 1 / 3))
        if (cbrtDiff * cbrtDiff * cbrtDiff == diff):
            count += 1
    return count


for i in range(1, 11):
    print('For n = ', i, ', ', countPairs(i), ' pair exists')

# Expected output:
For n =  1 ,  1  pair exists
For n =  2 ,  1  pair exists
For n =  3 ,  0  pair exists
For n =  4 ,  0  pair exists
For n =  5 ,  0  pair exists
For n =  6 ,  0  pair exists
For n =  7 ,  0  pair exists
For n =  8 ,  1  pair exists
For n =  9 ,  2  pair exists
For n =  10 ,  0  pair exists
# smallest number s such that n is a factor of s factorial or s ! | calculate prime factors for a given number ; container for prime factors ; iterate from 2 to i ^ 2 finding all factors ; if we still have a remainder it is also a prime factor ; calculate occurrence of an element in factorial of a number ; iterate through prime factors ; check if factorial contains less occurrences of prime factor ; function to binary search 1 to n ; prime factors are not in the factorial increase the lowerbound ; we have reached smallest occurrence ; smaller factorial satisfying requirements may exist , decrease upperbound ; calculate prime factors and search ; 
def primeFactors(num):
    ans = dict()
    i = 2
    while (i * i <= num):
        while (num % i == 0):
            num //= i
            if i not in ans:
                ans[i] = 0
            ans[i] += 1
    if (num > 1):
        if num not in ans:
            ans[num] = 0
        ans[num] += 1
    return ans


def legendre(factor, num):
    count = 0
    fac2 = factor
    while (num >= factor):
        count += num // factor
        factor *= fac2
    return count


def possible(factors, num):
    for it in factors.keys():
        if (legendre(it, num) < factors[it]):
            return False
    return True


def search(start, end, factors):
    mid = (start + end) // 2
    if (not possible(factors, mid)):
        return search(mid + 1, end, factors)
    if (start == mid):
        return mid
    return search(start, mid, factors)


def findFact(num):
    factors = primeFactors(num)
    return search(1, num, factors)


if __name__ == '__main__':

    print(findFact(6))

# Expected output:
3
# finding ' k ' such that its modulus with each array element is same | prints all k such that arr [ i ] % k is same for all i ; sort the numbers ; max difference will be the difference between first and last element of sorted array ; case when all the array elements are same ; find all divisors of d and store in a vector v [ ] ; check for each v [ i ] if its modulus with each array element is same or not ; checking for each array element if its modulus with k is equal to k or not ; if check is true print v [ i ] ; 
def printEqualModNumbers(arr, n):
    arr .sort()
    d = arr[n - 1] - arr[0]
    if (d == 0):
        print("Infinite solution")
        return
    v = []
    i = 1
    while (i * i <= d):
        if (d % i == 0):
            v .append(i)
            if (i != d / i):
                v .append(d / i)
        i += 1
    for i in range(len(v)):
        temp = arr[0] % v[i]
        j = 1
        while (j < n):
            if (arr[j] % v[i] != temp):
                break
            j += 1
        if (j == n):
            print(v[i], end=" ")


arr = [38, 6, 34]
printEqualModNumbers(arr, len(arr))

# Expected output:
1 2 4
# first digit in product of an array of numbers | python implementation of finding first digit of product of n numbers ; returns the first digit of product of elements of arr [ ] ; stores the logarithm of product of elements of arr [ ] ; fractional ( s ) = s - floor ( s ) ; ans = 10 ^ fract_s ; 

import math


def FirstDigit(arr, n):
    S = 0
    for i in range(n):
        S = S + math .log10(arr[i] * 1.0)
    fract_S = S - math .floor(S)
    ans = math .pow(10, fract_S)
    return ans


arr = [5, 8, 3, 7]
n = len(arr)
print((int)(FirstDigit(arr, n)))

# Expected output:
8
# find count of digits in a number that divide the number | return the number of digits that divides the number . ; fetching each digit of the number ; checking if digit is greater than 0 and can divides n . ; 
def countDigit(n):
    temp = n
    count = 0
    while temp != 0:
        d = temp % 10
        temp //= 10
        if d > 0 and n % d == 0:
            count += 1
    return count


n = 1012
print(countDigit(n))

# Expected output:
3
# minimum positive integer to divide a number such that the result is an odd | function to find the value ; return 1 if already odd ; check how many times it is divided by 2 ; 
def makeOdd(n):
    if (n % 2 != 0):
        return 1
    resul = 1
    while (n % 2 == 0):
        n = n / 2
        resul = resul * 2
    return resul


n = 36
print(makeOdd(n))

# Expected output:
4
# multiple of x closest to n | function to calculate the smallest multiple ; 
def closestMultiple(n, x):
    if x > n:
        return x
    z = (int)(x / 2)
    n = n + z
    n = n - (n % x)
    return n


n = 56287
x = 27
print(closestMultiple(n, x))

# Expected output:
56295
# perfect cubes in a range | python3 code for efficient method to print cubes between a and b ; an efficient solution to print perfect cubes between a and b ; find cube root of both a and b ; print cubes between acrt and bcrt ; 
def cbrt(n):
    return (int)(n ** (1. / 3))


def printCubes(a, b):
    acrt = cbrt(a)
    bcrt = cbrt(b)
    for i in range(acrt, bcrt + 1):
        if (i * i * i >= a and i * i * i <= b):
            print(i * i * i, " ", end="")


a = 24
b = 576
print("Perfect cubes in given range:")
printCubes(a, b)

# Expected output:
Perfect cubes in given range:
27  64  125  216  343  512
# number of occurrences of 2 as a digit in numbers from 0 to n | counts the number of '2' digits in a single number ; counts the number of '2' digits between 0 and n ; initialize result ; count 2 's in every number  from 2 to n ; 
def number0f2s(n):
    count = 0
    while (n > 0):
        if (n % 10 == 2):
            count = count + 1
        n = n // 10
    return count


def numberOf2sinRange(n):
    count = 0
    for i in range(2, n + 1):
        count = count + number0f2s(i)
    return count


print(numberOf2sinRange(22))
print(numberOf2sinRange(100))

# Expected output:
6
20
# minimum toggles to partition a binary array so that it has first 0 s then 1 s | function to calculate minimum toggling required by using dynamic programming ; fill entries in zero [ ] such that zero [ i ] stores count of zeroes to the left of i ( exl ; if zero found update zero [ ] array ; finding the minimum toggle required from every index ( 0 to n - 1 ) ; 
def minToggle(arr, n):
    zero = [0 for i in range(n + 1 + 1)]
    zero[0] = 0
    for i in range(1, n + 1):
        if (arr[i - 1] == 0):
            zero[i] = zero[i - 1] + 1
        else:
            zero[i] = zero[i - 1]
    ans = n
    for i in range(1, n + 1):
        ans = min(ans, i - zero[i] + zero[n] - zero[i])
    return ans


arr = [1, 0, 1, 1, 0]
n = len(arr)
print(minToggle(arr, n))

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


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

# Expected output:
Yes
# find ways an integer can be expressed as sum of n | python 3 program to find number of ways to express a number as sum of n - th powers of numbers . ; wrapper over checkrecursive ( ) ; 
def checkRecursive(num, rem_num, next_int, n, ans=0):
    if (rem_num == 0):
        ans += 1
    r = int(num ** (1 / n))
    for i in range(next_int + 1, r + 1):
        a = rem_num - int(i ** n)
        if a >= 0:
            ans += checkRecursive(num, rem_num - int(i ** n), i, n, 0)
    return ans


def check(x, n):
    return checkRecursive(x, x, 0, n)


if __name__ == '__main__':
    print(check(10, 2))

# Expected output:
1
# n 'th palindrome of k digits | a naive approach of python3 program of finding nth palindrome of k digit ; utility function to reverse the number n ; boolean function to check for palindromic number ; function for finding nth palindrome of k digits ; get the smallest k digit number ; check the number is palindrom or not ; if n 'th palindrome found  break the loop ; increment number for checking next palindrome ; 

import math


def reverseNum(n):
    rev = 0
    while (n):
        rem = n % 10
        rev = (rev * 10) + rem
        n = int(n / 10)
    return rev


def isPalindrom(num):
    return num == reverseNum(num)


def nthPalindrome(n, k):
    num = math .pow(10, k - 1)
    while (True):
        if (isPalindrom(num)):
            n -= 1
        if (not n):
            break
        num += 1
    return int(num)


n = 6
k = 5
print(n, "th palindrome of", k, "digit =", nthPalindrome(n, k))
n = 10
k = 6
print(n, "th palindrome of", k, "digit =", nthPalindrome(n, k))

# Expected output:
6 th palindrome of 5 digit = 10501
10 th palindrome of 6 digit = 109901