giulio98/xlcost-single-prompt huggingface dataset:
# count of octal numbers upto n digits | function to return the count of natural octal numbers upto n digits ; loop to iterate from 1 to n and calculating number of octal numbers for every ' i ' th digit . ; 
def count(N):
    sum = 0
    for i in range(N + 1):
        sum += 7 * (8 ** (i - 1))
    return int(sum)


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

# Expected output:
4095
# palindromic divisors of a number | python3 program to find all the palindromic divisors of a number ; function to check is num is palindromic or not ; convert n to string str ; starting and ending index of string str ; if char at s and e are not equals then return false ; function to find palindromic divisors ; to sore the palindromic divisors of number n ; if n is divisible by i ; check if number is a perfect square ; check divisor is palindromic , then store it ; check if divisors are palindrome ; check if n / divisors is palindromic or not ; print all palindromic divisors in sorted order ; 

from math import sqrt


def isPalindrome(n):
    string = str(n)
    s = 0
    e = len(string) - 1
    while (s < e):
        if (string[s] != string[e]):
            return False
        s += 1
        e -= 1
    return True


def palindromicDivisors(n):
    PalindromDivisors = []
    for i in range(1, int(sqrt(n))):
        if (n % i == 0):
            if (n // i == i):
                if (isPalindrome(i)):
                    PalindromDivisors .append(i)
            else:
                if (isPalindrome(i)):
                    PalindromDivisors .append(i)
                if (isPalindrome(n // i)):
                    PalindromDivisors .append(n // i)
    PalindromDivisors .sort()
    for i in range(len(PalindromDivisors)):
        print(PalindromDivisors[i], end=" ")


if __name__ == "__main__":
    n = 66
    palindromicDivisors(n)

# Expected output:
1 2 3 6 11 22 33 66
# remove minimum numbers from the array to get minimum or value | python3 implementation of the approach ; function to return the minimum deletions to get minimum or ; to store the minimum element ; find the minimum element from the array ; to store the frequency of the minimum element ; find the frequency of the minimum element ; return the final answer ; 

import sys


def findMinDel(arr, n):
    min_num = sys .maxsize
    for i in range(n):
        min_num = min(arr[i], min_num)
    cnt = 0
    for i in range(n):
        if (arr[i] == min_num):
            cnt += 1
    return n - cnt


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

# Expected output:
2
# program to print prime numbers from 1 to n . | function to print first n prime numbers ; declare the variables ; print display message ; traverse each number from 1 to n with the help of for loop ; skip 0 and 1 as they are neither prime nor composite ; flag variable to tell if i is prime or not ; flag = 1 means i is prime and flag = 0 means i is not prime ; 
def print_primes_till_N(N):
    i, j, flag = 0, 0, 0
    print("Prime numbers between 1 and ", N, " are:")
    for i in range(1, N + 1, 1):
        if (i == 1 or i == 0):
            continue
        flag = 1
        for j in range(2, ((i // 2) + 1), 1):
            if (i % j == 0):
                flag = 0
                break
        if (flag == 1):
            print(i, end=" ")


N = 100
print_primes_till_N(N)

# Expected output:
Prime numbers between 1 and  100  are:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
# maximize the expression ( a and x ) * ( b and x ) | bit manipulation | python3 implementation of the approach ; function to find x according to the given conditions ; int can have 32 bits ; temporary ith bit ; compute ith bit of x according to given conditions expression below is the direct conclusion from the illustration we had taken earlier ; add the ith bit of x to x ; 

MAX = 32


def findX(A, B):
    X = 0
    for bit in range(MAX):
        tempBit = 1 << bit
        bitOfX = A & B & tempBit
        X += bitOfX
    return X


if __name__ == "__main__":
    A = 11
    B = 13
    print(findX(A, B))

# Expected output:
9
# number of subsets whose mean is maximum | function to return the count of subsets with the maximum mean ; maximum value from the array ; to store the number of times maximum element appears in the array ; return the count of valid subsets ; 
def cntSubSets(arr, n):
    maxVal = max(arr)
    cnt = 0
    for i in range(n):
        if (arr[i] == maxVal):
            cnt += 1
    return ((2 ** cnt) - 1)


if __name__ == "__main__":
    arr = [1, 2, 1, 2]
    n = len(arr)
    print(cntSubSets(arr, n))

# Expected output:
3
# probability that a random pair chosen from an array ( a [ i ] , a [ j ] ) has the maximum sum | python3 implementation of the approach ; function to return the probability of getting the maximum pair sum when a random pair is chosen from the given array ; initialize the maximum sum , its count and the count of total pairs ; for every single pair ; get the sum of the current pair ; if the sum is equal to the current maximum sum so far ; increment its count ; if the sum is greater than the current maximum ; update the current maximum and re - initialize the count to 1 ; find the required probability ; 

import sys


def findProb(arr, n):
    maxSum = -(sys .maxsize - 1)
    maxCount = 0
    totalPairs = 0
    for i in range(n - 1):
        for j in range(i + 1, n):
            sum = arr[i] + arr[j]
            if (sum == maxSum):
                maxCount += 1
            elif (sum > maxSum):
                maxSum = sum
                maxCount = 1
            totalPairs += 1
    prob = maxCount / totalPairs
    return prob


if __name__ == "__main__":
    arr = [1, 1, 1, 2, 2, 2]
    n = len(arr)
    print(findProb(arr, n))

# Expected output:
0.2
# maximum count of common divisors of a and b such that all are co | python3 implementation of the approach ; function to return the count of common factors of a and b such that all the elements are co - prime to one another ; gcd of a and b ; include 1 initially ; find all the prime factors of the gcd ; if gcd is prime ; return the required answer ; 

import math


def maxCommonFactors(a, b):
    gcd = math .gcd(a, b)
    ans = 1
    i = 2
    while (i * i <= gcd):
        if (gcd % i == 0):
            ans += 1
            while (gcd % i == 0):
                gcd = gcd // i
        i += 1
    if (gcd != 1):
        ans += 1
    return ans


a = 12
b = 18
print(maxCommonFactors(a, b))

# Expected output:
3
# find the day number in the current year for the given date | python3 implementation of the approach ; function to return the day number of the year for the given date ; extract the year , month and the day from the date string ; if current year is a leap year and the date given is after the 28 th of february then it must include the 29 th february ; add the days in the previous months ; 

days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]


def dayOfYear(date):
    year = (int)(date[0:4])
    month = (int)(date[5:7])
    day = (int)(date[8:])
    if (month > 2 and year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)):
        day += 1
    month -= 1
    while (month > 0):
        day = day + days[month - 1]
        month -= 1
    return day


if __name__ == '__main__':
    date = "2019-01-09"
    print(dayOfYear(date))

# Expected output:
9
# find the number of cells in the table contains x | function to find number of cells in the table contains x ; 
def Cells(n, x):
    ans = 0
    for i in range(1, n + 1):
        if (x % i == 0 and x / i <= n):
            ans += 1
    return ans


if __name__ == '__main__':
    n = 6
    x = 12
    print(Cells(n, x))

# Expected output:
4
# smallest power of 4 greater than or equal to n | python3 implementation of above approach ; function to return the smallest power of 4 greater than or equal to n ; if n is itself is a power of 4 then return n ; 

import math


def nextPowerOfFour(n):
    x = math .floor((n ** (1 / 2))**(1 / 2))
    if ((x ** 4) == n):
        return n
    else:
        x = x + 1
        return (x ** 4)


n = 122
print(nextPowerOfFour(n))

# Expected output:
256
# minimum operations required to convert x to y by multiplying x with the given co | function to return the minimum operations required ; not possible ; to store the greatest power of p that divides d ; while divible by p ; to store the greatest power of q that divides d ; while divible by q ; if d > 1 ; since , d = p ^ a * q ^ b ; 
def minOperations(x, y, p, q):
    if (y % x != 0):
        return -1
    d = y // x
    a = 0
    while (d % p == 0):
        d //= p
        a += 1
    b = 0
    while (d % q == 0):
        d //= q
        b += 1
    if (d != 1):
        return -1
    return (a + b)


x = 12
y = 2592
p = 2
q = 3
print(minOperations(x, y, p, q))

# Expected output:
6
# number of quadruples with gcd equal to k | python3 implementation of the approach ; function to calculate nc4 ; base case to calculate nc4 ; function to return the count of required quadruples using inclusion exclusion ; effective n ; iterate over 2 to m ; number of divisors of i till m ; count stores the number of prime divisors occurring exactly once ; to prevent repetition of prime divisors ; if repetition of prime divisors present ignore this number ; if prime divisor count is odd subtract it from answer else add ; 

from math import sqrt


def nCr(n):
    if (n < 4):
        return 0
    answer = n * (n - 1) * (n - 2) * (n - 3)
    answer //= 24
    return answer


def countQuadruples(N, K):
    M = N // K
    answer = nCr(M)
    for i in range(2, M):
        j = i
        temp2 = M // i
        count = 0
        check = 0
        temp = j
        while (j % 2 == 0):
            count += 1
            j //= 2
            if (count >= 2):
                break
        if (count >= 2):
            check = 1
        for k in range(3, int(sqrt(temp)), 2):
            cnt = 0
            while (j % k == 0):
                cnt += 1
                j //= k
                if (cnt >= 2):
                    break
            if (cnt >= 2):
                check = 1
                break
            elif (cnt == 1):
                count += 1
        if (j > 2):
            count += 1
        if (check):
            continue
        else:
            if (count % 2 == 1):
                answer -= nCr(temp2)
            else:
                answer += nCr(temp2)
    return answer


if __name__ == "__main__":
    N = 10
    K = 2
    print(countQuadruples(N, K))

# Expected output:
5
# find the number which when added to the given ratio a : b , the ratio changes to c : d | function to return the required number x ; 
def getX(a, b, c, d):
    X = (b * c - a * d) // (d - c)
    return X


a = 2
b = 3
c = 4
d = 5
print(getX(a, b, c, d))

# Expected output:
2
# number of ways to arrange a word such that no vowels occur together | function to check if a character is vowel or consonent ; function to calculate factorial of a number ; calculating no of ways for arranging vowels ; iterate the map and count the number of vowels and calculate no of ways to arrange vowels ; calculating no of ways to arrange the given word such that vowels come together ; calculate no of ways to arrange vowels ; to store denominator of fraction ; count of consonents ; calculate the number of ways to arrange the word such that vowels come together ; to calculate total number of permutations ; to store length of the given word ; denominator of fraction ; return total number of permutations of the given word ; function to calculate number of permutations such that no vowels come together ; to store frequency of character ; count frequency of acharacters ; calculate total number of permutations ; calculate total number of permutations such that vowels come together ; substrat vwl_tgthr from total to get the result ; return the result ; 
def isVowel(ch):
    if (ch == 'a' or ch == 'e' or ch == 'i' or ch == 'o' or ch == 'u'):
        return True
    else:
        return False


def fact(n):
    if (n < 2):
        return 1
    return n * fact(n - 1)


def only_vowels(freq):
    denom = 1
    cnt_vwl = 0
    for itr in freq:
        if (isVowel(itr)):
            denom *= fact(freq[itr])
            cnt_vwl += freq[itr]
    return fact(cnt_vwl) // denom


def all_vowels_together(freq):
    vow = only_vowels(freq)
    denom = 1
    cnt_cnst = 0
    for itr in freq:
        if (isVowel(itr) == False):
            denom *= fact(freq[itr])
            cnt_cnst += freq[itr]
    ans = fact(cnt_cnst + 1) // denom
    return (ans * vow)


def total_permutations(freq):
    cnt = 0
    denom = 1
    for itr in freq:
        denom *= fact(freq[itr])
        cnt += freq[itr]
    return fact(cnt) // denom


def no_vowels_together(word):
    freq = dict()
    for i in word:
        ch = i .lower()
        freq[ch] = freq .get(ch, 0) + 1
    total = total_permutations(freq)
    vwl_tgthr = all_vowels_together(freq)
    res = total - vwl_tgthr
    return res


word = "allahabad"
ans = no_vowels_together(word)
print(ans)
word = "geeksforgeeks"
ans = no_vowels_together(word)
print(ans)
word = "abcd"
ans = no_vowels_together(word)
print(ans)

# Expected output:
7200
32205600
0
# program to find the number of men initially | function to return the number of men initially ; 
def numberOfMen(D, m, d):
    Men = (m * (D - d)) / d
    return int(Men)


D = 5
m = 4
d = 4
print(numberOfMen(D, m, d))

# Expected output:
1
# area of triangle formed by the axes of co | function to find area ; 
def area(a, b, c):
    d = abs((c * c) / (2 * a * b))
    return d


a = -2
b = 4
c = 3
print(area(a, b, c))

# Expected output:
0.5625
# sum of two numbers where one number is represented as array of digits | function to return the vector containing the answer ; vector v is to store each digits sum and vector ans is to store the answer ; no carry in the beginning ; start loop from the end and take element one by one ; array index and last digit of number ; maintain carry of summation ; push the digit value into the array ; k value is greater then 0 ; push digits of k one by one in the array ; also maintain carry with summation ; reverse the elements of vector v and store it in vector ans ; 
def addToArrayForm(A, K):
    v, ans = [], []
    rem, i = 0, 0
    for i in range(len(A) - 1, -1, -1):
        my = A[i] + (K % 10) + rem
        if my > 9:
            rem = 1
            v .append(my % 10)
        else:
            v .append(my)
            rem = 0
        K = K // 10
    while K > 0:
        my = (K % 10) + rem
        v .append(my % 10)
        if my // 10 > 0:
            rem = 1
        else:
            rem = 0
        K = K // 10
    if rem > 0:
        v .append(rem)
    for i in range(len(v) - 1, -1, -1):
        ans .append(v[i])
    return ans


if __name__ == "__main__":
    A = [2, 7, 4]
    K = 181
    ans = addToArrayForm(A, K)
    for i in range(0, len(ans)):
        print(ans[i], end="")

# Expected output:
455
# compute maximum of the function efficiently over all sub | python3 implementation of the above approach ; function to return maximum sum of a sub - array ; function to return maximum value of function f ; compute arrays b [ ] and c [ ] ; find maximum sum sub - array of both of the arrays and take maximum among them ; 

MAX = 100005


def kadaneAlgorithm(ar, n):
    sum = 0
    maxSum = 0
    for i in range(n):
        sum += ar[i]
        if (sum < 0):
            sum = 0
        maxSum = max(maxSum, sum)
    return maxSum


def maxFunction(arr, n):
    b = [0] * MAX
    c = [0] * MAX
    for i in range(n - 1):
        if (i & 1):
            b[i] = abs(arr[i + 1] - arr[i])
            c[i] = -b[i]
        else:
            c[i] = abs(arr[i + 1] - arr[i])
            b[i] = -c[i]
    ans = kadaneAlgorithm(b, n - 1)
    ans = max(ans, kadaneAlgorithm(c, n - 1))
    return ans


if __name__ == "__main__":
    arr = [1, 5, 4, 7]
    n = len(arr)
    print(maxFunction(arr, n))

# Expected output:
6
# third last digit in 5 ^ n for given n | function to find the element ; if n < 3 ; if n is even return 6 if n is odd return 1 ; 
def findThirdDigit(n):
    if n < 3:
        return 0
    return 1 if n and 1 else 6


n = 7
print(findThirdDigit(n))

# Expected output:
1
# probability of a winning the match when individual probabilities of hitting the target given | function to return the probability of a winning ; p and q store the values of fractions a / b and c / d ; to store the winning probability of a ; 
def getProbability(a, b, c, d):
    p = a / b
    q = c / d
    ans = p * (1 / (1 - (1 - q) * (1 - p)))
    return round(ans, 5)


if __name__ == "__main__":
    a = 1
    b = 2
    c = 10
    d = 11
    print(getProbability(a, b, c, d))

# Expected output:
0.52381
# largest palindromic number in an array | function to check if n is palindrome ; find the appropriate divisor to extract the leading digit ; if first and last digits are not same then return false ; removing the leading and trailing digits from the number ; reducing divisor by a factor of 2 as 2 digits are dropped ; function to find the largest palindromic number ; if a palindrome larger than the currentmax is found ; return the largest palindromic number from the array ; 
def isPalindrome(n):
    divisor = 1
    while (int(n / divisor) >= 10):
        divisor *= 10
    while (n != 0):
        leading = int(n / divisor)
        trailing = n % 10
        if (leading != trailing):
            return False
        n = int((n % divisor) / 10)
        divisor = int(divisor / 100)
    return True


def largestPalindrome(A, n):
    currentMax = -1
    for i in range(0, n, 1):
        if (A[i] > currentMax and isPalindrome(A[i])):
            currentMax = A[i]
    return currentMax


if __name__ == '__main__':
    A = [1, 232, 54545, 999991]
    n = len(A)
    print(largestPalindrome(A, n))

# Expected output:
54545
# reduce the array to a single element with the given operation | function to return the final element ; 
def getFinalElement(n):
    finalNum = 2
    while finalNum * 2 <= n:
        finalNum *= 2
    return finalNum


if __name__ == "__main__":
    N = 12
    print(getFinalElement(N))

# Expected output:
8
# sum of elements in an array having prime frequency | python3 program to find sum of elements in an array having prime frequency ; function to create sieve to check primes ; false here indicates that it is not prime ; if prime [ p ] is not changed , then it is a prime ; update all multiples of p , set them to non - prime ; function to return the sum of elements in an array having prime frequency ; map is used to store element frequencies ; traverse the map using iterators ; count the number of elements having prime frequencies ; 

import math as mt


def SieveOfEratosthenes(prime, p_size):
    prime[0] = False
    prime[1] = False
    for p in range(2, mt .ceil(mt .sqrt(p_size + 1))):
        if (prime[p]):
            for i in range(p * 2, p_size + 1, p):
                prime[i] = False


def SumOfElements(arr, n):
    prime = [True for i in range(n + 1)]
    SieveOfEratosthenes(prime, n + 1)
    i, j = 0, 0
    m = dict()
    for i in range(n):
        if arr[i] in m .keys():
            m[arr[i]] += 1
        else:
            m[arr[i]] = 1
    Sum = 0
    for i in m:
        if (prime[m[i]]):
            Sum += (i)
    return Sum


arr = [5, 4, 6, 5, 4, 6]
n = len(arr)
print(SumOfElements(arr, n))

# Expected output:
15
# sum of all odd length palindromic numbers within the range [ l , r ] | function that returns true if the given number is a palindrome ; here we are generating a new number ( reverse_num ) by reversing the digits of original input number ; if the original input number ( num ) is equal to its reverse ( reverse_num ) then its palindrome else it is not . ; function that returns true if the given number is of odd length ; function to return the sum of all odd length palindromic numbers within the given range ; if number is palindrome and of odd length ; 
def isPalindrome(num):
    reverse_num = 0
    temp = num
    while (temp != 0):
        remainder = temp % 10
        reverse_num = reverse_num * 10 + remainder
        temp = int(temp / 10)
    if (reverse_num == num):
        return True
    return False


def isOddLength(num):
    count = 0
    while (num > 0):
        num = int(num / 10)
        count += 1
    if (count % 2 != 0):
        return True
    return False


def sumOfAllPalindrome(L, R):
    sum = 0
    if (L <= R):
        for i in range(L, R + 1, 1):
            if (isPalindrome(i) and isOddLength(i)):
                sum += i
    return sum


if __name__ == '__main__':
    L = 110
    R = 1130
    print(sumOfAllPalindrome(L, R))

# Expected output:
49399
# number of ways to arrange a word such that all vowels occur together | factorial of a number ; calculating ways for arranging consonants ; ignore vowels ; calculating ways for arranging vowels ; function to count total no . of ways ; count vowels and consonant ; total no . of ways ; 
def fact(n):
    f = 1
    for i in range(2, n + 1):
        f = f * i
    return f


def waysOfConsonants(size1, freq):
    ans = fact(size1)
    for i in range(26):
        if (i == 0 or i == 4 or i == 8 or i == 14 or i == 20):
            continue
        else:
            ans = ans // fact(freq[i])
    return ans


def waysOfVowels(size2, freq):
    return (fact(size2) //
            (fact(freq[0]) *
             fact(freq[4]) *
             fact(freq[8]) *
             fact(freq[14]) *
             fact(freq[20])))


def countWays(str1):
    freq = [0] * 26
    for i in range(len(str1)):
        freq[ord(str1[i]) - ord('a')] += 1
    vowel = 0
    consonant = 0
    for i in range(len(str1)):
        if (str1[i] != 'a' and str1[i] != 'e' and str1[i]
                != 'i' and str1[i] != 'o' and str1[i] != 'u'):
            consonant += 1
        else:
            vowel += 1
    return (waysOfConsonants(consonant + 1, freq) * waysOfVowels(vowel, freq))


if __name__ == "__main__":
    str1 = "geeksforgeeks"
    print(countWays(str1))

# Expected output:
226800
# sum of fibonacci numbers with alternate negatives | computes value of first fibonacci numbers and stores their alternate sum ; initialize result ; add remaining terms ; for even terms ; for odd terms ; return the alternating sum ; 
def calculateAlternateSum(n):
    if (n <= 0):
        return 0
    fibo = [0] * (n + 1)
    fibo[0] = 0
    fibo[1] = 1
    sum = pow(fibo[0], 2) + pow(fibo[1], 2)
    for i in range(2, n + 1):
        fibo[i] = fibo[i - 1] + fibo[i - 2]
        if (i % 2 == 0):
            sum -= fibo[i]
        else:
            sum += fibo[i]
    return sum


if __name__ == "__main__":
    n = 8
    print("Alternating Fibonacci Sum upto ", n,
          " terms: ", calculateAlternateSum(n))

# Expected output:
Alternating Fibonacci Sum upto  8  terms:  -12
# Find nth Term of the Series 1 2 2 4 4 4 4 8 8 8 8 8 8 8 8 ... | Function that will return nth term ; Get n ; Get the value ; Get n ; Get the value
def getValue(n):
    i = 0
    k = 1
    while (i < n):
        i = i + k
        k = k * 2
    return int(k / 2)


n = 9
print(getValue(n))
n = 1025
print(getValue(n))

# Expected output:
8
1024
# construct a frequency array of digits of the values obtained from x ^ 1 , x ^ 2 , ... ... . . , x ^ n | python 3 implementation of above approach ; function that traverses digits in a number and modifies frequency count array ; array to keep count of digits ; traversing through x ^ 1 to x ^ n ; for power function , both its parameters are to be in double ; calling countdigits function on x ^ i ; printing count of digits 0 - 9 ; 

import math


def countDigits(val, arr):
    while (val > 0):
        digit = val % 10
        arr[int(digit)] += 1
        val = val // 10
    return


def countFrequency(x, n):
    freq_count = [0] * 10
    for i in range(1, n + 1):
        val = math .pow(x, i)
        countDigits(val, freq_count)
    for i in range(10):
        print(freq_count[i], end=" ")


if __name__ == "__main__":
    x = 15
    n = 3
    countFrequency(x, n)

# Expected output:
0 1 2 2 0 3 0 1 0 0