giulio98/xlcost-single-prompt huggingface dataset:
# program to find sum of the given sequence | python3 program to find the sum of the given sequence ; function to find moudulo inverse under 10 ^ 9 + 7 ; function to find the sum of the given sequence ; 

MOD = 1000000007


def modInv(x):
    n = MOD - 2
    result = 1
    while (n):
        if (n & 1):
            result = result * x % MOD
        x = x * x % MOD
        n = int(n / 2)
    return result


def getSum(n, k):
    ans = 1
    for i in range(n + 1, n - k, -1):
        ans = ans * i % MOD
    ans = ans * modInv(k + 1) % MOD
    return ans


n = 3
k = 2
print(getSum(n, k))

# Expected output:
8
# triplet with no element divisible by 3 and sum n | function to print a , b and c ; check if n - 2 is divisible by 3 or not ; 
def printCombination(n):
    print("1 ", end="")
    if ((n - 2) % 3 == 0):
        print("2", n - 3, end="")
    else:
        print("1", (n - 2), end="")


if __name__ == '__main__':
    n = 233
    printCombination(n)

# Expected output:
1 2 230
# pairs with gcd equal to one in the given range | function to print all pairs ; check if even ; we can print all adjacent pairs for i in range ( l , r , 2 ) : print ( " { " , i , " , " , i + 1 , " } , " ) ; 
def checkPairs(l, r):
    if (l - r) % 2 == 0:
        return False
    return True


if __name__ == "__main__":
    l, r = 1, 8
    if checkPairs(l, r):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# check if a number with even number of digits is palindrome or not | function to check if the number is palindrome ; if divisible by 11 then return true ; if not divisible by 11 then return false ; 
def isPalindrome(n):
    if n % 11 == 0:
        return True
    return False


if __name__ == "__main__":
    n = 123321
    if isPalindrome(n):
        print("Palindrome")
    else:
        print("Not Palindrome")

# Expected output:
Palindrome
# number of permutations such that no three terms forms increasing subsequence | 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 ) ; 
def binomialCoeff(n, k):
    res = 1
    if k > n - k:
        k = n - k
    for i in range(k):
        res = res * (n - i)
        res = res // (i + 1)
    return res


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


n = 3
print(catalan(n))

# Expected output:
5
# fascinating number | function to check if number is fascinating or not ; frequency count array using 1 indexing ; obtaining the resultant number using string concatenation ; traversing the string character by character ; gives integer value of a character digit ; to check if any digit has appeared multiple times ; traversing through freq array to check if any digit was missing ; 
def isFascinating(num):
    freq = [0] * 10
    val = (str(num) + str(num * 2) + str(num * 3))
    for i in range(len(val)):
        digit = int(val[i])
        if freq[digit] and digit != 0 > 0:
            return False
        else:
            freq[digit] += 1
    for i in range(1, 10):
        if freq[i] == 0:
            return False
    return True


if __name__ == "__main__":
    num = 192
    if num < 100:
        print("No")
    else:
        ans = isFascinating(num)
        if ans:
            print("Yes")
        else:
            print("No")

# Expected output:
Yes
# count ways to distribute m items among n people | function used to generate binomial coefficient time complexity o ( m ) ; helper function for generating no of ways to distribute m mangoes amongst n people ; not enough mangoes to be distributed ; ways -> ( n + m - 1 ) c ( n - 1 ) ; 
def binomial_coefficient(n, m):
    res = 1
    if m > n - m:
        m = n - m
    for i in range(0, m):
        res *= (n - i)
        res /= (i + 1)
    return res


def calculate_ways(m, n):
    if m < n:
        return 0
    ways = binomial_coefficient(n + m - 1, n - 1)
    return int(ways)


if __name__ == '__main__':
    m = 7
    n = 5
    result = calculate_ways(m, n)
    print(result)

# Expected output:
330
# queries to count the number of unordered co | python3 program to find number of unordered coprime pairs of integers from 1 to n ; to store euler 's totient function ; to store required answer ; computes and prints totient of all numbers smaller than or equal to n . ; initialise the phi [ ] with 1 ; compute other phi values ; if phi [ p ] is not computed already , then number p is prime ; phi of a prime number p is always equal to p - 1. ; update phi values of all multiples of p ; add contribution of p to its multiple i by multiplying with ( 1 - 1 / p ) ; function to compute number coprime pairs ; function call to compute euler totient function ; prefix sum of all euler totient function values ; 

N = 100005
phi = [0] * N
S = [0] * N


def computeTotient():
    for i in range(1, N):
        phi[i] = i
    for p in range(2, N):
        if (phi[p] == p):
            phi[p] = p - 1
            for i in range(2 * p, N, p):
                phi[i] = (phi[i] // p) * (p - 1)


def CoPrimes():
    computeTotient()
    for i in range(1, N):
        S[i] = S[i - 1] + phi[i]


if __name__ == "__main__":
    CoPrimes()
    q = [3, 4]
    n = len(q)
    for i in range(n):
        print("Numberofunorderedcoprime" +
              "pairsofintegersfrom1to",  q[i],  "are", S[q[i]])

# Expected output:
Numberofunorderedcoprimepairsofintegersfrom1to 3 are 4
Numberofunorderedcoprimepairsofintegersfrom1to 4 are 6
# n | function to return the decimal value of a binary number ; initializing base value to 1 , i . e 2 ^ 0 ; find the binary representation of the n - th number in sequence ; base case ; answer string ; add n - 1 1 's ; add 0 ; add n 1 's at end ; 
def binaryToDecimal(n):
    num = n
    dec_value = 0
    base = 1
    l = len(num)
    for i in range(l - 1, -1, -1):
        if (num[i] == '1'):
            dec_value += base
        base = base * 2
    return dec_value


def numberSequence(n):
    if (n == 1):
        return 1
    s = ""
    for i in range(1, n):
        s += '1'
    s += '0'
    for i in range(1, n + 1):
        s += '1'
    num = binaryToDecimal(s)
    return num


if __name__ == "__main__":
    n = 4
    print(numberSequence(n))

# Expected output:
239
# n | calculate nth term of series ; calculates the n - th term ; 
def numberSequence(n):
    num = pow(4, n) - pow(2, n) - 1
    return num


if __name__ == "__main__":
    n = 4
    print(numberSequence(n))

# Expected output:
239
# alternate primes till n | function for checking number is prime or not ; if flag = 0 then number is prime and return 1 otherwise return 0 ; function for printing alternate prime number ; counter is initialize with 0 ; looping through 2 to n - 1 ; function calling along with if condition ; if counter is multiple of 2 then only print prime number ; 
def prime(num):
    flag = 0
    for i in range(2, num // 2 + 1):
        if num % i == 0:
            flag = 1
            break
    if flag == 0:
        return 1
    else:
        return 0


def print_alternate_prime(n):
    counter = 0
    for num in range(2, n):
        if prime(num) == 1:
            if counter % 2 == 0:
                print(num, end=" ")
            counter += 1


if __name__ == "__main__":
    n = 15
    print("Following are the alternate prime" +
          "number smaller than or equal to", n)
    print_alternate_prime(n)

# Expected output:
Following are the alternate primenumber smaller than or equal to 15
2 5 11
# alternate primes till n | python 3 program to print all equal to n using sieve of eratosthenes ; create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . a value in prime [ i ] will finally be false if i is not a prime , else true . ; if prime [ p ] is not changed , then it is a prime ; update all multiples of p ; print all prime numbers ; for next prime to get printed ; 
def SieveOfEratosthenes(n):
    prime = [None] * (n + 1)
    for i in range(len(prime)):
        prime[i] = True
    p = 2
    while p * p <= n:
        if (prime[p]):
            for i in range(p * 2, n + 1, p):
                prime[i] = False
        p += 1
    flag = True
    for p in range(2, n + 1):
        if (prime[p]):
            if (flag):
                print(str(p), end=" ")
                flag = False
            else:
                flag = True


if __name__ == "__main__":
    n = 15
    print(
        "Following are the alternate" +
        " prime numbers smaller " +
        "than or equal to " +
        str(n))
    SieveOfEratosthenes(n)

# Expected output:
Following are the alternate prime numbers smaller than or equal to 15
2 5 11
# find maximum among x ^ ( y ^ 2 ) or y ^ ( x ^ 2 ) where x and y are given | function to find maximum ; case 1 ; case 2 ; 
def findGreater(x, y):
    if (x > y):
        return False
    else:
        return True


x = 4
y = 9
if (findGreater(x, y)):
    print("1")
else:
    print("2")

# Expected output:
1
# maximum profit after buying and selling stocks with transaction fees | python3 implementation of above approach ; b [ 0 ] will contain the maximum profit ; b [ 1 ] will contain the day on which we are getting the maximum profit ; here finding the max profit ; if we get less then or equal to zero it means we are not getting the profit ; check if sum is greater then maximum then store the new maximum ; 
def max_profit(a, b, n, fee):
    i, j, profit = 1, n - 1, 0
    l, r, diff_day = 0, 0, 1
    b[0] = 0
    b[1] = diff_day
    for i in range(1, n):
        l = 0
        r = diff_day
        Sum = 0
        for j in range(n - 1, i - 1, -1):
            profit = (a[r] - a[l]) - fee
            if (profit > 0):
                Sum = Sum + profit
            l += 1
            r += 1
        if (b[0] < Sum):
            b[0] = Sum
            b[1] = diff_day
    diff_day += 1
    return 0


arr = [6, 1, 7, 2, 8, 4]
n = len(arr)
b = [0 for i in range(2)]
tranFee = 2
max_profit(arr, b, n, tranFee)
print(b[0], ",", b[1])

# Expected output:
8 , 1
# eggs dropping puzzle ( binomial coefficient and binary search solution ) | find sum of binomial coefficients xci ( where i varies from 1 to n ) . if the sum becomes more than k ; do binary search to find minimum number of trials in worst case . ; initialize low and high as 1 st and last floors ; do binary search , for every mid , find sum of binomial coefficients and check if the sum is greater than k or not . ; 
def binomialCoeff(x, n, k):
    sum = 0
    term = 1
    i = 1
    while (i <= n and sum < k):
        term *= x - i + 1
        term /= i
        sum += term
        i += 1
    return sum


def minTrials(n, k):
    low = 1
    high = k
    while (low < high):
        mid = int((low + high) / 2)
        if (binomialCoeff(mid, n, k) < k):
            low = mid + 1
        else:
            high = mid
    return int(low)


print(minTrials(2, 10))

# Expected output:
4
# find next palindrome prime | python3 program to find next palindromic prime for a given number . ; if ( 8 <= n <= 11 ) return 11 ; generate odd length palindrome number which will cover given constraint . ; if y >= n and it is a prime number then return it . ; 

import math as mt


def isPrime(num):
    if (num < 2 or num % 2 == 0):
        return num == 2
    for i in range(3, mt .ceil(mt .sqrt(num + 1))):
        if (num % i == 0):
            return False
    return True


def primePalindrome(N):
    if (8 <= N and N <= 11):
        return 11
    for x in range(1, 100000):
        s = str(x)
        d = s[::-1]
        y = int(s + d[1:])
        if (y >= N and isPrime(y)):
            return y


print(primePalindrome(112))

# Expected output:
131
# number of integral solutions for equation x = b * ( sumofdigits ( x ) ^ a ) + c | this function returns the sum of the digits of a number ; this function creates the array of valid numbers ; this computes s ( x ) ^ a ; this gives the result of equation ; checking if the sum same as i ; counter to keep track of numbers ; resultant array ; prints the number ; 
def getsum(a):
    r = 0
    sum = 0
    while (a > 0):
        r = a % 10
        sum = sum + r
        a = a // 10
    return sum


def value(a, b, c):
    x = 0
    q = 0
    w = 0
    v = []
    for i in range(1, 82):
        no = pow(i, a)
        no = b * no + c
        if (no > 0 and no < 1000000000):
            x = getsum(no)
            if (x == i):
                q += 1
                v .append(no)
                w += 1
    for i in range(len(v)):
        print(v[i], end=" ")


if __name__ == "__main__":
    a = 2
    b = 2
    c = -1
    value(a, b, c)

# Expected output:
1 31 337 967
# cunningham chain | function to print cunningham chain of the second kind ; iterate till all elements are printed ; check prime or not ; 
def print_t(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 = 19
print_t(p0)

# Expected output:
19 37 73
# count number of right triangles possible with a given perimeter | function to return the count ; making a list to store ( a , b ) pairs ; no triangle if p is odd ; make ( a , b ) pair in sorted order ; check to avoid duplicates ; store the new pair ; 
def countTriangles(p):
    store = []
    if p % 2 != 0:
        return 0
    else:
        count = 0
        for b in range(1, p // 2):
            a = p / 2 * ((p - 2 * b) / (p - b))
            inta = int(a)
            if (a == inta):
                ab = tuple(sorted((inta, b)))
                if ab not in store:
                    count += 1
                    store .append(ab)
        return count


p = 840
print("number of right triangles = " + str(countTriangles(p)))

# Expected output:
number of right triangles = 8
# count pairs with bitwise and as odd number | python program to count pairs with odd and ; count total odd numbers ; return count of even pair ; 
def findOddPair(A, N):
    count = 0
    for i in range(0, N - 1):
        if ((A[i] % 2 == 1)):
            count = count + 1
    return count * (count - 1) / 2


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

# Expected output:
3
# surd number | define a issurd function which returns true if x is surd number . ; try all powers of i ; 
def isSurd(n):
    i = 2
    for i in range(2, (i * i) + 1):
        j = i
        while (j < n):
            j = j * i
        if (j == n):
            return False
    return True


if __name__ == "__main__":
    n = 15
    if (isSurd(n)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# program to find last two digits of 2 ^ n | 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 number of digits in x ; function to print last 2 digits of 2 ^ n ; generating 10 ^ 2 ; calling modular exponentiation ; printing leftmost zeros . since ( 2 ^ n ) % 2 can have digits less then 2. in that case we need to print zeros ; if temp is not zero then print temp if temp is zero then already printed ; 
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 numberOfDigits(x):

    i = 0
    while (x):
        x //= 10
        i += 1

    return i


def LastTwoDigit(n):

    print("Last " + str(2) +
          " digits of " + str(2), end="")
    print("^" + str(n) + " = ", end="")
    temp = 1
    for i in range(1, 3):
        temp *= 10
    temp = power(2, n, temp)
    for i in range(2 - numberOfDigits(temp)):
        print(0, end="")
    if temp:
        print(temp)


if __name__ == "__main__":
    n = 72
    LastTwoDigit(n)

# Expected output:
Last 2 digits of 2^72 = 96
# find gcd ( a ^ n , c ) where a , n and c can vary from 1 to 10 ^ 9 | 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 ; finds gcd of a and b ; finds gcd of a ^ n and c ; check if c is a divisor of a ; first compute ( a ^ n ) % c ; now simply return gcd of modulo power and c . ; 
def modPower(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 gcd(a, b):

    if (b == 0):
        return a
    return gcd(b, a % b)


def gcdPow(a, n, c):
    if (a % c == 0):
        return c
    modexpo = modPower(a, n, c)
    return gcd(modexpo, c)


if __name__ == "__main__":
    a = 10248585
    n = 1000000
    c = 12564
    print(gcdPow(a, n, c))

# Expected output:
9
# number of sub arrays with odd sum | python 3 code to find count of sub - arrays with odd sum ; find sum of all subarrays and increment result if sum is odd ; 
def countOddSum(ar, n):
    result = 0
    for i in range(n):
        val = 0
        for j in range(i, n):
            val = val + ar[j]
            if (val % 2 != 0):
                result += 1
    return (result)


ar = [5, 4, 4, 5, 1, 3]
print("The Number of Subarrays", "with odd", end="")
print(" sum is " + str(countOddSum(ar, 6)))

# Expected output:
The Number of Subarrays with odd sum is 12
# number of sub arrays with odd sum | python 3 proggram to find count of sub - arrays with odd sum ; a temporary array of size 2. temp [ 0 ] is going to store count of even subarrays and temp [ 1 ] count of odd . temp [ 0 ] is initialized as 1 because there is a single odd element is also counted as a subarray ; initialize count . sum is sum of elements under modulo 2 and ending with arr [ i ] . ; i ' th ▁ iteration ▁ computes ▁ ▁ sum ▁ of ▁ arr [ 0 . . i ] ▁ under ▁ ▁ modulo ▁ 2 ▁ and ▁ increments ▁ ▁ even / odd ▁ count ▁ according ▁ ▁ to ▁ sum ' s value ; 2 is added to handle negative numbers ; increment even / odd count ; an odd can be formed by even - odd pair ; 
def countOddSum(ar, n):
    temp = [1, 0]
    result = 0
    val = 0
    for i in range(n):
        val = ((val + ar[i]) % 2 + 2) % 2
        temp[val] += 1
    result = (temp[0] * temp[1])
    return (result)


ar = [5, 4, 4, 5, 1, 3]
print("The Number of Subarrays" " with odd sum is " + str(countOddSum(ar, 6)))

# Expected output:
The Number of Subarrays with odd sum is 12
# program to print factors of a number in pairs | python 3 program to print prime factors in pairs . ; 
def printPFsInPairs(n):
    for i in range(1, int(pow(n, 1 / 2)) + 1):
        if n % i == 0:
            print(str(i) + "*" + str(int(n / i)))


n = 24
printPFsInPairs(n)

# Expected output:
1*24
2*12
3*8
4*6
# sum of elements in range l | function to find the sum between l and r ; array created ; fill the first half of array ; fill the second half of array ; find the sum between range ; 
def rangesum(n, l, r):
    arr = [0] * n
    c = 1
    i = 0
    while (c <= n):
        arr[i] = c
        i += 1
        c += 2
    c = 2
    while (c <= n):
        arr[i] = c
        i += 1
        c += 2
    sum = 0
    for i in range(l - 1, r, 1):
        sum += arr[i]
    return sum


if __name__ == '__main__':
    n = 12
    l, r = 1, 11
    print(rangesum(n, l, r))

# Expected output:
66
# sum of elements in range l | function to calculate the sum if n is even ; both l and r are to the left of mid ; first and last element ; total number of terms in the sequence is r - l + 1 ; use of formula derived ; both l and r are to the right of mid ; first and last element ; use of formula derived ; left is to the left of mid and right is to the right of mid ; take two sums i . e left and right differently and add ; first and last element ; total terms ; no of terms ; the first even number is 2 ; the last element is ; formula applied ; function to calculate the sum if n is odd ; take ceil value if n is odd ; both l and r are to the left of mid ; first and last element ; number of terms ; formula ; both l and r are to the right of mid ; first and last term , ; no of terms ; formula used ; if l is on left and r on right ; calculate separate sums ; first half ; calculate terms ; second half ; add both halves ; function to find the sum between l and r ; if n is even ; if n is odd ; 
def sumeven(n, l, r):
    sum = 0
    mid = n // 2
    if (r <= mid):
        first = (2 * l - 1)
        last = (2 * r - 1)
        no_of_terms = r - l + 1
        sum = ((no_of_terms) * ((first + last))) // 2
    elif (l >= mid):
        first = (2 * (l - n // 2))
        last = (2 * (r - n // 2))
        no_of_terms = r - l + 1
        sum = ((no_of_terms) * ((first + last))) // 2
    else:
        sumleft, sumright = 0, 0
        first_term1 = (2 * l - 1)
        last_term1 = (2 * (n // 2) - 1)
        no_of_terms1 = n // 2 - l + 1
        sumleft = (((no_of_terms1) * ((first_term1 + last_term1))) // 2)
        first_term2 = 2
        last_term2 = (2 * (r - n // 2))
        no_of_terms2 = r - mid
        sumright = (((no_of_terms2) * ((first_term2 + last_term2))) // 2)
        sum = (sumleft + sumright)
    return sum


def sumodd(n, l, r):
    mid = n // 2 + 1
    sum = 0
    if (r <= mid):
        first = (2 * l - 1)
        last = (2 * r - 1)
        no_of_terms = r - l + 1
        sum = (((no_of_terms) * ((first + last))) // 2)
    elif (l > mid):
        first = (2 * (l - mid))
        last = (2 * (r - mid))
        no_of_terms = r - l + 1
        sum = (((no_of_terms) * ((first + last))) // 2)
    else:
        sumleft, sumright = 0, 0
        first_term1 = (2 * l - 1)
        last_term1 = (2 * mid - 1)
        no_of_terms1 = mid - l + 1
        sumleft = (((no_of_terms1) * ((first_term1 + last_term1))) // 2)
        first_term2 = 2
        last_term2 = (2 * (r - mid))
        no_of_terms2 = r - mid
        sumright = (((no_of_terms2) * ((first_term2 + last_term2))) // 2)
        sum = (sumleft + sumright)
    return sum


def rangesum(n, l, r):
    sum = 0
    if (n % 2 == 0):
        return sumeven(n, l, r)
    else:
        return sumodd(n, l, r)


if __name__ == "__main__":
    n = 12
    l = 1
    r = 11
    print(rangesum(n, l, r))

# Expected output:
66
# program to find the interior and exterior angle of a regular polygon | function to find the interior and exterior angle ; formula to find the interior angle ; formula to find the exterior angle ; displaying the output ; 
def findAngle(n):
    interiorAngle = int((n - 2) * 180 / n)
    exteriorAngle = int(360 / n)
    print("Interior angle: ", interiorAngle)
    print("Exterior angle: ", exteriorAngle)


n = 10
findAngle(n)

# Expected output:
Interior angle:  144
Exterior angle:  36
# program to calculate distance between two points in 3 d | python program to find distance between two points in 3 d . ; function to find distance ; 

import math


def distance(x1, y1, z1, x2, y2, z2):
    d = math .sqrt(
        math .pow(
            x2 -
            x1,
            2) +
        math .pow(
            y2 -
            y1,
            2) +
        math .pow(
            z2 -
            z1,
            2) *
        1.0)
    print("Distance is ")
    print(d)


x1 = 2
y1 = -5
z1 = 7
x2 = 3
y2 = 4
z2 = 5
distance(x1, y1, z1, x2, y2, z2)

# Expected output:
Distance is 
9.273618495495704
# check if the large number formed is divisible by 41 or not | check if a number is divisible by 41 or not ; array to store all the digits ; base values ; calculate remaining digits ; calculate answer ; check for divisibility ; 
def DivisibleBy41(first, second, c, n):
    digit = [0] * n
    digit[0] = first
    digit[1] = second
    for i in range(2, n):
        digit[i] = (digit[i - 1] * c + digit[i - 2]) % 10
    ans = digit[0]
    for i in range(1, n):
        ans = (ans * 10 + digit[i]) % 41
    if (ans % 41 == 0):
        return True
    else:
        return False


first = 1
second = 2
c = 1
n = 3
if (DivisibleBy41(first, second, c, n)):
    print("YES")
else:
    print("NO")

# Expected output:
YES