giulio98/xlcost-single-prompt huggingface dataset:
# program to find the common ratio of three numbers | python 3 implementation of above approach ; function to print a : b : c ; to print the given proportion in simplest form . ; 

import math


def solveProportion(a, b1, b2, c):
    A = a * b2
    B = b1 * b2
    C = b1 * c
    gcd1 = math .gcd(math .gcd(A, B), C)
    print(str(A // gcd1) + ":" + str(B // gcd1) + ":" + str(C // gcd1))


if __name__ == "__main__":
    a = 3
    b1 = 4
    b2 = 8
    c = 9
    solveProportion(a, b1, b2, c)

# Expected output:
6:8:9
# number of divisors of a given number n which are divisible by k | function to count number of divisors of n which are divisible by k ; variable to store count of divisors ; traverse from 1 to n ; increase the count if both the conditions are satisfied ; 
def countDivisors(n, k):
    count = 0
    for i in range(1, n + 1):
        if (n % i == 0 and i % k == 0):
            count += 1
    return count


if __name__ == "__main__":
    n, k = 12, 3
    print(countDivisors(n, k))

# Expected output:
3
# program to find the nth term of the series 0 , 14 , 40 , 78 , 124 , ... | calculate sum upto nth term of series ; return the final sum ; 
def nthTerm(n):
    return int(6 * pow(n, 2) - 4 * n - 2)


N = 4
print(nthTerm(N))

# Expected output:
78
# program to find the nth term of series 5 , 10 , 17 , 26 , 37 , 50 , 65 , 82 , ... | python3 program to find the n - th term of the series : 5 , 10 , 17 , 26 , 37 , 50 , 65 , 82 , ... ; calculate nth term of series ; return the final sum ; 

from math import *


def nthTerm(n):
    return pow(n, 2) + 2 * n + 2


if __name__ == "__main__":
    N = 4
    print(nthTerm(N))

# Expected output:
26.0
# find nth term of a 
def sum(n):
    ans = (n * (n - 1)) / 2
    return ans


n = 5
print(int(sum(n)))

# Expected output:
10
# program to find nth term of the series 3 , 12 , 29 , 54 , 87 , ... | calculate nth term of series ; return nth term ; 
def getNthTerm(N):
    return 4 * pow(N, 2) - 3 * N + 2


if __name__ == '__main__':
    N = 10
    print(getNthTerm(N))

# Expected output:
372
# find sum of product of number in given series | python 3 program to find sum of product of number in given series ; function to calculate ( a ^ b ) % p ; initialize result ; 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 return required answer ; modulo inverse of denominator ; calculating commentator part ; calculating t ! ; accumulating the final answer ; 
def power(x, y, p):
    res = 1
    x = x % p
    while y > 0:

        if y & 1:
            res = (res * x) % p
        y = y >> 1
        x = (x * x) % p

    return res


def sumProd(n, t):

    dino = power(t + 1, MOD - 2, MOD)

    ans = 1
    for i in range(n + t + 1, n, -1):
        ans = (ans % MOD * i % MOD) % MOD
    tfact = 1
    for i in range(1, t+1):
        tfact = (tfact * i) % MOD
    ans = ans * dino - tfact + MOD

    return ans % MOD


if __name__ == "__main__":
    MOD = 1000000007
    n, t = 3, 2
    print(sumProd(n, t))

# Expected output:
38
# find the sum of series 3 , 7 , 13 , 21 , 31. ... | function to calculate sum ; return sum ; 
def findSum(n):
    return (n * (pow(n, 2) + 3 * n + 5)) / 3


n = 25
print(int(findSum(n)))

# Expected output:
5875
# minimum players required to win the game | python 3 program to find minimum players required to win the game anyhow ; function to calculate ( a ^ b ) % ( 10 ^ 9 + 7 ) . ; function to find the minimum required player ; computing the nenomenator ; computing modulo inverse of denominator ; final result ; 

mod = 1000000007


def power(a, b):
    res = 1
    while (b):
        if (b & 1):
            res *= a
            res %= mod
        b //= 2
        a *= a
        a %= mod
    return res


def minPlayer(n, k):
    num = ((power(k, n) - 1) + mod) % mod
    den = (power(k - 1, mod - 2) + mod) % mod
    ans = (((num * den) % mod) * k) % mod
    return ans


if __name__ == "__main__":
    n, k = 3, 3
    print(minPlayer(n, k))

# Expected output:
39
# sum of factors of a number using prime factorization | using sieveoferatosthenes to find smallest prime factor of all the numbers . for example , if n is 10 , s [ 2 ] = s [ 4 ] = s [ 6 ] = s [ 10 ] = 2 s [ 3 ] = s [ 9 ] = 3 s [ 5 ] = 5 s [ 7 ] = 7 ; create a boolean list " prime [ 0 . . n ] " and initialize all entries in it as false . ; initializing smallest factor equal to 2 for all the even numbers ; for odd numbers less then equal to n ; s [ i ] for a prime is the number itself ; for all multiples of current prime number ; i is the smallest prime factor for number " i * j " . ; function to find sum of all prime factors ; declaring list to store smallest prime factor of i at i - th index ; filling values in s [ ] using sieve function calling ; current prime factor of n ; power of current prime factor ; n is now ns [ n ] . if new n also has smallest prime factor as currfactor , increment power ; update current prime factor as s [ n ] and initializing power of factor as 1. ; 
def sieveOfEratosthenes(N, s):
    prime = [False] * (N + 1)
    for i in range(2, N + 1, 2):
        s[i] = 2
    for i in range(3, N + 1, 2):
        if not prime[i]:
            s[i] = i
            for j in range(i, (N + 1) // i, 2):
                if prime[i * j] == False:
                    prime[i * j] = True
                    s[i * j] = i


def findSum(N):
    s = [0] * (N + 1)
    ans = 1
    sieveOfEratosthenes(N, s)
    currFactor = s[N]
    power = 1
    while N > 1:
        N //= s[N]
        if currFactor == s[N]:
            power += 1
            continue
        sum = 0
        for i in range(power + 1):
            sum += pow(currFactor, i)
        ans *= sum
        currFactor = s[N]
        power = 1
    return ans


if __name__ == "__main__":
    n = 12
    print("Sum of the factors is :", end=" ")
    print(findSum(n))

# Expected output:
Sum of the factors is : 28
# find multiples of 2 or 3 or 5 less than or equal to n | function to count number of multiples of 2 or 3 or 5 less than or equal to n ; as we have to check divisibility by three numbers , so we can implement bit masking ; we check whether jth bit is set or not , if jth bit is set , simply multiply to prod ; check for set bit ; check multiple of product ; 
def countMultiples(n):
    multiple = [2, 3, 5]
    count = 0
    mask = int(pow(2, 3))
    for i in range(1, mask):
        prod = 1
        for j in range(3):
            if (i & (1 << j)):
                prod = prod * multiple[j]
        if (bin(i).count('1') % 2 == 1):
            count = count + n // prod
        else:
            count = count - n // prod
    return count


if __name__ == '__main__':
    n = 10
    print(countMultiples(n))

# Expected output:
8
# minimum value of n such that xor from 1 to n is equal to k | function to find the value of n ; handling case for '0 ; handling case for '1 ; when number is completely divided by 4 then minimum ' x ' will be 'k ; when number divided by 4 gives 3 as remainder then minimum ' x ' will be 'k-1 ; else it is not possible to get k for any value of x ; let the 
def findN(k):
    if (k == 0):
        ans = 3
    if (k == 1):
        ans = 1
    elif (k % 4 == 0):
        ans = k
    elif (k % 4 == 3):
        ans = k - 1
    else:
        ans = -1
    return ans


k = 7
res = findN(k)
if (res == -1):
    print("Not possible")
else:
    print(res)

# Expected output:
6
# permutations to arrange n persons around a circular table | function to find no . of permutations ; 
def Circular(n):
    Result = 1
    while n > 0:
        Result = Result * n
        n -= 1
    return Result


if __name__ == "__main__":
    n = 4
    print(Circular(n - 1))

# Expected output:
6
# minimum time required to complete a work by n persons together | function to calculate the time ; 
def calTime(arr, n):
    work = 0
    for i in range(n):
        work += 1 / arr[i]
    return 1 / work


arr = [6.0, 3.0, 4.0]
n = len(arr)
print(calTime(arr, n), "Hours")

# Expected output:
1.3333333333333333 Hours
# find the largest twins in given range | function to find twins ; create a boolean array " prime [ 0 . . high ] " and initialize all entries it as true . a value in prime [ i ] will finally be false if i is not a prime , else true . ; look for the smallest twin ; if p is not marked , then it is a prime ; update all multiples of p ; now print the largest twin in range ; 

from math import sqrt, floor


def printTwins(low, high):
    prime = [True for i in range(high + 1)]
    twin = False
    prime[0] = False
    prime[1] = False
    k = floor(sqrt(high)) + 2
    for p in range(2, k, 1):
        if (prime[p]):
            for i in range(p * 2, high + 1, p):
                prime[i] = False
    i = high
    while (i >= low):
        if (prime[i] and (i - 2 >= low and prime[i - 2])):
            print("Largest twins in given range:(", (i - 2), ",", (i), ")")
            twin = True
            break
        i -= 1
    if (twin == False):
        print("No such pair exists")


if __name__ == '__main__':
    printTwins(10, 100)

# Expected output:
Largest twins in given range:( 71 , 73 )
# complement of a number with any base b | function to find ( b - 1 ) 's complement ; calculate number of digits in the given number ; largest digit in the number system with base b ; largest number in the number system with base b ; return complement ; function to find b 's complement ; b ' s ▁ complement ▁ = ▁ ( b - 1 ) ' s complement + 1 ; 
def prevComplement(n, b):
    maxNum, digits, num = 0, 0, n
    while n > 1:
        digits += 1
        n = n // 10
    maxDigit = b - 1
    while digits:
        maxNum = maxNum * 10 + maxDigit
        digits -= 1
    return maxNum - num


def complement(n, b):
    return prevComplement(n, b) + 1


if __name__ == "__main__":
    print(complement(25, 7))

# Expected output:
42
# minimum positive integer value possible of x for 
def gcd(a, b):
    if a == 0:
        return b
    return gcd(b % a, a)


a = 2
b = 4
print(gcd(a, b))

# Expected output:
2
# count elements in the given range which have maximum number of divisors | function to count the elements with maximum number of divisors ; to store number of divisors initialise with zero ; to store the maximum number of divisors ; to store required answer ; find the first divisible number ; count number of divisors ; find number of elements with maximum number of divisors ; 
def MaximumDivisors(X, Y):
    arr = [0] * (Y - X + 1)
    mx = 0
    cnt = 0
    i = 1
    while i * i <= Y:
        sq = i * i
        if ((X // i) * i >= X):
            first_divisible = (X // i) * i
        else:
            first_divisible = (X // i + 1) * i
        for j in range(first_divisible, Y + 1, i):
            if j < sq:
                continue
            elif j == sq:
                arr[j - X] += 1
            else:
                arr[j - X] += 2
        i += 1
    for i in range(X, Y + 1):
        if arr[i - X] > mx:
            cnt = 1
            mx = arr[i - X]
        elif arr[i - X] == mx:
            cnt += 1
    return cnt


if __name__ == "__main__":
    X = 1
    Y = 10
    print(MaximumDivisors(X, Y))

# Expected output:
3
# find first element in ap which is multiple of given prime | iterative function to calculate ( x ^ y ) % p in o ( log y ) ; initialize result ; 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 / 2 ; function to find nearest element in common ; base conditions ; 
def power(x, y, p):
    res = 1
    x = x % p
    while y > 0:
        if y & 1:
            res = (res * x) % p
        y = y >> 1
        x = (x * x) % p
    return res


def NearestElement(A, D, P):
    if A == 0:
        return 0
    elif D == 0:
        return -1
    else:
        X = power(D, P - 2, P)
        return (X * (P - A)) % P


if __name__ == "__main__":
    A, D, P = 4, 9, 11
    A %= P
    D %= P
    print(NearestElement(A, D, P))

# Expected output:
2
# cunningham chain | function to print cunningham chain of the first kind ; iterate till all elements are printed ; check prime or not ; 
def print_C(p0):
    i = 0
    while (True):
        flag = 1
        x = pow(2, i)
        p1 = x * p0 + (x - 1)
        for k in range(2, p1):
            if (p1 % k == 0):
                flag = 0
                break
        if (flag == 0):
            break
        print(p1, end=" ")
        i += 1


p0 = 2
print_C(p0)

# Expected output:
2 5 11 23 47
# count pairs with bitwise and as odd number | function to count number of odd pairs ; variable for counting odd pairs ; find all pairs ; find and operation check odd or even ; return number of odd pair ; 
def findOddPair(A, N):
    oddPair = 0
    for i in range(0, N - 1):
        for j in range(i + 1, N - 1):
            if ((A[i] & A[j]) % 2 != 0):
                oddPair = oddPair + 1
    return oddPair


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

# Expected output:
3
# sudo placement [ 1.7 ] | greatest digital root | function to return dig - sum ; function to print the digital roots ; store the largest digital roots ; iterate till sqrt ( n ) ; if i is a factor ; get the digit sum of both factors i and n / i ; if digit sum is greater then previous maximum ; if digit sum is greater then previous maximum ; if digit sum is same as then previous maximum , then check for larger divisor ; if digit sum is same as then previous maximum , then check for larger divisor ; print the digital roots ; 
def summ(n):
    if (n == 0):
        return 0
    if (n % 9 == 0):
        return 9
    else:
        return (n % 9)


def printDigitalRoot(n):
    maxi = 1
    dig = 1
    for i in range(1, int(pow(n, 1 / 2) + 1)):
        if (n % i == 0):
            d1 = summ(n / i)
            d2 = summ(i)
            if (d1 > maxi):
                dig = n / i
                maxi = d1
            if (d2 > maxi):
                dig = i
                maxi = d2
            if (d1 == maxi):
                if (dig < (n / i)):
                    dig = n / i
                    maxi = d1
            if (d2 == maxi):
                if (dig < i):
                    dig = i
                    maxi = d2
    print(int(dig), " ", int(maxi))


if __name__ == '__main__':
    n = 10
    printDigitalRoot(n)

# Expected output:
5   5
# sum of all elements up to nth row in a pascal triangle | function to find sum of all elements upto nth row . ; initialize sum with 0 ; calculate 2 ^ n ; 
def calculateSum(n):
    sum = 0
    sum = 1 << n
    return (sum - 1)


n = 10
print("Sum of all elements:", calculateSum(n))

# Expected output:
Sum of all elements: 1023
# divide two integers without using multiplication , division and mod operator | set2 | python3 program for above approach ; returns the quotient of dividend / divisor . ; calculate sign of divisor i . e . , sign will be negative only if either one of them is negative otherwise it will be positive ; remove signs of dividend and divisor ; zero division exception . ; using formula derived above . ; 

import math


def Divide(a, b):
    dividend = a
    divisor = b
    sign = -1 if ((dividend < 0) ^ (divisor < 0))else 1
    dividend = abs(dividend)
    divisor = abs(divisor)
    if (divisor == 0):
        print("Cannot Divide by 0")
    if (dividend == 0):
        print(a, "/", b, "is equal to :", 0)
    if (divisor == 1):
        print(a, "/", b, "is equal to :", (sign * dividend))
    print(
        a,
        "/",
        b,
        "is equal to :",
        math .floor(
            sign *
            math .exp(
                math .log(dividend) -
                math .log(divisor))))


a = 10
b = 5
Divide(a, b)
a = 49
b = -7
Divide(a, b)

# Expected output:
10 / 5 is equal to : 2
49 / -7 is equal to : -7
# represent the fraction of two numbers in the string format | function to return the required fraction in string format ; if the numerator is zero , answer is 0 ; if any one ( out of numerator and denominator ) is - ve , sign of resultant answer - ve . ; calculate the absolute part ( before decimal point ) . ; output string to store the answer ; append sign ; append the initial part ; if completely divisible , return answer . ; initialize remainder ; position at which fraction starts repeating if it exists ; if this remainder is already seen , then there exists a repeating fraction . ; index to insert parentheses ; calculate quotient , append it to result and calculate next remainder ; if repeating fraction exists , insert parentheses . ; return result . ; 
def calculateFraction(num, den):
    if (num == 0):
        return "0"
    sign = -1 if (num < 0) ^ (den < 0)else 1
    num = abs(num)
    den = abs(den)
    initial = num // den
    res = ""
    if (sign == -1):
        res += "-"
    res += str(initial)
    if (num % den == 0):
        return res
    res += "."
    rem = num % den
    mp = {}
    index = 0
    repeating = False
    while (rem > 0 and not repeating):
        if (rem in mp):
            index = mp[rem]
            repeating = True
            break
        else:
            mp[rem] = len(res)
        rem = rem * 10
        temp = rem // den
        res += str(temp)
        rem = rem % den
    if (repeating):
        res += ")"
        x = res[:index]
        x += "("
        x += res[index:]
        res = x
    return res


if __name__ == "__main__":
    num = 50
    den = 22
    print(calculateFraction(num, den))
    num = -1
    den = 2
    print(calculateFraction(num, den))

# Expected output:
2.(27)
-0.5
# check if the n | return if the nth term is even or odd . ; if a is even ; if b is even ; if b is odd ; if a is odd ; if b is odd ; if b is eve ; 
def findNature(a, b, n):
    if (n == 0):
        return (a & 1)
    if (n == 1):
        return (b & 1)
    if ((a & 1) == 0):
        if ((b & 1) == 0):
            return False
        else:
            return True if (n % 3 != 0)else False
    else:
        if ((b & 1) == 0):
            return True if ((n - 1) % 3 != 0)else False
        else:
            return True if ((n + 1) % 3 != 0)else False


a = 2
b = 4
n = 3
if (findNature(a, b, n)):
    print("Odd", end=" ")
else:
    print("Even", end=" ")

# Expected output:
Even
# check if mirror image of a number is same if displayed in seven segment display | return " yes " , if the mirror image of number is same as the given number else return " no " ; checking if the number contain only 0 , 1 , 8. ; checking if the number is palindrome or not . ; if corresponding index is not equal . ; 
def checkEqual(S):
    for i in range(len(S)):
        if (S[i] != '1' and S[i] != '0' and S[i] != '8'):
            return "No"
    start = 0
    end = len(S) - 1
    while (start < end):
        if (S[start] != S[end]):
            return "No"
        start += 1
        end -= 1
    return "Yes"


S = "101"
print(checkEqual(S))

# Expected output:
Yes
# check whether a given number is polydivisible or not | function to check polydivisible number ; digit extraction of input number ; store the digits in an array ; n contains first i digits ; n should be divisible by i ; 
def check_polydivisible(n):
    N = n
    digit = []
    while (n > 0):
        digit .append(n % 10)
        n /= 10
    digit .reverse()
    flag = True
    n = digit[0]
    for i in range(1, len(digit), 1):
        n = n * 10 + digit[i]
        if (n % (i + 1) != 0):
            flag = False
            break
    if (flag):
        print(N, "is Polydivisible number.")
    else:
        print(N, "is Not Polydivisible number.")


if __name__ == '__main__':
    n = 345654
    check_polydivisible(n)

# Expected output:
345654 is Not Polydivisible number.
# check if given number is a power of d where d is a power of 2 | python3 program to find if a number is power of d where d is power of 2. ; function to count the number of ways to paint n * 3 grid based on given conditions ; check if there is only one bit set in n ; count 0 bits before set bit ; if count is a multiple of log2 ( d ) then return true else false ; if there are more than 1 bit set then n is not a power of 4 ; 
def Log2n(n):
    return (1 + Log2n(n / 2))if (n > 1)else 0


def isPowerOfd(n, d):
    count = 0
    if (n and (n & (n - 1)) == 0):
        while (n > 1):
            n >>= 1
            count += 1
        return (count % (Log2n(d)) == 0)
    return False


n = 64
d = 8
if (isPowerOfd(n, d)):
    print(n, "is a power of", d)
else:
    print(n, "is not a power of", d)

# Expected output:
64 is a power of 8
# octahedral number | function to find octahedral number ; formula to calculate nth octahedral number ; 
def octahedral_num(n):
    return n * (2 * n * n + 1) // 3


if __name__ == '__main__':
    n = 5
    print(n, "th Octahedral number: ", octahedral_num(n))

# Expected output:
5 th Octahedral number:  85
# centered tetrahedral number | function to calculate centered tetrahedral number ; formula to calculate nth centered tetrahedral number and return it into main function ; 
def centeredTetrahedralNumber(n):
    return (2 * n + 1) * (n * n + n + 3) // 3


n = 6
print(centeredTetrahedralNumber(n))

# Expected output:
195
# swapping four variables without temporary variable | python 3 program to swap 4 variables without using temporary variable . ; swapping a and b variables ; swapping b and c variables ; swapping c and d variables ; 
def swap(a, b, c, d):
    a = a + b
    b = a - b
    a = a - b
    b = b + c
    c = b - c
    b = b - c
    c = c + d
    d = c - d
    c = c - d
    print("values after swapping are : ")
    print("a = ", a)
    print("b = ", b)
    print("c = ", c)
    print("d = ", d)


a = 1
b = 2
c = 3
d = 4
print("values before swapping are : ")
print("a = ", a)
print("b = ", b)
print("c = ", c)
print("d = ", d)
print("")
swap(a, b, c, d)

# Expected output:
values before swapping are : 
a =  1
b =  2
c =  3
d =  4

values after swapping are : 
a =  2
b =  3
c =  4
d =  1
# sum of first n natural numbers | function to find the sum of series ; 
def seriessum(n):
    sum = 0
    for i in range(1, n + 1):
        sum += i * (i + 1) / 2
    return sum


n = 4
print(seriessum(n))

# Expected output:
20.0
# centrosymmetric matrix | python3 program to check whether given matrix is centrosymmetric or not . ; finding the middle row of the matrix ; for each row upto middle row . ; if each element and its corresponding element is not equal then return false . ; 
def checkCentrosymmetricted(n, m):
    mid_row = 0
    if ((n & 1) > 0):
        mid_row = n / 2 + 1
    else:
        mid_row = n / 2
    for i in range(int(mid_row)):
        for j in range(n):
            if (m[i][j] != m[n - i - 1][n - j - 1]):
                return False
    return True


n = 3
m = [[1, 3, 5], [6, 8, 6], [5, 3, 1]]
if (checkCentrosymmetricted(n, m)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# centered triangular number | function for centered triangular number ; formula to calculate nth centered triangular number ; 
def Centered_Triangular_num(n):
    return (3 * n * n + 3 * n + 2) // 2


if __name__ == '__main__':
    n = 3
    print(Centered_Triangular_num(n))
    n = 12
    print(Centered_Triangular_num(n))

# Expected output:
19
235
# array with gcd of any of its subset belongs to the given array | python 3 implementation to generate the required array ; function to find gcd of array of numbers ; function to generate the array with required constraints . ; computing gcd of the given set ; solution exists if gcd of array is equal to the minimum element of the array ; printing the built array ; 

from math import gcd


def findGCD(arr, n):
    result = arr[0]
    for i in range(1, n):
        result = gcd(arr[i], result)
    return result


def compute(arr, n):
    answer = []
    GCD_of_array = findGCD(arr, n)
    if (GCD_of_array == arr[0]):
        answer .append(arr[0])
        for i in range(1, n):
            answer .append(arr[0])
            answer .append(arr[i])
        for i in range(len(answer)):
            print(answer[i], end=" ")
    else:
        print("No array can be build")


if __name__ == '__main__':
    n = 3
    input = [2, 5, 6, 7, 11]
    GCD = set()
    for i in range(len(input)):
        GCD .add(input[i])
    arr = []
    for i in GCD:
        arr .append(i)
    compute(arr, n)

# Expected output:
No array can be build