giulio98/xlcost-single-prompt huggingface dataset:
# date after adding given number of days to the given date | return if year is leap year or not . ; given a date , returns number of days elapsed from the beginning of the current year ( 1 stjan ) . ; given a year and days elapsed in it , finds date by storing results in d and m . ; add x days to the given date . ; y2 is going to store result year and offset2 is going to store offset days in result year . ; x may store thousands of days . we find correct year and offset in the year . ; find values of day and month from offset of result year . ; 
def isLeap(y):
    if (y % 100 != 0 and y % 4 == 0 or y % 400 == 0):
        return True
    return False


def offsetDays(d, m, y):
    offset = d
    switcher = {
        10: 30,
        9: 31,
        8: 30,
        7: 31,
        6: 31,
        5: 30,
        4: 31,
        3: 30,
        2: 31,
        1: 28,
        0: 31}
    if (isLeap(y) and m > 1):
        offset += 1
    offset += switcher .get(m)
    return offset


def revoffsetDays(offset, y, d, m):
    month = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    if (isLeap(y)):
        month[2] = 29
    for i in range(1, 13):
        if (offset <= month[i]):
            break
        offset = offset - month[i]
    d[0] = offset
    m[0] = i + 1


def addDays(d1, m1, y1, x):
    offset1 = offsetDays(d1, m1, y1)
    if isLeap(y1):
        remDays = 366 - offset1
    else:
        remDays = 365 - offset1
    if (x <= remDays):
        y2 = y1
        offset2 = offset1 + x
    else:
        x -= remDays
        y2 = y1 + 1
        if isLeap(y2):
            y2days = 366
        else:
            y2days = 365
        while (x >= y2days):
            x -= y2days
            y2 += 1
            if isLeap(y2):
                y2days = 366
            else:
                y2days = 365
        offset2 = x
    m2 = [0]
    d2 = [0]
    revoffsetDays(offset2, y2, d2, m2)
    print("d2 = ", *d2, ", m2 = ", *m2, ", y2 = ", y2, sep="")


d = 14
m = 3
y = 2015
x = 366
addDays(d, m, y, x)

# Expected output:
d2 = 14, m2 = 3, y2 = 2016
# determine whether a given number is a hyperperfect number | python3 program to check whether a given number is k - hyperperfect ; function to find the sum of all proper divisors ( excluding 1 and n ) ; iterate only until sqrt n as we are going to generate pairs to produce divisors ; as divisors occur in pairs , we can take the values i and n / i as long as i divides n ; function to check whether the given number is prime ; base and corner cases ; since integers can be represented as some 6 * k + y where y >= 0 , we can eliminate all integers that can be expressed in this form ; start from 5 as this is the next prime number ; returns true if n is a k - hyperperfect number . else returns false . ; condition from the definition of hyperperfect ; 

import math


def divisorSum(N, K):
    Sum = 0
    for i in range(2, math .ceil(math .sqrt(N))):
        if (N % i == 0):
            Sum += (i + int(N / i))
    return Sum


def isPrime(n):
    if (n == 1 or n == 0):
        return False
    if (n <= 3):
        return True
    if (n % 2 == 0 or n % 3 == 0):
        return False
    i = 5
    while (i * i <= n):
        if (n % i == 0 or n % (i + 2) == 0):
            return False
        i += 6
    return True


def isHyperPerfect(N, K):
    Sum = divisorSum(N, K)
    if ((1 + K * (Sum)) == N):
        return True
    else:
        return False


N1 = 1570153
K1 = 12
N2 = 321
K2 = 3
if (isHyperPerfect(N1, K1)):
    print(N1, " is ", K1, "-HyperPerfect", sep="")
else:
    print(N1, " is not ", K1, "-HyperPerfect", sep="")
if (isHyperPerfect(N2, K2)):
    print(N2, " is ", K2, "-HyperPerfect", sep="")
else:
    print(N2, " is not ", K2, "-HyperPerfect", sep="")

# Expected output:
1570153 is 12-HyperPerfect
321 is not 3-HyperPerfect
# given a number n , find the first k digits of n ^ n | python3 program to generate k digits of n ^ n ; function to calculate first k digits of n ^ n ; take log10 of n ^ n . log10 ( n ^ n ) = n * log10 ( n ) ; we now try to separate the decimal and integral part of the / product . the floor function returns the smallest integer less than or equal to the argument . so in this case , product - floor ( product ) will give us the decimal part of product ; we now exponentiate this back by raising 10 to the power of decimal part ; we now try to find the power of 10 by which we will have to multiply the decimal part to obtain our final answer ; 

import math


def firstkdigits(n, k):
    product = n * math .log(n, 10)
    decimal_part = product - math .floor(product)
    decimal_part = pow(10, decimal_part)
    digits = pow(10, k - 1)
    return math .floor(decimal_part * digits)


n = 1450
k = 6
print(firstkdigits(n, k))

# Expected output:
962948
# generate k digit numbers with digits in strictly increasing order | number -- > current value of number . x -- > current digit to be considered k -- > remaining number of digits ; try all possible greater digits ; generates all well ordered numbers of length k . ; 
def printWellOrdered(number, x, k):
    if (k == 0):
        print(number, end=" ")
        return
    for i in range((x + 1), 10):
        printWellOrdered(number * 10 + i, i, k - 1)


def generateWellOrdered(k):
    printWellOrdered(0, 0, k)


if __name__ == "__main__":
    k = 3
    generateWellOrdered(k)

# Expected output:
123 124 125 126 127 128 129 134 135 136 137 138 139 145 146 147 148 149 156 157 158 159 167 168 169 178 179 189 234 235 236 237 238 239 245 246 247 248 249 256 257 258 259 267 268 269 278 279 289 345 346 347 348 349 356 357 358 359 367 368 369 378 379 389 456 457 458 459 467 468 469 478 479 489 567 568 569 578 579 589 678 679 689 789
# number of occurrences of 2 as a digit in numbers from 0 to n | write python3 code here ; 
def numberOf2sinRange(n):
    s = ""
    for i in range(0, n + 1):
        s += str(i)
    return (list(s).count('2'))


n = 30
print(numberOf2sinRange(n))

# Expected output:
13
# number of occurrences of 2 as a digit in numbers from 0 to n | counts the number of 2 s in a number at d - th digit ; if the digit in spot digit is ; counts the number of '2' digits between 0 and n ; convert integer to string to find its length ; traverse every digit and count for every digit ; 
def count2sinRangeAtDigit(number, d):
    powerOf10 = int(pow(10, d))
    nextPowerOf10 = powerOf10 * 10
    right = number % powerOf10
    roundDown = number - number % nextPowerOf10
    roundup = roundDown + nextPowerOf10
    digit = (number // powerOf10) % 10
    if (digit < 2):
        return roundDown // 10
    if (digit == 2):
        return roundDown // 10 + right + 1
    return roundup // 10


def numberOf2sinRange(number):
    s = str(number)
    len1 = len(s)
    count = 0
    for digit in range(len1):
        count += count2sinRangeAtDigit(number, digit)
    return count


print(numberOf2sinRange(22))
print(numberOf2sinRange(100))

# Expected output:
6
20
# program to find star number | returns n - th star number ; 
def findStarNum(n):
    return (6 * n * (n - 1) + 1)


n = 3
print(findStarNum(n))

# Expected output:
37
# check if a large number is divisible by 5 or not | function to find that number divisible by 5 or not . the function assumes that string length is at least one . ; 
def isDivisibleBy5(st):
    n = len(st)
    return ((st[n - 1] == '0') or (st[n - 1] == '5'))


st = "76955"
if isDivisibleBy5(st):
    print("Yes")
else:
    print("No ")

# Expected output:
Yes
# tidy number ( digits in non | returns true if num is tidy ; to store previous digit ( assigning initial value which is more than any digit ) ; traverse all digits from right to left and check if any digit is smaller than previous . ; 
def isTidy(num):
    prev = 10
    while (num):
        rem = num % 10
        num /= 10
        if rem > prev:
            return False
        prev = rem
    return True


num = 1556
if isTidy(num):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# nth square free number | function to find nth square free number ; to maintain count of square free number ; loop for square free numbers ; checking whether square of a number is divisible by any number which is a perfect square ; if number is square free ; if the cnt becomes n , return the number ; 
def squareFree(n):
    cnt = 0
    i = 1
    while (True):
        isSqFree = True
        j = 2
        while (j * j <= i):
            if (i % (j * j) == 0):
                isSqFree = False
                break
            j += 1
        if (isSqFree):
            cnt += 1
            if (cnt == n):
                return i
        i += 1
    return 0


n = 10
print(squareFree(n))

# Expected output:
14
# find if n can be written as product of k numbers | python3 program to find if it is possible to write a number n as product of exactly k positive numbers greater than 1. ; prints k factors of n if n can be written as multiple of k numbers . else prints - 1 ; list to store all prime factors of n ; insert all 2 's in list ; n must be odd at this point so we skip one element ( i = i + 2 ) ; this is to handle when n > 2 and n is prime ; if size ( a ) < k , k factors are not possible ; printing first k - 1 factors ; calculating and printing product of rest of numbers ; 

import math as mt


def kFactors(n, k):
    a = list()
    while n % 2 == 0:
        a .append(2)
        n = n // 2
    for i in range(3, mt .ceil(mt .sqrt(n)), 2):
        while n % i == 0:
            n = n / i
            a .append(i)
    if n > 2:
        a .append(n)
    if len(a) < k:
        print("-1")
        return
    for i in range(k - 1):
        print(a[i], end=", ")
    product = 1
    for i in range(k - 1, len(a)):
        product *= a[i]
    print(product)


n, k = 54, 3
kFactors(n, k)

# Expected output:
2, 3, 9
# largest number smaller than or equal to n and digits in non | returns the required number ; loop to recursively check the numbers less than or equal to given number ; keep traversing digits from right to left . for every digit check if it is smaller than prev_dig ; we found the required number ; 
def nondecdigits(n):
    x = 0
    for x in range(n, 0, -1):
        no = x
        prev_dig = 11
        flag = True
        while (no != 0):
            if (prev_dig < no % 10):
                flag = False
                break
            prev_dig = no % 10
            no //= 10
        if (flag):
            break
    return x


if __name__ == "__main__":
    n = 200
    print(nondecdigits(n))

# Expected output:
199
# largest number smaller than or equal to n and digits in non | prints the largest number smaller than s and digits in non - decreasing order . ; array to store digits of number ; conversion of characters of string int number ; variable holds the value of index after which all digits are set 9 ; checking the condition if the digit is less than its left digit ; if first digit is 0 no need to print it ; 
def nondecdigits(s):
    m = len(s)
    a = [0] * m
    for i in range(m):
        a[i] = ord(s[i]) - ord('0')
    level = m - 1
    for i in range(m - 1, 0, -1):
        if (a[i] < a[i - 1]):
            a[i - 1] -= 1
            level = i - 1
    if (a[0] != 0):
        for i in range(level + 1):
            print(a[i], end="")
        for i in range(level + 1, m):
            print("9", end="")
    else:
        for i in range(1, level):
            print(a[i], end="")
        for i in range(level + 1, m):
            print("9", end="")


n = "200"
nondecdigits(n)

# Expected output:
199
# count divisors of n in o ( n ^ 1 / 3 ) | python3 implementation of naive method to count all divisors ; function to count the divisors ; if divisors are equal , count only one ; else : otherwise count both ; 

import math


def countDivisors(n):
    cnt = 0
    for i in range(1, (int)(math .sqrt(n)) + 1):
        if (n % i == 0):
            if (n / i == i):
                cnt = cnt + 1
                cnt = cnt + 2
    return cnt


print("Total distinct divisors of 100 are : ", countDivisors(100))

# Expected output:
Total distinct divisors of 100 are :  3
# check if the door is open or closed | python 3 implementation of doors open or closed ; function to check whether ' n ' has even number of factors or not ; if ' n ' is a perfect square it has odd number of factors ; else ' n ' has even number of factors ; function to find and print status of each door ; if even number of factors final status is closed ; else odd number of factors final status is open ; 

import math


def hasEvenNumberOfFactors(n):
    root_n = math .sqrt(n)
    if ((root_n * root_n) == n):
        return False
    return True


def printStatusOfDoors(n):
    for i in range(1, n + 1):
        if (hasEvenNumberOfFactors(i)):
            print("closed", end=" ")
        else:
            print("open", end=" ")


n = 5
printStatusOfDoors(n)

# Expected output:
open closed closed open closed
# check if frequency of each digit is less than the digit | function to validate number ( check iffrequency of a digit is less than thedigit itself or not ) ; if current digit of temp is same as i ; if frequency is greater than digit value , return false ; 
def validate(n):
    for i in range(10):
        temp = n
        count = 0
        while (temp):
            if (temp % 10 == i):
                count += 1
            if (count > i):
                return -1
            temp //= 10
    return 1


n = 1552793
geek = "True"if validate(n)else "False"
print(geek)

# Expected output:
True
# check if a larger number divisible by 36 | function to check whether a number is divisible by 36 or not ; null number cannot be divisible by 36 ; single digit number other than 0 is not divisible by 36 ; number formed by the last 2 digits ; if number is not divisible by 4 ; number is divisible by 4 calculate sum of digits ; sum of digits is not divisible by 9 ; number is divisible by 4 and 9 hence , number is divisible by 36 ; 
def divisibleBy36(num):
    l = len(num)
    if (l == 0):
        return ("No")
    if (l == 1 and num[0] != '0'):
        return ("No")
    two_digit_num = (((int)(num[l - 2])) * 10 + (int)(num[l - 1]))
    if (two_digit_num % 4 != 0):
        return "No"
    sm = 0
    for i in range(0, l):
        sm = sm + (int)(num[i])
    if (sm % 9 != 0):
        return ("No")
    return ("Yes")


num = "92567812197966231384"
print(divisibleBy36(num))

# Expected output:
Yes
# check if a large number is divisible by 8 or not | function to find that number divisible by 8 or not ; empty string ; if there is single digit ; if there is double digit ; if number formed by last three digits is divisible by 8. ; 
def check(st):
    n = len(st)
    if (n == 0):
        return False
    if (n == 1):
        return ((int)(st[0]) % 8 == 0)
    if (n == 2):
        return ((int)(st[n - 2]) * 10 + ((int)(str[n - 1]) % 8 == 0))
    last = (int)(st[n - 1])
    second_last = (int)(st[n - 2])
    third_last = (int)(st[n - 3])
    return ((third_last * 100 + second_last * 10 + last) % 8 == 0)


st = "76952"
if (check(st)):
    print("Yes")
else:
    print("No ")

# Expected output:
Yes
# prime points ( points that split a number into two primes ) | function to count number of digits ; function to check whether a number is prime or not . returns 0 if prime else - 1 ; corner cases ; this is checked so that we can skip middle five numbers in below loop ; function to print prime points ; counting digits ; as single and double digit numbers do not have left and right number pairs ; finding all left and right pairs . printing the prime points accordingly . discarding first and last index point ; calculating left number ; calculating right number ; prime point condition ; no prime point found ; 
def countDigits(n):
    count = 0
    while (n > 0):
        count += 1
        n = n // 10
    return count


def checkPrime(n):
    if (n <= 1):
        return -1
    if (n <= 3):
        return 0
    if (n % 2 == 0 or n % 3 == 0):
        return -1
    i = 5
    while i * i <= n:
        if (n % i == 0 or n % (i + 2) == 0):
            return -1
        i += 6
    return 0


def printPrimePoints(n):
    count = countDigits(n)
    if (count == 1 or count == 2):
        print("-1")
        return
    found = False
    for i in range(1, (count - 1)):
        left = n // (pow(10, count - i))
        right = n % (pow(10, count - i - 1))
        if (checkPrime(left) == 0 and checkPrime(right) == 0):
            print(i, end=" ")
            found = True
    if (found == False):
        print("-1")


if __name__ == "__main__":
    n = 2317
    printPrimePoints(n)

# Expected output:
1 2
# find ways an integer can be expressed as sum of n | function to calculate and return the power of any given number ; function to check power representations recursively ; initialize number of ways to express x as n - th powers of different natural numbers ; calling power of ' i ' raised to 'n ; recursively check all greater values of i ; if sum of powers is equal to x then increase the value of result . ; return the final result ; 
def power(num, n):

    if (n == 0):
        return 1
    elif (n % 2 == 0):
        return power(num, n // 2) * power(num, n // 2)
    else:
        return num * power(num, n // 2) * power(num, n // 2)


def checkRecursive(x, n, curr_num=1, curr_sum=0):
    results = 0
    p = power(curr_num, n)
    while (p + curr_sum < x):
        results += checkRecursive(x, n, curr_num + 1, p + curr_sum)
        curr_num = curr_num + 1
        p = power(curr_num, n)
    if (p + curr_sum == x):
        results = results + 1
    return results


if __name__ == '__main__':
    x = 10
    n = 2
    print(checkRecursive(x, n))

# Expected output:
1
# number theory | generators of finite cyclic group under addition | function to return gcd of a and b ; print generators of n ; 1 is always a generator ; a number x is generator of gcd is 1 ; 
def gcd(a, b):
    if (a == 0):
        return b
    return gcd(b % a, a)


def printGenerators(n):
    print("1", end=" ")
    for i in range(2, n):
        if (gcd(i, n) == 1):
            print(i, end=" ")


n = 10
printGenerators(n)

# Expected output:
1 3 7 9
# check if a large number is divisible by 3 or not | function to find that number divisible by 3 or not ; compute sum of digits ; check if sum of digits is divisible by 3. ; 
def check(num):
    digitSum = 0
    while num > 0:
        rem = num % 10
        digitSum = digitSum + rem
        num = num / 10
    return (digitSum % 3 == 0)


num = 1332
if (check(num)):
    print("Yes")
else:
    print("No")

# Expected output:
No
# largest subsequence having gcd greater than 1 | returns length of the largest subsequence with gcd more than 1. ; finding the maximum value in arr [ ] ; iterate from 2 to maximum possible divisor of all give values ; if we found divisor , increment count ; 
def largestGCDSubsequence(arr, n):
    ans = 0
    maxele = max(arr)
    for i in range(2, maxele + 1):
        count = 0
        for j in range(n):
            if (arr[j] % i == 0):
                count += 1
        ans = max(ans, count)
    return ans


if __name__ == '__main__':
    arr = [3, 6, 2, 5, 4]
    size = len(arr)
    print(largestGCDSubsequence(arr, size))

# Expected output:
3
# generation of n numbers with given set of factors | generate n numbers with factors in factor [ ] ; array of k to store next multiples of given factors ; prints n numbers output = 0 ; next number to print as output ; find the next smallest multiple ; printing minimum in each iteration print the value if output is not equal to current value ( to avoid the duplicates ) ; incrementing the current value by the respective factor ; 
def generateNumbers(factor, n, k):
    next = [0] * k
    output = 0
    i = 0
    while (i < n):
        toincrement = 0
        for j in range(k):
            if (next[j] < next[toincrement]):
                toincrement = j
        if (output != next[toincrement]):
            output = next[toincrement]
            print(next[toincrement], end=" ")
            i += 1
        next[toincrement] += factor[toincrement]


factor = [3, 5, 7]
n = 10
k = len(factor)
generateNumbers(factor, n, k)

# Expected output:
3 5 6 7 9 10 12 14 15 18
# reversible numbers | function to check reversible number ; calculate reverse of n ; calculate sum of number and its reverse ; check for reverse number reach digit must be odd ; 
def checkReversible(n):
    rev = 0
    flag = n
    while (flag != 0):
        rem = flag % 10
        rev *= 10
        rev += rem
        flag //= 10
    sum = rev + n
    while (sum and ((rem % 2) != 0)):
        rem = sum % 10
        sum //= 10
    if (sum == 0):
        print("Reversible Number")
    else:
        print("Non-Reversible Number")


n = 36
checkReversible(n)

# Expected output:
Reversible Number
# multiplicative order | function for gcd ; function return smallest + ve integer that holds condition a ^ k ( mod n ) = 1 ; result store power of a that rised to the power n - 1 ; modular arithmetic ; return smallest + ve integer ; increment power ; 
def GCD(a, b):
    if (b == 0):
        return a
    return GCD(b, a % b)


def multiplicativeOrder(A, N):
    if (GCD(A, N) != 1):
        return -1
    result = 1
    K = 1
    while (K < N):
        result = (result * A) % N
        if (result == 1):
            return K
        K = K + 1
    return -1


A = 4
N = 7
print(multiplicativeOrder(A, N))

# Expected output:
3
# sum of product of x and y such that floor ( n / x ) = y | python3 program to find sum of product of x and y such that n / x = y ( integer division ) ; return the sum of natural number in a range . ; n * ( n + 1 ) / 2. ; return the sum of product x * y . ; iterating i from 1 to sqrt ( n ) ; finding the upper limit . ; finding the lower limit . ; 

import math


def sumOfRange(a, b):
    i = (a * (a + 1)) >> 1
    j = (b * (b + 1)) >> 1
    return (i - j)


def sumofproduct(n):
    sum = 0
    root = int(math .sqrt(n))
    for i in range(1, root + 1):
        up = int(n / i)
        low = max(int(n / (i + 1)), root)
        sum += (i * sumOfRange(up, low))
        sum += (i * int(n / i))
    return sum


n = 10
print(sumofproduct(n))

# Expected output:
87