giulio98/xlcost-single-prompt huggingface dataset:
# check if a number is full fibonacci or not | python3 program to check if a given number is a full fibonacci number or not ; a utility function that returns true if x is perfect square ; returns true if n is a fibonacci number and false otherwise ; n is fibonacci if one of 5 * n ^ 2 + 4 or 5 * n ^ 2 - 4 or both is a perferct square ; function to check digits ; check if all digits are fibonacci or not ; extract digit ; check if the current digit is not fibonacci ; function to check and return if n is a full fibonacci number or not ; 

from math import *


def isPerfectSquare(x):
    s = sqrt(x)
    return (s * s == x)


def isFibonacci(n):
    return (isPerfectSquare(5 * n * n + 4) or isPerfectSquare(5 * n * n - 4))


def checkDigits(n):
    while (n):
        dig = n % 10
        if (dig == 4 and dig == 6 and dig == 7 and dig == 9):
            return False
        n /= 10
    return True


def isFullfibonacci(n):
    return (checkDigits(n) and isFibonacci(n))


if __name__ == '__main__':
    n = 13
    if (isFullfibonacci(n)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# check whether a large number is divisible by 53 or not | function to check if the number is divisible by 53 or not ; 
def isDivisible(s):
    flag = 0
    while (len(s) > 4):
        l = len(s) - 1
        x = (ord(s[l]) - ord('0')) * 37
        s = s[::-1]
        s = s .replace('0', '', 1)
        i = 0
        carry = 0
        while (x):
            d = ((ord(s[i]) - ord('0')) - (x % 10) - carry)
            if (d < 0):
                d += 10
                carry = 1
            else:
                carry = 0
            s = s .replace(s[i], chr(d + ord('0')), 1)
            x //= 10
            i += 1
        while (carry and i < l):
            d = (ord(s[i]) - ord('0')) - carry
            if (d < 0):
                d += 10
                carry = 1
            else:
                carry = 0
            s = s .replace(s[i], chr(d + ord('0')), 1)
            i += 1
        s = s[::-1]
    num = 0
    for i in range(len(s)):
        num = num * 10 + (ord(s[i]) - ord('0'))
    if (num % 53 == 0):
        return True
    else:
        return False


if __name__ == '__main__':
    N = "1843246219106"
    if (isDivisible(N)):
        print("No")
    else:
        print("Yes")

# Expected output:
Yes
# check whether two numbers are in silver ratio | function to check that two numbers are in silver ratio ; swapping the numbers such that a contains the maximum number between these numbers ; first ratio ; second ratio ; condition to check that two numbers are in silver ratio ; 
def checksilverRatio(a, b):
    a, b = max(a, b), min(a, b)
    ratio1 = round(a / b, 3)
    ratio2 = round((2 * a + b) / a, 3)
    if ratio1 == ratio2 and ratio1 == 2.414:
        print("Yes")
        return True
    else:
        print("No")
        return False


if __name__ == "__main__":
    a = 2.414
    b = 1
    checksilverRatio(a, b)

# Expected output:
Yes
# nth term where k + 1 th term is product of kth term with difference of max and min digit of kth term | function to find minimum digit in the decimal representation of n ; loop to find the minimum digit in the number ; function to find maximum digit in the decimal representation of n ; loop to find the maximum digit in the number ; function to find the value of the given function ; loop to compute the values of the given number ; 
def MIN(n):
    ans = 11
    while n:
        ans = min(ans, n % 10)
        n //= 10
    return ans


def MAX(n):
    ans = -1
    while n:
        ans = max(ans, n % 10)
        n //= 10
    return ans


def Find_value(n, k):
    k -= 1
    (x, y) = (0, 0)
    while k:
        k -= 1
        x = MIN(n)
        y = MAX(n)
        if ((y - x) == 0):
            break
        n *= (y - x)
    print(n, end=' ')


if __name__ == '__main__':
    (N, D) = (487, 5)
    Find_value(N, D)

# Expected output:
981792
# program to check if n is a decagonal number | python3 program for the above approach ; function to check if n is a decagonal number ; condition to check if the number is a decagonal number ; 

import math


def isdecagonal(N):
    n = (3 + math .sqrt(16 * N + 9)) / 8
    return (n - int(n)) == 0


if __name__ == '__main__':
    N = 10
    if isdecagonal(N):
        print('Yes')
    else:
        print('No')

# Expected output:
Yes
# program to check if n is a octadecagon number | python3 program for the above approach ; function to check if n is a octadecagon number ; condition to check if the number is a octadecagon number ; 

import math


def isOctadecagon(N):
    n = (14 + math .sqrt(128 * N + 196)) // 32
    return ((n - int(n)) == 0)


if __name__ == '__main__':
    N = 18
    if isOctadecagon(N):
        print('Yes')
    else:
        print('No')

# Expected output:
Yes
# count of subsequences which consists exactly k prime numbers | returns factorial of n ; function to return total number of combinations ; function check whether a number is prime or not ; corner case ; check from 2 to n - 1 ; function for finding number of subsequences which consists exactly k primes ; if number of primes are less than k ; 
def fact(n):
    res = 1
    for i in range(2, n + 1):
        res = res * i
    return res


def nCr(n, r):
    return (fact(n) // (fact(r) * fact(n - r)))


def isPrime(n):
    if (n <= 1):
        return False
    for i in range(2, n):
        if (n % i == 0):
            return False
    return True


def countSubsequences(arr, n, k):
    countPrime = 0
    for i in range(n):
        if (isPrime(arr[i])):
            countPrime += 1
    if (countPrime < k):
        return 0
    return (nCr(countPrime, k) * pow(2, (n - countPrime)))


arr = [1, 2, 3, 4, 5, 6, 7]
K = 3
n = len(arr)
print(countSubsequences(arr, n, K))

# Expected output:
32
# length of the longest alternating even odd subarray | function to find the longest subarray ; length of longest alternating subarray ; iterate in the array ; increment count if consecutive elements has an odd sum ; store maximum count in longest ; reinitialize cnt as 1 consecutive elements does not have an odd sum ; length of ' longest ' can never be 1 since even odd has to occur in pair or more so return 0 if longest = 1 ; 
def longestEvenOddSubarray(arr, n):
    longest = 1
    cnt = 1
    for i in range(n - 1):
        if ((arr[i] + arr[i + 1]) % 2 == 1):
            cnt = cnt + 1
        else:
            longest = max(longest, cnt)
            cnt = 1
    if (longest == 1):
        return 0
    return max(cnt, longest)


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

# Expected output:
5
# check whether given number n is a moran number or not | function to calculate digit sum ; function to check if number is prime ; function to check if number is moran number ; calculate digit sum ; check if n is completely divisible by digit sum ; calculate the quotient ; check if the number is prime ; 
def digSum(a):
    _sum = 0
    while (a):
        _sum += a % 10
        a = a // 10
    return _sum


def isPrime(r):
    s = True
    i = 2
    while i * i <= r:
        if (r % i == 0):
            s = False
            break
        i += 1
    return s


def moranNo(n):
    dup = n
    _sum = digSum(dup)
    if (n % _sum == 0):
        c = n // _sum
        if (isPrime(c)):
            print("Yes")
            return
    print("No")


n = 21
moranNo(n)

# Expected output:
Yes
# program to check if n is a hendecagonal number | python3 program for the above approach ; function to check if n is a hendecagonal number ; condition to check if the number is a hendecagonal number ; 

import math


def ishendecagonal(N):
    n = (7 + math .sqrt(72 * N + 49)) // 18
    return (n - int(n)) == 0


N = 11
if (ishendecagonal(N)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# program to check if n is a hexadecagonal number | python3 program for the above approach ; function to check if n is a hexadecagonal number ; condition to check if the number is a hexadecagonal number ; 

from math import sqrt


def ishexadecagonal(N):
    n = (12 + sqrt(112 * N + 144)) / 28
    return (n - int(n)) == 0


if __name__ == "__main__":
    N = 16
    if (ishexadecagonal(N)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# program to check if n is a nonagonal number | function to check if n is a nonagonal number ; condition to check if the number is a nonagonal number ; 
def isnonagonal(N):
    n = (5 + pow((56 * N + 25), 1 / 2)) / 14
    return (n - int(n)) == 0


if __name__ == '__main__':
    N = 9
    if (isnonagonal(N)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# count of decreasing pairs formed from numbers 1 to n | function to count the possible number of pairs ; if the number is even then the answer in ( n / 2 ) - 1 ; if the number is odd then the answer in n / 2 ; 
def divParts(N):
    if (N % 2 == 0):
        print((N / 2) - 1)
    else:
        print(N / 2)


N = 8
divParts(N)

# Expected output:
3.0
# count the minimum steps to reach 0 from the given integer n | function returns min step to reach 0 from n ; direct possible reduction of value n ; remaining steps needs to be reduced by 1 ; summation of both the values ; return the final answer ; 
def getMinSteps(n, jump):
    quotient = int(n / jump)
    remainder = n % jump
    steps = quotient + remainder
    return steps


N = 6
K = 3
print(getMinSteps(N, K))

# Expected output:
2
# construct an array of size n whose sum of cube of all elements is a perfect square | function to construct an array of size n ; prints the first n natural numbers ; 
def constructArray(N):
    for i in range(1, N + 1):
        print(i, end=' ')


if __name__ == '__main__':
    N = 5
    constructArray(N)

# Expected output:
1 2 3 4 5
# program to check if n is a centered decagonal number | python3 program for the above approach ; function to check if the number n is a centered decagonal number ; condition to check if n is centered decagonal number ; 

import numpy as np


def isCentereddecagonal(N):
    n = (5 + np .sqrt(20 * N + 5)) / 10
    return (n - int(n)) == 0


N = 11
if (isCentereddecagonal(N)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# count sexy prime pairs in the given array | python 3 program to count sexy prime pairs in array ; a utility function to check if the number n is prime or not ; base cases ; check to skip middle five numbers in below loop ; if n is divisible by i and i + 2 then it is not prime ; a utility function that check if n1 and n2 are spp ( sexy prime pair ) or not ; function to find spp ( sexy prime pair ) pairs from the given array ; iterate through all pairs ; increment count if spp ( sexy prime pair ) pair ; 

from math import sqrt


def isPrime(n):
    if (n <= 1):
        return False
    if (n <= 3):
        return True
    if (n % 2 == 0 or n % 3 == 0):
        return False
    for i in range(5, int(sqrt(n)) + 1, 6):
        if (n % i == 0 or n % (i + 6) == 0):
            return False
    return True


def SexyPrime(n1, n2):
    return (isPrime(n1) and isPrime(n2) and abs(n1 - n2) == 6)


def countSexyPairs(arr, n):
    count = 0
    for i in range(n):
        for j in range(i + 1, n):
            if (SexyPrime(arr[i], arr[j])):
                count += 1
    return count


if __name__ == '__main__':
    arr = [6, 7, 5, 11, 13]
    n = len(arr)
    print(countSexyPairs(arr, n))

# Expected output:
2
# find the k | function to find the index of number at first position of kth sequence of set of size n ; n_actual_fact = n ! ; first position of the kth sequence will be occupied by the number present at index = k / ( n - 1 ) ! ; function to find the kth permutation of n numbers ; store final answer ; insert all natural number upto n in set ; subtract 1 to get 0 based indexing ; mark the first position ; itr now points to the number at index in set s ; remove current number from the set ; 
def findFirstNumIndex(k, n):
    if (n == 1):
        return 0, k
    n -= 1
    first_num_index = 0
    n_partial_fact = n
    while (k >= n_partial_fact and n > 1):
        n_partial_fact = n_partial_fact * (n - 1)
        n -= 1
    first_num_index = k // n_partial_fact
    k = k % n_partial_fact
    return first_num_index, k


def findKthPermutation(n, k):
    ans = ""
    s = set()
    for i in range(1, n + 1):
        s .add(i)
    k = k - 1
    for i in range(n):
        itr = list(s)
        index, k = findFirstNumIndex(k, n - i)
        ans += str(itr[index])
        itr .pop(index)
        s = set(itr)
    return ans


if __name__ == '__main__':
    n = 3
    k = 4
    kth_perm_seq = findKthPermutation(n, k)
    print(kth_perm_seq)

# Expected output:
231
# find the largest n digit perfect square number in base b | python3 implementation to find the largest n digit perfect square number in base b ; function to find the largest n digit number ; largest n - digit perfect square ; print the result ; 

import math


def nDigitPerfectSquares(n, b):
    largest = pow(math .ceil(math .sqrt(pow(b, n))) - 1, 2)
    print(largest)


N = 1
B = 8
nDigitPerfectSquares(N, B)

# Expected output:
4
# find cube root of a number using log function | python3 program to find cube root of a number using logarithm ; function to find the cube root ; calculate the cube root ; return the final answer ; 

import numpy as np


def cubeRoot(n):
    ans = pow(3, (1.0 / 3) * (np .log(n) / np .log(3)))
    return ans


N = 8
print("%.2f" % cubeRoot(N))

# Expected output:
2.00
# find the maximum possible value for the given periodic function | function to return the maximum value of f ( x ) ; 
def floorMax(A, B, N):
    x = min(B - 1, N)
    return (A * x) // B


A = 11
B = 10
N = 9
print(floorMax(A, B, N))

# Expected output:
9
# minimum moves taken to move coin of each cell to any one cell of matrix | function to find the minimum number of moves taken to move the element of each cell to any one cell of the square matrix of odd length ; initializing count to 0 ; number of layers that are around the centre element ; iterating over ranger of layers ; increase the value of count by 8 * k * k ; 
def calculateMoves(n):
    count = 0
    layers = n // 2
    for k in range(1, layers + 1):
        count += 8 * k * k
    return count


if __name__ == "__main__":
    N = 5
    print(calculateMoves(N))

# Expected output:
40
# check if n can be represented as sum of squares of two consecutive integers | python3 implementation to check that a number is sum of squares of 2 consecutive numbers or not ; function to check that the a number is sum of squares of 2 consecutive numbers or not ; condition to check if the a number is sum of squares of 2 consecutive numbers or not ; 

import math


def isSumSquare(N):
    n = (2 + math .sqrt(8 * N - 4)) / 2
    return (n - int(n)) == 0


if __name__ == '__main__':
    i = 13
    if isSumSquare(i):
        print('Yes')
    else:
        print('No')

# Expected output:
Yes
# program to check if n is a centered heptagonal number | python3 implementation to check that a number is a centered heptagonal number or not ; function to check that the number is a centered heptagonal number ; condition to check if the number is a centered heptagonal number ; 

import math


def isCenteredheptagonal(N):
    n = (7 + math .sqrt(56 * N - 7)) / 14
    return (n - int(n)) == 0


n = 8
if (isCenteredheptagonal(n)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# program to check if n is a centered nonadecagonal number | python3 implementation to check that a number is a centered nonadecagonal number or not ; function to check that the number is a centered nonadecagonal number ; condition to check if the number is a centered nonadecagonal number ; 

import math


def isCenterednonadecagonal(N):
    n = (19 + math .sqrt(152 * N + 209)) / 38
    return (n - int(n)) == 0


n = 20
if (isCenterednonadecagonal(n)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# program to check if n is a centered octadecagonal number | python3 implementation to check that a number is a centered octadecagonal number or not ; function to check that the number is a centered octadecagonal number ; implement the formula generated ; condition to check if the number is a centered octadecagonal number ; 

import math


def isCenteredOctadecagonal(N):
    n = (9 + math .sqrt(36 * N + 45)) / 18
    return (n - int(n)) == 0


if __name__ == '__main__':
    n = 19
    if isCenteredOctadecagonal(n):
        print('Yes')
    else:
        print('No')

# Expected output:
Yes
# logarithm tricks for competitive programming | python3 implementation to find kth root of the number ; function to find the kth root of the number ; 

import math


def kth_root(n, k):
    return (pow(k, ((1.0 / k) * (math .log(n) / math .log(k)))))


if __name__ == "__main__":
    n = 8.0
    k = 3
    print(round(kth_root(n, k)))

# Expected output:
2
# logarithm tricks for competitive programming | python3 implementation to check if a number is a power of the other number ; function to check if the number is power of k ; logarithm function to calculate value ; compare to the result1 or result2 both are equal ; 

from math import log


def isPower(n, k):
    res1 = int(log(n) / log(k))
    res2 = log(n) / log(k)
    return (res1 == res2)


if __name__ == "__main__":
    n = 8
    k = 2
    if (isPower(n, k)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# count of subarrays which start and end with the same element | function to find total sub - array which start and end with same element ; initialize result with 0 ; array to count frequency of 1 to n ; update frequency of a [ i ] ; update result with sub - array contributed by number i ; print the result ; 
def cntArray(A, N):
    result = 0
    frequency = [0] * (N + 1)
    for i in range(0, N):
        frequency[A[i]] = frequency[A[i]] + 1
    for i in range(1, N + 1):
        frequency_of_i = frequency[i]
        result = result + ((frequency_of_i) * (frequency_of_i + 1)) / 2
    print(int(result))
    print("")


A = [1, 5, 6, 1, 9, 5, 8, 10, 8, 9]
N = len(A)
cntArray(A, N)

# Expected output:
14
# length of array pair formed where one contains all distinct elements and other all same elements | function to find the max size possible ; counting the maximum frequency ; counting total distinct elements ; find max of both the answer ; 
def findMaxSize(a, n):
    frq = [0] * (n + 1)
    for i in range(n):
        frq[a[i]] += 1
    maxfrq = max(frq)
    dist = n + 1 - frq .count(0)
    ans1 = min(maxfrq - 1, dist)
    ans2 = min(maxfrq, dist - 1)
    ans = max(ans1, ans2)
    return ans


arr = [4, 2, 4, 1, 4, 3, 4]
n = len(arr)
print(findMaxSize(arr, n))

# Expected output:
3
# longest subarray with all elements same | function to find largest sub array with all equal elements . ; traverse the array ; if element is same as previous increment temp value ; return the required answer ; 
def subarray(arr, n):
    ans, temp = 1, 1
    for i in range(1, n):
        if arr[i] == arr[i - 1]:
            temp = temp + 1
        else:
            ans = max(ans, temp)
            temp = 1
    ans = max(ans, temp)
    return ans


arr = [2, 2, 1, 1, 2, 2, 2, 3, 3]
n = len(arr)
print(subarray(arr, n))

# Expected output:
3
# min and max length subarray having adjacent element difference atmost k | function to find the maximum and minimum length subarray ; initialise minimum and maximum size of subarray in worst case ; left will scan the size of possible subarray in left of selected element ; right will scan the size of possible subarray in right of selected element ; temp will store size of group associateed with every element ; loop to find size of subarray for every element of array ; left will move in left direction and compare difference between itself and element left to it ; right will move in right direction and compare difference between itself and element right to it ; if subarray of much lesser or much greater is found than yet known then update ; print minimum and maximum possible size of subarray ; 
def findMaxMinSubArray(arr, K, n):
    min = n
    max = 0
    left = 0
    right = n
    tmp = 0
    for i in range(0, n):
        tmp = 1
        left = i
        while (left - 1 >= 0 and abs(arr[left] - arr[left - 1]) <= K):
            left = left - 1
            tmp = tmp + 1
        right = i
        while (right + 1 <= n - 1 and abs(arr[right] - arr[right + 1]) <= K):
            right = right + 1
            tmp = tmp + 1
        if (min > tmp):
            min = tmp
        if (max < tmp):
            max = tmp
    print(min, end=', ')
    print(max, end='')


arr = [1, 2, 5, 6, 7]
K = 2
n = len(arr)
findMaxMinSubArray(arr, K, n)

# Expected output:
2, 3
# count of elements which is the sum of a subarray of the given array | function to count element such that their exist a subarray whose sum is equal to this element ; loop to compute frequency of the given elements ; loop to iterate over every possible subarray of array ; 
def countElement(arr, n):
    freq = {}
    ans = 0
    for i in range(n):
        freq[arr[i]] = freq .get(arr[i], 0) + 1
    for i in range(n - 1):
        tmpsum = arr[i]
        for j in range(i + 1, n):
            tmpsum += arr[j]
            if tmpsum in freq:
                ans += freq[tmpsum]
    return ans


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

# Expected output:
4
# nth root of a number using log | python3 implementation to find the kth root of a number using log ; function to find the kth root of the number using log function ; 

import numpy as np


def kthRoot(n, k):
    return pow(k, ((1.0 / k) * (np .log(n) / np .log(k))))


n = 81
k = 4
print("%.6f" % kthRoot(n, k))

# Expected output:
3.000000
# make a , b and c equal by adding total value n to them | python3 program to distribute integer n among a , b , c such that they become equal ; find maximum among the three elements ; summation of three elements ; check if difference is divisible by 3 ; 
def distributeN(A, B, C, n):
    maximum = max(A, B, C)
    sum = A + B + C
    p = (3 * maximum) - sum
    diff = n - p
    if diff < 0 or diff % 3:
        print("No")
    else:
        print("Yes")


A = 10
B = 20
C = 15
n = 14
distributeN(A, B, C, n)

# Expected output:
No
# represent n as sum of k odd numbers with repetitions allowed | function to print the representation ; n must be greater than equal to 2 * k and must be odd ; 
def sumOddNumbers(N, K):
    check = N - (K - 1)
    if (check > 0 and check % 2 == 1):
        for i in range(0, K - 1):
            print("1", end=" ")
        print(check, end=" ")
    else:
        print("-1")


N = 5
K = 3
sumOddNumbers(N, K)

# Expected output:
1 1 3
# number of continuous reductions of a from b or b from a to make them ( 1 , 1 ) | function to find the minimum number of steps required ; condition to check if it is not possible to reach ; condition to check if the pair is reached to 1 , 1 ; condition to change the a as the maximum element ; 
def minimumSteps(a, b, c):
    if a < 1 or b < 1:
        return -1
    if a == 1 and b == 1:
        return c
    if a < b:
        a, b = b, a
    return minimumSteps(a - b, b, c + 1)


if __name__ == "__main__":
    a = 75
    b = 17
    print(minimumSteps(a, b, 0))

# Expected output:
10