giulio98/xlcost-single-prompt huggingface dataset:
# count pairs in array such that one element is reverse of another | function to reverse the digits of the number ; loop to iterate till the number is greater than 0 ; extract the last digit and keep multiplying it by 10 to get the reverse of the number ; function to find the pairs from the such that one number is reverse of the other ; iterate through all pairs ; increment count if one is the reverse of other ; 
def reverse(num):
    rev_num = 0
    while (num > 0):
        rev_num = rev_num * 10 + num % 10
        num = num // 10
    return rev_num


def countReverse(arr, n):
    res = 0
    for i in range(n):
        for j in range(i + 1, n):
            if (reverse(arr[i]) == arr[j]):
                res += 1
    return res


if __name__ == '__main__':
    a = [16, 61, 12, 21, 25]
    n = len(a)
    print(countReverse(a, n))

# Expected output:
2
# count pairs in array such that one element is reverse of another | python3 program to count the pairs in array such that one element is reverse of another ; function to reverse the digits of the number ; loop to iterate till the number is greater than 0 ; extract the last digit and keep multiplying it by 10 to get the reverse of the number ; function to find the pairs from the array such that one number is reverse of the other ; iterate over every element in the array and increase the frequency of the element in hash map ; iterate over every element in the array ; remove the current element from the hash map by decreasing the frequency to avoid counting when the number is a palindrome or when we visit its reverse ; increment the count by the frequency of reverse of the number ; 

from collections import defaultdict


def reverse(num):
    rev_num = 0
    while (num > 0):
        rev_num = rev_num * 10 + num % 10
        num = num // 10
    return rev_num


def countReverse(arr, n):
    freq = defaultdict(int)
    for i in range(n):
        freq[arr[i]] += 1
    res = 0
    for i in range(n):
        freq[arr[i]] -= 1
        res += freq[reverse(arr[i])]
    return res


if __name__ == "__main__":
    a = [16, 61, 12, 21, 25]
    n = len(a)
    print(countReverse(a, n))

# Expected output:
2
# sum of i * countdigits ( i ) ^ countdigits ( i ) for all i in range [ l , r ] | python 3 program to find the required sum ; function to return the required sum ; iterating for all the number of digits from 1 to 10 ; if the range is valid ; sum of ap ; computing the next minimum and maximum numbers by for the ( i + 1 ) - th digit ; 

MOD = 1000000007


def rangeSum(l, r):
    a = 1
    b = 9
    res = 0
    for i in range(1, 11):
        L = max(l, a)
        R = min(r, b)
        if (L <= R):
            sum = (L + R) * (R - L + 1) // 2
            res += pow(i, i) * (sum % MOD)
            res %= MOD
        a = a * 10
        b = b * 10 + 9
    return res


if __name__ == "__main__":
    l = 98
    r = 102
    print(rangeSum(l, r))

# Expected output:
8969
# trial division algorithm for prime factorization | function to check if a number is a prime number or not ; initializing with the value 2 from where the number is checked ; computing the square root of the number n ; while loop till the square root of n ; if any of the numbers between [ 2 , sqrt ( n ) ] is a factor of n then the number is composite ; if none of the numbers is a factor , then it is a prime number ; 
def TrialDivision(N):
    i = 2
    k = int(N ** 0.5)
    while (i <= k):
        if (N % i == 0):
            return 0
        i += 1
    return 1


if __name__ == "__main__":
    N = 49
    p = TrialDivision(N)
    if (p):
        print("Prime")
    else:
        print("Composite")

# Expected output:
Composite
# find if it is possible to choose subarray that it contains exactly k even integers | function to check if it is possible to choose a subarray that contains exactly k even integers ; variable to store the count of even numbers ; if we have to select 0 even numbers but there is all odd numbers in the array ; if the count of even numbers is greater than or equal to k then we can select a subarray with exactly k even integers ; if the count of even numbers is less than k then we cannot select any subarray with exactly k even integers ; 
def isPossible(A, n, k):
    countOfTwo = 0
    for i in range(n):
        if (A[i] % 2 == 0):
            countOfTwo += 1
    if (k == 0 and countOfTwo == n):
        print("NO")
    elif (countOfTwo >= k):
        print("Yes")
    else:
        print("No")


if __name__ == '__main__':
    arr = [1, 2, 4, 5]
    K = 2
    N = len(arr)
    isPossible(arr, N, K)

# Expected output:
Yes
# count of pairs ( a , b ) in range 1 to n such that last digit of a is equal to the first digit of b | function to count of pairs ( a , b ) in range 1 to n ; count c i , j ; calculate number of pairs ; 
def pairs(n):
    c = [[0 for i in range(10)]for i in range(10)]
    tmp = 1
    for i in range(1, n + 1):
        if (i >= tmp * 10):
            tmp *= 10
        c[i // tmp][i % 10] += 1
    ans = 0
    for i in range(1, 10):
        for j in range(1, 10):
            ans += c[i][j] * c[j][i]
    return ans


if __name__ == '__main__':
    n = 25
    print(pairs(n))

# Expected output:
17
# count ways to divide c in two parts and add to a and b to make a strictly greater than b | function to count the number of ways to divide c into two parts and add to a and b such that a is strictly greater than b ; minimum value added to a to satisfy the given relation ; number of different values of a , i . e . , number of ways to divide c ; 
def countWays(A, B, C):
    minAddA = max(0, (C + B - A + 2) // 2)
    count_ways = max(C - minAddA + 1, 0)
    return count_ways


A = 3
B = 5
C = 5
print(countWays(A, B, C))

# Expected output:
2
# gcd of elements occurring fibonacci number of times in an array | python 3 program to find the gcd of elements which occur fibonacci number of times ; function to create hash table to check fibonacci numbers ; inserting the first two numbers into the hash ; adding the remaining fibonacci numbers using the previously added elements ; function to return the gcd of elements in an array having fibonacci frequency ; creating the hash ; map is used to store the frequencies of the elements ; iterating through the array ; traverse the map using iterators ; calculate the gcd of elements having fibonacci frequencies ; 

from collections import defaultdict
import math


def createHash(hash1, maxElement):
    prev, curr = 0, 1
    hash1 .add(prev)
    hash1 .add(curr)
    while (curr <= maxElement):
        temp = curr + prev
        if temp <= maxElement:
            hash1 .add(temp)
        prev = curr
        curr = temp


def gcdFibonacciFreq(arr, n):
    hash1 = set()
    createHash(hash1, max(arr))
    m = defaultdict(int)
    for i in range(n):
        m[arr[i]] += 1
    gcd = 0
    for it in m .keys():
        if (m[it] in hash1):
            gcd = math .gcd(gcd, it)
    return gcd


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

# Expected output:
3
# program to print the series 1 , 9 , 17 , 33 , 49 , 73 , 97. . . till n terms | function to prthe series ; generate the ith term and print ; 
def printSeries(N):
    ith_term = 0
    for i in range(1, N + 1):
        ith_term = 0
        if (i % 2 == 0):
            ith_term = 2 * i * i + 1
        else:
            ith_term = 2 * i * i - 1
        print(ith_term, end=", ")


if __name__ == '__main__':
    N = 7
    printSeries(N)

# Expected output:
1, 9, 17, 33, 49, 73, 97,
# find nth term of the series where each term differs by 6 and 2 alternately | function to find nth term ; iterate from 1 till nth term ; check if i is even and then add 6 ; else add 2 ; print ans ; 
def findNthTerm(N):
    ans = 0
    for i in range(N):
        if (i % 2 == 0):
            ans = ans + 6
        else:
            ans = ans + 2
    print(ans)


if __name__ == '__main__':
    N = 3
    findNthTerm(N)

# Expected output:
14
# find the number of ordered pairs such that a * p + b * q = n , where p and q are primes | python3 program to find the number of ordered pairs such that a * p + b * q = n where p and q are primes ; sieve of erastothenes to store the prime numbers and their frequency in form a * p + b * q ; performing sieve of eratosthenes to find the prime numbers unto 10001 ; loop to find the number of ordered pairs for every combination of the prime numbers ; 

from math import sqrt
size = 1000
prime = [0 for i in range(size)]
freq = [0 for i in range(size)]


def sieve(a, b):
    prime[1] = 1
    for i in range(2, int(sqrt(size)) + 1, 1):
        if (prime[i] == 0):
            for j in range(i * 2, size, i):
                prime[j] = 1
    for p in range(1, size, 1):
        for q in range(1, size, 1):
            if (prime[p] == 0 and prime[q] == 0 and a * p + b * q < size):
                freq[a * p + b * q] += 1


if __name__ == '__main__':
    queries = 2
    a = 1
    b = 2
    sieve(a, b)
    arr = [15, 25]
    for i in range(queries):
        print(freq[arr[i]], end=" ")

# Expected output:
2 3
# find the sum of the series 1 / 2 | function to find the sum of series ; generate the ith term and add it to the sum if i is even and subtract if i is odd ; print the sum ; 
def printSeriesSum(N):
    sum = 0
    for i in range(1, N + 1):
        if (i & 1):
            sum += i / (i + 1)
        else:
            sum -= i / (i + 1)
    print(sum)


if __name__ == "__main__":
    N = 10
    printSeriesSum(N)

# Expected output:
-0.2634559884559883
# find the sum of the series 1 + 2 + 9 + 64 + 625 + 7776 . . . till n terms | function to find the summ of series ; generate the ith term and add it to the summ ; prthe summ ; 
def printSeriessumm(N):
    summ = 0
    for i in range(1, N + 1):
        summ += pow(i, i - 1)
    print(summ)


N = 5
printSeriessumm(N)

# Expected output:
701
# find the sum of the series 1 + 1 / 3 + 1 / 5 + 1 / 7 + ... till n terms | function to find the sum of the given series ; initialise the sum to 0 ; generate the ith term and add it to the sum ; print the final sum ; 
def printSumSeries(N):
    sum = 0
    for i in range(1, N + 1):
        sum += 1.0 / (2 * i - 1)
    print(sum)


if __name__ == "__main__":
    N = 6
    printSumSeries(N)

# Expected output:
1.878210678210678
# find the sum of the series 1 , 2 , 3 , 6 , 9 , 18 , 27 , 54 , ... till n terms | function to find the sum of series ; flag to find the multiplicating factor . . i . e , by 2 or 3 / 2 ; first term ; if flag is true , multiply by 2 ; if flag is false , multiply by 3 / 2 ; update the previous element to nextelement ; print the sum ; 
def printSeriesSum(N):
    sum = 0
    a = 1
    cnt = 0
    flag = True
    sum += a
    while (cnt < N):
        nextElement = None
        if (flag):
            nextElement = a * 2
            sum += nextElement
            flag = not flag
        else:
            nextElement = a * (3 / 2)
            sum += nextElement
            flag = not flag
        a = nextElement
        cnt += 1
    print(sum)


if __name__ == "__main__":
    N = 8
    printSeriesSum(N)

# Expected output:
201.0
# program to add two integers of given base | function to find the sum of two integers of base b ; padding 0 in front of the number to make both numbers equal ; condition to check if the strings have lengths mis - match ; loop to find the find the sum of two integers of base b ; current place value for the resultant sum ; update carry ; find current digit ; update sum result ; 
def sumBaseB(a, b, base):
    len_a = len(a)
    len_b = len(b)
    s = ""
    sum = ""
    diff = abs(len_a - len_b)
    for i in range(1, diff + 1):
        s += "0"
    if (len_a < len_b):
        a = s + a
    else:
        b = s + b
    carry = 0
    for i in range(max(len_a, len_b) - 1, -1, -1):
        curr = carry + (ord(a[i]) - ord('0')) + (ord(b[i]) - ord('0'))
        carry = curr // base
        curr = curr % base
        sum = chr(curr + ord('0')) + sum
    if (carry > 0):
        sum = chr(carry + ord('0')) + sum
    return sum


a = "123"
b = "234"
base = 6
sum = sumBaseB(a, b, base)
print(sum)

# Expected output:
401
# minimum number of swaps required to make a number divisible by 60 | function that prminimum number of swap operations required ; condition if more than one zero exist ; condition if zero_exist ; computing total sum of all digits ; condition if zero does not exist or the sum is not divisible by 3 ; condition to find a digit that is multiple of 2 other than one zero ; condition if multiple of 2 do not exist ; condition for zero swaps means the number is already is divisible by 60 ; condition for only one swap ; otherwise 2 swaps required ; 
def MinimumSwapOperations(s):
    zero_exist = False
    multiple_of_2 = False
    sum = 0
    index_of_zero = 0
    more_zero = False
    for i in range(len(s)):
        val = ord(s[i]) - ord('0')
        if (zero_exist):
            more_zero = True
        if (val == 0):
            zero_exist = True
            index_of_zero = i
        sum += val
    if (zero_exist == False or sum % 3 != 0):
        print("-1")
        return
    for i in range(len(s)):
        val = ord(s[i]) - ord('0')
        if (val % 2 == 0 and i != index_of_zero):
            multiple_of_2 = True
    if (multiple_of_2 == False):
        print("-1")
        return
    last_val = ord(s[len(s) - 1]) - ord('0')
    second_last_val = ord(s[len(s) - 2]) - ord('0')
    if (last_val == 0 and second_last_val % 2 == 0):
        print(0)
    elif ((last_val == 0 and second_last_val % 2 != 0) or (last_val % 2 == 0 and second_last_val == 0)):
        print(1)
    elif (more_zero and (last_val == 0 and second_last_val % 2 != 0)):
        print(1)
    else:
        print(2)


if __name__ == '__main__':
    N = "20"
    MinimumSwapOperations(N)

# Expected output:
-1
# largest palindrome which is product of two n | function to check if a number is a palindrome or not ; taking the string value of the number ; loop to check if every i - th character from beginning is equal to every ( n - i ) th char ; function to find the largest palindrome which is a product of two n digited numbers ; find lowerbound , upperbound for a given ndigits . for n = 2 [ 10 , 99 ] ; result variables ; keep p decrementing by 11 ; find the nearest number divisible by 11 ; keep decrementing q by 1 ; update the result if t > r and is a palindrome ; printing the final result ; 
def isPalindrome(x):
    num = str(x)
    result = True
    i = 0
    j = len(num) - 1
    while (i < j and result):
        result = num[i] == num[j]
        i += 1
        j -= 1
    return result


def find(nDigits):
    lowerBound = pow(10, nDigits - 1)
    upperBound = (lowerBound * 10) - 1
    resultP = 0
    resultQ = 0
    resultR = 0
    for p in range(upperBound, lowerBound, -11):
        while (p % 11 != 0):
            p -= 1
        for q in range(upperBound, lowerBound, -1):
            t = p * q
            if (t > resultR and isPalindrome(t)):
                resultP = p
                resultQ = q
                resultR = t
                break
    print(resultR)


if __name__ == "__main__":
    N = 2
    find(N)

# Expected output:
9009
# minimize the cost of selecting two numbers whose product is x | python3 implementation of the above approach ; max_prime [ i ] represents maximum prime number that divides the number i ; min_prime [ i ] represents minimum prime number that divides the number i ; function to store the minimum prime factor and the maximum prime factor in two arrays ; check for prime number if min_prime [ i ] > 0 , then it is not a prime number ; if i is a prime number , then both minimum and maximum prime numbers that divide the number is the number itself ; if this number is being visited for first time then this divisor must be the smallest prime number that divides this number ; the last prime number that divides this number will be maximum . ; function to minimize the cost of finding two numbers for every number such that the product of those two is equal to x ; pre - calculation ; if x == 1 , then there is no way to find n and m . print - 1 ; case 3 is always valid and cost for that is c + x c for choosing 1 and m = x / 1 ; case 1 n is prime , first number cost is fixed n is max_prime number divides this number ; if x is prime then the maximum prime number is the number itself . for this case , m becomes 1 and this shouldn 't be considered. ; find m for this case ; add cost for the second number also ; update min_cost , if the cost for prime is minimum ; case 2 if n is composite for this find the minimum prime number that divides a [ i ] and consider this as m ; find n for that number ; check if this number is composite or not if n is prime then there is no way to find any composite number that divides x if n = min_prime [ n ] then n is prime ; update min_cost , if the cost for the composite is minimum ; 

MAX = 10000
max_prime = [0] * MAX
min_prime = [0] * MAX


def sieve(n):
    for i in range(2, n):
        if (min_prime[i] > 0):
            continue
        min_prime[i] = i
        max_prime[i] = i
        j = i + i
        while (j < n):
            if (min_prime[j] == 0):
                min_prime[j] = i
            max_prime[j] = i
            j += i


def findCost(A, B, C, X):
    sieve(MAX)
    if (X == 1):
        return -1
    min_cost = C + X
    cost_for_prime = A
    N = max_prime[X]
    if (N != X):
        M = X // N
        cost_for_prime += M
        min_cost = min(min_cost, cost_for_prime)
    M = min_prime[X]
    N = X // M
    if (N != min_prime[N]):
        cost_for_comp = B + M
        min_cost = min(min_cost, cost_for_comp)
    return min_cost


if __name__ == "__main__":
    A = 7
    B = 11
    C = 2
    X = 20
    print(findCost(A, B, C, X))

# Expected output:
11
# count the occurrence of digit k in a given number n using recursion | function to count the digit k in the given number n ; extracting least significant digit ; 
def countdigits(n, k):
    if n == 0:
        return 0
    digit = n % 10
    if digit == k:
        return 1 + countdigits(n / 10, k)
    return countdigits(n / 10, k)


if __name__ == "__main__":
    n = 1000
    k = 0
    print(countdigits(n, k))

# Expected output:
3
# product of all subarrays of an array | function to find product of all subarrays ; variable to store the product ; compute the product while traversing for subarrays ; printing product of all subarray ; 
def product_subarrays(arr, n):
    res = 1
    for i in range(n):
        product = 1
        for j in range(i, n):
            product *= arr[j]
            res = res * product
    print(res)


if __name__ == '__main__':
    arr = [10, 3, 7]
    n = len(arr)
    product_subarrays(arr, n)

# Expected output:
27783000
# find numbers that divide x and y to produce the same remainder | function to find the required number as m ; finding the maximum value among x and y ; loop to iterate through maximum value among x and y . ; if the condition satisfies , then print the value of m ; 
def printModulus(X, Y):
    n = max(X, Y)
    for i in range(1, n + 1):
        if (X % i == Y % i):
            print(i, end=" ")


X = 10
Y = 20
printModulus(X, Y)

# Expected output:
1 2 5 10
# find numbers that divide x and y to produce the same remainder | function to prall the possible values of m such that x % m = y % m ; finding the absolute difference of x and y ; iterating from 1 ; loop to prall the factors of d ; if i is a factor of d , then pri ; if d / i is a factor of d , then prd / i ; 
def printModulus(X, Y):
    d = abs(X - Y)
    i = 1
    while (i * i <= d):
        if (d % i == 0):
            print(i, end="")
            if (d // i != i):
                print(d // i, end=" ")
        i += 1


if __name__ == '__main__':
    X = 10
    Y = 26
    printModulus(X, Y)

# Expected output:
116 28 4
# check whether a number can be represented as difference of two squares | function to check whether a number can be represented by the difference of two squares ; checking if n % 4 = 2 or not ; 
def difSquare(n):
    if (n % 4 != 2):
        return True
    return False


if __name__ == '__main__':
    n = 45
    if (difSquare(n)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# count of fibonacci divisors of a given number | python3 program to count number of divisors of n which are fibonacci numbers ; function to create hash table to check fibonacci numbers ; function to count number of divisors of n which are fibonacci numbers ; if divisors are equal , check and count only one ; otherwise check and count both ; 

from math import sqrt, ceil, floor


def createHash(maxElement):
    prev = 0
    curr = 1
    d = dict()
    d[prev] = 1
    d[curr] = 1
    while (curr <= maxElement):
        temp = curr + prev
        d[temp] = 1
        prev = curr
        curr = temp
    return d


def countFibonacciDivisors(n):
    hash = createHash(n)
    cnt = 0
    for i in range(1, ceil(sqrt(n))):
        if (n % i == 0):
            if ((n // i == i) and (n // i in hash)):
                cnt += 1
            else:
                if (n // i in hash):
                    cnt += 1
                if (n // (n // i) in hash):
                    cnt += 1
    return cnt


n = 12
print(countFibonacciDivisors(n))

# Expected output:
3
# make array elements equal with minimum cost | function to find the minimum cost required to make array elements equal ; loop to find the count of odd elements ; 
def makearrayequal(arr, n):
    x = 0
    for i in range(n):
        x += arr[i] & 1
    print(min(x, n - x))


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

# Expected output:
2
# count of prime digits in a number | function to find the count of prime digits in a number ; loop to compute all the digits of the number ; finding every digit of the given number ; checking if digit is prime or not only 2 , 3 , 5 and 7 are prime one - digit number ; 
def countDigit(n):
    temp = n
    count = 0
    while (temp != 0):
        d = temp % 10
        temp //= 10
        if (d == 2 or d == 3 or d == 5 or d == 7):
            count += 1
    return count


if __name__ == '__main__':
    n = 1234567890
    print(countDigit(n))

# Expected output:
4
# check if an array is a permutation of numbers from 1 to n : set 2 | function to check if an array represents a permutation or not ; counting the frequency ; check if each frequency is 1 only ; 
def permutation(arr, N):
    hash = [0] * (N + 1)
    for i in range(N):
        hash[arr[i]] += 1
    for i in range(1, N + 1):
        if (hash[i] != 1):
            return "No"
    return "Yes"


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

# Expected output:
No
# check if the xor of an array of integers is even or odd | function to check if the xor of an array of integers is even or odd ; count the number of odd elements ; if count of odd elements is odd , then xor will be odd ; else even ; 
def check(arr, n):
    count = 0
    for i in range(n):
        if (arr[i] & 1):
            count = count + 1
    if (count & 1):
        return "Odd"
    else:
        return "Even"


if __name__ == '__main__':
    arr = [3, 9, 12, 13, 15]
    n = len(arr)
    print(check(arr, n))

# Expected output:
Even