giulio98/xlcost-single-prompt huggingface dataset:
# highest power of a number that divides other number | python program to implement the above approach ; function to get the prime factors and its count of times it divides ; count the number of 2 s that divide n ; n must be odd at this point . so we can skip one element ( note i = i + 2 ) ; while i divides n , count i and divide n ; this condition is to handle the case when n is a prime number greater than 2 ; function to return the highest power ; initialize two arrays ; get the prime factors of n and m ; iterate and find the maximum power ; if i not a prime factor of n and m ; if i is a prime factor of n and m if count of i dividing m is more than i dividing n , then power will be 0 ; if i is a prime factor of m ; get the maximum power ; 

import math


def primeFactors(n, freq):
    cnt = 0
    while n % 2 == 0:
        cnt = cnt + 1
        n = int(n // 2)
    freq[2] = cnt
    i = 3
    while i <= math .sqrt(n):
        cnt = 0
        while (n % i == 0):
            cnt = cnt + 1
            n = int(n // i)
        freq[int(i)] = cnt
        i = i + 2
    if (n > 2):
        freq[int(n)] = 1


def getMaximumPower(n, m):
    freq1 = [0] * (n + 1)
    freq2 = [0] * (m + 1)
    primeFactors(n, freq1)
    primeFactors(m, freq2)
    maxi = 0
    i = 2
    while i <= m:
        if (freq1[i] == 0 and freq2[i] == 0):
            i = i + 1
            continue
        if (freq2[i] > freq1[i]):
            return 0
        if (freq2[i]):
            maxi = max(maxi, int(freq1[i] // freq2[i]))
        i = i + 1
    return maxi


n = 48
m = 4
print(getMaximumPower(n, m))

# Expected output:
2
# find the number of divisors of all numbers in the range [ 1 , n ] | function to find the number of divisors of all numbers in the range [ 1 , n ] ; list to store the count of divisors ; for every number from 1 to n ; increase divisors count for every number divisible by i ; print the divisors ; 
def findDivisors(n):
    div = [0 for i in range(n + 1)]
    for i in range(1, n + 1):
        for j in range(1, n + 1):
            if j * i <= n:
                div[i * j] += 1
    for i in range(1, n + 1):
        print(div[i], end=" ")


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

# Expected output:
1 2 2 3 2 4 2 4 3 4
# predict the winner of the game on the basis of absolute difference of sum by selecting numbers | function to decide the winner ; iterate for all numbers in the array ; if mod gives 0 ; if mod gives 1 ; if mod gives 2 ; if mod gives 3 ; check the winning condition for x ; 
def decideWinner(a, n):
    count0 = 0
    count1 = 0
    count2 = 0
    count3 = 0
    for i in range(n):
        if (a[i] % 4 == 0):
            count0 += 1
        elif (a[i] % 4 == 1):
            count1 += 1
        elif (a[i] % 4 == 2):
            count2 += 1
        elif (a[i] % 4 == 3):
            count3 += 1
    if (count0 %
        2 == 0 and count1 %
        2 == 0 and count2 %
            2 == 0 and count3 == 0):
        return 1
    else:
        return 2


a = [4, 8, 5, 9]
n = len(a)
if (decideWinner(a, n) == 1):
    print("X wins")
else:
    print("Y wins")

# Expected output:
X wins
# count all prefixes of the given binary array which are divisible by x | function to return the count of total binary prefix which are divisible by x ; initialize with zero ; instead of converting all prefixes to decimal , take reminder with x ; if number is divisible by x then reminder = 0 ; 
def CntDivbyX(arr, n, x):
    number = 0
    count = 0
    for i in range(0, n):
        number = (number * 2 + arr[i]) % x
        if number == 0:
            count += 1
    return count


arr = [1, 0, 1, 0, 1, 1, 0]
n = 7
x = 2
print(CntDivbyX(arr, n, x))

# Expected output:
3
# length of the smallest number which is divisible by k and formed by using 1 's only | function to return length of the resultant number ; if k is a multiple of 2 or 5 ; instead of generating all possible numbers 1 , 11 , 111 , 111 , ... , k 1 's  take remainder with k ; if number is divisible by k then remainder will be 0 ; 
def numLen(K):
    if (K % 2 == 0 or K % 5 == 0):
        return -1
    number = 0
    len = 1
    for len in range(1, K + 1):
        number = (number * 10 + 1) % K
        if number == 0:
            return len
    return -1


K = 7
print(numLen(K))

# Expected output:
6
# sum of multiplication of triplet of divisors of a number | global array declaration global max_element ; function to find the sum of multiplication of every triplet in the divisors of a number ; global max_element sum1 [ x ] represents the sum of all the divisors of x ; adding i to sum1 [ j ] because i is a divisor of j ; sum2 [ x ] represents the sum of all the divisors of x ; here i is divisor of j and sum1 [ j ] - i represents sum of all divisors of j which do not include i so we add i * ( sum1 [ j ] - i ) to sum2 [ j ] ; in the above implementation we have considered every pair two times so we have to divide every sum2 array element by 2 ; here i is the divisor of j and we are trying to add the sum of multiplication of all triplets of divisors of j such that one of the divisors is i ; in the above implementation we have considered every triplet three times so we have to divide every sum3 array element by 3 ; print the results ; 

max_Element = 100005
sum1 = [0 for i in range(max_Element)]
sum2 = [0 for i in range(max_Element)]
sum3 = [0 for i in range(max_Element)]


def precomputation(arr, n):
    for i in range(1, max_Element, 1):
        for j in range(i, max_Element, i):
            sum1[j] += i
    for i in range(1, max_Element, 1):
        for j in range(i, max_Element, i):
            sum2[j] += (sum1[j] - i) * i
    for i in range(1, max_Element, 1):
        sum2[i] = int(sum2[i] / 2)
    for i in range(1, max_Element, 1):
        for j in range(i, max_Element, i):
            sum3[j] += i * (sum2[j] - i * (sum1[j] - i))
    for i in range(1, max_Element, 1):
        sum3[i] = int(sum3[i] / 3)
    for i in range(n):
        print(sum3[arr[i]], end=" ")


if __name__ == '__main__':
    arr = [9, 5, 6]
    n = len(arr)
    precomputation(arr, n)

# Expected output:
27 0 72
# sum of fibonacci numbers in a range | python3 implementation of the approach ; function to return the nth fibonacci number ; function to return the required sum ; using our deduced result ; 

import math


def fib(n):
    phi = (1 + math .sqrt(5)) / 2
    return int(round(pow(phi, n) / math .sqrt(5)))


def calculateSum(l, r):
    sum = fib(r + 2) - fib(l + 1)
    return sum


l = 4
r = 8
print(calculateSum(l, r))

# Expected output:
50
# print the balanced bracket expression using given brackets | function to print balanced bracket expression if it is possible ; if the condition is met ; print brackets of type - 1 ; print brackets of type - 3 ; print brackets of type - 4 ; print brackets of type - 2 ; if the condition is not met ; 
def printBalancedExpression(a, b, c, d):
    if ((a == d and a) or (a == 0 and c == 0 and d == 0)):
        for i in range(1, a + 1):
            print("((", end="")
        for i in range(1, c + 1):
            print(")(", end="")
        for i in range(1, d + 1):
            print("))", end="")
        for i in range(1, b + 1):
            print("()", end="")
    else:
        print("-1")


if __name__ == "__main__":
    a, b, c, d = 3, 1, 4, 3
    printBalancedExpression(a, b, c, d)

# Expected output:
(((((()()()()())))))()
# count numbers having n 0 ' s ▁ and ▁ and ▁ m ▁ 1' s with no leading zeros | function to return the factorial of a number ; function to return the count of distinct ( n + m ) digit numbers having n 0 ' s ▁ and ▁ and ▁ m ▁ 1' s with no leading zeros ; 
def factorial(f):
    fact = 1
    for i in range(2, f + 1):
        fact *= i
    return fact


def findPermuatation(N, M):
    permutation = (factorial(N + M - 1) // (factorial(N) * factorial(M - 1)))
    return permutation


N = 3
M = 3
print(findPermuatation(N, M))

# Expected output:
10
# maximum value of | arr [ 0 ] | function to return the maximum required value ; 
def maxValue(n):
    if (n == 1):
        return 0
    return ((n * n // 2) - 1)


n = 4
print(maxValue(n))

# Expected output:
7
# count of a , b & c after n seconds for given reproduction rate | function to print the count of a , b and c after n seconds ; number of multiples of 60 below n ; multiple of 60 nearest to n ; change all a to b ; change all b to c ; change each c to two a ; print the updated values of a , b and c ; 

import math


def findCount(n):
    a, b, c = 1, 0, 0
    x = (int)(n / 60)
    a = int(math .pow(32, x))
    x = 60 * x
    for i in range(x + 1, n + 1):
        if (i % 2 == 0):
            b += a
            a = 0
        if (i % 5 == 0):
            c += b
            b = 0
        if (i % 12 == 0):
            a += (2 * c)
            c = 0
    print("a =", a, end=", ")
    print("b =", b, end=", ")
    print("c =", c)


if __name__ == '__main__':
    n = 72
    findCount(n)

# Expected output:
a = 64, b = 0, c = 0
# find gcd of factorial of elements of given array | implementation of factorial function ; function to find gcd of factorial of elements from array ; find the minimum element of array ; return the factorial of minimum element ; 
def factorial(n):
    if n == 1 or n == 0:
        return 1
    else:
        return factorial(n - 1) * n


def gcdOfFactorial(arr, n):
    minm = arr[0]
    for i in range(1, n):
        if minm > arr[i]:
            minm = arr[i]
        else:
            arr[i] = minm
    return factorial(minm)


arr = [9, 12, 122, 34, 15]
n = len(arr)
print(gcdOfFactorial(arr, n))

# Expected output:
362880
# sum of the series 1 ^ 1 + 2 ^ 2 + 3 ^ 3 + ... . . + n ^ n using recursion | recursive function to return the sum of the given series ; 1 ^ 1 = 1 ; recursive call ; 
def sum(n):
    if n == 1:
        return 1
    else:
        return pow(n, n) + sum(n - 1)


n = 2
print(sum(n))

# Expected output:
5
# count permutations that are first decreasing then increasing . | python3 implementation of the above approach ; function to compute a ^ n % mod ; function to count permutations that are first decreasing and then increasing ; for n = 1 return 0 ; calculate and return result ; 

mod = 1000000007


def power(a, n):
    if (n == 0):
        return 1
    p = power(a, int(n / 2)) % mod
    p = (p * p) % mod
    if (n & 1):
        p = (p * a) % mod
    return p


def countPermutations(n):
    if (n == 1):
        return 0
    return (power(2, n - 1) - 2) % mod


if __name__ == '__main__':
    n = 5
    print(countPermutations(n))

# Expected output:
14
# find the count of numbers that can be formed using digits 3 , 4 only and having length at max n . | function to find the count of numbers that can be formed using digits 3 , 4 only and having length at max n . ; 
def numbers(n):
    return pow(2, n + 1) - 2


n = 2
print(numbers(n))

# Expected output:
6
# ways to place 4 items in n ^ 2 positions such that no row / column contains more than one | function to return the number of ways to place 4 items in n ^ 2 positions ; 
def NumbersofWays(n):
    x = (n * (n - 1) * (n - 2) * (n - 3)) // (4 * 3 * 2 * 1)
    y = n * (n - 1) * (n - 2) * (n - 3)
    return x * y


n = 4
print(NumbersofWays(n))

# Expected output:
24
# find nth term of the series 1 , 6 , 18 , 40 , 75 , ... . | function to generate a fixed number ; ( n ^ 2 * ( n + 1 ) ) / 2 ; 
def nthTerm(N):
    nth = 0
    nth = (N * N * (N + 1)) // 2
    return nth


N = 5
print(nthTerm(N))

# Expected output:
75
# print n numbers such that their sum is a perfect square | function to print n numbers such that their sum is a perfect square ; print ith odd number ; 
def findNumber(n):
    i = 1
    while i <= n:
        print((2 * i) - 1, end=" ")
        i += 1


n = 3
findNumber(n)

# Expected output:
1 3 5
# missing even and odd elements from the given arrays | python3 implementation of the approach ; function to find the missing numbers ; to store the minimum and the maximum odd and even elements from the arrays ; to store the sum of the array elements ; get the minimum and the maximum even elements from the array ; get the minimum and the maximum odd elements from the array ; to store the total terms in the series and the required sum of the array ; total terms from 2 to mineven ; sum of all even numbers from 2 to mineven ; total terms from 2 to maxeven ; sum of all even numbers from 2 to maxeven ; required sum for the even array ; missing even number ; total terms from 1 to minodd ; sum of all odd numbers from 1 to minodd ; total terms from 1 to maxodd ; sum of all odd numbers from 1 to maxodd ; required sum for the odd array ; missing odd number ; 

import sys


def findMissingNums(even, sizeEven, odd, sizeOdd):
    minEven = sys .maxsize
    maxEven = -(sys .maxsize - 1)
    minOdd = sys .maxsize
    maxOdd = -(sys .maxsize - 1)
    sumEvenArr = 0
    sumOddArr = 0
    for i in range(sizeEven):
        minEven = min(minEven, even[i])
        maxEven = max(maxEven, even[i])
        sumEvenArr += even[i]
    for i in range(sizeOdd):
        minOdd = min(minOdd, odd[i])
        maxOdd = max(maxOdd, odd[i])
        sumOddArr += odd[i]
    totalTerms = 0
    reqSum = 0
    totalTerms = minEven // 2
    evenSumMin = (totalTerms * (totalTerms + 1))
    totalTerms = maxEven // 2
    evenSumMax = (totalTerms * (totalTerms + 1))
    reqSum = (evenSumMax - evenSumMin + minEven)
    print("Even =", reqSum - sumEvenArr)
    totalTerms = (minOdd // 2) + 1
    oddSumMin = totalTerms * totalTerms
    totalTerms = (maxOdd // 2) + 1
    oddSumMax = totalTerms * totalTerms
    reqSum = (oddSumMax - oddSumMin + minOdd)
    print("Odd =", reqSum - sumOddArr)


if __name__ == "__main__":
    even = [6, 4, 8, 14, 10]
    sizeEven = len(even)
    odd = [7, 5, 3, 11, 13]
    sizeOdd = len(odd)
    findMissingNums(even, sizeEven, odd, sizeOdd)

# Expected output:
Even = 12
Odd = 9
# minimum matches the team needs to win to qualify | function to return the minimum number of matches to win to qualify for next round ; do a binary search to find ; find mid element ; check for condition to qualify for next round ; 
def findMinimum(x, y):
    low = 0
    high = y
    while (low <= high):
        mid = (low + high) >> 1
        if ((mid * 2 + (y - mid)) >= x):
            high = mid - 1
        else:
            low = mid + 1
    return low


if __name__ == '__main__':
    x = 6
    y = 5
    print(findMinimum(x, y))

# Expected output:
1
# check if product of digits of a number at even and odd places is equal | python3 implementation of the approach ; to store the respective product ; converting integer to string ; traversing the string ; 
def getResult(n):
    proOdd = 1
    proEven = 1
    num = str(n)
    for i in range(len(num)):
        if (i % 2 == 0):
            proOdd = proOdd * int(num[i])
        else:
            proEven = proEven * int(num[i])
    if (proOdd == proEven):
        print("Yes")
    else:
        print("No")


if __name__ == "__main__":
    n = 4324
    getResult(n)

# Expected output:
No
# count of all even numbers in the range [ l , r ] whose sum of digits is divisible by 3 | function to return the sum of digits of x ; function to return the count of required numbers ; if i is divisible by 2 and sum of digits of i is divisible by 3 ; return the required count ; 
def sumOfDigits(x):
    sum = 0
    while x != 0:
        sum += x % 10
        x = x // 10
    return sum


def countNumbers(l, r):
    count = 0
    for i in range(l, r + 1):
        if i % 2 == 0 and sumOfDigits(i) % 3 == 0:
            count += 1
    return count


l = 1000
r = 6000
print(countNumbers(l, r))

# Expected output:
834
# sum of minimum element of all subarrays of a sorted array | function to find the sum of minimum of all subarrays ; 
def findMinSum(arr, n):
    sum = 0
    for i in range(0, n):
        sum += arr[i] * (n - i)
    return sum


arr = [3, 5, 7, 8]
n = len(arr)
print(findMinSum(arr, n))

# Expected output:
49
# longest sub | function to return the max length of the sub - array that have the maximum average ( average value of the elements ) ; finding the maximum value ; if consecutive maximum found ; find the max length of consecutive max ; 
def maxLenSubArr(a, n):
    cm, Max = 1, 0
    for i in range(0, n):
        if a[i] > Max:
            Max = a[i]
    i = 0
    while i < n - 1:
        count = 1
        if a[i] == a[i + 1] and a[i] == Max:
            for j in range(i + 1, n):
                if a[j] == Max:
                    count += 1
                    i += 1
                else:
                    break
            if count > cm:
                cm = count
        else:
            i += 1
        i += 1
    return cm


if __name__ == "__main__":
    arr = [6, 1, 6, 6, 0]
    n = len(arr)
    print(maxLenSubArr(arr, n))

# Expected output:
2
# minimum possible sum of array elements after performing the given operation | function to return the minimized sum ; to store the largest element from the array which is divisible by x ; sum of array elements before performing any operation ; if current element is divisible by x and it is maximum so far ; update the minimum element ; if no element can be reduced then there 's  no point in performing the operation as  we will end up increasing the sum when an  element is multiplied by x ; subtract the chosen elements from the sum and then add their updated values ; return the minimized sum ; 
def minSum(arr, n, x):
    Sum = 0
    largestDivisible, minimum = -1, arr[0]
    for i in range(0, n):
        Sum += arr[i]
        if (arr[i] % x == 0 and largestDivisible < arr[i]):
            largestDivisible = arr[i]
        if arr[i] < minimum:
            minimum = arr[i]
    if largestDivisible == -1:
        return Sum
    sumAfterOperation = (Sum - minimum - largestDivisible +
                         (x * minimum) + (largestDivisible // x))
    return min(Sum, sumAfterOperation)


if __name__ == "__main__":
    arr = [5, 5, 5, 5, 6]
    n = len(arr)
    x = 3
    print(minSum(arr, n, x))

# Expected output:
26
# maximum bitwise and pair from given range | function to return the maximum bitwise and possible among all the possible pairs ; if there is only a single value in the range [ l , r ] ; if there are only two values in the range [ l , r ] ; 
def maxAND(L, R):
    if (L == R):
        return L
    elif ((R - L) == 1):
        return (R & L)
    else:
        if (((R - 1) & R) > ((R - 2) & (R - 1))):
            return ((R - 1) & R)
        else:
            return ((R - 2) & (R - 1))


L = 1
R = 632
print(maxAND(L, R))

# Expected output:
630
# smallest special prime which is greater than or equal to a given number | function to check whether the number is a special prime or not ; while number is not equal to zero ; if the number is not prime return false . ; else remove the last digit by dividing the number by 10. ; if the number has become zero then the number is special prime , hence return true ; function to find the smallest special prime which is greater than or equal to a given number ; sieve for finding the primes ; there is always an answer possible ; checking if the number is a special prime or not ; if yes print the number and break the loop . ; else increment the number . ; 
def checkSpecialPrime(sieve, num):
    while (num):
        if (sieve[num] == False):
            return False
        num = int(num / 10)
    return True


def findSpecialPrime(N):
    sieve = [True for i in range(N * 10 + 1)]
    sieve[0] = False
    sieve[1] = False
    for i in range(2, N * 10 + 1):
        if (sieve[i]):
            for j in range(i * i, N * 10 + 1, i):
                sieve[j] = False
    while (True):
        if (checkSpecialPrime(sieve, N)):
            print(N)
            break
        else:
            N += 1


if __name__ == '__main__':
    N = 379
    findSpecialPrime(N)
    N = 100
    findSpecialPrime(N)

# Expected output:
379
233
# minimum number of given moves required to make n divisible by 25 | python3 implementation of the approach ; function to return the minimum number of moves required to make n divisible by 25 ; convert number into string ; to store required answer ; length of the string ; to check all possible pairs ; make a duplicate string ; number of swaps required to place ith digit in last position ; number of swaps required to place jth digit in 2 nd last position ; find first non zero digit ; place first non zero digit in the first position ; convert string to number ; if this number is divisible by 25 then cur is one of the possible answer ; if not possible ; 

import sys


def minMoves(n):
    s = str(n)
    ans = sys .maxsize
    len1 = len(s)
    for i in range(len1):
        for j in range(len1):
            if (i == j):
                continue
            t = s
            cur = 0
            list1 = list(t)
            for k in range(i, len1 - 1):
                e = list1[k]
                list1[k] = list1[k + 1]
                list1[k + 1] = e
                cur += 1
            t = ''.join(list1)
            list1 = list(t)
            for k in range(j - (j > i), len1 - 2):
                e = list1[k]
                list1[k] = list1[k + 1]
                list1[k + 1] = e
                cur += 1
            t = ''.join(list1)
            pos = -1
            for k in range(len1):
                if (t[k] != '0'):
                    pos = k
                    break
            for k in range(pos, 0, -1):
                e = list1[k]
                list1[k] = list1[k + 1]
                list1[k + 1] = e
                cur += 1
            t = ''.join(list1)
            nn = int(t)
            if (nn % 25 == 0):
                ans = min(ans, cur)
    if (ans == sys .maxsize):
        return -1
    return ans


n = 509201
print(minMoves(n))

# Expected output:
4
# maximum positive integer divisible by c and is in the range [ a , b ] | function to return the required number ; if b % c = 0 then b is the required number ; else get the maximum multiple of c smaller than b ; 
def getMaxNum(a, b, c):
    if (b % c == 0):
        return b
    x = ((b // c) * c)
    if (x >= a and x <= b):
        return x
    else:
        return -1


a, b, c = 2, 10, 3
print(getMaxNum(a, b, c))

# Expected output:
9
# count of pairs ( x , y ) in an array such that x < y | function to return the number of pairs ( x , y ) such that x < y ; length of the array ; calculate the number of valid pairs ; return the count of valid pairs ; 
def getPairs(a):
    n = len(a)
    count = (n * (n - 1)) // 2
    return count


a = [2, 4, 3, 1]
print(getPairs(a))

# Expected output:
6
# Count the total number of squares that can be visited by Bishop in one move | Function to return the count of total positions the Bishop can visit in a single move ; Count top left squares ; Count bottom right squares ; Count top right squares ; Count bottom left squares ; Return total count ; Bishop 's Position
def countSquares(row, column):
    topLeft = min(row, column) - 1
    bottomRight = 8 - max(row, column)
    topRight = min(row, 9 - column) - 1
    bottomLeft = 8 - max(row, 9 - column)
    return (topLeft + topRight + bottomRight + bottomLeft)


row = 4
column = 4
print(countSquares(row, column))

# Expected output:
13
# Check whether Bishop can take down Pawn or not | Function that return true if the Bishop can take down the pawn ; If pawn is at angle 45 or 225 degree from bishop 's Position ; If pawn is at angle 135 or 315 degree from bishop 's Position ; Bishop 's Position ; Pawn 's Position
def canTakeDown(bishopX, bishopY, pawnX, pawnY):
    if (pawnX - bishopX == pawnY - bishopY):
        return True
    elif (-pawnX + bishopX == pawnY - bishopY):
        return True
    else:
        return False


bishopX = 5
bishopY = 5
pawnX = 1
pawnY = 1
if (canTakeDown(bishopX, bishopY, pawnX, pawnY)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes