giulio98/xlcost-single-prompt huggingface dataset:
# construct an array from its pair | python3 implementation of the approach ; utility function to print the array ; function to generate the original array from the pair - product array ; first element of the resulting array ; find all the other elements ; print the elements of the generated array ; 

from math import sqrt


def printArr(arr, n):
    for i in range(n):
        print(arr[i], end=" ")


def constructArr(pair, n):
    size = int((1 + sqrt(1 + 8 * n)) // 2)
    arr = [0] * (size)
    arr[0] = int(sqrt((pair[0] * pair[1]) / pair[size - 1]))
    for i in range(1, size):
        arr[i] = pair[i - 1] // arr[0]
    printArr(arr, size)


if __name__ == "__main__":
    pair = [48, 18, 24, 24, 32, 12]
    n = len(pair)
    constructArr(pair, n)

# Expected output:
6 8 3 4
# count of odd and even sum pairs in an array | function to find the count of pairs with odd sum and the count of pairs with even sum ; to store the count of even and odd number from the array ; if the current element is even ; if it is odd ; to store the count of pairs with even sum ; all the even elements will make pairs with each other and the sum of the pair will be even ; all the odd elements will make pairs with each other and the sum of the pair will be even ; to store the count of pairs with odd sum ; all the even elements will make pairs with all the odd element and the sum of the pair will be odd ; 
def findPairs(arr, n):
    cntEven = 0
    cntOdd = 0
    for i in range(n):
        if (arr[i] % 2 == 0):
            cntEven += 1
        else:
            cntOdd += 1
    evenPairs = 0
    evenPairs += ((cntEven * (cntEven - 1)) // 2)
    evenPairs += ((cntOdd * (cntOdd - 1)) // 2)
    oddPairs = 0
    oddPairs += (cntEven * cntOdd)
    print("Odd pairs = ", oddPairs)
    print("Even pairs = ", evenPairs)


if __name__ == "__main__":
    arr = [1, 2, 3, 4, 5]
    n = len(arr)
    findPairs(arr, n)

# Expected output:
Odd pairs =  6
Even pairs =  4
# number of ways to distribute n paper set among m students | python3 implementation of the approach ; function to return n ! % 1000000007 ; to store the factorial ; find the factorial ; function to return the count of possible ways ; 

MOD = 1000000007


def factMod(n):
    fact = 1
    for i in range(2, n + 1):
        fact *= (i % MOD)
        fact %= MOD
    return fact


def countWays(n, m):
    return factMod(m)


if __name__ == "__main__":
    n = 2
    m = 2
    print(countWays(n, m))

# Expected output:
2
# program for nth fuss – catalan number | 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 fusscatalan number in o ( n ) time ; calculate value of 3 ncn ; return 3 ncn / ( 2 n + 1 ) ; 
def binomialCoeff(n, k):
    res = 1
    if (k > n - k):
        k = n - k
    for i in range(k):
        res *= (n - i)
        res //= (i + 1)
    return res


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


if __name__ == "__main__":
    for i in range(10):
        print(Fuss_catalan(i), end=" ")

# Expected output:
1 1 3 12 55 273 1428 7752 43263 246675
# check if a number is euler pseudoprime | python3 program for nth fusscatalan number ; function that returns true if n is composite ; check if there is any divisor of n . we only need check divisor till sqrt ( n ) because if there is divisor which is greater than sqrt ( n ) then there must be a divisor which is less than sqrt ( n ) ; iterative function to calculate ( x ^ y ) % p in o ( log y ) ; initialize result ; update x if it is greater 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 that returns true if n is euler pseudoprime to the base a ; invalid base ; n is not a composite odd number ; if a and n are not coprime ; all the conditions for euler pseudoprime are satisfied ; 

from math import gcd, sqrt


def isComposite(n):
    for i in range(2, int(sqrt(n)) + 1):
        if (n % i == 0):
            return True

    return False


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 isEulerPseudoprime(N, A):

    if (A <= 0):
        return False
    if (N % 2 == 0 or not isComposite(N)):
        return False
    if (gcd(A, N) != 1):
        return false

    mod = Power(A, (N - 1) // 2, N)
    if (mod != 1 and mod != N - 1):
        return False
    return True


if __name__ == "__main__":

    N = 121
    A = 3

    if (isEulerPseudoprime(N, A)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# number of non | function to return the required count ; to store the final result ; two pointer loop ; initialising j ; looping till the subarray increases ; update ret ; update i ; return ret ; 
def findCnt(arr, n, k):
    ret = 0
    i = 0
    while (i < n):
        j = i + 1
        while (j < n and arr[j] >= arr[j - 1]):
            j += 1
        x = max(0, j - i - k)
        ret += (x * (x + 1)) / 2
        i = j
    return ret


if __name__ == "__main__":
    arr = [5, 4, 3, 2, 1]
    n = len(arr)
    k = 2
    print(findCnt(arr, n, k))

# Expected output:
0.0
# count of elements which are second smallest among three consecutive elements | function to return the count of elements p [ i ] such that p [ i ] is the second smallest among p [ i 1 ] , p [ i ] and p [ i + 1 ] ; to store the required answer ; traverse from the second element to the second last element ; return the required answer ; 
def countElements(p, n):
    ans = 0
    for i in range(1, n - 1):
        if (p[i - 1] > p[i] and p[i] > p[i + 1]):
            ans += 1
        elif (p[i - 1] < p[i] and p[i] < p[i + 1]):
            ans += 1
    return ans


if __name__ == "__main__":
    p = [2, 5, 1, 3, 4]
    n = len(p)
    print(countElements(p, n))

# Expected output:
1
# count integers in the range [ a , b ] that are not divisible by c and d | python3 implementation of the approach ; function to return the count of integers from the range [ a , b ] that are not divisible by c and d ; numbers which are divisible by c ; numbers which are divisible by d ; find lowest common factor of c and d ; numbers which are divisible by both c and d ; return the required answer ; 

from math import gcd


def countNums(a, b, c, d):
    x = b // c - (a - 1) // c
    y = b // d - (a - 1) // d
    k = (c * d) // gcd(c, d)
    z = b // k - (a - 1) // k
    return (b - a + 1 - x - y + z)


if __name__ == "__main__":
    a = 10
    b = 50
    c = 4
    d = 6
    print(countNums(a, b, c, d))

# Expected output:
28
# number of non | function to return the count of increasing subarrays of length k ; to store the final result ; two pointer loop ; initialising j ; looping till the subarray increases ; updating the required count ; updating i ; returning res ; 
def cntSubArrays(arr, n, k):
    res = 0
    i = 0
    while (i < n):
        j = i + 1
        while (j < n and arr[j] >= arr[j - 1]):
            j += 1
        res += max(j - i - k + 1, 0)
        i = j
    return res


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

# Expected output:
3
# longest sub | function to return the length of the largest subsequence with minimum possible lcm ; minimum value from the array ; to store the frequency of the minimum element in the array ; if current element is equal to the minimum element ; 
def maxLen(arr, n):
    min_val = min(arr)
    freq = 0
    for i in range(n):
        if (arr[i] == min_val):
            freq += 1
    return freq


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

# Expected output:
2
# program to find the last digit of x in base y | function to find the last digit of x in base y ; 
def last_digit(X, Y):
    print(X % Y)


if __name__ == "__main__":
    X = 55
    Y = 3
    last_digit(X, Y)

# Expected output:
1
# count of squares that can be drawn without lifting the pencil | function to return the count of squares that can be formed ; 
def countSquares(n):
    return (pow(n, 2) - (2 * n) + 2)


if __name__ == "__main__":
    n = 2
    print(countSquares(n))

# Expected output:
2
# integer part of the geometric mean of the divisors of n | python3 implementation of the approach ; function to return the integer part of the geometric mean of the divisors of n ; 

from math import sqrt


def geometricMean(n):
    return int(sqrt(n))


if __name__ == "__main__":
    n = 16
    print(geometricMean(n))

# Expected output:
4
# number of k 's such that the given array can be divided into two sets satisfying the given conditions | function to return the count of k 's such that the array can be divided into two sets containing equal number of elements when all the elements less than k are in one set and the rest of the elements are in the other set ; sort the given array ; return number of such ks ; 
def two_sets(a, n):
    a .sort()
    return (a[n // 2] - a[(n // 2) - 1])


if __name__ == "__main__":
    a = [1, 4, 4, 6, 7, 9]
    n = len(a)
    print(two_sets(a, n))

# Expected output:
2
# count of pairs in an array such that the highest power of 2 that divides their product is 1 | function to return the count of valid pairs ; to store the count of odd numbers and the count of even numbers such that 2 is the only even factor of that number ; if current number is odd ; if current number is even and 2 is the only even factor of it ; calculate total number of valid pairs ; 
def cntPairs(a, n):
    odd = 0
    even = 0
    for i in range(n):
        if (a[i] % 2 == 1):
            odd += 1
        elif ((a[i] / 2) % 2 == 1):
            even += 1
    ans = odd * even + (odd * (odd - 1)) // 2
    return ans


if __name__ == "__main__":
    a = [4, 2, 7, 11, 14, 15, 18]
    n = len(a)
    print(cntPairs(a, n))

# Expected output:
12
# make the array non | function to make array non - decreasing ; take the first element ; perform the operation ; traverse the array ; next element ; if next element is greater than the current element then decrease it to increase the possibilities ; it is not possible to make the array non - decreasing with the given operation ; next element is now the current ; the array can be made non - decreasing with the given operation ; 
def isPossible(a, n):
    cur = a[0]
    cur -= 1
    for i in range(1, n):
        nxt = a[i]
        if (nxt > cur):
            nxt -= 1
        elif (nxt < cur):
            return False
        cur = nxt
    return True


if __name__ == "__main__":
    a = [1, 2, 1, 2, 3]
    n = len(a)
    if (isPossible(a, n)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# find the maximum element in the array other than ai | function to find maximum element among ( n - 1 ) elements other than a [ i ] for each i from 1 to n ; to store prefix max element ; to store suffix max element ; find the maximum element in the array other than a [ i ] ; 
def max_element(a, n):
    pre = [0] * n
    pre[0] = a[0]
    for i in range(1, n):
        pre[i] = max(pre[i - 1], a[i])
    suf = [0] * n
    suf[n - 1] = a[n - 1]
    for i in range(n - 2, -1, -1):
        suf[i] = max(suf[i + 1], a[i])
    for i in range(n):
        if (i == 0):
            print(suf[i + 1], end=" ")
        elif (i == n - 1):
            print(pre[i - 1], end=" ")
        else:
            print(max(pre[i - 1], suf[i + 1]), end=" ")


if __name__ == "__main__":
    a = [2, 5, 6, 1, 3]
    n = len(a)
    max_element(a, n)

# Expected output:
6 6 5 6 6
# find the kth position element of the given sequence | function to return the kth number from the required sequence ; count of odd integers in the sequence ; kth number is even ; it is odd ; 
def kthNum(n, k):
    a = (n + 1) // 2
    if (k > a):
        return (2 * (k - a))
    return (2 * k - 1)


if __name__ == "__main__":
    n = 7
    k = 7
    print(kthNum(n, k))

# Expected output:
6
# find k such that | a | function to find k such that | a - k | = | b - k | ; if ( a + b ) is even ; 
def find_k(a, b):
    if ((a + b) % 2 == 0):
        return ((a + b) // 2)
    return -1


if __name__ == "__main__":
    a = 2
    b = 16
    print(find_k(a, b))

# Expected output:
9
# increasing permutation of first n natural numbers | function that returns true if it is possible to make the permutation increasing by swapping any two numbers ; to count misplaced elements ; count all misplaced elements ; if possible ; 
def isPossible(a, n):
    k = 0
    for i in range(n):
        if (a[i] != i + 1):
            k += 1
    if (k <= 2):
        return True
    return False


if __name__ == "__main__":
    a = [5, 2, 3, 4, 1]
    n = len(a)
    if (isPossible(a, n)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# find the number of positive integers less than or equal to n that have an odd number of digits | function to return the number of positive integers less than or equal to n that have odd number of digits ; 
def odd_digits(n):
    if (n < 10):
        return n
    elif (n / 10 < 10):
        return 9
    elif (n / 100 < 10):
        return 9 + n - 99
    elif (n / 1000 < 10):
        return 9 + 900
    elif (n / 10000 < 10):
        return 909 + n - 9999
    else:
        return 90909


if __name__ == "__main__":
    n = 893
    print(odd_digits(n))

# Expected output:
803
# count of n | function to return the count of n - digit palindrome numbers ; 
def nDigitPalindromes(n):
    return (9 * pow(10, (n - 1) // 2))


if __name__ == "__main__":
    n = 2
    print(nDigitPalindromes(n))

# Expected output:
9
# maximum lcm among all pairs ( i , j ) of first n natural numbers | function to return the maximum lcm among all the pairs ( i , j ) of first n natural numbers ; 
def maxLCM(n):
    return (n * (n - 1))


if __name__ == "__main__":
    n = 3
    print(maxLCM(n))

# Expected output:
6
# sum of all the numbers in the nth row of the given triangle | function to return the sum of the nth row elements of the given triangle ; 
def getSum(n):
    return ((n - 1) + pow(n, 2))


if __name__ == "__main__":
    n = 3
    print(getSum(n))

# Expected output:
11
# number of edges in a perfect binary tree with n levels | function to return the count of edges in an n - level perfect binary tree ; 
def cntEdges(n):
    edges = 2 ** n - 2
    return edges


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

# Expected output:
14
# number of cells in the nth order figure | function to return the number of cells in the nth order figure of the given type ; 
def cntCells(n):
    cells = pow(n, 2) + pow(n - 1, 2)
    return cells


if __name__ == "__main__":
    n = 3
    print(cntCells(n))

# Expected output:
13
# sum of all the numbers in the nth parenthesis | function to return the sum of the numbers in the nth parenthesis ; 
def findSum(n):
    return n ** 3


if __name__ == "__main__":
    n = 3
    print(findSum(n))

# Expected output:
27
# find the count of natural hexadecimal numbers of size n | function to return the count of n - digit natural hexadecimal numbers ; 
def count(n):
    return 15 * pow(16, n - 1)


if __name__ == "__main__":
    n = 2
    print(count(n))

# Expected output:
240
# largest even and odd n | function to print the largest n - digit even and odd numbers in octal number system ; append '7' ( n - 1 ) times ; append '6' for an even number ; append '7' for an odd number ; 
def findNumbers(N):
    ans = '7' * (N - 1)
    even = ans + '6'
    odd = ans + '7'
    print("Even : ", even)
    print("Odd : ", odd)


if __name__ == "__main__":
    n = 4
    findNumbers(n)

# Expected output:
Even :  7776
Odd :  7777
# nth term of a custom fibonacci series | function to return the nth term of the required sequence ; 
def nth_term(a, b, n):
    z = 0
    if (n % 6 == 1):
        z = a
    elif (n % 6 == 2):
        z = b
    elif (n % 6 == 3):
        z = b - a
    elif (n % 6 == 4):
        z = -a
    elif (n % 6 == 5):
        z = -b
    if (n % 6 == 0):
        z = -(b - a)
    return z


if __name__ == '__main__':
    a = 10
    b = 17
    n = 3
    print(nth_term(a, b, n))

# Expected output:
7
# check whether the given integers a , b , c and d are in proportion | function that returns true if the given four integers are in proportion ; array will consist of only four integers ; sort the array ; find the product of extremes and means ; if the products are equal ; 
def inProportion(arr):
    n = 4
    arr .sort()
    extremes = arr[0] * arr[3]
    means = arr[1] * arr[2]
    if (extremes == means):
        return True
    return False


if __name__ == "__main__":
    arr = [1, 2, 4, 2]
    if (inProportion(arr)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# find the deleted value from the array when average of original elements is given | function to return the missing element ; find the sum of the array elements ; the numerator and the denominator of the equation ; if not divisible then x is not an integer it is a floating ponumber ; return x ; 
def findMissing(arr, n, k, avg):
    sum = 0
    for i in range(n):
        sum += arr[i]
    num = (avg * (n + k)) - sum
    den = k
    if (num % den != 0):
        return -1
    return (int)(num / den)


k = 3
avg = 4
arr = [2, 7, 3]
n = len(arr)
print(findMissing(arr, n, k, avg))

# Expected output:
4
# count of n | function to return the factorial of n ; function to return the count of n - digit numbers with all distinct digits ; 
def factorial(n):
    if (n == 0):
        return 1
    return n * factorial(n - 1)


def countNum(n):
    if (n > 10):
        return 0
    return (9 * factorial(9) // factorial(10 - n))


if __name__ == "__main__":
    n = 3
    print(countNum(n))

# Expected output:
648
# maximum number of distinct positive integers that can be used to represent n | python3 implementation of the approach ; function to return the required count ; 

from math import sqrt


def count(n):
    return (-1 + sqrt(1 + 8 * n)) // 2


if __name__ == "__main__":
    n = 10
    print(count(n))

# Expected output:
4.0
# find the previous fibonacci number | python3 implementation of the approach ; function to return the previous fibonacci number ; 

from math import *


def previousFibonacci(n):
    a = n / ((1 + sqrt(5)) / 2.0)
    return round(a)


n = 8
print(previousFibonacci(n))

# Expected output:
5
# find the quadratic equation from the given roots | function to find the quadratic equation whose roots are a and b ; 
def findEquation(a, b):
    summ = (a + b)
    product = (a * b)
    print("x^2 - (", summ, "x) + (", product, ") = 0")


a = 2
b = 3
findEquation(a, b)

# Expected output:
x^2 - ( 5 x) + ( 6 ) = 0
# smallest n digit number which is a perfect fourth power | python3 implementation of the approach ; function to return the smallest n - digit number which is a perfect fourth power ; 

from math import *


def cal(n):
    res = pow(ceil((pow(pow(10, (n - 1)), 1 / 4))), 4)
    return int(res)


n = 1
print(cal(n))

# Expected output:
1
# count of 0 s in an n | function to return the count of 0 s in an n - level hexagon ; 
def count(n):
    return 3 * n * (n - 1) + 1


n = 3
print(count(n))

# Expected output:
19
# number of words that can be made using exactly p consonants and q vowels from the given string | function to return the value of nck ; function to return the factorial of n ; function that returns true if ch is a vowel ; function to return the number of words possible ; to store the count of vowels and consonanats in the given string ; if current character is a vowel ; find the total possible words ; 
def binomialCoeff(n, k):
    if (k == 0 or k == n):
        return 1
    return binomialCoeff(n - 1, k - 1) + binomialCoeff(n - 1, k)


def fact(n):
    if (n >= 1):
        return n * fact(n - 1)
    else:
        return 1


def isVowel(ch):
    if (ch == 'a' or ch == 'e' or ch == 'i' or ch == 'o' or ch == 'u'):
        return True
    return False


def countWords(s, p, q):
    countc = 0
    countv = 0
    for i in range(len(s)):
        if (isVowel(s[i])):
            countv += 1
        else:
            countc += 1
    a = binomialCoeff(countc, p)
    b = binomialCoeff(countv, q)
    c = fact(p + q)
    ans = (a * b) * c
    return ans


s = "crackathon"
p = 4
q = 3
print(countWords(s, p, q))

# Expected output:
176400
# count of elements on the left which are divisible by current element | utility function to print the elements of the array ; function to generate and print the required array ; for every element of the array ; to store the count of elements on the left that the current element divides ; print the generated array ; 
def printArr(arr, n):
    for i in arr:
        print(i, end=" ")


def generateArr(A, n):
    B = [0 for i in range(n)]
    for i in range(n):
        cnt = 0
        for j in range(i):
            if (A[j] % A[i] == 0):
                cnt += 1
        B[i] = cnt
    printArr(B, n)


A = [3, 5, 1]
n = len(A)
generateArr(A, n)

# Expected output:
0 0 2
# represent the given number as the sum of two composite numbers | function to find two composite numbers which when added give sum as n ; only 8 and 10 can be represented as the sum of two composite integers ; if n is even ; if n is odd ; 
def findNums(n):
    if (n <= 11):
        if (n == 8):
            print("4 4", end=" ")
        if (n == 10):
            print("4 6", end=" ")
        else:
            print("-1", end=" ")
    if (n % 2 == 0):
        print("4 ", (n - 4), end=" ")
    else:
        print("9 ", n - 9, end=" ")


n = 13
findNums(n)

# Expected output:
9  4
# count of n | function to return the count of possible numbers ; 
def count(n):
    return pow(2, n - 1)


n = 4
print(count(n))

# Expected output:
8
# find the next fibonacci number | python3 implementation of the approach ; function to return the next fibonacci number ; 

from math import *


def nextFibonacci(n):
    a = n * (1 + sqrt(5)) / 2.0
    return round(a)


n = 5
print(nextFibonacci(n))

# Expected output:
8
# sum of all the numbers present at given level in pascal 's triangle | function to find sum of numbers at lth level in pascals triangle ; 
def summ(h):
    return pow(2, h - 1)


L = 3
print(summ(L))

# Expected output:
4
# product of values of all possible non | function to find product of all elements in all subsets ; 
def product(a, n):
    ans = 1
    val = pow(2, n - 1)
    for i in range(n):
        ans *= pow(a[i], val)
    return ans


n = 2
a = [3, 7]
print(product(a, n))

# Expected output:
441
# program to find nth odd fibonacci number | function to find nth odd fibonacci number ; 
def oddFib(n):
    n = (3 * n + 1) // 2
    a = -1
    b = 1
    c = 0
    for i in range(1, n + 1):
        c = a + b
        a = b
        b = c
    return c


n = 4
print(oddFib(n))

# Expected output:
5
# find a pair ( n , r ) in an integer array such that value of npr is maximum | function to print the pair ( n , r ) such that npr is maximum possible ; there should be atleast 2 elements ; findex the largest 2 elements ; 
def findPair(arr, n):
    if (n < 2):
        print("-1")
        return
    i = 0
    first = -1
    second = -1
    for i in range(n):
        if (arr[i] > first):
            second = first
            first = arr[i]
        elif (arr[i] > second):
            second = arr[i]
    print("n =", first, "and r =", second)


arr = [0, 2, 3, 4, 1, 6, 8, 9]
n = len(arr)
findPair(arr, n)

# Expected output:
n = 9 and r = 8
# sum of values of all possible non | function to return the required sum ; find the sum of the array elements ; every element appears 2 ^ ( n - 1 ) times ; 
def sum(arr, n):
    sum = 0
    for i in arr:
        sum += i
    sum = sum * pow(2, n - 1)
    return sum


arr = [2, 1, 5, 6]
n = len(arr)
print(sum(arr, n))

# Expected output:
112
# number of ways in which n can be represented as the sum of two positive integers | function to return the number of distinct ways to represent n as the sum of two integers ; 
def ways(n):
    return n // 2


n = 2
print(ways(n))

# Expected output:
1