giulio98/xlcost-single-prompt huggingface dataset:
# program to convert centimeters to pixels | function to convert centimeters to pixels ; 
def Conversion(centi):
    pixels = (96 * centi) / 2.54
    print(round(pixels, 2))


centi = 15
Conversion(centi)

# Expected output:
566.93
# kth array element after m replacements of array elements by xor of adjacent pairs | method that returns the corresponding output by taking the given inputs . ; if this condition is satisfied , value of m is invalid ; check if index k is valid ; loop to perform m operations ; creating a temporary list ; traversing the array ; calculate xor values of adjacent elements ; adding this value to the temporary list ; update the original array ; getting value at index k ; number of elements ; 
def xor_operations(N, arr, M, K):
    if M < 0 or M >= N:
        return -1
    if K < 0 or K >= N - M:
        return -1
    for _ in range(M):
        temp = []
        for i in range(len(arr) - 1):
            value = arr[i] ^ arr[i + 1]
            temp .append(value)
        arr = temp[:]
    ans = arr[K]
    return ans


N = 5
arr = [1, 4, 5, 6, 7]
M = 1
K = 2
print(xor_operations(N, arr, M, K))

# Expected output:
3
# check if n can be divided into k consecutive elements with a sum equal to n | function to find the k consecutive elements with a sum equal to n ; iterate over [ 2 , inf ] ; store the sum ; if the sum exceeds n then break the loop ; common difference should be divisible by number of terms ; print value of i & return ; print " - 1" if not possible to break n ; 
def canBreakN(n):
    for i in range(2, n):
        m = i * (i + 1) // 2
        if (m > n):
            break
        k = n - m
        if (k % i):
            continue
        print(i)
        return
    print("-1")


N = 12
canBreakN(N)

# Expected output:
3
# coprime divisors of a number | python3 program to find two coprime divisors of a 

import math


def findCoprimePair(N):
    for x in range(2, int(math .sqrt(N)) + 1):
        if (N % x == 0):
            while (N % x == 0):
                N //= x
            if (N > 1):
                print(x, N)
                return
    print("-1")


N = 45
findCoprimePair(N)
N = 25
findCoprimePair(N)

# Expected output:
3 5
-1
# hexanacci numbers | function to print the nth hexanacci number ; 
def printhexaRec(n):
    if (n == 0 or n == 1 or n == 2 or n == 3 or n == 4 or n == 5):
        return 0
    elif (n == 6):
        return 1
    else:
        return (
            printhexaRec(
                n -
                1) +
            printhexaRec(
                n -
                2) +
            printhexaRec(
                n -
                3) +
            printhexaRec(
                n -
                4) +
            printhexaRec(
                n -
                5) +
            printhexaRec(
                n -
                6))


def printhexa(n):
    print(printhexaRec(n))


n = 11
printhexa(n)

# Expected output:
16
# hexanacci numbers | function to print the nth term of the hexanacci number ; initialize first five numbers to base cases ; declare a current variable ; loop to add previous five numbers for each number starting from 5 and then assign first , second , third , fourth fifth to second , third , fourth , fifth and curr to sixth respectively ; 
def printhexa(n):
    if (n < 0):
        return
    first = 0
    second = 0
    third = 0
    fourth = 0
    fifth = 0
    sixth = 1
    curr = 0
    if (n < 6):
        print(first)
    elif (n == 6):
        print(sixth)
    else:
        for i in range(6, n):
            curr = first + second + third + fourth + fifth + sixth
            first = second
            second = third
            third = fourth
            fourth = fifth
            fifth = sixth
            sixth = curr
    print(curr)


n = 11
printhexa(n)

# Expected output:
16
# find the smallest number whose sum of digits is n | function to find the smallest number whose sum of digits is also n ; 
def smallestNumber(N):
    print((N % 9 + 1) * pow(10, (N // 9)) - 1)


N = 10
smallestNumber(N)

# Expected output:
19
# distinct powers of a number n such that the sum is equal to k | initializing the powerarray with all 0 's ; function to find the powers of n that add up to k ; initializing the counter ; executing the while loop until k is greater than 0 ; if k % n == 1 , then the power array is incremented by 1 ; checking if any power is occurred more than once ; for any other value , the sum of powers cannot be added up to k ; printing the powers of n that sum up to k ; 

b = [0 for i in range(50)]


def PowerArray(n, k):
    count = 0
    while (k):
        if (k % n == 0):
            k //= n
            count += 1
        elif (k % n == 1):
            k -= 1
            b[count] += 1
            if (b[count] > 1):
                print(-1)
                return 0
        else:
            print(-1)
            return 0
    for i in range(50):
        if (b[i]):
            print(i, end=",")


if __name__ == '__main__':
    N = 3
    K = 40
    PowerArray(N, K)

# Expected output:
0,1,2,3,
# program to find value of 1 ^ k + 2 ^ k + 3 ^ k + ... + n ^ k | python 3 program to find the value 1 ^ k + 2 ^ k + 3 ^ k + . . + n ^ k ; function to find value of 1 ^ k + 2 ^ k + 3 ^ k + . . + n ^ k ; initialise sum to 0 ; find the value of pow ( i , 4 ) and then add it to the sum ; return the sum ; 

from math import pow


def findSum(N, k):
    sum = 0
    for i in range(1, N + 1, 1):
        sum += pow(i, k)
    return sum


if __name__ == '__main__':
    N = 8
    k = 4
    print(int(findSum(N, k)))

# Expected output:
8772
# count of indices in an array that satisfy the given condition | function to return the count of indices that satisfy the given condition ; to store the result ; to store the current maximum initialized to 0 since there are only positive elements in the array ; i is a valid index ; update the maximum so far ; increment the counter ; 
def countIndices(arr, n):
    cnt = 0
    max = 0
    for i in range(n):
        if (max < arr[i]):
            max = arr[i]
            cnt += 1
    return cnt


if __name__ == '__main__':
    arr = [1, 2, 3, 4]
    n = len(arr)
    print(countIndices(arr, n))

# Expected output:
4
# maximum frequency of a remainder modulo 2 i | binary representation of the digits ; function to return the maximum frequency of s modulo with a power of 2 ; store the binary representation ; convert the octal to binary ; remove the lsb ; if there is 1 in the binary representation ; find the number of zeroes in between two 1 's in the binary representation ; 

bin = ["000", "001", "010", "011", "100", "101", "110", "111"]


def maxFreq(s):
    binary = ""
    for i in range(len(s)):
        binary += bin[ord(s[i]) - ord('0')]
    binary = binary[0:len(binary) - 1]
    count = 1
    prev = -1
    j = 0
    for i in range(len(binary) - 1, -1, -1):
        if (binary[i] == '1'):
            count = max(count, j - prev)
            prev = j
        j += 1
    return count


if __name__ == "__main__":
    octal = "13"
    print(maxFreq(octal))

# Expected output:
2
# find the number of cells in the table contains x | function to find number of cells in the table contains x ; 
def Cells(n, x):
    if (n <= 0 or x <= 0 or x > n * n):
        return 0
    i = 1
    count = 0
    while (i * i < x):
        if (x % i == 0 and x <= n * i):
            count += 2
        i += 1
    if (i * i == x):
        return count + 1
    else:
        return count


n = 6
x = 12
print(Cells(n, x))

# Expected output:
4
# find the maximum possible value of the minimum value of modified array | function to find the maximum possible value of the minimum value of the modified array ; to store minimum value of array ; to store sum of elements of array ; solution is not possible ; zero is the possible value ; minimum possible value ; maximum possible value ; to store a required answer ; binary search ; if mid is possible then try to increase required answer ; if mid is not possible then decrease required answer ; return required answer ; 
def maxOfMin(a, n, S):
    mi = 10 ** 9
    s1 = 0
    for i in range(n):
        s1 += a[i]
        mi = min(a[i], mi)
    if (s1 < S):
        return -1
    if (s1 == S):
        return 0
    low = 0
    high = mi
    ans = 0
    while (low <= high):
        mid = (low + high) // 2
        if (s1 - (mid * n) >= S):
            ans = mid
            low = mid + 1
        else:
            high = mid - 1
    return ans


a = [10, 10, 10, 10, 10]
S = 10
n = len(a)
print(maxOfMin(a, n, S))

# Expected output:
8
# program to print ' n ' alphabet using the number pattern from 1 to n | function to print the desired alphabet n pattern ; declaring the values of right , left and diagonal values ; main loop for the rows ; for the left values ; spaces for the diagonals ; condition for the diagonals ; spaces for the right values ; for the right values ; 
def Alphabet_N_Pattern(N):
    Right = 1
    Left = 1
    Diagonal = 2
    for index in range(N):
        print(Left, end="")
        Left += 1
        for side_index in range(0, 2 * (index), 1):
            print(" ", end="")
        if (index != 0 and index != N - 1):
            print(Diagonal, end="")
            Diagonal += 1
        else:
            print(" ", end="")
        for side_index in range(0, 2 * (N - index - 1), 1):
            print(" ", end="")
        print(Right, end="")
        Right += 1
        print("",  end="")


if __name__ == '__main__':
    Size = 6
    Alphabet_N_Pattern(Size)

# Expected output:
1           12  2        23    3      34      4    45        5  56           6
# check if the sum of digits of a number n divides it | function to check if sum of digits of a number divides it ; calculate sum of all of digits of n ; 
def isSumDivides(N):
    temp = N
    sum = 0
    while (temp):
        sum += temp % 10
        temp = int(temp / 10)
    if (N % sum == 0):
        return 1
    else:
        return 0


if __name__ == '__main__':
    N = 12
    if (isSumDivides(N)):
        print("YES")
    else:
        print("NO")

# Expected output:
YES
# Sum of numbers from 1 to N which are divisible by 3 or 4 | Function to calculate the sum of numbers divisible by 3 or 4 ;
def sum(N):
    global S1, S2, S3
    S1 = (((N // 3)) * (2 * 3 + (N // 3 - 1) * 3) // 2)
    S2 = (((N // 4)) * (2 * 4 + (N // 4 - 1) * 4) // 2)
    S3 = (((N // 12)) * (2 * 12 + (N // 12 - 1) * 12) // 2)
    return int(S1 + S2 - S3)


if __name__ == '__main__':
    N = 12
    print(sum(N))

# Expected output:
42
# next greater number than n with exactly one bit different in binary representation of n | function to find next greater number than n with exactly one bit different in binary representation of n ; it is guaranteed that there is a bit zero in the number ; if the shifted bit is zero then break ; increase the bit shift ; increase the power of 2 ; set the lowest bit of the number ; 
def nextGreater(N):
    power_of_2 = 1
    shift_count = 0
    while (True):
        if (((N >> shift_count) & 1) % 2 == 0):
            break
        shift_count += 1
        power_of_2 = power_of_2 * 2
    return (N + power_of_2)


N = 11
print("The next number is =", nextGreater(N))

# Expected output:
The next number is = 15
# count number of ways to cover a distance | set 2 | function to return the count of the total number of ways to cover the distance with 1 , 2 and 3 steps ; base conditions ; to store the last three stages ; find the numbers of steps required to reach the distance i ; return the required answer ; 
def countWays(n):
    if (n == 0):
        return 1
    if (n <= 2):
        return n
    f0 = 1
    f1 = 1
    f2 = 2
    ans = 0
    for i in range(3, n + 1):
        ans = f0 + f1 + f2
        f0 = f1
        f1 = f2
        f2 = ans
    return ans


n = 4
print(countWays(n))

# Expected output:
7
# maximize sum by choosing elements from different section of a matrix | python3 program for the above approach ; function to find the maximum value ; dp table ; fill the dp in bottom up manner ; maximum of the three sections ; maximum of the first section ; maximum of the second section ; maximum of the third section ; if we choose element from section 1 , we cannot have selection from same section in adjacent rows ; print the maximum sum ; 

import numpy as np
n = 6
m = 6


def maxSum(arr):
    dp = np .zeros((n + 1, 3))
    for i in range(n):
        m1 = 0
        m2 = 0
        m3 = 0
        for j in range(m):
            if ((j // (m // 3)) == 0):
                m1 = max(m1, arr[i][j])
            elif ((j // (m // 3)) == 1):
                m2 = max(m2, arr[i][j])
            elif ((j // (m // 3)) == 2):
                m3 = max(m3, arr[i][j])
        dp[i + 1][0] = max(dp[i][1], dp[i][2]) + m1
        dp[i + 1][1] = max(dp[i][0], dp[i][2]) + m2
        dp[i + 1][2] = max(dp[i][1], dp[i][0]) + m3
    print(max(max(dp[n][0], dp[n][1]), dp[n][2]))


if __name__ == "__main__":
    arr = [[1, 3, 5, 2, 4, 6], [6, 4, 5, 1, 3, 2], [1, 3, 5, 2, 4, 6],
           [6, 4, 5, 1, 3, 2], [6, 4, 5, 1, 3, 2], [1, 3, 5, 2, 4, 6]]
    maxSum(arr)

# Expected output:
35.0
# total number of odd length palindrome sub | function to find the total palindromic odd length sub - sequences ; dp array to store the number of palindromic subsequences for 0 to i - 1 and j + 1 to n - 1 ; we will start with the largest distance between i and j ; for each len , we fix our i ; for this i we will find our j ; base cases ; if the characters are equal then look for out of bound index ; we have only 1 way that is to just pick these characters ; if the characters are not equal ; subtract it as we have counted it twice ; we have just 1 palindrome sequence of length 1 ; else total ways would be sum of dp [ i - 1 ] [ i + 1 ] , that is number of palindrome sub - sequences from 1 to i - 1 + number of palindrome sub - sequences from i + 1 to n - 1 ; 
def solve(s):
    n = len(s)
    dp = [[0 for i in range(n)]for i in range(n)]
    for Len in range(n - 1, -1, -1):
        for i in range(n):
            if i + Len >= n:
                break
            j = i + Len
            if (i == 0 and j == n - 1):
                if (s[i] == s[j]):
                    dp[i][j] = 2
                elif (s[i] != s[j]):
                    dp[i][j] = 1
            else:
                if (s[i] == s[j]):
                    if (i - 1 >= 0):
                        dp[i][j] += dp[i - 1][j]
                    if (j + 1 <= n - 1):
                        dp[i][j] += dp[i][j + 1]
                    if (i - 1 < 0 or j + 1 >= n):
                        dp[i][j] += 1
                elif (s[i] != s[j]):
                    if (i - 1 >= 0):
                        dp[i][j] += dp[i - 1][j]
                    if (j + 1 <= n - 1):
                        dp[i][j] += dp[i][j + 1]
                    if (i - 1 >= 0 and j + 1 <= n - 1):
                        dp[i][j] -= dp[i - 1][j + 1]
    ways = []
    for i in range(n):
        if (i == 0 or i == n - 1):
            ways .append(1)
        else:
            total = dp[i - 1][i + 1]
            ways .append(total)
    for i in ways:
        print(i, end=" ")


s = "xyxyx"
solve(s)

# Expected output:
1 3 4 3 1
# find the number of chicks in a zoo at nth day | function to return the number of chicks on the nth day ; size of dp [ ] has to be at least 6 ( 1 - based indexing ) ; every day current population will be three times of the previous day ; manually calculated value ; from 8 th day onwards ; chick population decreases by 2 / 3 everyday . for 8 th day on [ i - 6 ] i . e 2 nd day population was 3 and so 2 new born die on the 6 th day and so on for the upcoming days ; 
def getChicks(n):
    size = max(n, 7)
    dp = [0] * size
    dp[0] = 0
    dp[1] = 1
    for i in range(2, 7):
        dp[i] = dp[i - 1] * 3
    dp[6] = 726
    for i in range(8, n + 1):
        dp[i] = (dp[i - 1] - (2 * dp[i - 6] // 3)) * 3
    return dp[n]


n = 3
print(getChicks(n))

# Expected output:
9
# find the number of chicks in a zoo at nth day | function to return the number of chicks on the nth day ; 
def getChicks(n):
    chicks = pow(3, n - 1)
    return chicks


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

# Expected output:
9
# find minimum steps required to reach the end of a matrix | set 2 | python3 implementation of the approach ; 2d array to store states of dp ; array to determine whether a state has been solved before ; function to return the minimum steps required ; base cases ; if a state has been solved before it won 't be evaluated again ; recurrence relation ; 

import numpy as np
n = 3
dp = np .zeros((n, n))
v = np .zeros((n, n))


def minSteps(i, j, arr):
    if (i == n - 1 and j == n - 1):
        return 0
    if (i > n - 1 or j > n - 1):
        return 9999999
    if (v[i][j]):
        return dp[i][j]
    v[i][j] = 1
    dp[i][j] = 9999999
    for k in range(max(0, arr[i][j] + j - n + 1),
                   min(n - i - 1, arr[i][j]) + 1):
        dp[i][j] = min(dp[i][j], minSteps(i + k, j + arr[i][j] - k, arr))
    dp[i][j] += 1
    return dp[i][j]


if __name__ == "__main__":
    arr = [[4, 1, 2], [1, 1, 1], [2, 1, 1]]
    ans = minSteps(0, 0, arr)
    if (ans >= 9999999):
        print(-1)
    else:
        print(ans)

# Expected output:
1.0
# find minimum steps required to reach the end of a matrix | set | python3 program to implement above approach ; 2d array to store states of dp ; array to determine whether a state has been solved before ; function to find the minimum number of steps to reach the end of matrix ; base cases ; if a state has been solved before it won 't be evaluated again. ; recurrence relation ; 

import numpy as np
n = 3
dp = np .zeros((n, n))
v = np .zeros((n, n))


def minSteps(i, j, arr):
    if (i == n - 1 and j == n - 1):
        return 0
    if (i > n - 1 or j > n - 1):
        return 9999999
    if (v[i][j]):
        return dp[i][j]
    v[i][j] = 1
    dp[i][j] = 1 + min(minSteps(i + arr[i][j], j, arr),
                       minSteps(i, j + arr[i][j], arr))
    return dp[i][j]


arr = [[2, 1, 2], [1, 1, 1], [1, 1, 1]]
ans = minSteps(0, 0, arr)
if (ans >= 9999999):
    print(-1)
else:
    print(ans)

# Expected output:
2.0
# treasure and cities | a memoization based program to find maximum treasure that can be collected . ; k is current index and col is previous color . ; base case ; we have two options either visit current city or skip that ; check if color of this city is equal to prev visited city ; return max of both options ; 

MAX = 1001
dp = [[-1 for i in range(MAX)]for i in range(MAX)]


def MaxProfit(treasure, color, n, k, col, A, B):
    if (k == n):
        dp[k][col] = 0
        return dp[k][col]
    if (dp[k][col] != -1):
        return dp[k][col]
    summ = 0
    if (col == color[k]):
        summ += max(A *
                    treasure[k] +
                    MaxProfit(treasure, color, n, k +
                              1, color[k], A, B), MaxProfit(treasure, color, n, k +
                                                            1, col, A, B))
    else:
        summ += max(B *
                    treasure[k] +
                    MaxProfit(treasure, color, n, k +
                              1, color[k], A, B), MaxProfit(treasure, color, n, k +
                                                            1, col, A, B))
    dp[k][col] = summ
    return dp[k][col]


A = -5
B = 7
treasure = [4, 8, 2, 9]
color = [2, 2, 6, 2]
n = len(color)
print(MaxProfit(treasure, color, n, 0, 0, A, B))

# Expected output:
133
# tetranacci numbers | function to print the n - th tetranacci number ; base cases ; 
def printTetra(n):
    dp = [0] * (n + 5)
    dp[0] = 0
    dp[1] = 1
    dp[2] = 1
    dp[3] = 2
    for i in range(4, n + 1):
        dp[i] = (dp[i - 1] + dp[i - 2] + dp[i - 3] + dp[i - 4])
    print(dp[n])


n = 10
printTetra(n)

# Expected output:
208
# maximum sum in circular array such that no two elements are adjacent | function to calculate the sum from 0 th position to ( n - 2 ) th position ; copy the element of original array to dp [ ] ; find the maximum element in the array ; start from 2 nd to n - 1 th pos ; traverse for all pairs bottom - up approach ; dp - condition ; find maximum sum ; return the maximum ; function to find the maximum sum from 1 st position to n - 1 - th position ; traverse from third to n - th pos ; bootom - up approach ; dp condition ; find max sum ; return max ; 
def maxSum1(arr, n):
    dp = [0] * n
    maxi = 0
    for i in range(n - 1):
        dp[i] = arr[i]
        if (maxi < arr[i]):
            maxi = arr[i]
    for i in range(2, n - 1):
        for j in range(i - 1):
            if (dp[i] < dp[j] + arr[i]):
                dp[i] = dp[j] + arr[i]
                if (maxi < dp[i]):
                    maxi = dp[i]
    return maxi


def maxSum2(arr, n):
    dp = [0] * n
    maxi = 0
    for i in range(1, n):
        dp[i] = arr[i]
        if (maxi < arr[i]):
            maxi = arr[i]
    for i in range(3, n):
        for j in range(1, i - 1):
            if (dp[i] < arr[i] + dp[j]):
                dp[i] = arr[i] + dp[j]
                if (maxi < dp[i]):
                    maxi = dp[i]
    return maxi


def findMaxSum(arr, n):
    return max(maxSum1(arr, n), maxSum2(arr, n))


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

# Expected output:
4
# permutation coefficient | returns value of permutation coefficient p ( n , k ) ; calculate value of permutation coefficient in bottom up manner ; base cases ; calculate value using previously stored values ; this step is important as p ( i , j ) = 0 for j > i ; 
def permutationCoeff(n, k):
    P = [[0 for i in range(k + 1)]for j in range(n + 1)]
    for i in range(n + 1):
        for j in range(min(i, k) + 1):
            if (j == 0):
                P[i][j] = 1
            else:
                P[i][j] = P[i - 1][j] + (j * P[i - 1][j - 1])
            if (j < k):
                P[i][j + 1] = 0
    return P[n][k]


n = 10
k = 2
print("Value fo P(", n, ", ", k, ") is ", permutationCoeff(n, k), sep="")

# Expected output:
Value fo P(10, 2) is 90
# permutation coefficient | returns value of permutation coefficient p ( n , k ) ; base case ; calculate value factorials up to n ; p ( n , k ) = n ! / ( n - k ) ! ; 
def permutationCoeff(n, k):
    fact = [0 for i in range(n + 1)]
    fact[0] = 1
    for i in range(1, n + 1):
        fact[i] = i * fact[i - 1]
    return int(fact[n] / fact[n - k])


n = 10
k = 2
print("Value of P(", n, ", ", k, ") is ", permutationCoeff(n, k), sep="")

# Expected output:
Value of P(10, 2) is 90
# dynamic programming | returns true if there is a subset of set [ ] with sun equal to given sum ; base cases ; if last element is greater than sum , then ignore it ; else , check if sum can be obtained by any of the following ( a ) including the last element ( b ) excluding the last element ; 
def isSubsetSum(set, n, sum):
    if (sum == 0):
        return True
    if (n == 0):
        return False
    if (set[n - 1] > sum):
        return isSubsetSum(set, n - 1, sum)
    return isSubsetSum(set, n - 1, sum) or isSubsetSum(set,
                                                       n - 1, sum - set[n - 1])


set = [3, 34, 4, 12, 5, 2]
sum = 9
n = len(set)
if (isSubsetSum(set, n, sum)):
    print("Found a subset with given sum")
else:
    print("No subset with given sum")

# Expected output:
Found a subset with given sum