giulio98/xlcost-single-prompt huggingface dataset:
# longest mountain subarray | function to find the longest mountain subarray ; if the size of the array is less than 3 , the array won 't show  mountain like behaviour ; when a new mountain sub - array is found , there is a need to set the variables k , j to - 1 in order to help calculate the length of new mountain sub - array ; j marks the starting index of a new mountain sub - array . so set the value of j to current index i . ; checks if next element is less than current element ; checks if starting element exists or not , if the starting element of the mountain sub - array exists then the index of ending element is stored in k ; this condition checks if both starting index and ending index exists or not , if yes , the length is calculated . ; d holds the length of the longest mountain sub - array . if the current length is greater than the calculated length , then value of d is updated . ; ignore if there is no increase or decrease in the value of the next element ; checks and calculates the length if last element of the array is the last element of a mountain sub - array ; 
def LongestMountain(a):
    i = 0
    j = -1
    k = -1
    p = 0
    d = 0
    n = 0
    if (len(a) < 3):
        return 0
    for i in range(len(a) - 1):
        if (a[i + 1] > a[i]):
            if (k != -1):
                k = -1
                j = -1
            if (j == -1):
                j = i
        else:
            if (a[i + 1] < a[i]):
                if (j != -1):
                    k = i + 1
                if (k != -1 and j != -1):
                    if (d < k - j + 1):
                        d = k - j + 1
            else:
                k = -1
                j = -1
    if (k != -1 and j != -1):
        if (d < k - j + 1):
            d = k - j + 1
    return d


d = [1, 3, 1, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5]
print(LongestMountain(d))

# Expected output:
11
# check whether an array can be made 0 by splitting and merging repeatedly | function that finds if it is possible to make the array contain only 1 element i . e . 0 ; check if element is odd ; according to the logic in above approach ; 
def solve(A):
    ctr = 0
    for i in range(len(A)):
        if A[i] % 2 == 1:
            ctr += 1
    if ctr % 2 == 1:
        return 'No'
    else:
        return 'Yes'


if __name__ == '__main__':
    arr = [9, 17]
    print(solve(arr))

# Expected output:
Yes
# count of perfect squares of given length | python3 program to count perfect squares of given length ; function to check if a number is perfect square ; find floating point value of square root of x . ; if square root is an integer ; function to return the count of n digit perfect squares ; initialize result ; traverse through all numbers of n digits ; check if current number ' i ' is perfect square ; 

import math


def isPerfectSquare(x):
    sr = math .sqrt(x)
    return ((sr - math .floor(sr)) == 0)


def countSquares(n):
    cnt = 0
    for i in range(int(math .pow(10, (n - 1))), int(math .pow(10, n))):
        if (i != 0 and isPerfectSquare(i)):
            cnt += 1
    return cnt


n = 3
print(countSquares(n))

# Expected output:
22
# count of perfect squares of given length | python3 program to count perfect squares of given length ; function to return the count of n digit perfect squares ; 

import math


def countSquares(n):
    r = math .ceil(math .sqrt(math .pow(10, n)))
    l = math .ceil(math .sqrt(math .pow(10, n - 1)))
    return r - l


n = 3
print(countSquares(n))

# Expected output:
22
# sum of alternating sign cubes of first n natural numbers | function to compute sum of the cubes with alternating sign ; 
def summation(N):
    co = (N + 1) / 2
    co = int(co)
    ce = N / 2
    ce = int(ce)
    se = 2 * ((ce * (ce + 1)) * (ce * (ce + 1)))
    so = (co * co) * (2 * (co * co) - 1)
    return so - se


n = 3
print(summation(n))

# Expected output:
20
# program to check if n is a star number | python3 implementation to check that a number is a star number or not ; function to check that the number is a star number ; condition to check if the number is a star number ; 

import math


def isStar(N):
    n = (math .sqrt(24 * N + 12) + 6) / 6
    return (n - int(n)) == 0


i = 13
if isStar(i):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# smallest number to make array sum at most k by dividing each element | python3 program to find the smallest number such that the sum of the array becomes less than or equal to k when every element of the array is divided by that number ; function to find the smallest number such that the sum of the array becomes less than or equal to k when every element of the array is divided by that number ; binary search between 1 and 10 ^ 9 ; calculating the new sum after dividing every element by mid ; if after dividing by mid , if the new sum is less than or equal to limit move low to mid + 1 ; else , move mid + 1 to high ; returning the minimum number ; 

from math import ceil


def findMinDivisor(arr, n, limit):
    low = 0
    high = 10 ** 9
    while (low < high):
        mid = (low + high) // 2
        sum = 0
        for i in range(n):
            sum += ceil(arr[i] / mid)
        if (sum <= limit):
            high = mid
        else:
            low = mid + 1
    return low


if __name__ == '__main__':
    arr = [2, 3, 4, 9]
    N = len(arr)
    K = 6
    print(findMinDivisor(arr, N, K))

# Expected output:
4
# count of triplets ( a , b , c ) in the array such that a divides b and b divides c | function to count triplets ; iterate for middle element ; iterate left array for a [ i ] ; iterate right array for a [ k ] ; return the final result ; 
def getCount(arr, n):
    count = 0
    for j in range(1, n - 1):
        p, q = 0, 0
        for i in range(j):
            if (arr[j] % arr[i] == 0):
                p += 1
        for k in range(j + 1, n):
            if (arr[k] % arr[j] == 0):
                q += 1
        count += p * q
    return count


if __name__ == '__main__':
    arr = [1, 2, 2]
    N = len(arr)
    print(getCount(arr, N))

# Expected output:
1
# find pair with maximum ratio in an array | function to find the maximum pair possible for the array ; loop to iterate over every possible pair in the array ; check pair ( x , y ) as well as ( y , x ) for maximum value ; update the answer ; 
def computeMaxValue(arr, n):
    ans = 0
    for i in range(n - 1):
        for j in range(i + 1, n):
            val = max(arr[i] / arr[j], arr[j] / arr[i])
            ans = max(ans, val)
    return ans


if __name__ == "__main__":
    arr = [15, 10, 3, 2]
    n = len(arr)
    print(computeMaxValue(arr, n))

# Expected output:
7.5
# find the kth number which is not divisible by n | python3 implementation to find the k 'th non-divisible number of n ; function to find the kth not divisible by n ; 

import math


def kthNonDivisible(N, K):
    return K + math .floor((K - 1) / (N - 1))


N = 3
K = 6
print(kthNonDivisible(N, K))

# Expected output:
8
# find the maximum sum pair in an array with even parity | python3 implementation to find a pair with even parity and maximum sum ; function that returns true if count of set bits in given number is even ; parity will store the count of set bits ; function to print the elements of the array ; function to remove all the even parity elements from the given array ; traverse the array ; if the current element has even parity ; 

import sys
sz = 1e3


def isEvenParity(x):
    parity = 0
    while x != 0:
        if (x & 1):
            parity = parity + 1
        x = x >> 1
    if (parity % 2 == 0):
        return True
    else:
        return False


def printArray(arr, n):
    for i in range(0, n):
        print(arr[i], end=' ')


def findPairEvenParity(arr, n):
    firstMaximum = -1
    secondMaximum = -1
    for i in range(0, n):
        if isEvenParity(arr[i]):
            if (arr[i] >= firstMaximum):
                secondMaximum = firstMaximum
                firstMaximum = arr[i]
            elif (arr[i] >= secondMaximum):
                secondMaximum = arr[i]
    print(firstMaximum, secondMaximum)


if __name__ == "__main__":
    arr = [18, 15, 8, 9, 14]
    n = len(arr)
    findPairEvenParity(arr, n)

# Expected output:
18 15
# program to check if n is a hexagonal number or not | python3 program to check if n is a hexagonal number ; function to check if number is hexagonal ; calculate the value for n ; check if n - floor ( n ) is equal to 0 ; 

from math import sqrt


def isHexagonal(N):
    val = 8 * N + 1
    x = 1 + sqrt(val)
    n = x / 4
    if ((n - int(n)) == 0):
        return True
    else:
        return False


if __name__ == '__main__':
    N = 14
    if (isHexagonal(N)):
        print("Yes")
    else:
        print("No")

# Expected output:
No
# program to convert number in characters | python3 program to convert number in characters ; to calculate the reverse of the number ; the remainder will give the last digit of the number ; extract the first digit of the reversed number ; match it with switch case ; divide the number by 10 to get the next number ; 
def NumbertoCharacter(n):
    rev = 0
    r = 0
    while (n > 0):
        r = n % 10
        rev = rev * 10 + r
        n = n // 10
    while (rev > 0):
        r = rev % 10
        switcher = {
            0: "zero ",
            1: "one ",
            2: "two ",
            3: "three ",
            4: "four ",
            5: "five ",
            6: "six ",
            7: "seven ",
            8: "eight ",
            9: "nine "}
        print(switcher .get(r, "UnValid"), end=" ")
        rev = rev // 10


if __name__ == '__main__':
    n = 12345
    NumbertoCharacter(n)

# Expected output:
one  two  three  four  five
# count all subarrays whose sum can be split as difference of squares of two integers | function to count all the non - contiguous subarrays whose sum can be split as the difference of the squares ; loop to iterate over all the possible subsequences of the array ; finding the sum of all the possible subsequences ; condition to check whether the number can be split as difference of squares ; 
def Solve(arr, n):
    temp = 0
    count = 0
    for i in range(0, n):
        temp = 0
        for j in range(i, n):
            temp = temp + arr[j]
            if ((temp + 2) % 4 != 0):
                count += 1
    return count


arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
N = len(arr)
print(Solve(arr, N))

# Expected output:
40
# find product of all elements at indexes which are factors of m for all possible sorted subsequences of length m | python3 program to find the product of all the combinations of m elements from an array whose index in the sorted order divides m completely ; iterative function to calculate ( x ^ y ) % p in o ( log y ) ; if y is odd , multiply x with result ; y must be even now ; iterative function to calculate ( ncr ) % p and save in f [ n ] [ r ] c ( n , r ) % p = [ c ( n - 1 , r - 1 ) % p + c ( n - 1 , r ) % p ] % p and c ( n , 0 ) = c ( n , n ) = 1 ; if j > i then c ( i , j ) = 0 ; if i is equal to j then c ( i , j ) = 1 ; c ( i , j ) = ( c ( i - 1 , j ) + c ( i - 1 , j - 1 ) ) % p ; initialize the answer ; for every element arr [ i ] , x is count of occurrence of arr [ i ] in different set such that index of arr [ i ] in those sets divides m completely . ; finding the count of arr [ i ] by placing it at the index which divides m completely ; using fermat 's little theorem ; multiplying with the count ; 

m = 4


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


def nCr(n, p, f):
    for i in range(n):
        for j in range(m + 1):
            if (j > i):
                f[i][j] = 0
            elif (j == 0 or j == i):
                f[i][j] = 1
            else:
                f[i][j] = ((f[i - 1][j] + f[i - 1][j - 1]) % p)


def operations(arr, n, f):
    p = 1000000007
    nCr(n, p - 1, f)
    arr .sort()
    ans = 1
    for i in range(n):
        x = 0
        for j in range(1, m + 1):
            if (m % j == 0):
                x = ((x + (f[n - i - 1][m - j] * f[i][j - 1]) %
                     (p - 1)) % (p - 1))
        ans = ((ans * power(arr[i], x, p)) % p)
    print(ans)


if __name__ == "__main__":
    arr = [4, 5, 7, 9, 3]
    n = len(arr)
    f = [[0 for x in range(m + 1)]for y in range(n + 1)]
    operations(arr, n, f)

# Expected output:
808556639
# check whether a number can be represented by the product of two squares | function to check if there exist two numbers product of whose squares is n . ; check whether the product of the square of both numbers is equal to n ; 
def prodSquare(n):
    for i in range(2, (n) + 1):
        if (i * i < (n + 1)):
            for j in range(2, n + 1):
                if ((i * i * j * j) == n):
                    return True
    return False


if __name__ == '__main__':
    n = 25
    if (prodSquare(n)):
        print("Yes")
    else:
        print("No")

# Expected output:
No
# check whether a number can be represented by the product of two squares | function to check if there exist two numbers product of whose squares is n ; initialize dict / map ; store square value in hashmap ; 
def prodSquare(n):
    s = dict()
    i = 2
    while (i * i <= n):
        s[i * i] = 1
        if ((n // (i * i)) in s):
            return True
        i += 1
    return False


if __name__ == '__main__':
    n = 25
    if (prodSquare(n)):
        print("Yes")
    else:
        print("No")

# Expected output:
No
# print n distinct numbers following the given operations | function to print the required array ; check if number is a multiple of 4 ; printing left half of the array ; printing right half of the array ; 
def printArr(n):
    if (n % 4 == 0):
        for i in range(1, (n / 2) + 1):
            print(i * 2, end=" ")
        for i in range(1, n / 2):
            print(i * 2 - 1, end=" ")
        print(n + n / 2 - 1, end="")
    else:
        print("-1")


n = 22
printArr(n)

# Expected output:
-1
# sum of product of all subsets formed by only divisors of n | function to find the sum of product of all the subsets formed by only divisors of n ; vector to store all the divisors of n ; loop to find out the divisors of n ; both ' i ' and ' n / i ' are the divisors of n ; check if ' i ' and ' n / i ' are equal or not ; calculating the answer ; excluding the value of the empty set ; 
def GetSum(n):
    divisors = []
    i = 1
    while i * i <= n:
        if (n % i == 0):
            divisors .append(i)
            if (i != n // i):
                divisors .append(n // i)
        i += 1
    ans = 1
    for i in divisors:
        ans *= (i + 1)
    ans = ans - 1
    return ans


if __name__ == "__main__":
    N = 4
    print(GetSum(N))

# Expected output:
29
# length of longest powerful number subsequence in an array | python3 program to find the length of longest powerful subsequence in an array ; function to check if the number is powerful ; first divide the number repeatedly by 2 ; check if only 2 ^ 1 divides n , then return false ; check if n is not a power of 2 then this loop will execute repeat above process ; find highest power of " factor " that divides n ; if only factor ^ 1 divides n , then return false ; n must be 1 now if it is not a prime number . since prime numbers are not powerful , we return false if n is not 1. ; function to find the longest subsequence which contain all powerful numbers ; 

import math


def isPowerful(n):
    while (n % 2 == 0):
        power = 0
        while (n % 2 == 0):
            n //= 2
            power += 1
        if (power == 1):
            return False
    for factor in range(3, int(math .sqrt(n)) + 1, 2):
        power = 0
        while (n % factor == 0):
            n = n // factor
            power += 1
        if (power == 1):
            return False
    return (n == 1)


def longestPowerfulSubsequence(arr, n):
    answer = 0
    for i in range(n):
        if (isPowerful(arr[i])):
            answer += 1
    return answer


arr = [6, 4, 10, 13, 9, 25]
n = len(arr)
print(longestPowerfulSubsequence(arr, n))

# Expected output:
3
# maximum or value of a pair in an array without using or operator | python3 implementation of the approach ; function to return the maximum bitwise or for any pair of the given array without using bitwise or operation ; find maximum element in the array ; finding complement will set all unset bits in a number ; iterate through all other array elements to find maximum and value ; c will give the maximum value that could be added to max_value to produce maximum or value ; 

from math import log2, floor


def maxOR(arr, n):
    max_value = max(arr)
    number_of_bits = floor(log2(max_value)) + 1
    complement = (((1 << number_of_bits) - 1) ^ max_value)
    c = 0
    for i in range(n):
        if (arr[i] != max_value):
            c = max(c, (complement & arr[i]))
    return (max_value + c)


if __name__ == '__main__':
    arr = [3, 6, 8, 16]
    n = len(arr)
    print(maxOR(arr, n))

# Expected output:
24
# shortest subarray to be removed to make all array elements unique | python3 program to make array elements pairwise distinct by removing at most one subarray of minimum length ; function to check if elements of prefix and suffix of each sub array of size k are pairwise distinct or not ; hash map to store frequencies of elements of prefix and suffix ; variable to store number of occurrences of an element other than one ; adding frequency of elements of suffix to hash for subarray starting from first index there is no prefix for this sub array ; counting extra elements in current hash map ; if there are no extra elements return true ; check for remaining sub arrays ; first element of suffix is now part of subarray which is being removed so , check for extra elements ; decrement frequency of first element of the suffix ; increment frequency of last element of the prefix ; check for extra elements ; if there are no extra elements return true ; function for calculating minimum length of the subarray , which on removing make all elements pairwise distinct ; possible range of length of subarray ; binary search to find minimum ans ; 

from collections import defaultdict


def check(a, n, k):
    m = defaultdict(int)
    extra = 0
    for i in range(k, n):
        m[a[i]] += 1
    for x in m:
        extra += m[x] - 1
    if (extra == 0):
        return True
    for i in range(1, i + k - 1 < n):
        if (m[a[i + k - 1]] > 1):
            extra -= 1
        m[a[i + k - 1]] -= 1
        m[a[i - 1]] += 1
        if (m[a[i - 1]] > 1):
            extra += 1
        if (extra == 0):
            return True
    return False


def minlength(a, n):
    lo = 0
    hi = n + 1
    ans = 0
    while (lo < hi):
        mid = (lo + hi) // 2
        if (check(a, n, mid)):
            ans = mid
            hi = mid
        else:
            lo = mid + 1
    return ans


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

# Expected output:
2
# find n distinct integers with zero sum | function to print distinct n numbers such that their sum is 0 ; print 2 symmetric numbers ; print a extra 0 if n is odd ; 
def findNumbers(N):
    for i in range(1, N // 2 + 1):
        print(i, end=', ')
        print(-i, end=', ')
    if N % 2 == 1:
        print(0, end='')


if __name__ == '__main__':
    N = 10
    findNumbers(N)

# Expected output:
1, -1, 2, -2, 3, -3, 4, -4, 5, -5,
# program to calculate electricity bill | function to calculate the electricity bill ; condition to find the charges bar in which the units consumed is fall ; 
def calculateBill(units):
    if (units <= 100):
        return units * 10
    elif (units <= 200):
        return ((100 * 10) + (units - 100) * 15)
    elif (units <= 300):
        return ((100 * 10) + (100 * 15) + (units - 200) * 20)
    elif (units > 300):
        return ((100 * 10) + (100 * 15) + (100 * 20) + (units - 300) * 25)
    return 0


units = 250
print(calculateBill(units))

# Expected output:
3500
# find all divisors of first n natural numbers | function to find the factors of the numbers from 1 to n ; loop to find the factors of the first n natural numbers of the integer ; 
def factors(n):
    print("1 -->1")
    for i in range(2, n + 1):
        print(i, " -->", end="")
        for j in range(1, int(pow(i, 1))):
            if (i % j == 0):
                print(j, ", ", end="")
                if (i // j != j):
                    print(i // j, ", ", end="")
        print(end="")


if __name__ == '__main__':
    n = 5
    factors(n)

# Expected output:
1 -->1
2  -->1 , 2 , 3  -->1 , 3 , 4  -->1 , 4 , 2 , 5  -->1 , 5 ,
# maximum number of prime factors a number can have with exactly x factors | python3 implementation to find the maximum count of the prime factors by the count of factors of number ; function to count number of prime factors of x ; count variable is incremented form every prime factor of x ; loop to count the number of the prime factors of the given number ; 

import math


def countPrimeFactors(n):
    if (n == 1):
        return 0
    cnt = 0
    while (n % 2 == 0):
        cnt += 1
        n = n // 2
    for i in range(3, int(math .sqrt(n)) + 1, 2):
        while (n % i == 0):
            cnt += 1
            n = n // i
    if (n > 2):
        cnt += 1
    return cnt


x = 8
prime_factor_cnt = countPrimeFactors(x)
print(prime_factor_cnt)

# Expected output:
3
# count of n digit palindromic numbers divisible by 9 | function to find the count of n digits palindromic numbers which are divisible by 9 ; if n is odd ; if n is even ; 
def countPalindromic(n):
    count = 0
    if (n % 2 == 1):
        count = pow(9, (n - 1) // 2)
    else:
        count = pow(9, (n - 2) // 2)
    return count


n = 3
print(countPalindromic(n))

# Expected output:
9
# count of sub | function that returns the count of sub - arrays with odd product ; initialize the count variable ; initialize variable to store the last index with even number ; initialize variable to store count of continuous odd numbers ; loop through the array ; check if the number is even or not ; calculate count of continuous odd numbers ; increase the count of sub - arrays with odd product ; store the index of last even number ; n considered as index of even number ; 
def countSubArrayWithOddProduct(A, N):
    count = 0
    last = -1
    K = 0
    for i in range(N):
        if (A[i] % 2 == 0):
            K = (i - last - 1)
            count += (K * (K + 1) / 2)
            last = i
    K = (N - last - 1)
    count += (K * (K + 1) / 2)
    return count


if __name__ == '__main__':
    arr = [12, 15, 7, 3, 25, 6, 2, 1, 1, 7]
    n = len(arr)
    print(int(countSubArrayWithOddProduct(arr, n)))

# Expected output:
16
# calculate the cgpa and cgpa % of marks obtained by a student in n subjects | python3 program to calculate the cgpa and cgpa percentage of a student ; variable to store the grades in every subject ; variables to store cgpa and the sum of all the grades ; computing the grades ; computing the sum of grades ; computing the cgpa ; 
def CgpaCalc(marks, n):
    grade = [0] * n
    Sum = 0
    for i in range(n):
        grade[i] = (marks[i] / 10)
    for i in range(n):
        Sum += grade[i]
    cgpa = Sum / n
    return cgpa


n = 5
marks = [90, 80, 70, 80, 90]
cgpa = CgpaCalc(marks, n)
print("CGPA = ", '%.1f' % cgpa)
print("CGPA Percentage = ", '%.2f' % (cgpa * 9.5))

# Expected output:
CGPA =  8.2
CGPA Percentage =  77.90