giulio98/xlcost-single-prompt huggingface dataset:
# find a square matrix such that sum of elements in every row and column is k | function to print the required matrix ; print k for the left diagonal elements ; print 0 for the rest ; 
def printMatrix(n, k):
    for i in range(n):
        for j in range(n):
            if (i == j):
                print(k, end=" ")
            else:
                print("0", end=" ")
        print()


if __name__ == "__main__":
    n = 3
    k = 7
    printMatrix(n, k)

# Expected output:
7 0 0 
0 7 0 
0 0 7
# divide first n natural numbers into 3 equal sum subsets | function that returns true if the subsets are possible ; if n <= 3 then it is not possible to divide the elements in three subsets satisfying the given conditions ; sum of all the elements in the range [ 1 , n ] ; if the sum is divisible by 3 then it is possible ; 
def possible(n):
    if (n > 3):
        sum = (n * (n + 1)) // 2
        if (sum % 3 == 0):
            return True
    return False


if __name__ == "__main__":
    n = 5
    if (possible(n)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# find the nth element of the modified fibonacci series | function to return the nth number of the modified fibonacci series where a and b are the first two terms ; to store the current element which is the sum of previous two elements of the series ; this loop will terminate when the nth element is found ; return the nth element ; 
def findNthNumber(A, B, N):
    sum = 0
    for i in range(2, N):
        sum = A + B
        A = B
        B = sum
    return sum


if __name__ == '__main__':
    A = 5
    B = 7
    N = 10
    print(findNthNumber(A, B, N))

# Expected output:
343
# check if an array is increasing or decreasing | function to check the type of the array ; if the first two and the last two elements of the array are in increasing order ; if the first two and the last two elements of the array are in decreasing order ; if the first two elements of the array are in increasing order and the last two elements of the array are in decreasing order ; if the first two elements of the array are in decreasing order and the last two elements of the array are in increasing order ; 
def checkType(arr, n):
    if (arr[0] <= arr[1] and arr[n - 2] <= arr[n - 1]):
        print("Increasing")
    elif (arr[0] >= arr[1] and arr[n - 2] >= arr[n - 1]):
        print("Decreasing")
    elif (arr[0] <= arr[1] and arr[n - 2] >= arr[n - 1]):
        print("Increasing then decreasing")
    else:
        print("Decreasing then increasing")


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

# Expected output:
Increasing
# calculate the ist : indian standard time | python3 implementation of the approach ; function to calculate indian standard time ; separate integer part ; separate float part and return ceil value ; 

from math import ceil


def cal_IST(h, r):
    IST = round((h * r * 1.0) / 360, 3)
    int_IST = int(IST)
    float_IST = ceil((IST - int_IST) * 60)
    print(int_IST, ":", float_IST)


if __name__ == "__main__":
    h = 20
    r = 150
    cal_IST(h, r)

# Expected output:
8 : 20
# check if it is possible to perform the given grid division | function that returns true if it is possible to divide the grid satisfying the given conditions ; to store the sum of all the cells of the given parts ; if the sum is equal to the total number of cells in the given grid ; 
def isPossible(arr, p, n, m):
    sum = 0
    for i in range(p):
        sum += arr[i]
    if (sum == (n * m)):
        return True
    return False


if __name__ == '__main__':
    n = 3
    m = 4
    arr = [6, 3, 2, 1]
    p = len(arr)
    if (isPossible(arr, p, n, m)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# find the first n integers such that the sum of their digits is equal to 10 | function to return the sum of digits of n ; add the last digit to the sum ; remove last digit ; return the sum of digits ; function to print the first n numbers whose sum of digits is 10 ; first number of the series is 19 ; if the sum of digits of the current number is equal to 10 ; print the number ; add 9 to the previous number ; 
def sum(n):
    sum = 0
    while (n):
        sum = sum + n % 10
        n = n // 10
    return sum


def firstN(n):
    num = 19
    cnt = 1
    while (cnt != n):
        if (sum(num) == 10):
            print(num, end=" ")
            cnt += 1
        num += 9


if __name__ == "__main__":
    n = 10
    firstN(n)

# Expected output:
19 28 37 46 55 64 73 82 91
# sum of last digit of all integers from 1 to n divisible by m | function to return the required sum ; number of element between 1 to n divisible by m ; array to store the last digit of elements in a cycle ; storing and adding last digit of cycle ; number of elements present in last cycle ; sum of k / 10 cycle ; adding value of digits of last cycle to the answer ; 
def sumOfLastDig(n, m):
    sum = 0
    k = n // m
    arr = [0] * 10
    for i in range(10):
        arr[i] = m * (i + 1) % 10
        sum += arr[i]
    rem = k % 10
    ans = (k // 10) * sum
    for i in range(rem):
        ans += arr[i]
    return ans


if __name__ == "__main__":
    n = 100
    m = 3
    print(sumOfLastDig(n, m))

# Expected output:
153
# number of subsequences with even and odd sum | set 2 | function to find number of subsequences with even and odd sum ; counting number of odds ; even count ; total subsequences is ( 2 ^ n - 1 ) for numberofevensubsequences subtract numberofoddsubsequences from total ; 
def countSum(arr, n):
    NumberOfOdds = 0
    NumberOfEvens = 0
    for i in range(n):
        if (arr[i] & 1):
            NumberOfOdds += 1
    NumberOfEvens = n - NumberOfOdds
    NumberOfOddSubsequences = (1 << NumberOfEvens) * (1 << (NumberOfOdds - 1))
    NumberOfEvenSubsequences = (1 << n) - 1 - NumberOfOddSubsequences
    return (NumberOfEvenSubsequences, NumberOfOddSubsequences)


if __name__ == "__main__":
    arr = [1, 2, 2, 3]
    n = len(arr)
    ans = countSum(arr, n)
    print("EvenSum =", ans[0], end=" ")
    print("OddSum =", ans[1])

# Expected output:
EvenSum = 7 OddSum = 8
# program to find the next prime number | python3 implementation of the approach ; function that returns true if n is prime else returns false ; corner cases ; this is checked so that we can skip middle five numbers in below loop ; function to return the smallest prime number greater than n ; base case ; loop continuously until isprime returns true for a number greater than n ; 

import math


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(math .sqrt(n) + 1), 6):
        if (n % i == 0 or n % (i + 2) == 0):
            return False
    return True


def nextPrime(N):
    if (N <= 1):
        return 2
    prime = N
    found = False
    while (not found):
        prime = prime + 1
        if (isPrime(prime)):
            found = True
    return prime


N = 3
print(nextPrime(N))

# Expected output:
5
# check if a number is flavius number | return the number is flavious number or not ; index i starts from 2 because at 1 st iteration every 2 nd element was remove and keep going for k - th iteration ; removing the elements which are already removed at kth iteration ; 
def Survives(n):
    i = 2
    while (True):
        if (i > n):
            return True
        if (n % i == 0):
            return False
        n -= n // i
        i += 1


if __name__ == "__main__":
    n = 17
    if (Survives(n)):
        print("Yes")
    else:
        print("No")

# Expected output:
No
# nth xor fibonacci number | function to return the nth xor fibonacci number ; 
def nthXorFib(n, a, b):
    if n == 0:
        return a
    if n == 1:
        return b
    if n == 2:
        return a ^ b
    return nthXorFib(n % 3, a, b)


a = 1
b = 2
n = 10
print(nthXorFib(n, a, b))

# Expected output:
2
# sand timer flip counting problem | recursive function to return the gcd of a and b ; everything divides 0 ; function to print the number of flips for both the sand timers ; 
def gcd(a, b):
    if (b == 0):
        return a
    return gcd(b, a % b)


def flip(a, b):
    lcm = (a * b) // gcd(a, b)
    a = lcm // a
    b = lcm // b
    print(a - 1, b - 1)


a = 10
b = 5
flip(a, b)

# Expected output:
0 1
# sum of n terms in the expansion of arcsin ( x ) | function to find the arcsin ( x ) ; the power to which ' x ' is raised ; numerator value ; denominator value ; 
def find_Solution(x, n):
    Sum = x
    e = 2
    o = 1
    p = 1
    for i in range(2, n + 1):
        p += 2
        Sum += (o / e) * (pow(x, p) / p)
        o = o * (o + 2)
        e = e * (e + 2)
    print(round(Sum, 10))


x = -0.5
if (abs(x) >= 1):
    print("InvalidInput")
n = 8
find_Solution(x, n)

# Expected output:
-0.5233948501
# minimize the cost of buying the objects | function that will calculate the price ; calculate the number of items we can get for free ; calculate the number of items we will have to pay the price for ; calculate the price ; 
def totalPay(totalItems, priceOfOneItem, N, M):
    freeItems = 0
    actual = 0
    freeItems = totalItems // (N + M)
    actual = totalItems - freeItems
    amount = actual * priceOfOneItem
    return amount


T = 12
P = 8
N = 2
M = 1
print("Amount = ", totalPay(T, P, N, M))

# Expected output:
Amount =  64
# count of all possible pairs of disjoint subsets of integers from 1 to n | python3 implementation of the approach ; modulo exponentiation function ; function to calculate ( x ^ y ) % p in o ( log ( y ) ) ; 

p = 1000000007


def power(x, y):
    res = 1
    x = x % p
    while (y > 0):
        if (y & 1):
            res = (res * x) % p
        y = y >> 1
        x = (x * x) % p
    return res % p


n = 3
x = (power(3, n) % p + 1) % p
x = (x - power(2, n + 1) + p) % p
x = (x * power(2, p - 2)) % p
print(x)

# Expected output:
6
# find the remainder when first digit of a number is divided by its last digit | function to find the remainder ; get the last digit ; get the first digit ; compute the remainder ; 
def findRemainder(n):
    l = n % 10
    while (n >= 10):
        n //= 10
    f = n
    remainder = f % l
    print(remainder)


n = 5223
findRemainder(n)

# Expected output:
2
# percentage increase in the volume of cuboid if length , breadth and height are increased by fixed percentages | function to return the percentage increase in the volume of the cuboid ; 
def increaseInVol(l, b, h):
    percentInc = ((1 + (l / 100)) * (1 + (b / 100)) * (1 + (h / 100)))
    percentInc -= 1
    percentInc *= 100
    return percentInc


l = 50
b = 20
h = 10
print(increaseInVol(l, b, h), "%")

# Expected output:
98.0 %
# count the number of occurrences of a particular digit in a number | function to count the occurrences of the digit d in n ; loop to find the digits of n ; check if the digit is d ; return the count of the occurrences of d in n ; 
def countOccurrances(n, d):
    count = 0
    while (n > 0):
        if (n % 10 == d):
            count = count + 1
        n = n // 10
    return count


d = 2
n = 214215421
print(countOccurrances(n, d))

# Expected output:
3
# find number of factors of n when location of its two factors whose product is n is given | function to find the number of factors ; print the number of factors ; 
def findFactors(a, b):
    c = a + b - 1
    print(c)


if __name__ == '__main__':
    a = 13
    b = 36
    findFactors(a, b)

# Expected output:
48
# 21 matchsticks problem | function to return the optimal strategy ; removing matchsticks in blocks of five ; 
def TwentyoneMatchstick(arr, N):
    for i in range(N):
        print(5 - arr[i], end=" ")


arr = [3, 4, 2, 2]
N = len(arr)
TwentyoneMatchstick(arr, N)

# Expected output:
2 1 3 3
# find the smallest positive number which can not be represented by given digits | function to find the smallest positive number which can not be represented by given digits ; storing the count of 0 digit or store the value at 0 th index ; calculates the min value in the array starting from 1 st index and also store its index . ; if its value at 0 th index is less than min value than either 10 , 100 , 1000 ... can 't be expressed ; if its value is greater than min value then iterate the loop upto first min value index and simply print its index value . ; 
def expressDigit(arr, n):
    min = 9
    index = 0
    temp = 0
    temp = arr[0]
    for i in range(1, 10):
        if (arr[i] < min):
            min = arr[i]
            index = i
    if (temp < min):
        print(1, end="")
        for i in range(1, temp + 1):
            print(0, end="")
    else:
        for i in range(min):
            print(index, end="")
        print(index)


arr = [2, 2, 1, 2, 1, 1, 3, 1, 1, 1]
N = 10
expressDigit(arr, N)

# Expected output:
22
# find the average of k digits from the beginning and l digits from the end of the given number | implementation of the approach ; function to return the count of digits in num ; function to return the sum of first n digits of num ; remove the unnecessary digits ; function to return the sum of the last n digits of num ; if the average can 't be calculated without  using the same digit more than once ; sum of the last l digits of n ; sum of the first k digits of n ( totaldigits - k ) must be removed from the end of the number to get the remaining k digits from the beginning ; return the average ; 

from math import pow


def countDigits(num):
    cnt = 0
    while (num > 0):
        cnt += 1
        num //= 10
    return cnt


def sumFromStart(num, n, rem):
    num //= pow(10, rem)
    sum = 0
    while (num > 0):
        sum += (num % 10)
        num //= 10
    return sum


def sumFromEnd(num, n):
    sum = 0
    for i in range(n):
        sum += (num % 10)
        num //= 10
    return sum


def getAverage(n, k, l):
    totalDigits = countDigits(n)
    if (totalDigits < (k + l)):
        return -1
    sum1 = sumFromEnd(n, l)
    sum2 = sumFromStart(n, k, totalDigits - k)
    return (sum1 + sum2) / (k + l)


if __name__ == '__main__':
    n = 123456
    k = 2
    l = 3
    print(getAverage(n, k, l))

# Expected output:
3.6
# print n terms of withoff sequence | python3 program to find wythoff array ; function to find the n , k term of wythoff array ; tau = ( sqrt ( 5 ) + 1 ) / 2 ; already_stored ; t ( n , - 1 ) = n - 1. ; t ( n , 0 ) = floor ( n * tau ) . ; t ( n , k ) = t ( n , k - 1 ) + t ( n , k - 2 ) for k >= 1. ; store ; return the ans ; function to find first n terms of wythoff array by traversing in anti - diagonal ; map to store the wythoff array ; anti diagonal ; 

import math


def Wythoff(mp, n, k):
    tau = (math .sqrt(5) + 1) / 2
    t_n_k = 0
    if ((n in mp) and (k in mp[n])):
        return mp[n][k]
    if (k == -1):
        return n - 1
    elif (k == 0):
        t_n_k = math .floor(n * tau)
    else:
        t_n_k = Wythoff(mp, n, k - 1) + Wythoff(mp, n, k - 2)
    if n not in mp:
        mp[n] = dict()
    mp[n][k] = t_n_k
    return int(t_n_k)


def Wythoff_Array(n):
    i = 0
    j = 0
    count = 0
    mp = dict()
    while (count < n):
        print(Wythoff(mp, i + 1, j + 1), end='')
        count += 1
        if (count != n):
            print(", ", end='')
        i += 1
        j -= 1
        if (j < 0):
            j = i
            i = 0


if __name__ == '__main__':
    n = 15
    Wythoff_Array(n)

# Expected output:
1, 2, 4, 3, 7, 6, 5, 11, 10, 9, 8, 18, 16, 15, 12
# sum of two numbers if the original ratio and new ratio obtained by adding a given number to each number is given | function to return the sum of numbers which are in the ration a : b and after adding x to both the numbers the new ratio becomes c : d ; 
def sum(a, b, c, d, x):
    ans = ((x * (a + b) * (c - d)) / ((a * d) - (b * c)))
    return ans


if __name__ == '__main__':
    a, b, c, d, x = 1, 2, 9, 13, 5
    print(sum(a, b, c, d, x))

# Expected output:
12.0
# triangle of numbers arising from gilbreath 's conjecture | python3 code for printing the triangle of numbers arising from gilbreath 's conjecture ; check whether the number is prime or not ; set the 0 th row of the matrix with c primes from 0 , 0 to 0 , c - 1 ; find the n , k term of matrix of gilbreath 's conjecture ; recursively find ; store the ans ; print first n terms of gilbreath sequence successive absolute differences of primes read by antidiagonals upwards . ; map to store the matrix and hash to check if the element is present or not ; set the primes of first row ; print the gilbreath number ; increase the count ; anti diagonal upwards ; 

import math


def is_Prime(n):
    if (n < 2):
        return False
    for i in range(2, int(math .sqrt(n)) + 1):
        if (n % i == 0):
            return False
    return True


def set_primes(mp, hash, c):
    count = 0
    i = 2
    while (count < c):
        if (is_Prime(i)):
            if 0 not in mp:
                mp[0] = dict()
            mp[0][count] = i
            count += 1
            if 0 not in hash:
                hash[0] = dict()
            hash[0][count - 1] = 1
        i += 1


def Gilbreath(mp, hash, n, k):
    if (n in hash and k in hash[n] and hash[n][k] != 0):
        return mp[n][k]
    ans = abs(
        Gilbreath(
            mp,
            hash,
            n -
            1,
            k +
            1) -
        Gilbreath(
            mp,
            hash,
            n -
            1,
            k))
    if n not in mp:
        mp[n] = dict()
    mp[n][k] = ans
    return ans


def solve(n):
    i = 0
    j = 0
    count = 0
    mp = dict()
    hash = dict()
    set_primes(mp, hash, 100)
    while (count < n):
        print(Gilbreath(mp, hash, i, j), end=', ')
        count += 1
        i -= 1
        j += 1
        if (i < 0):
            i = j
            j = 0


if __name__ == '__main__':
    n = 15
    solve(n)

# Expected output:
2, 1, 3, 1, 2, 5, 1, 0, 2, 7, 1, 2, 2, 4, 11,
# roots of the quadratic equation when a + b + c = 0 without using shridharacharya formula | function to print the roots of the quadratic equation when a + b + c = 0 ; 
def printRoots(a, b, c):
    print(1, ",", c / (a * 1.0))


a = 2
b = 3
c = -5
printRoots(a, b, c)

# Expected output:
1 , -2.5
# longest alternative parity subsequence | function to find the longest ; marks the starting of odd number as sequence and alternatively changes ; finding the longest odd / even sequence ; find odd number ; find even number ; length of the longest even / odd sequence ; find odd number ; find even number ; answer is maximum of both odd / even or even / odd subsequence ; 
def longestAlternativeSequence(a, n):
    maxi1 = 0
    f1 = 0
    for i in range(n):
        if (f1 == 0):
            if (a[i] % 2):
                f1 = 1
                maxi1 += 1
        else:
            if (a[i] % 2 == 0):
                maxi1 += 1
                f1 = 0
    maxi2 = 0
    f2 = 0
    for i in range(n):
        if (f2):
            if (a[i] % 2):
                f2 = 1
                maxi2 += 1
        else:
            if (a[i] % 2 == 0):
                maxi2 += 1
                f2 = 0
    return max(maxi1, maxi2)


a = [13, 16, 8, 9, 32, 10]
n = len(a)
print(longestAlternativeSequence(a, n))

# Expected output:
4
# nambiar number generator | function to return the nambiar number of the given number ; if there is no digit to choose ; choose the first digit ; chosen digit 's parity ; to store the sum of the consecutive digits starting from the chosen digit ; while there are digits to choose ; update the sum ; if the parity differs ; return the current sum concatenated with the numbiar number for the rest of the string ; 
def nambiarNumber(Str, i):
    if (i >= len(Str)):
        return ""
    firstDigit = ord(Str[i]) - ord('0')
    digitParity = firstDigit % 2
    sumDigits = 0
    while (i < len(Str)):
        sumDigits += (ord(Str[i]) - ord('0'))
        sumParity = sumDigits % 2
        if (digitParity != sumParity):
            break
        i += 1
    return ("" + str(sumDigits) + nambiarNumber(Str, i + 1))


Str = "9880127431"
print(nambiarNumber(Str, 0))

# Expected output:
26971
# program to find the depreciation of value | python 3 program to find depreciation of the value initial value , rate and time are given ; function to return the depreciation of value ; 

from math import pow


def Depreciation(v, r, t):
    D = v * pow((1 - r / 100), t)
    return D


if __name__ == '__main__':
    V1 = 200
    R = 10
    T = 2
    print(int(Depreciation(V1, R, T)))

# Expected output:
162
# program to find first n fermat numbers | iterative function to calculate ( x ^ y ) in o ( log y ) ; res = 1 initialize result ; if y is odd , multiply x with the result ; n must be even now y = y >> 1 y = y / 2 x = x * x change x to x ^ 2 ; function to find nth fermat number ; 2 to the power i ; 2 to the power 2 ^ i ; function to find first n fermat numbers ; calculate 2 ^ 2 ^ i ; 
def power(x, y):

    res = 1

    while (y > 0):
        if (y & 1):
            res = res * x
        y = y >> 1
        x = x * x
    return res


def Fermat(i):
    power2_i = power(2, i)
    power2_2_i = power(2, power2_i)

    return power2_2_i + 1


def Fermat_Number(n):

    for i in range(n):
        print(Fermat(i), end="")

        if (i != n - 1):
            print(end=", ")


n = 7
Fermat_Number(n)

# Expected output:
3, 5, 17, 257, 65537, 4294967297, 18446744073709551617
# highly totient number | function to find euler totient number ; consider all prime factors of n and subtract their multiples from result ; check if p is a prime factor . ; if yes , then update n and result ; if n has a prime factor greater than sqrt ( n ) ( there can be at - most one such prime factor ) ; function to find first n highly totient numbers ; count of highly totient numbers and value of count of phi of previous numbers ; store all the values of phi ( x ) upto 10 ^ 5 with frequencies ; if count is greater than count of previous element ; display the number ; store the value of phi ; 
def phi(n):
    result = n
    p = 2
    while (p * p <= n):
        if (n % p == 0):
            while (n % p == 0):
                n //= p
            result -= (result // p)
        p += 1
    if (n > 1):
        result -= (result // n)
    return result


def Highly_Totient(n):
    count = 0
    p_count = -1
    mp = dict()
    i = 1
    while i < 100000:
        tmp = phi(i)
        if tmp not in mp:
            mp[tmp] = 0
        mp[tmp] += 1
        i += 1
    i = 1
    while (count < n):
        if ((i in mp) and mp[i] > p_count):
            print(i, end='')
            if (count < n - 1):
                print(", ", end='')
            p_count = mp[i]
            count += 1
        i += 1


if __name__ == '__main__':
    n = 20
    Highly_Totient(n)

# Expected output:
1, 2, 4, 8, 12, 24, 48, 72, 144, 240, 432, 480, 576, 720, 1152, 1440, 2880, 4320, 5760, 8640
# maximum value of division of two numbers in an array | function to maximum value of division of two numbers in an array ; traverse through the array ; return the required answer ; 
def Division(a, n):
    maxi = -10 ** 9
    mini = 10 ** 9
    for i in a:
        maxi = max(i, maxi)
        mini = min(i, mini)
    return maxi // mini


a = [3, 7, 9, 3, 11]
n = len(a)
print(Division(a, n))

# Expected output:
3