giulio98/xlcost-single-prompt huggingface dataset:
# divisors of n | python3 program to count number of divisors of n ^ 2 which are not divisible by divisor of n ; function to count divisors of n ^ 2 having no factors of 'n ; increment count of i - th prime divisor ; find next prime divisor ; increment count if divisor still remains ; initialize variable for counting the factors of n ^ 2 and n as ans1 and ans2 respectively ; range based for - loop ; use formula as discussed in above ; return the difference of answers ; 

import math as mt


def factors(n):
    prime = dict()
    for i in range(2, mt .ceil(mt .sqrt(n + 1))):
        while (n % i == 0):
            if i in prime .keys():
                prime[i] += 1
            else:
                prime[i] = 1
            n = n // i
    if (n > 2):
        if n in prime .keys():
            prime[n] += 1
        else:
            prime[n] = 1
    ans1 = 1
    ans2 = 1
    for it in prime:
        ans1 *= 2 * prime[it] + 1
        ans2 *= prime[it] + 1
    return ans1 - ans2


n = 5
print(factors(n))
n = 8
print(factors(n))

# Expected output:
1
3
# print digit 's position to be removed to make a number divisible by 6 | python program to print digit 's position to be removed to make number divisible by 6 ; function to print the number divisible by 6 after exactly removing a digit ; stores the sum of all elements ; traverses the string and converts string to number array and sums up ; if second last is odd or sum of n - 1 elements are not divisible by 3. ; second last is even and prn - 1 elements removing last digit ; last digit removed ; counter to check if any element after removing , its sum % 3 == 0 ; traverse till second last element ; to check if any element after removing , its sum % 3 == 0 ; the leftmost element ; break at the leftmost element ; stores the right most element ; if no element has been found as a [ i + 1 ] > a [ i ] ; if second last is even , then remove last if ( sum - last ) % 3 == 0 ; if no element which on removing gives sum % 3 == 0 ; 

import math as mt


def greatest(s):
    n = len(s)
    a = [0 for i in range(n)]
    Sum = 0
    for i in range(n):
        a[i] = ord(s[i]) - ord('0')
        Sum += a[i]
        if (a[n - 2] % 2 != 0 or (Sum - a[n - 1]) % 3 != 0):
            print("-1")
        else:
            print(n)
    else:
        re = Sum % 3
        dell = -1
        flag = 0
        for i in range(n - 1):
            if ((a[i]) % 3 == re):
                if (a[i + 1] > a[i]):
                    dell = i
                    flag = 1
                    break
                else:
                    dell = i
        if (flag == 0):
            if (a[n - 2] % 2 == 0 and re == a[n - 1] % 3):
                dell = n - 1
        if (dell == -1):
            print("-1")
        else:
            print(dell + 1)


s = "7510222"
greatest(s)

# Expected output:
-1
7
-1
-1
7
-1
-1
3
# representation of a number in powers of other | python3 program to check if m can be represented as powers of w . ; if m is not zero means , it can 't be  represented in terms of powers of w. ; 
def asPowerSum(w, m):
    while (m > 0):
        if ((m - 1) % w == 0):
            m = (m - 1) / w
        elif ((m + 1) % w == 0):
            m = (m + 1) / w
        elif (m % w == 0):
            m = m / w
        else:
            return (m == 0)


w = 3
m = 7
if (asPowerSum(w, m)):
    print("Yes")
else:
    print("No")

# Expected output:
No
# number of digits to be removed to make a number divisible by 3 | function to count the no of removal of digits to make a very large number divisible by 3 ; add up all the digits of num ; if num is already is divisible by 3 then no digits are to be removed ; if there is single digit , then it is not possible to remove one digit . ; traverse through the number and find out if any number on removal makes the sum divisible by 3 ; if there are two numbers then it is not possible to remove two digits . ; otherwise we can always make a number multiple of 2 by removing 2 digits . ; 
def divisible(num):
    n = len(num)
    sum_ = 0
    for i in range(n):
        sum_ += int(num[i])
    if (sum_ % 3 == 0):
        return 0
    if (n == 1):
        return -1
    for i in range(n):
        if (sum_ % 3 == int(num[i]) % 3):
            return 1
    if (n == 2):
        return -1
    return 2


if __name__ == '__main__':
    num = "1234"
    print(divisible(num))

# Expected output:
1
# number of n digit numbers that do not contain 9 | function to find number of n digit numbers possible ; 
def totalNumber(n):
    return 8 * pow(9, n - 1)


n = 3
print(totalNumber(n))

# Expected output:
648
# subsequences of size three in an array whose sum is divisible by m | python program to find count of subsequences of size three divisible by m . ; three nested loop to find all the sub sequences of length three in the given array a [ ] . ; checking if the sum of the chosen three number is divisible by m . ; 
def coutSubSeq(A, N, M):
    sum = 0
    ans = 0
    for i in range(0, N):
        for j in range(i + 1, N):
            for k in range(j + 1, N):
                sum = A[i] + A[j] + A[k]
                if (sum % M == 0):
                    ans = ans + 1
    return ans


M = 3
A = [1, 2, 4, 3]
N = len(A)
print(coutSubSeq(A, N, M))

# Expected output:
2
# subsequences of size three in an array whose sum is divisible by m | python program to find count of subsequences of size three divisible by m . ; storing frequencies of all remainders when divided by m . ; including i and j in the sum rem calculate the remainder required to make the sum divisible by m ; if the required number is less than j , we skip as we have already calculated for that value before . as j here starts with i and rem is less than j . ; if satisfies the first case . ; if satisfies the second case ; if satisfies the third case ; 
def countSubSeq(A, N, M):
    ans = 0
    h = [0] * M
    for i in range(0, N):
        A[i] = A[i] % M
        h[A[i]] = h[A[i]] + 1
    for i in range(0, M):
        for j in range(i, M):
            rem = (M - (i + j) % M) % M
            if (rem < j):
                continue
            if (i == j and rem == j):
                ans = ans + h[i] * (h[i] - 1) * (h[i] - 2) / 6
            elif (i == j):
                ans = ans + (h[i] * (h[i] - 1) * h[rem] / 2)
            elif (i == rem):
                ans = ans + h[i] * (h[i] - 1) * h[j] / 2
            elif (rem == j):
                ans = ans + h[j] * (h[j] - 1) * h[i] / 2
            else:
                ans = ans + h[i] * h[j] * h[rem]
        return ans


M = 3
A = [1, 2, 4, 3]
N = len(A)
print(countSubSeq(A, N, M))

# Expected output:
2.0
# find n | utility function ; since first element of the series is 7 , we initialise a variable with 7 ; using iteration to find nth term ; 
def findTerm(n):
    if n == 1:
        return n
    else:
        term = 7
        for i in range(2, n + 1):
            term = term * 2 + (i - 1)
    return term


print(findTerm(5))

# Expected output:
138
# find n | returns n - th number in sequence 1 , 1 , 2 , 1 , 2 , 3 , 1 , 2 , 4 , ... ; one by one subtract counts elements in different blocks ; 
def findNumber(n):
    n -= 1
    i = 1
    while n >= 0:
        n -= i
        i += 1
    return (n + i)


n = 3
print(findNumber(n))

# Expected output:
2
# program to find correlation coefficient | python program to find correlation coefficient . ; function that returns correlation coefficient . ; sum of elements of array x . ; sum of elements of array y . ; sum of x [ i ] * y [ i ] . ; sum of square of array elements . ; use formula for calculating correlation coefficient . ; 

import math


def correlationCoefficient(X, Y, n):
    sum_X = 0
    sum_Y = 0
    sum_XY = 0
    squareSum_X = 0
    squareSum_Y = 0
    i = 0
    while i < n:
        sum_X = sum_X + X[i]
        sum_Y = sum_Y + Y[i]
        sum_XY = sum_XY + X[i] * Y[i]
        squareSum_X = squareSum_X + X[i] * X[i]
        squareSum_Y = squareSum_Y + Y[i] * Y[i]
        i = i + 1
    corr = (float)(n * sum_XY - sum_X * sum_Y) / (float)(math .sqrt((n *
                                                                     squareSum_X - sum_X * sum_X) * (n * squareSum_Y - sum_Y * sum_Y)))
    return corr


X = [15, 18, 21, 24, 27]
Y = [25, 25, 27, 31, 32]
n = len(X)
print('{0:.6f}'.format(correlationCoefficient(X, Y, n)))

# Expected output:
0.953463
# Find the number of spectators standing in the stadium at time t | Python program to find number of spectators standing at a time ; If the time is less than k then we can print directly t time . ; If the time is n then k spectators are standing . ; Otherwise we calculate the spectators standing . ; Stores the value of n , k and t t is time n & k is the number of specators
def result(n, k, t):
    if (t <= k):
        print(t)
    elif (t <= n):
        print(k)
    else:
        temp = t - n
        temp = k - temp
        print(temp)


n = 10
k = 5
t = 12
result(n, k, t)

# Expected output:
3
# program for weighted mean of natural numbers . | function to calculate weighted mean . ; take num array and corresponding weight array and 
def weightedMean(X, W, n):
    sum = 0
    numWeight = 0
    i = 0
    while i < n:
        numWeight = numWeight + X[i] * W[i]
        sum = sum + W[i]
        i = i + 1
    return (float)(numWeight / sum)


X = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
W = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
n = len(X)
m = len(W)
if (n == m):
    print(weightedMean(X, W, n))
else:
    print("-1")

# Expected output:
7.0
# program to find gcd of floating point numbers | python code for finding the gcd of two floating numbers . ; recursive function to return gcd of a and b ; base case ; 

import math


def gcd(a, b):
    if (a < b):
        return gcd(b, a)
    if (abs(b) < 0.001):
        return a
    else:
        return (gcd(b, a - math .floor(a / b) * b))


a = 1.20
b = 22.5
print('{0:.1f}'.format(gcd(a, b)))

# Expected output:
0.3
# program for harmonic mean of numbers | function that returns harmonic mean . ; declare sum variables and initialize with zero . ; 
def harmonicMean(arr, n):
    sm = 0
    for i in range(0, n):
        sm = sm + (1) / arr[i]
    return n / sm


arr = [13.5, 14.5, 14.8, 15.2, 16.1]
n = len(arr)
print(harmonicMean(arr, n))

# Expected output:
14.770680729373776
# program for harmonic mean of numbers | function that returns harmonic mean . ; 
def harmonicMean(arr, freq, n):
    sm = 0
    frequency_sum = 0
    for i in range(0, n):
        sm = sm + freq[i] / arr[i]
        frequency_sum = frequency_sum + freq[i]
    return (round(frequency_sum / sm, 4))


num = [13, 14, 15, 16, 17]
freq = [2, 5, 13, 7, 3]
n = len(num)
print(harmonicMean(num, freq, n))

# Expected output:
15.0631
# first collision point of two series | function to calculate the colliding point of two series ; iterating through n terms of the first series ; x is i - th term of first series ; d is first element of second series and c is common difference for second series . ; if no term of first series is found ; 
def point(a, b, c, d, n):
    for i in range(n):
        x = b + i * a
        if (x - d) % c == 0 and x - d >= 0:
            print(x)
            return
    else:
        print("No collision point")


a = 20
b = 2
c = 9
d = 19
n = 20
point(a, b, c, d, n)

# Expected output:
82
# lucas primality test | python3 program for lucas primality test ; function to generate prime factors of n ; if 2 is a factor ; if prime > 2 is factor ; this function produces power modulo some number . it can be optimized to using ; base cases ; generating and storing factors of n - 1 ; array for random generator . this array is to ensure one number is generated only once ; shuffle random array to produce randomness ; now one by one perform lucas primality test on random numbers generated . ; this is to check if every factor of n - 1 satisfy the condition ; if a ^ ( ( n - 1 ) / q ) equal 1 ; if all condition satisfy ; 

import random
import math


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


def power(n, r, q):
    total = n
    for i in range(1, r):
        total = (total * n) % q
    return total


def lucasTest(n):
    if (n == 1):
        return "neither prime nor composite"
    if (n == 2):
        return "prime"
    if (n % 2 == 0):
        return "composite1"
    factors = []
    factors = primeFactors(n - 1, factors)
    rand = [i + 2 for i in range(n - 3)]
    random .shuffle(rand)
    for i in range(n - 2):
        a = rand[i]
        if (power(a, n - 1, n) != 1):
            return "composite"
        flag = True
        for k in range(len(factors)):
            if (power(a, (n - 1) // factors[k], n) == 1):
                flag = False
                break
        if (flag):
            return "prime"
    return "probably composite"


if __name__ == "__main__":
    print(str(7) + " is " + lucasTest(7))
    print(str(9) + " is " + lucasTest(9))
    print(str(37) + " is " + lucasTest(37))

# Expected output:
7 is prime
9 is composite
37 is prime
# pair with maximum gcd from two arrays | find the maximum gcd pair with maximum sum ; array to keep a count of existing elements ; first [ i ] and second [ i ] are going to store maximum multiples of i in a [ ] and b [ ] respectively . ; traverse through the first array to mark the elements in cnt ; find maximum multiple of every number in first array ; find maximum multiple of every number in second array we re - initialise cnt [ ] and traverse through the second array to mark the elements in cnt ; if the multiple is present in the second array then store the max of number or the pre - existing element ; traverse for every elements and checks the maximum n that is present in both the arrays ; 
def gcdMax(a, b, n, N):
    cnt = [0] * N
    first = [0] * N
    second = [0] * N
    for i in range(n):
        cnt[a[i]] = 1
    for i in range(1, N):
        for j in range(i, N, i):
            if (cnt[j]):
                first[i] = max(first[i], j)
    cnt = [0] * N
    for i in range(n):
        cnt[b[i]] = 1
    for i in range(1, N):
        for j in range(i, N, i):
            if (cnt[j] > 0):
                second[i] = max(second[i], j)
    i = N - 1
    while i >= 0:
        if (first[i] > 0 and second[i] > 0):
            break
        i -= 1
    print(str(first[i]) + " " + str(second[i]))


if __name__ == "__main__":
    a = [3, 1, 4, 2, 8]
    b = [5, 2, 12, 8, 3]
    n = len(a)
    N = 20
    gcdMax(a, b, n, N)

# Expected output:
8 8
# pierpont prime | python3 program to print pierpont prime numbers smaller than n . ; finding all numbers having factor power of 2 and 3 using sieve ; storing number of the form 2 ^ i . 3 ^ k + 1. ; finding prime number using sieve of eratosthenes . reusing same array as result of above computations in v . ; printing n pierpont primes smaller than n ; 
def printPierpont(n):
    arr = [False] * (n + 1)
    two = 1
    three = 1
    while (two + 1 < n):
        arr[two] = True
        while (two * three + 1 < n):
            arr[three] = True
            arr[two * three] = True
            three *= 3
        three = 1
        two *= 2
    v = []
    for i in range(n):
        if (arr[i]):
            v .append(i + 1)
    arr1 = [False] * (len(arr))
    p = 2
    while (p * p < n):
        if (arr1[p] == False):
            for i in range(p * 2, n, p):
                arr1[i] = True
        p += 1
    for i in range(len(v)):
        if (not arr1[v[i]]):
            print(v[i], end=" ")


n = 200
printPierpont(n)

# Expected output:
2 3 5 7 13 17 19 37 73 97 109 163 193
# woodall number | python program to check if a number is woodball or not . ; if number is even , return false . ; if x is 1 , return true . ; while x is divisible by 2 ; divide x by 2 ; count the power ; if at any point power and x became equal , return true . ; 
def isWoodall(x):
    if (x % 2 == 0):
        return False
    if (x == 1):
        return True
    p = 0
    while (x % 2 == 0):
        x = x / 2
        p = p + 1
        if (p == x):
            return True
    return False


x = 383
if (isWoodall(x)):
    print("Yes")
else:
    print("No")

# Expected output:
No
# print k numbers where all pairs are divisible by m | function to generate k numbers whose difference is divisible by m ; using an adjacency list like representation to store numbers that lead to same remainder . ; stores the modulus when divided by m ; if we found k elements which have same remainder . ; if we could not find k elements ; 
def print_result(a, n, k, m):
    v = [[]for i in range(m)]
    for i in range(0, n):
        rem = a[i] % m
        v[rem].append(a[i])
        if (len(v[rem]) == k):
            for j in range(0, k):
                print(v[rem][j], end=" ")
            return
    print(-1)


if __name__ == '__main__':
    a = [1, 8, 4]
    n = len(a)
    print_result(a, n, 2, 3)

# Expected output:
1 4
# smallest x such that 1 * n , 2 * n , ... x * n have all digits from 1 to 9 | returns smallest value x such that 1 * n , 2 * n , 3 * n ... x * n have all digits from 1 to 9 at least once ; taking temporary array and variable . ; iterate till we get all the 10 digits at least once ; checking all the digits ; 
def smallestX(n):
    temp = [0] * 10
    if (n == 0):
        return -1
    count = 0
    x = 1
    while (count < 10):
        y = x * n
        while (y > 0):
            if (temp[y % 10] == 0):
                count += 1
                temp[y % 10] = 1
            y = int(y / 10)
        x += 1
    return x - 1


if __name__ == '__main__':
    n = 5
    print(smallestX(n))

# Expected output:
18
# find a number x such that sum of x and its digits is equal to given n . | utility function for digit sum ; function for finding x ; iterate from 1 to n . for every no . check if its digit sum with it isequal to n . ; if no such i found return - 1 ; 
def digSum(n):
    sum = 0
    rem = 0
    while (n):
        rem = n % 10
        sum = sum + rem
        n = int(n / 10)
    return sum


def findX(n):
    for i in range(n + 1):
        if (i + digSum(i) == n):
            return i
    return -1


n = 43
print("x = ", findX(n))

# Expected output:
x =  35
# 9 's complement of a decimal number | python3 program to find 9 's complement of a number. ; 
def complement(number):
    for i in range(0, len(number)):
        if (number[i] != '.'):
            a = 9 - int(number[i])
            number = (number[:i] + str(a) + number[i + 1:])
    print("9's complement is : ", number)


if __name__ == '__main__':
    number = "345.45"
    complement(number)

# Expected output:
9's complement is :  654.54
# ways to express a number as product of two different factors | to count number of ways in which number expressed as product of two different numbers ; to store count of such pairs ; counting number of pairs upto sqrt ( n ) - 1 ; to return count of pairs ; 
def countWays(n):
    count = 0
    i = 1
    while ((i * i) < n):
        if (n % i == 0):
            count += 1
        i += 1
    return count


n = 12
print(countWays(n))

# Expected output:
3
# count divisors of n that have at | python3 program to count divisors of n that have at least one digit common with n ; function to return true if any digit of m is present in hash [ ] . ; check till last digit ; if number is also present in original number then return true ; if no number matches then return 1 ; count the no of divisors that have at least 1 digits same ; store digits present in n in a hash [ ] ; marks that the number is present ; last digit removed ; loop to traverse from 1 to sqrt ( n ) to count divisors ; if i is the factor ; call the function to check if any digits match or not ; if n / i != i then a different number , then check it also ; return the answer ; 

import math


def isDigitPresent(m, Hash):
    while (m):
        if (Hash[m % 10]):
            return True
        m = m // 10
    return False


def countDivisibles(n):
    Hash = [False for i in range(10)]
    m = n
    while (m):
        Hash[m % 10] = True
        m = m // 10
    ans = 0
    for i in range(1, int(math .sqrt(n)) + 1):
        if (n % i == 0):
            if (isDigitPresent(i, Hash)):
                ans += 1
            if (n // i != i):
                if (isDigitPresent(n // i, Hash)):
                    ans += 1
    return ans


n = 15
print(countDivisibles(n))

# Expected output:
3
# doolittle algorithm : lu decomposition | python3 program to decompose a matrix into lower and upper triangular matrix ; decomposing matrix into upper and lower triangular matrix ; upper triangular ; summation of l ( i , j ) * u ( j , k ) ; evaluating u ( i , k ) ; lower triangular ; lower [ i ] [ i ] = 1 diagonal as 1 ; summation of l ( k , j ) * u ( j , i ) ; evaluating l ( k , i ) ; setw is for displaying nicely ; displaying the result : ; lower ; upper ; 

MAX = 100


def luDecomposition(mat, n):

    lower = [[0 for x in range(n)]
             for y in range(n)]
    upper = [[0 for x in range(n)]
             for y in range(n)]
    for i in range(n):
        for k in range(i, n):
            sum = 0
            for j in range(i):
                sum += (lower[i][j] * upper[j][k])
            upper[i][k] = mat[i][k] - sum
        for k in range(i, n):
            if (i == k):
                lower[i][i] = 1
            else:
                sum = 0
                for j in range(i):
                    sum += (lower[k][j] * upper[j][i])
                lower[k][i] = int((mat[k][i] - sum) /
                                  upper[i][i])
    print("Lower Triangular		Upper Triangular")
    for i in range(n):
        for j in range(n):
            print(lower[i][j], end="	")
        print("", end="	")
        for j in range(n):
            print(upper[i][j], end="	")
        print("")


mat = [[2, -1, -2],
       [-4, 6, 3],
       [-4, -2, 8]]

luDecomposition(mat, 3)

# Expected output:
Lower Triangular		Upper Triangular
1	0	0		2	-1	-2	
-2	1	0		0	4	-1	
-2	-1	1		0	0	3
# divide number into two parts divisible by given numbers | method prints divisible parts if possible , otherwise prints 'not possible ; creating arrays to store reminder ; looping over all suffix and storing reminder with f ; getting suffix reminder from previous suffix reminder ; looping over all prefix and storing reminder with s ; getting prefix reminder from next prefix reminder ; updating base value ; now looping over all reminders to check partition condition ; if both reminders are 0 and digit itself is not 0 , then print result and return ; if we reach here , then string can ' be partitioned  under constraints ; 
def printTwoDivisibleParts(num, f, s):
    N = len(num)
    prefixReminder = [0]*(N + 1)
    suffixReminder = [0]*(N + 1)
    for i in range(1, N):
        suffixReminder[i] = (suffixReminder[i - 1] * 10 +
                             (ord(num[i - 1]) - 48)) % f

    base = 1
    for i in range(N - 1, -1, -1):
        prefixReminder[i] = (prefixReminder[i + 1] +
                             (ord(num[i]) - 48) * base) % s
        base = (base * 10) % s
    for i in range(N):
        if (prefixReminder[i] == 0 and suffixReminder[i] == 0 and num[i] != '0'):
            print(num[0:i], num[i:N])
            return 0
    print("Not Possible")


if __name__ == '__main__':
    num = "246904096"
    f = 12345
    s = 1024
    printTwoDivisibleParts(num, f, s)

# Expected output:
24690 4096