giulio98/xlcost-single-prompt huggingface dataset:
# find smallest number formed by inverting digits of given number n | function to invert the digits of integer n to form minimum possible number ; initialize the array ; iterate till the number n exists ; last digit of the number n ; checking if the digit is smaller than 9 - digit ; store the smaller digit in the array ; reduce the number each time ; check if the digit starts with 0 or not ; print the answer ; 
def number(num):
    a = [0] * 20
    r, i, j = 0, 0, 0
    while (num > 0):
        r = num % 10
        if (9 - r > r):
            a[i] = r
        else:
            a[i] = 9 - r
        i += 1
        num = num // 10
    if (a[i - 1] == 0):
        print(9, end="")
        i -= 1
    for j in range(i - 1, -1, -1):
        print(a[j], end="")


if __name__ == '__main__':
    num = 4545
    number(num)

# Expected output:
4444
# check if the concatenation of first n natural numbers is divisible by 3 | function that returns true if concatenation of first n natural numbers is divisible by 3 ; check using the formula ; 
def isDivisible(N):
    return (N - 1) % 3 != 0


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

# Expected output:
Yes
# count of n digit numbers whose sum of every k consecutive digits is equal | function to count the number of n - digit numbers such that sum of every k consecutive digits are equal ; range of numbers ; extract digits of the number ; store the sum of first k digits ; check for every k - consecutive digits ; if sum is not equal then break the loop ; increment the count if it satisfy the given condition ; 
def countDigitSum(N, K):
    l = pow(10, N - 1)
    r = pow(10, N) - 1
    count = 0
    for i in range(l, r + 1):
        num = i
        digits = [0] * N
        for j in range(N - 1, -1, -1):
            digits[j] = num % 10
            num //= 10
        sum = 0
        flag = 0
        for j in range(0, K):
            sum += digits[j]
        for j in range(1, N - K + 1):
            curr_sum = 0
            for m in range(j, j + K):
                curr_sum += digits[m]
            if (sum != curr_sum):
                flag = 1
                break
        if (flag == 0):
            count += 1
    return count


N = 2
K = 1
print(countDigitSum(N, K))

# Expected output:
9
# perfect square factors of a number | function that returns the count of factors that are perfect squares ; stores the count of number of times a prime number divides n . ; stores the number of factors that are perfect square ; count number of 2 's  that divides n ; calculate ans according to above formula ; check for all the possible numbers that can divide it ; check the number of times prime number i divides it ; calculate ans according to above formula ; return final count ; 
def noOfFactors(N):
    if (N == 1):
        return 1
    count = 0
    ans = 1
    while (N % 2 == 0):
        count += 1
        N = N // 2
    ans *= (count // 2 + 1)
    i = 3
    while i * i <= N:
        count = 0
        while (N % i == 0):
            count += 1
            N = N // i
        ans *= (count // 2 + 1)
        i += 2
    return ans


if __name__ == "__main__":
    N = 100
    print(noOfFactors(N))

# Expected output:
4
# check if the given string is linear or not | function to check if the given is linear or not ; iterate over string ; if character is not same as the first character then return false ; increment the tmp ; 
def is_linear(s):
    tmp = 0
    first = s[0]
    pos = 0
    while pos < len(s):
        if (s[pos] != first):
            return False
        tmp += 1
        pos += tmp
    return True


if __name__ == '__main__':
    str = "aapaxyayziabcde"
    if (is_linear(str)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# minimize cost to color all the vertices of an undirected graph | function to implement dfs traversal to marks all the vertices visited from vertex u ; mark u as visited ; traverse the adjacency list of u ; function to find the minimum cost to color all the vertices of graph ; to store adjacency list ; loop through the edges to create adjacency list ; to check if a vertex of the graph is visited ; mark visited to all the vertices that can be reached by colored vertices ; perform dfs ; to store count of uncolored sub - graphs ; loop through vertex to count uncolored sub - graphs ; if vertex not visited ; increase count of uncolored sub - graphs ; perform dfs to mark visited to all vertices of current sub - graphs ; calculate minimum cost to color all vertices ; print the result ; 
def DFS(U, vis, adj):
    vis[U] = 1
    for V in adj[U]:
        if (vis[V] == 0):
            DFS(V, vis, adj)


def minCost(N, M, vCost, eCost, sorc, colored, destination):
    adj = [[]for i in range(N + 1)]
    for i in range(M):
        adj[sorc[i]].append(destination[i])
        adj[destination[i]].append(sorc[i])
    vis = [0] * (N + 1)
    for i in range(len(colored)):
        DFS(colored[i], vis, adj)
    X = 0
    for i in range(1, N + 1):
        if (vis[i] == 0):
            X += 1
            DFS(i, vis, adj)
    mincost = X * min(vCost, eCost)
    print(mincost)


if __name__ == '__main__':
    N = 3
    M = 1
    sorc = [1]
    destination = [2]
    vCost = 3
    eCost = 2
    colored = [1]
    minCost(N, M, vCost, eCost, sorc, colored, destination)

# Expected output:
2
# minimum cost to reduce the integer n to 1 as per given conditions | function to find the minimum cost to reduce the integer n to 1 by the given operations ; check if x is 1 ; print the answer ; prestore the answer ; iterate till n exists ; divide n by x ; reduce n by x ; add the cost ; update the answer ; return the answer ; 
def min_cost(n, x, p, q):
    if (x == 1):
        print((n - 1) * p)
        return 0
    ans = (n - 1) * p
    pre = 0
    while (n > 1):
        tmp = n // x
        if (tmp < 0):
            break
        pre += (n - tmp * x) * p
        n //= x
        pre += q
        ans = min(ans, pre + (n - 1) * p)
    return ans


if __name__ == '__main__':
    n = 5
    x = 2
    p = 2
    q = 3
    print(min_cost(n, x, p, q))

# Expected output:
7
# number of ways to sum up a total of n from limited denominations | function to find the number of ways to sum up a total of n from limited denominations ; store the count of denominations ; stores the final result ; as one of the denominations is rupee 1 , so we can reduce the computation by checking the equality for n - ( a * 1 ) = n - a ; increment the count for number of ways ; 
def calculateWays(arr1, arr2, N):
    A = arr2[0]
    B = arr2[1]
    C = arr2[2]
    D = arr2[3]
    ans, b, c, d = 0, 0, 0, 0
    while b <= B and b * 5 <= (N):
        c = 0
        while (c <= C and b * 5 + c * 10 <= (N)):
            d = 0
            while (d <= D and b * 5 + c * 10 + d * 20 <= (N)):
                if ((b * 5) + (c * 10) + (d * 20) >= (N - A)):
                    ans += 1
                d += 1
            c += 1
        b += 1
    return ans


if __name__ == '__main__':
    N = 123
    arr1 = [1, 5, 10, 20]
    arr2 = [6, 4, 3, 5]
    print(calculateWays(arr1, arr2, N))

# Expected output:
5
# number of ways to sum up a total of n from limited denominations | python3 program for the above approach ; function to find the number of ways to sum up a total of n from limited denominations ; store the count of denominations ; stores the final result ; this will give the number of coins for all combinations of coins with value 1 and 5 ; l2 will sum the values of those indices of ways which is equal to ( n - ( c * 10 + d * 20 ) ) ; return the final count ; 

ways = [0 for i in range(1010)]


def calculateWays(arr1, arr2, N):
    A = arr2[0]
    B = arr2[1]
    C = arr2[2]
    D = arr2[3]
    ans = 0
    for b in range(0, B + 1):
        if (b * 5 > N):
            break
        for a in range(0, A + 1):
            if (a + b * 5 > N):
                break
            ways[a + b * 5] += 5
    for c in range(0, C):
        if (c * 10 > N):
            break
        for d in range(0, D):
            if (c * 10 + d * 20 > N):
                break
            ans += ways[N - c * 10 - d * 20]
    return ans


if __name__ == '__main__':
    N = 123
    arr1 = [1, 5, 10, 20]
    arr2 = [6, 4, 3, 5]
    print(calculateWays(arr1, arr2, N))

# Expected output:
5
# count of missing numbers in a sorted array | function that find the count of missing numbers in array a [ ] ; calculate the count of missing numbers in the array ; 
def countMissingNum(a, N):
    count = a[N - 1] - a[0] + 1 - N
    print(count)


arr = [5, 10, 20, 40]
N = len(arr)
countMissingNum(arr, N)

# Expected output:
32
# length of longest subsequence whose xor value is odd | function for find max xor subsequence having odd value ; initialize odd and even count ; count the number of odd and even numbers in given array ; if all values are odd in given array ; if all values are even in given array ; if both odd and even are present in given array ; 
def maxXorSubsequence(arr, n):
    odd = 0
    even = 0
    for i in range(0, n):
        if arr[i] % 2 != 0:
            odd += 1
        else:
            even += 1
    if odd == n:
        if odd % 2 == 0:
            maxlen = n - 1
        else:
            maxlen = n
    elif even == n:
        maxlen = 0
    else:
        if odd % 2 == 0:
            maxlen = even + odd - 1
        else:
            maxlen = even + odd
    return maxlen


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

# Expected output:
6
# count of pairs with sum n from first n natural numbers | function to calculate the value of count ; if n is even ; count of pairs ; otherwise ; 
def numberOfPairs(n):
    if (n % 2 == 0):
        return n // 2 - 1
    else:
        return n // 2


n = 8
print(numberOfPairs(n))

# Expected output:
3
# count pair of strings whose concatenation has every vowel | function to return the count of all concatenated string with each vowel at least once ; concatenating all possible pairs of string ; creating an array which checks , the presence of each vowel ; checking for each vowel by traversing the concatenated string ; checking if all the elements are set in vowel [ ] ; check if all vowels are present or not ; return the final count ; 
def good_pair(st, N):
    countStr = 0
    for i in range(N):
        for j in range(i + 1, N):
            res = st[i] + st[j]
            vowel = [0] * 5
            for k in range(len(res)):
                if (res[k] == 'a'):
                    vowel[0] = 1
                elif (res[k] == 'e'):
                    vowel[1] = 1
                elif (res[k] == 'i'):
                    vowel[2] = 1
                elif (res[k] == 'o'):
                    vowel[3] = 1
                elif (res[k] == 'u'):
                    vowel[4] = 1
            temp = 0
            for ind in range(5):
                if (vowel[ind] == 1):
                    temp += 1
            if (temp == 5):
                countStr += 1
    return countStr


if __name__ == "__main__":
    arr = ["aaweiolkju", "oxdfgujkmi"]
    N = len(arr)
    print(good_pair(arr, N))

# Expected output:
1
# find sum of exponents of prime factors of numbers 1 to n | function to implement sieve of erastosthenes ; create a boolean array and initialize all entries as false ; initializing smallest factor equal to 2 for all the even numbers ; iterate for odd numbers less then equal to n ; s ( i ) for a prime is the number itself ; for all multiples of current prime number ; i is the smallest prime factor for number " i * j " ; function to generate prime factors and its power ; s [ i ] is going to store smallest prime factor of i ; current prime factor of n ; power of current prime factor ; calculating prime factors and their powers sum ; increment the count and continue the process ; add count to the sum ; reinitialize count ; return the result ; function to find the sum of all the power of prime factors of n ; iterate for in [ 2 , n ] ; 
def sieveOfEratosthenes(N, s):
    prime = [False] * (N + 1)
    for i in range(2, N + 1, 2):
        s[i] = 2
    for i in range(3, N + 1, 2):
        if (prime[i] == False):
            s[i] = i
            j = i
            while (j * i <= N):
                if (prime[i * j] == False):
                    prime[i * j] = True
                    s[i * j] = i
                j += 2


def generatePrimeFactors(N):
    s = [0] * (N + 1)
    sum = 0
    sieveOfEratosthenes(N, s)
    curr = s[N]
    cnt = 1
    while (N > 1):
        N //= s[N]
        if (curr == s[N]):
            cnt += 1
            continue
        sum = sum + cnt
        curr = s[N]
        cnt = 1
    return sum


def findSum(N):
    sum = 0
    for i in range(2, N + 1):
        sum += generatePrimeFactors(i)
    print(sum)


if __name__ == '__main__':
    N = 4
    findSum(N)

# Expected output:
4
# minimum lcm of all subarrays of length at least 2 | python3 program for the above approach ; function to find lcm pf two numbers ; initialise lcm value ; check for divisibility of a and b by the lcm ; function to find the minimum lcm of all subarrays of length greater than 1 ; store the minimum lcm ; traverse the array ; find lcm of consecutive element ; check if the calculated lcm is less than the minlcm then update it ; print the minimum lcm ; 

import sys


def LCM(a, b):
    lcm = a if a > b else b
    while (True):
        if (lcm % a == 0 and lcm % b == 0):
            break
        else:
            lcm += 1
    return lcm


def findMinLCM(arr, n):
    minLCM = sys .maxsize
    for i in range(n - 1):
        val = LCM(arr[i], arr[i + 1])
        if (val < minLCM):
            minLCM = val
    print(minLCM)


arr = [4, 8, 12, 16, 20, 24]
n = len(arr)
findMinLCM(arr, n)

# Expected output:
8
# minimum integer with at most k bits set such that their bitwise and with n is maximum | function to find the integer with maximum bitwise with n ; store answer in the bitset initialized with 0 ; to maintain the count of set bits that should exceed k ; start traversing from the most significant if that bit is set in n then we will set in our answer i . e in x ; checking if the ith bit is set in n or not ; converting into integer ; 
def find_max(n, k):
    X = [0] * 32
    cnt = 0
    i = 31
    while (i >= 0 and cnt != k):
        if ((n & (1 << i)) != 0):
            X[i] = 1
            cnt += 1
        i -= 1
    s = ""
    for i in range(31, -1, -1):
        if X[i] == 0:
            s += '0'
        else:
            s += '1'
    return int(s, 2)


n, k = 10, 2
print(find_max(n, k))

# Expected output:
10
# convert string to integer without using any in | function to convert string to integer without using functions ; initialize a variable ; iterate till length of the string ; subtract 48 from the current digit ; print the answer ; 
def convert(s):
    num = 0
    n = len(s)
    for i in s:
        num = num * 10 + (ord(i) - 48)
    print(num)


if __name__ == '__main__':
    s = "123"
    convert(s)

# Expected output:
123
# permutation of array such that sum of adjacent elements are not divisible by 3 | python3 implementation to find the permutation of the array such that sum of adjacent elements is not divisible by 3 ; function to segregate numbers based on their remainder when divided by three ; loop to iterate over the elements of the given array ; condition to check the remainder of the number ; function to find the permutation of the array such that sum of adjacent elements is not divisible by 3 ; condition to check when it 's impossible to arrange ; condition to check when there are no zeros , and only ones or only twos ; array to store the permutation ; place the ones on alternate places in the answer array , leaving spaces for zeros remainder elements in the array ; adding a zero to connect it with a two ; place the twos on alternate places in the answer array , leaving spaces for zeros ; fill the zeros finally , between the ones and the twos ; print the arrangement of the array ; function to solve the problem ; as there can be only 3 remainders ; function call ; 

hell = 1000000007
N = 100005
c_0 = 0
c_1 = 0
c_2 = 0


def count_k(arr, ones, twos, zeros):
    global c_0, c_1, c_2
    for i in range(len(arr)):
        if (arr[i] % 3 == 0):
            c_0 += 1
            zeros .append(arr[i])
        elif (arr[i] % 3 == 1):
            c_1 += 1
            ones .append(arr[i])
        else:
            c_2 += 1
            twos .append(arr[i])


def printArrangement(arr, ones, twos, zeros):
    global c_0, c_1, c_2
    if ((c_0 == 0 and c_1 != 0 and c_2 != 0) or c_0 > c_1 + c_2 + 1):
        print("-1", end="")
        return
    if (c_0 == 0):
        for i in range(len(arr)):
            print(arr[i], end=" ")
        return
    ans = [-1] * N
    i, j = 1, 0
    while (j < c_1):
        ans[i] = ones[-1]
        ones .pop()
        i += 2
        j += 1
    ans[i - 1] = zeros[-1]
    zeros .pop()
    c_0 -= 1
    j = 0
    while (j < c_2):
        ans[i] = twos[-1]
        twos .pop()
        j += 1
        i += 2
    k = 0
    while c_0 > 0:
        if (ans[k] == -1):
            ans[k] = zeros[-1]
            c_0 -= 1
        k += 2
    for i1 in range(N):
        if (ans[i1] != -1):
            print(ans[i1], end=" ")
    return


def solve(n, arr):
    ones = []
    zeros = []
    twos = []
    count_k(arr, ones, twos, zeros)
    printArrangement(arr, ones, twos, zeros)


n = 5
arr = [1, 2, 3, 4, 5]
solve(n, arr)

# Expected output:
4 1 3 5 2
# count of unique digits in a given number n | function that returns the count of unique digits of the given number ; initialize a variable to store count of unique digits ; initialize cnt list to store digit count ; iterate through the digits of n ; retrieve the last digit of n ; increase the count of the last digit ; remove the last digit of n ; iterate through the cnt list ; if frequency of digit is 1 ; increment the count of unique digits ; return the count of unique digit ; 
def countUniqueDigits(N):
    res = 0
    cnt = [0] * 10
    while (N > 0):
        rem = N % 10
        cnt[rem] += 1
        N = N // 10
    for i in range(10):
        if (cnt[i] == 1):
            res += 1
    return res


N = 2234262
print(countUniqueDigits(N))

# Expected output:
3
# check if frequency of each element in given array is unique or not | python3 code for the above approach ; function to check whether the frequency of elements in array is unique or not . ; freq map will store the frequency of each element of the array ; store the frequency of each element from the array ; check whether frequency of any two or more elements are same or not . if yes , return false ; return true if each frequency is unique ; 

from collections import defaultdict


def checkUniqueFrequency(arr, n):
    freq = defaultdict(int)
    for i in range(n):
        freq[arr[i]] += 1
    uniqueFreq = set([])
    for i in freq:
        if (freq[i] in uniqueFreq):
            return False
        else:
            uniqueFreq .add(freq[i])
    return True


if __name__ == "__main__":
    arr = [1, 1, 2, 5, 5]
    n = len(arr)
    res = checkUniqueFrequency(arr, n)
    if (res):
        print("Yes")
    else:
        print("No")

# Expected output:
No
# sum of first n natural numbers with all powers of 2 added twice | python3 program to implement the above approach ; function to raise n to the power p and return the value ; function to calculate the log base 2 of an integer ; calculate log2 ( n ) indirectly using log ( ) method ; function to calculate and return the required sum ; sum of first n natural numbers ; sum of all powers of 2 up to n ; 

import math


def power(N, P):
    return math .pow(N, P)


def Log2(N):
    result = (math .log(N) // math .log(2))
    return result


def specialSum(n):
    sum = n * (n + 1) // 2
    a = Log2(n)
    sum = sum + power(2, a + 1) - 1
    return sum


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

# Expected output:
17.0
# rearrange an array such that product of every two consecutive elements is a multiple of 4 | function to rearrange array elements such that the every two consecutive elements is a multiple of 4 ; if element is odd ; odd ; if element is divisible by 4 ; divisible by 4 ; if element is not divisible by 4 ; even but not divisible by 4 ; condition for rearrangement to be possible ; print odd [ i ] and four [ i ] consecutively ; print the remaining four [ i ] , if any ; condition for rearrangement to be possible ; print odd [ i ] and four [ i ] consecutively ; print the remaining four [ i ] , if any ; print the non_four [ i ] elements at the end ; no possible configuration ; 
def Permute(arr, n):
    odd = 0
    four = 0
    non_four = 0
    ODD, FOUR, NON_FOUR = [], [], []
    for x in arr:
        if (x & 1):
            odd += 1
            ODD .append(x)
        elif (x % 4 == 0):
            four += 1
            FOUR .append(x)
        else:
            non_four += 1
            NON_FOUR .append(x)
    if (non_four == 0 and four >= odd - 1):
        x = len(ODD)
        y = len(FOUR)
        i = 0
        while i < x:
            print(ODD[i], end=" ")
            if (i < y):
                print(FOUR[i], end=" ")
        while (i < y):
            print(FOUR[i], end=" ")
            i += 1
        print()
    elif (non_four > 0 and four >= odd):
        x = len(ODD)
        y = len(FOUR)
        i = 0
        while i < x:
            print(ODD[i], end=" ")
            if (i < y):
                print(FOUR[i], end=" ")
            i += 1
        while (i < y):
            print(FOUR[i], end=" ")
            i += 1
        for j in NON_FOUR:
            print(j, end=" ")
    else:
        print("Not Possible")


if __name__ == '__main__':
    arr = [2, 7, 1, 8, 2, 8]
    N = len(arr)
    Permute(arr, N)

# Expected output:
7 8 1 8 2 2
# construct the smallest possible array with given sum and xor | function to find array ; array not possible ; array possible with exactly 1 or no element ; checking array with two elements possible or not . ; 
def findArray(_sum, xorr):
    if (xorr > _sum or _sum % 2 != xorr % 2):
        print("No Array Possible")
        return
    if (_sum == xorr):
        if (_sum == 0):
            print("Array is empty", " with size 0")
        else:
            print("Array size is", 1)
            print("Array is", _sum)
        return
    mid = (_sum - xorr) // 2
    if (xorr & mid == 1):
        print("Array size is", 3)
        print("Array is", xorr, mid, mid)
    else:
        print("Array size is", 2)
        print("Array is", (xorr + mid), mid)


_sum = 4
xorr = 2
findArray(_sum, xorr)

# Expected output:
Array size is 2
Array is 3 1
# smallest number greater than x which is k | function to find the smallest k periodic integer greater than x ; stores the number in a temporary string ; set x [ i ] = x [ i - k ] for i > k ; start from the current index ; loop upto n change x [ j ] to x [ i ] ; return x if current value is greater than original value ; find the first digit not equal to 9 ; increment x [ i ] ; set pos to current index ; change x [ i ] to 0 for all indices from pos + 1 to k ; set x [ i ] = x [ i - k ] for i > k ; loop upto n change x [ j ] to x [ i ] ; return the final string ; 
def Kperiodicinteger(X, N, K):
    X = list(X)
    temp = X .copy()
    for i in range(K):
        j = i
        while (j < N):
            X[j] = X[i]
            j += K
    if (X >= temp):
        return X
    POS = 0
    for i in range(K - 1, -1, -1):
        if (X[i] != '9'):
            X[i] = str(int(X[i]) + 1)
            POS = i
            break
    for i in range(POS + 1, K):
        X[i] = '0'
    for i in range(K):
        j = i
        while (j < N):
            X[j] = X[i]
            j += K
    return X


N = 4
K = 2
X = "1215"
print(*Kperiodicinteger(X, N, K), sep='')

# Expected output:
1313