giulio98/xlcost-single-prompt huggingface dataset:
# find the integers that doesnot ends with t1 or t2 when squared and added x | function to print the elements not ending with t1 or t2 ; flag to check if none of the elements do not end with t1 or t2 ; traverse through all the elements ; temporary variable to store the value ; if the last digit is neither t1 nor t2 then ; print the number ; set the flag as false ; if none of the elements meets the specification ; test case 1 ; call the function ; 
def findIntegers(n, a, x, t1, t2):
    flag = True
    for i in range(n):
        temp = pow(a[i], 2) + x
        if (temp % 10 != t1 and temp % 10 != t2):
            print(temp, end=" ")
            flag = False
    if flag:
        print(-1)


N, X, T1, T2 = 4, 10, 5, 6
a = [3, 1, 4, 7]
findIntegers(N, a, X, T1, T2)
print()
N, X, T1, T2 = 4, 2, 5, 6
b = [2, 18, 22, 8]
findIntegers(N, b, X, T1, T2)

# Expected output:
19 11 59 
-1
# first n terms whose sum of digits is a multiple of 10 | python3 code for above implementation ; function to return the sum of digits of n ; add last digit to the sum ; remove last digit ; function to return the nth term of the required series ; if sum of digit is already a multiple of 10 then append 0 ; to store the minimum digit that must be appended ; return n after appending the required digit ; function to print the first n terms of the required series ; 

TEN = 10


def digitSum(n):
    sum = 0
    while (n > 0):
        sum += n % TEN
        n //= TEN
    return sum


def getNthTerm(n):
    sum = digitSum(n)
    if (sum % TEN == 0):
        return (n * TEN)
    extra = TEN - (sum % TEN)
    return ((n * TEN) + extra)


def firstNTerms(n):
    for i in range(1, n + 1):
        print(getNthTerm(i), end=" ")


n = 10
firstNTerms(n)

# Expected output:
19 28 37 46 55 64 73 82 91 109
# nearest greater number by interchanging the digits | python3 program to find nearest greater value ; find all the possible permutation of value a . ; convert into integer ; find the minimum value of a by interchanging the digits of a and store min1 . ; 

min1 = 10 ** 9
_count = 0


def permutation(str1, i, n, p):
    global min1, _count
    if (i == n):
        str1 = "".join(str1)
        q = int(str1)
        if (q - p > 0 and q < min1):
            min1 = q
            _count = 1
    else:
        for j in range(i, n + 1):
            str1[i], str1[j] = str1[j], str1[i]
            permutation(str1, i + 1, n, p)
            str1[i], str1[j] = str1[j], str1[i]
    return min1


A = 213
B = 111
str2 = str(A)
str1 = [i for i in str2]
le = len(str1)
h = permutation(str1, 0, le - 1, B)
if _count == 1:
    print(h)
else:
    print(-1)

# Expected output:
123
# alcuin 's sequence | python3 program for alcuinatms sequence ; find the nth term of alcuin 's sequence ; return the ans ; print first n terms of alcuin number ; display the number ; 

from math import ceil, floor


def Alcuin(n):
    _n = n
    ans = 0
    ans = (round((_n * _n) / 12) - floor(_n / 4) * floor((_n + 2) / 4))
    return ans


def solve(n):
    for i in range(1, n + 1):
        print(Alcuin(i), end=", ")


n = 15
solve(n)

# Expected output:
0, 0, 1, 0, 1, 1, 2, 1, 3, 2, 4, 3, 5, 4, 7,
# find the final sequence of the array after performing given operations | python3 implementation of the approach ; 
def solve(arr, n):
    b = [0 for i in range(n)]
    p = 0
    i = n - 1
    while i >= 0:
        b[p] = arr[i]
        i -= 1
        if (i >= 0):
            b[n - 1 - p] = arr[i]
        p += 1
        i -= 1
    return b


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

# Expected output:
[4, 2, 1, 3]
# check if the product of every contiguous subsequence is different or not in a number | function that returns true if the product of every digit of a contiguous subsequence is distinct ; to store the given number as a string ; append all the digits starting from the end ; reverse the string to get the original number ; store size of the string ; set to store product of each contiguous subsequence ; find product of every contiguous subsequence ; if current product already exists in the set ; 
def productsDistinct(N):
    s = ""
    while (N):
        s += chr(N % 10 + ord('0'))
        N //= 10
    s = s[::-1]
    sz = len(s)
    se = []
    for i in range(sz):
        product = 1
        for j in range(i, sz, 1):
            product *= ord(s[j]) - ord('0')
            for p in range(len(se)):
                if se[p] == product:
                    return False
                else:
                    se .append(product)
    return True


if __name__ == '__main__':
    N = 2345
    if (productsDistinct(N)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# number of ways to arrange 2 * n persons on the two sides of a table with x and y persons on opposite sides | function to find factorial of a number ; function to find ncr ; function to find the number of ways to arrange 2 * n persons ; 
def factorial(n):
    if (n <= 1):
        return 1
    return n * factorial(n - 1)


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


def NumberOfWays(n, x, y):
    return (nCr(2 * n - x - y, n - x) * factorial(n) * factorial(n))


n, x, y = 5, 4, 2
print(int(NumberOfWays(n, x, y)))

# Expected output:
57600
# find smallest positive number y such that bitwise and of x and y is zero | method to find smallest number y for a given value of x such that x and y is zero ; convert the number into its binary form ; case 1 : if all bits are ones , then return the next number ; case 2 : find the first 0 - bit index and return the y ; 
def findSmallestNonZeroY(A_num):
    A_binary = bin(A_num)
    B = 1
    length = len(A_binary)
    no_ones = (A_binary).count('1')
    if length == no_ones:
        return A_num + 1
    for i in range(length):
        ch = A_binary[length - i - 1]
        if (ch == '0'):
            B = pow(2.0, i)
            break
    return B


if __name__ == "__main__":
    X = findSmallestNonZeroY(10)
    print(X)

# Expected output:
1.0
# count number of set bits in a range using bitset | python3 implementation of above approach ; function for converting binary string into integer value ; function to count number of 1 's using bitset  ; converting the string into bitset ; bitwise operations left shift ; right shifts ; return count of one in [ l , r ] ; 

N = 32


def binStrToInt(binary_str):
    length = len(binary_str)
    num = 0
    for i in range(length):
        num = num + int(binary_str[i])
        num = num * 2
    return num / 2


def GetOne(s, L, R):
    length = len(s)
    bit = s .zfill(32 - len(s))
    bit = int(binStrToInt(bit))
    bit <<= (N - length + L - 1)
    bit >>= (N - length + L - 1)
    bit >>= (length - R)
    return bin(bit).count('1')


if __name__ == "__main__":
    s = "01010001011"
    L = 2
    R = 4
    print(GetOne(s, L, R))

# Expected output:
2
# maximum element in an array such that its previous and next element product is maximum | function to return the largest element such that its previous and next element product is maximum ; calculate the product of the previous and the next element for the current element ; update the maximum product ; if current product is equal to the current maximum product then choose the maximum element ; 
def maxElement(a, n):
    if n < 3:
        return -1
    maxElement = a[0]
    maxProd = a[n - 1] * a[1]
    for i in range(1, n):
        currprod = a[i - 1] * a[(i + 1) % n]
        if currprod > maxProd:
            maxProd = currprod
            maxElement = a[i]
        elif currprod == maxProd:
            maxElement = max(maxElement, a[i])
    return maxElement


a = [5, 6, 4, 3, 2]
n = len(a)
print(maxElement(a, n))

# Expected output:
6
# total ways of choosing x men and y women from a total of m men and w women | function to return the value of ncr effectively ; 
def ncr(n, r):
    ans = 1
    for i in range(1, r + 1):
        ans *= (n - r + i)
        ans //= i
    return ans


def totalWays(X, Y, M, W):
    return (ncr(M, X) * ncr(W, Y))


X = 4
Y = 3
M = 6
W = 5
print(totalWays(X, Y, M, W))

# Expected output:
150
# generate elements of the array following given conditions | function to generate the required array ; initialize cnt variable for assigning unique value to prime and its multiples ; when we get a prime for the first time then assign a unique smallest value to it and all of its multiples ; print the generated array ; 
def specialSieve(n):
    cnt = 0
    prime = [0] * (n + 1)
    for i in range(2, n + 1):
        if (not prime[i]):
            cnt += 1
            for j in range(i, n + 1, i):
                prime[j] = cnt
    for i in range(2, n + 1):
        print(prime[i], end=" ")


if __name__ == "__main__":
    n = 6
    specialSieve(n)

# Expected output:
1 2 1 3 2
# find the node whose sum with x has maximum set bits | python implementation of the approach ; function to perform dfs to find the maximum set bits value ; if current set bits value is greater than the current maximum ; if count is equal to the maximum then choose the node with minimum value ; 

from sys import maxsize
maximum, x, ans = -maxsize, None, maxsize
graph = [[]for i in range(100)]
weight = [0] * 100


def dfs(node, parent):
    global x, ans, graph, weight, maximum
    a = bin(weight[node] + x).count('1')
    if maximum < a:
        maximum = a
        ans = node
    elif maximum == a:
        ans = min(ans, node)
    for to in graph[node]:
        if to == parent:
            continue
        dfs(to, node)


if __name__ == "__main__":
    x = 15
    weight[1] = 5
    weight[2] = 10
    weight[3] = 11
    weight[4] = 8
    weight[5] = 6
    graph[1].append(2)
    graph[2].append(3)
    graph[2].append(4)
    graph[1].append(5)
    dfs(1, 1)
    print(ans)

# Expected output:
4
# count pairs from two arrays with even sum | function to return count of required pairs ; count of odd and even numbers from both the arrays ; find the count of odd and even elements in a [ ] ; find the count of odd and even elements in b [ ] ; count the number of pairs ; return the number of pairs ; 
def count_pairs(a, b, n, m):
    odd1 = 0
    even1 = 0
    odd2 = 0
    even2 = 0
    for i in range(n):
        if (a[i] % 2 == 1):
            odd1 += 1
        else:
            even1 += 1
    for i in range(m):
        if (b[i] % 2 == 1):
            odd2 += 1
        else:
            even2 += 1
    pairs = min(odd1, odd2) + min(even1, even2)
    return pairs


if __name__ == '__main__':
    a = [9, 14, 6, 2, 11]
    b = [8, 4, 7, 20]
    n = len(a)
    m = len(b)
    print(count_pairs(a, b, n, m))

# Expected output:
4
# find an index such that difference between product of elements before and after it is minimum |  ; function to find index ; array to store log values of elements ; prefix array to maintain sum of log values till index i ; answer index ; find minimum absolute value ; 

import math


def solve(Array, N):
    Arraynew = [0] * N
    for i in range(N):
        Arraynew[i] = math .log(Array[i])
    prefixsum = [0] * N
    prefixsum[0] = Arraynew[0]
    for i in range(1, N):
        prefixsum[i] = prefixsum[i - 1] + Arraynew[i]
    answer = 0
    minabs = abs(prefixsum[N - 1] - 2 * prefixsum[0])
    for i in range(1, N - 1):
        ans1 = abs(prefixsum[N - 1] - 2 * prefixsum[i])
        if (ans1 < minabs):
            minabs = ans1
            answer = i
    print("Index is: ", answer)


if __name__ == "__main__":
    Array = [1, 4, 12, 2, 6]
    N = 5
    solve(Array, N)

# Expected output:
Index is:  2
# find the value of n when f ( n ) = f ( a ) + f ( b ) where a + b is the minimum possible and a * b = n | function to return the value of f ( n ) ; base cases ; count the number of times a number if divisible by 2 ; return the summation ; 
def getValueOfF(n):
    if (n == 1):
        return 0
    if (n == 2):
        return 1
    cnt = 0
    while (n % 2 == 0):
        cnt += 1
        n /= 2
    return 2 * cnt


n = 20
print(getValueOfF(n))

# Expected output:
4
# find the sum of the number of divisors | python3 code for above given approach ; to store the number of divisors ; function to find the number of divisors of all numbers in the range 1 to n ; for every number 1 to n ; increase divisors count for every number ; function to find the sum of divisors ; to store sum ; count the divisors ; 

N = 100005
mod = 1000000007
cnt = [0] * N


def Divisors():
    for i in range(1, N):
        for j in range(1, N // i):
            cnt[i * j] += 1


def Sumofdivisors(A, B, C):
    sum = 0
    Divisors()
    for i in range(1, A + 1):
        for j in range(1, B + 1):
            for k in range(1, C + 1):
                x = i * j * k
                sum += cnt[x]
                if (sum >= mod):
                    sum -= mod
    return sum


if __name__ == "__main__":
    A = 5
    B = 6
    C = 7
    print(Sumofdivisors(A, B, C))

# Expected output:
1520
# find ( 1 ^ n + 2 ^ n + 3 ^ n + 4 ^ n ) mod 5 | set 2 | function to return a mod b ; length of the string ; to store required answer ; function to return ( 1 ^ n + 2 ^ n + 3 ^ n + 4 ^ n ) % 5 ; calculate and return ans ; 
def A_mod_B(N, a):
    Len = len(N)
    ans = 0
    for i in range(Len):
        ans = (ans * 10 + int(N[i])) % a
    return ans % a


def findMod(N):
    mod = A_mod_B(N, 4)
    ans = (1 + pow(2, mod) + pow(3, mod) + pow(4, mod))
    return ans % 5


N = "4"
print(findMod(N))

# Expected output:
4
# elements greater than the previous and next element in an array | function to print elements greater than the previous and next element in an array ; traverse array from index 1 to n - 2 and check for the given condition ; 
def printElements(arr, n):
    for i in range(1, n - 1, 1):
        if (arr[i] > arr[i - 1] and arr[i] > arr[i + 1]):
            print(arr[i], end=" ")


if __name__ == '__main__':
    arr = [2, 3, 1, 5, 4, 9, 8, 7, 5]
    n = len(arr)
    printElements(arr, n)

# Expected output:
3 5 9
# sum of the series kn + ( k ( n |  ; recursive python3 program to compute modular power ; base cases ; if b is even ; if b is odd ; function to return sum ; 
def exponent(A, B):
    if (A == 0):
        return 0
    if (B == 0):
        return 1
    if (B % 2 == 0):
        y = exponent(A, B / 2)
        y = (y * y)
    else:
        y = A
        y = (y * exponent(A, B - 1))
    return y


def sum(k, n):
    sum = exponent(k, n + 1) - exponent(k - 1, n + 1)
    return sum


n = 3
K = 3
print(sum(K, n))

# Expected output:
65
# minimize the cost to split a number | python3 implementation of the approach ; check if a number is prime or not ; run a loop upto square root of x ; function to return the minimized cost ; if n is prime ; if n is odd and can be split into ( prime + 2 ) then cost will be 1 + 1 = 2 ; every non - prime even number can be expressed as the sum of two primes ; n is odd so n can be split into ( 3 + even ) further even part can be split into ( prime + prime ) ( 3 + prime + prime ) will cost 3 ; 

from math import sqrt


def isPrime(x):
    for i in range(2, int(sqrt(x)) + 1):
        if (x % i == 0):
            return 0
    return 1


def minimumCost(n):
    if (isPrime(n)):
        return 1
    if (n % 2 == 1 and isPrime(n - 2)):
        return 2
    if (n % 2 == 0):
        return 2
    return 3


if __name__ == "__main__":
    n = 6
    print(minimumCost(n))

# Expected output:
2
# find amount of water wasted after filling the tank | function to calculate amount of wasted water ; filled amount of water in one minute ; total time taken to fill the tank because of leakage ; wasted amount of water ; 
def wastedWater(V, M, N):
    amt_per_min = M - N
    time_to_fill = V / amt_per_min
    wasted_amt = N * time_to_fill
    return wasted_amt


V = 700
M = 10
N = 3
print(wastedWater(V, M, N))
V = 1000
M = 100
N = 50
print(wastedWater(V, M, N))

# Expected output:
300.0
1000.0
# smallest and largest n | python3 implementation of the approach ; function to print the largest and the smallest n - digit perfect cube ; smallest n - digit perfect cube ; largest n - digit perfect cube ; 

from math import ceil


def nDigitPerfectCubes(n):
    print(pow(ceil((pow(10, (n - 1)))**(1 / 3)), 3), end=" ")
    print(pow(ceil((pow(10, (n)))**(1 / 3)) - 1, 3))


if __name__ == "__main__":
    n = 3
    nDigitPerfectCubes(n)

# Expected output:
125 729
# count numbers which are divisible by all the numbers from 2 to 10 | function to return the count of numbers from 1 to n which are divisible by all the numbers from 2 to 10 ; 
def countNumbers(n):
    return n // 2520


n = 3000
print(countNumbers(n))

# Expected output:
1
# number of ways in which an item returns back to its initial position in n swaps in array of size k | python3 program to find the number of ways in which an item returns back to its initial position in n swaps in an array of size k ; function to calculate ( x ^ y ) % p in o ( log y ) ; 

mod = 1000000007


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


def solve(n, k):
    if (n == 1):
        return 0
    return (power((k - 1), n - 1) % mod - solve(n - 1, k) + mod) % mod


mod = 1000000007
n, k = 4, 5
print(solve(n, k))

# Expected output:
52
# numbers less than n that are perfect cubes and the sum of their digits reduced to a single digit is 1 | python3 implementation of the approach ; function that returns true if the eventual digit sum of number nm is 1 ; if reminder will 1 then eventual sum is 1 ; function to print the required numbers less than n ; if it is the required perfect cube ; 

import math


def isDigitSumOne(nm):
    if (nm % 9 == 1):
        return True
    else:
        return False


def printValidNums(n):
    cbrt_n = math .ceil(n ** (1. / 3.))
    for i in range(1, cbrt_n + 1):
        cube = i * i * i
        if (cube >= 1 and cube <= n and isDigitSumOne(cube)):
            print(cube, end=" ")


n = 1000
printValidNums(n)

# Expected output:
1 64 343 1000
# count the number of rhombi possible inside a rectangle of given size | function to return the count of rhombi possible ; all possible diagonal lengths ; update rhombi possible with the current diagonal lengths ; return the total count of rhombi possible ; 
def countRhombi(h, w):
    ct = 0
    for i in range(2, h + 1, 2):
        for j in range(2, w + 1, 2):
            ct += (h - i + 1) * (w - j + 1)
    return ct


if __name__ == "__main__":
    h = 2
    w = 2
    print(countRhombi(h, w))

# Expected output:
1
# program to calculate the area between two concentric circles | function to find area between the two given concentric circles ; declare value of pi ; calculate area of outer circle ; calculate area of inner circle ; difference in areas ; 
def calculateArea(x, y):
    pi = 3.1415926536
    arx = pi * x * x
    ary = pi * y * y
    return arx - ary


x = 2
y = 1
print(calculateArea(x, y))

# Expected output:
9.4247779608
# find the winner by adding pairwise difference of elements in the array until possible | python3 implementation of the approach ; function to return the winner of the game ; to store the gcd of the original array ; to store the maximum element from the original array ; if number of moves are odd ; 

from math import gcd


def getWinner(arr, n):
    __gcd = arr[0]
    maxEle = arr[0]
    for i in range(1, n):
        __gcd = gcd(__gcd, arr[i])
        maxEle = max(maxEle, arr[i])
    totalMoves = (maxEle / __gcd) - n
    if (totalMoves % 2 == 1):
        return 'A'
    return 'B'


if __name__ == "__main__":
    arr = [5, 6, 7]
    n = len(arr)
    print(getWinner(arr, n))

# Expected output:
B
# count of pairs of ( i , j ) such that ( ( n % i ) % j ) % n is maximized | function to return the count of required pairs ; number which will give the max value for ( ( n % i ) % j ) % n ; to store the maximum possible value of ( ( n % i ) % j ) % n ; to store the count of possible pairs ; check all possible pairs ; calculating the value of ( ( n % i ) % j ) % n ; if value is equal to maximum ; return the number of possible pairs ; 
def countPairs(n):
    num = ((n // 2) + 1)
    Max = n % num
    count = 0
    for i in range(1, n + 1):
        for j in range(1, n + 1):
            val = ((n % i) % j) % n
            if (val == Max):
                count += 1
    return count


n = 5
print(countPairs(n))

# Expected output:
3
# program to check if a number is divisible by any of its digits | function to check if given number is divisible by any of its digits ; check if any of digit divides n ; check if k divides n ; 
def isDivisible(n):
    temp = n
    while (n):
        k = n % 10
        if (temp % k == 0):
            return "YES"
        n /= 10
    return "NO"


n = 9876543
print(isDivisible(n))

# Expected output:
YES
# program to find sum of harmonic series | function to return sum of harmonic series ; 
def sum(n):
    i = 1
    s = 0.0
    for i in range(1, n + 1):
        s = s + 1 / i
    return s


n = 5
print("Sum is", round(sum(n), 6))

# Expected output:
Sum is 2.283333
# find nth term of the series 5 2 13 41 | python3 program to find nth term of the series 5 2 13 41 ; function to calculate nth term of the series ; if n is even number ; if n is odd number ; return nth term ; 

from math import pow


def nthTermOfTheSeries(n):
    if (n % 2 == 0):
        nthTerm = pow(n - 1, 2) + n
    else:
        nthTerm = pow(n + 1, 2) + n
    return nthTerm


if __name__ == '__main__':
    n = 8
    print(int(nthTermOfTheSeries(n)))
    n = 12
    print(int(nthTermOfTheSeries(n)))
    n = 102
    print(int(nthTermOfTheSeries(n)))
    n = 999
    print(int(nthTermOfTheSeries(n)))
    n = 9999
    print(int(nthTermOfTheSeries(n)))

# Expected output:
57
133
10303
1000999
100009999
# check whether a number is non | python3 program to check if a given number is non - hypotenuse number or not . ; function to find prime factor and check if it is of the form 4 k + 1 or not ; 2 is a prime number but not of the form 4 k + 1 so , keep dividing n by 2 until n is divisible by 2 ; n must be odd at this point . so we can skip one element ( note i = i + 2 ) ; if i divides n check if i is of the form 4 k + 1 or not ; while i divides n divide n by i and update n ; this condition is to handle the case when n is a prime number greater than 2 ; test function ; 

from math import sqrt


def isNonHypotenuse(n):
    while (n % 2 == 0):
        n = n // 2
    for i in range(3, int(sqrt(n)) + 1, 2):
        if (n % i == 0):
            if ((i - 1) % 4 == 0):
                return False
            while (n % i == 0):
                n = n // i
    if (n > 2 and (n - 1) % 4 == 0):
        return False
    else:
        return True


def test(n):
    print("Testing for", n, ":", end=" ")
    if (isNonHypotenuse(n)):
        print("YES")
    else:
        print("NO")


if __name__ == "__main__":
    n = 11
    test(n)
    n = 10
    test(n)

# Expected output:
Testing for 11 : YES
Testing for 10 : NO
# find the n | python3 implementation of the above approach ; function to return the nth string in the required sequence ; length of the resultant string ; relative index ; initial string of length len consists of all a 's since the list is sorted ; convert relative index to binary form and set 0 = a and 1 = b ; reverse and return the string ; 

from math import log2


def obtain_str(n):
    length = int(log2(n + 1))
    rel_ind = n + 1 - pow(2, length)
    i = 0
    string = ""
    for i in range(length):
        string += 'a'
    i = 0
    string_list = list(string)
    while (rel_ind > 0):
        if (rel_ind % 2 == 1):
            string_list[i] = 'b'
        rel_ind //= 2
        i += 1
    string_list .reverse()
    string = "".join(string_list)
    return string


if __name__ == "__main__":
    n = 11
    print(obtain_str(n))

# Expected output:
baa
# program to find the nth term of the series 0 , 3 / 1 , 8 / 3 , 15 / 5. . ... ... | function to return the nth term of the given series ; nth term ; 
def Nthterm(n):
    numerator = n ** 2 - 1
    denomenator = 2 * n - 3
    print(numerator, "/", denomenator)


n = 3
Nthterm(n)

# Expected output:
8 / 3