giulio98/xlcost-single-prompt huggingface dataset:
# largest odd divisor game to check which player wins | python3 implementation to find the largest odd divisor game to check which player wins ; function to find the largest odd divisor game to check which player wins ; check if n == 1 then player 2 will win ; check if n == 2 or n is odd ; while n is greater than k and divisible by 2 keep incrementing tha val ; loop to find greatest odd divisor ; check if n is a power of 2 ; check if cnt is not one then player 1 wins ; 

import math


def findWinner(n, k):
    cnt = 0
    if (n == 1):
        print("No")
    elif ((n & 1) or n == 2):
        print("Yes")
    else:
        tmp = n
        val = 1
        while (tmp > k and tmp % 2 == 0):
            tmp //= 2
            val *= 2
        for i in range(3, int(math .sqrt(tmp)) + 1):
            while (tmp % i == 0):
                cnt += 1
                tmp //= i
        if (tmp > 1):
            cnt += 1
        if (val == n):
            print("No")
        elif (n / tmp == 2 and cnt == 1):
            print("No")
        else:
            print("Yes")


if __name__ == "__main__":
    n = 1
    k = 1
    findWinner(n, k)

# Expected output:
No
# find all numbers up to n which are both pentagonal and hexagonal | python3 program of the above approach ; function to print numbers upto n which are both pentagonal as well as hexagonal numbers ; calculate i - th pentagonal number ; check if the pentagonal number pn is hexagonal or not ; 

import math


def pen_hex(n):
    pn = 1
    for i in range(1, N):
        pn = (int)(i * (3 * i - 1) / 2)
        if (pn > n):
            break
        seqNum = (1 + math .sqrt(8 * pn + 1)) / 4
        if (seqNum == (int)(seqNum)):
            print(pn, end=", ")


N = 1000000
pen_hex(N)

# Expected output:
1, 40755,
# check if row | function to check if row - major order traversal of the matrix is is palindrome ; loop to check if the matrix is matrix is palindrome or not ; 
def isPal(a, n, m):
    for i in range(0, n // 2):
        for j in range(0, m - 1):
            if (a[i][j] != a[n - 1 - i][m - 1 - j]):
                return False
    return True


if __name__ == '__main__':
    n = 3
    m = 3
    a = [[1, 2, 3], [4, 5, 4], [3, 2, 1]]
    if (isPal(a, n, m)):
        print("YES")
    else:
        print("NO")

# Expected output:
YES
# find the smallest number whose sum of digits is n | function to get sum of digits ; function to find the smallest number whose sum of digits is also n ; checking if number has sum of digits = n ; 
def getSum(n):
    sum1 = 0
    while (n != 0):
        sum1 = sum1 + n % 10
        n = n // 10
    return sum1


def smallestNumber(N):
    i = 1
    while (1):
        if (getSum(i) == N):
            print(i)
            break
        i += 1


N = 10
smallestNumber(N)

# Expected output:
19
# rare numbers | python3 implementation to check if n is a rare number ; iterative function to reverse digits of num ; function to check if n is perfect square ; find floating point value of square root of x . ; if square root is an integer ; function to check if n is an rare number ; find reverse of n ; number should be non - palindromic ; 

import math


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


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


def isRare(N):
    reverseN = reversDigits(N)
    if (reverseN == N):
        return False
    return (isPerfectSquare(N + reverseN) and isPerfectSquare(N - reverseN))


N = 65
if (isRare(N)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# count numbers from range whose prime factors are only 2 and 3 using arrays | set 2 | function which will calculate the elements in the given range ; store the current power of 2 ; store the current power of 3 ; power23 [ ] will store pairwise product of elements of power2 and power3 that are <= r ; insert in power23 ] [ ] only if mul <= r ; store the required answer ; print the result ; 
def calc_ans(l, r):
    power2 = []
    power3 = []
    mul2 = 1
    while (mul2 <= r):
        power2 .append(mul2)
        mul2 *= 2
    mul3 = 1
    while (mul3 <= r):
        power3 .append(mul3)
        mul3 *= 3
    power23 = []
    for x in range(len(power2)):
        for y in range(len(power3)):
            mul = power2[x] * power3[y]
            if (mul == 1):
                continue
            if (mul <= r):
                power23 .append(mul)
    ans = 0
    for x in power23:
        if (x >= l and x <= r):
            ans += 1
    print(ans)


if __name__ == "__main__":
    l = 1
    r = 10
    calc_ans(l, r)

# Expected output:
6
# count of k length subsequence whose product is even | function to calculate ncr ; returns factorial of n ; function for finding number of k length subsequences whose product is even number ; counting odd numbers in the array ; 
def nCr(n, r):
    if (r > n):
        return 0
    return fact(n) // (fact(r) * fact(n - r))


def fact(n):
    res = 1
    for i in range(2, n + 1):
        res = res * i
    return res


def countSubsequences(arr, n, k):
    countOdd = 0
    for i in range(n):
        if (arr[i] & 1):
            countOdd += 1
    ans = nCr(n, k) - nCr(countOdd, k)
    return ans


arr = [2, 4]
K = 1
N = len(arr)
print(countSubsequences(arr, N, K))

# Expected output:
2
# find most significant bit of a number x in base y | python3 program to find the first digit of x in base y ; function to find the first digit of x in base y ; calculating number of digits of x in base y ; finding first digit of x in base y ; 

import math


def first_digit(x, y):
    length = int(math .log(x) / math .log(y) + 1)
    first_digit = x / math .pow(y, length - 1)
    print(int(first_digit))


X = 55
Y = 3
first_digit(X, Y)

# Expected output:
2
# curzon numbers | function to check if a number is a curzon number or not ; find 2 ^ n + 1 ; find 2 * n + 1 ; check for divisibility ; 
def checkIfCurzonNumber(N):
    powerTerm, productTerm = 0, 0
    powerTerm = pow(2, N) + 1
    productTerm = 2 * N + 1
    if (powerTerm % productTerm == 0):
        print("Yes")
    else:
        print("No")


if __name__ == '__main__':
    N = 5
    checkIfCurzonNumber(N)
    N = 10
    checkIfCurzonNumber(N)

# Expected output:
Yes
No
# minimum count of numbers required ending with 7 to sum as a given number | function to return the count of minimum numbers ending with 7 required such that the sum of these numbers is n ; hasharr [ i ] will store the minimum numbers ending with 7 so that it sums to number ending with digit i ; its always possible to write numbers > 69 to write as numbers ending with 7 ; if the number is atleast equal to the sum of minimum numbers ending with 7 ; 
def minCount(n):
    hasharr = [10, 3, 6, 9, 2, 5, 8, 1, 4, 7]
    if (n > 69):
        return hasharr[n % 10]
    else:
        if (n >= hasharr[n % 10] * 7):
            return hasharr[n % 10]
        else:
            return -1


n = 38
print(minCount(n))

# Expected output:
4
# program to print modified binary triangle pattern | function to print the modified binary pattern ; loop to traverse the rows ; loop to traverse the numbers in each row ; check if j is 1 or i in either case print 1 ; else print 0 ; change the cursor to next line after each row ; 
def modifiedBinaryPattern(n):
    for i in range(1, n + 1, 1):
        for j in range(1, i + 1, 1):
            if (j == 1 or j == i):
                print(1, end="")
            else:
                print(0, end="")
        print('', end="")


if __name__ == '__main__':
    n = 7
    modifiedBinaryPattern(n)

# Expected output:
1111011001100011000011000001
# find the real and imaginary part of a complex number | function to find real and imaginary parts of a complex number ; string length stored in variable l ; variable for the index of the separator ; storing the index of '+ ; else storing the index of '- ; finding the real part of the complex number ; finding the imaginary part of the complex number ; 
def findRealAndImag(s):
    l = len(s)
    i = 0
    if (s.find('+') != -1):
        i = s.find('+')
    else:
        i = s.find('-')
    real = s[:i]
    imaginary = s[i + 1:l - 1]

    print("Real part:", real)
    print("Imaginary part:", imaginary)


s = "3+4i"

findRealAndImag(s)

# Expected output:
Real part: 3
Imaginary part: 4
# distinct powers of a number n such that the sum is equal to k | python 3 implementation to find distinct powers of n that add up to k ; function to return the highest power of n not exceeding k ; loop to find the highest power less than k ; initializing the powerarray with all 0 's. ; function to print the distinct powers of n that add upto k ; getting the highest power of n before k ; to check if the power is being used twice or not ; print - 1 if power is being used twice ; if the power is not visited , then mark the power as visited ; decrementing the value of k ; printing the powers of n that sum up to k ; 

from math import pow


def highestPower(n, k):
    i = 0
    a = pow(n, i)
    while (a <= k):
        i += 1
        a = pow(n, i)
    return i - 1


b = [0 for i in range(50)]


def PowerArray(n, k):
    while (k):
        t = highestPower(n, k)
        if (b[t]):
            print(-1)
            return 0
        else:
            b[t] = 1
        k -= pow(n, t)
    for i in range(50):
        if (b[i]):
            print(i, end=', ')


if __name__ == '__main__':
    N = 3
    K = 40
    PowerArray(N, K)

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

N = 100005


def SieveOfEratosthenes(composite):
    for p in range(2, N):
        if p * p > N:
            break
        if (composite[p] == False):
            for i in range(2 * p, N, p):
                composite[i] = True


def sumOfElements(arr, n):
    composite = [False] * N
    SieveOfEratosthenes(composite)
    m = dict()
    for i in range(n):
        m[arr[i]] = m .get(arr[i], 0) + 1
    sum = 0
    for it in m:
        if (composite[m[it]]):
            sum += (it)
    return sum


if __name__ == '__main__':
    arr = [1, 2, 1, 1, 1, 3, 3, 2, 4]
    n = len(arr)
    print(sumOfElements(arr, n))

# Expected output:
1
# delete all odd frequency elements from an array | function that removes the elements which have odd frequencies in the array ; create a map to store the frequency of each element ; remove the elements which have odd frequencies ; if the element has odd frequency then skip ; 
def remove(arr, n):
    m = dict .fromkeys(arr, 0)
    for i in range(n):
        m[arr[i]] += 1
    for i in range(n):
        if ((m[arr[i]] & 1)):
            continue
        print(arr[i], end=", ")


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

# Expected output:
2, 2, 7, 7,
# maximize the first element of the array such that average remains constant | maximum value of the first array element that can be attained ; variable to store the sum ; loop to find the sum of array ; desired maximum value ; 
def getmax(arr, n, x):
    s = 0
    for i in range(n):
        s = s + arr[i]
    print(min(s, x))


if __name__ == '__main__':
    arr = [1, 2, 3, 4]
    x = 5
    arr_size = len(arr)
    getmax(arr, arr_size, x)

# Expected output:
5
# minimum length of the shortest path of a triangle | function to get the minimum length of the shorter side of the triangle ; traversing through each points on the plane ; if sum of a points is greater than the previous one , the maximum gets replaced ; print the length ; 
def shortestLength(n, x, y):
    answer = 0
    i = 0
    while n > 0:
        if (x[i] + y[i] > answer):
            answer = x[i] + y[i]
        i += 1
        n -= 1
    print("Length -> " + str(answer))
    print("Path -> " + "( 1, " + str(answer) + " )" +
          "and ( " + str(answer) + ", 1 )")


if __name__ == "__main__":
    n = 4
    x = [1, 4, 2, 1]
    y = [4, 1, 1, 2]
    shortestLength(n, x, y)

# Expected output:
Length -> 5
Path -> ( 1, 5 )and ( 5, 1 )
# intersecting rectangle when bottom | function to find intersection rectangle of given two rectangles . ; gives bottom - left point of intersection rectangle ; gives top - right point of intersection rectangle ; no intersection ; gives top - left point of intersection rectangle ; gives bottom - right point of intersection rectangle ; 
def FindPoints(x1, y1, x2, y2, x3, y3, x4, y4):
    x5 = max(x1, x3)
    y5 = max(y1, y3)
    x6 = min(x2, x4)
    y6 = min(y2, y4)
    if (x5 > x6 or y5 > y6):
        print("No intersection")
        return
    print("(", x5, ", ", y5, ") ", end=" ")
    print("(", x6, ", ", y6, ") ", end=" ")
    x7 = x5
    y7 = y6
    print("(", x7, ", ", y7, ") ", end=" ")
    x8 = x6
    y8 = y5
    print("(", x8, ", ", y8, ") ")


if __name__ == "__main__":
    x1 = 0
    y1 = 0
    x2 = 10
    y2 = 8
    x3 = 2
    y3 = 3
    x4 = 7
    y4 = 9
    FindPoints(x1, y1, x2, y2, x3, y3, x4, y4)

# Expected output:
( 2 ,  3 )  ( 7 ,  8 )  ( 2 ,  8 )  ( 7 ,  3 )
# find corners of rectangle using mid points | python3 program to find corner points of a rectangle using given length and middle points . ; structure to represent a co - ordinate point ; this function receives two points and length of the side of rectangle and prints the 4 corner points of the rectangle ; horizontal rectangle ; vertical rectangle ; slanted rectangle ; calculate slope of the side ; calculate displacements along axes ; 

import math


class Point:
    def __init__(self, a=0, b=0):
        self .x = a
        self .y = b


def printCorners(p, q, l):
    a, b, c, d = Point(), Point(), Point(), Point()
    if (p .x == q .x):
        a .x = p .x - (l / 2.0)
        a .y = p .y
        d .x = p .x + (l / 2.0)
        d .y = p .y
        b .x = q .x - (l / 2.0)
        b .y = q .y
        c .x = q .x + (l / 2.0)
        c .y = q .y
    elif (p .y == q .y):
        a .y = p .y - (l / 2.0)
        a .x = p .x
        d .y = p .y + (l / 2.0)
        d .x = p .x
        b .y = q .y - (l / 2.0)
        b .x = q .x
        c .y = q .y + (l / 2.0)
        c .x = q .x
    else:
        m = (p .x - q .x) / (q .y - p .y)
        dx = (l / math .sqrt(1 + (m * m))) * 0.5
        dy = m * dx
        a .x = p .x - dx
        a .y = p .y - dy
        d .x = p .x + dx
        d .y = p .y + dy
        b .x = q .x - dx
        b .y = q .y - dy
        c .x = q .x + dx
        c .y = q .y + dy
    print(int(a .x), ", ", int(a .y), sep="")
    print(int(b .x), ", ", int(b .y), sep="")
    print(int(c .x), ", ", int(c .y), sep="")
    print(int(d .x), ", ", int(d .y), sep="")
    print()


p1 = Point(1, 0)
q1 = Point(1, 2)
printCorners(p1, q1, 2)
p = Point(1, 1)
q = Point(-1, -1)
printCorners(p, q, 2 * math .sqrt(2))

# Expected output:
0, 0
0, 2
2, 2
2, 0

0, 2
-2, 0
0, -2
2, 0
# minimize cost to modify the array such that even indices have even elements and vice versa | function to find the minimum cost to modify the array according to the given criteria ; count of wrong positioned odd and even elements ; odd count ; even count ; swapping cost ; decrementing cost after swapping ; only decrementing cost ; return the minimum cost of the two cases ; 
def minimumCost(arr, N, X, Y):
    even_count = 0
    odd_count = 0
    for i in range(0, N):
        if ((arr[i] & 1) and (i % 2 == 0)):
            odd_count += 1
        if ((arr[i] % 2) == 0 and (i & 1)):
            even_count += 1
    cost1 = X * min(odd_count, even_count)
    cost2 = Y * (max(odd_count, even_count) - min(odd_count, even_count))
    cost3 = (odd_count + even_count) * Y
    return min(cost1 + cost2, cost3)


if __name__ == "__main__":
    arr = [5, 3, 7, 2, 1]
    X = 10
    Y = 2
    N = len(arr)
    print(minimumCost(arr, N, X, Y))

# Expected output:
8
# minimum product of maximum and minimum element over all possible subarrays | function to find the minimum product of the minimum and maximum among all the possible subarrays ; stores resultant minimum product ; traverse the given array arr [ ] ; min of product of all two pair of consecutive elements ; return the resultant value ; 
def findMinMax(a):
    min_val = 1000000000
    for i in range(1, len(a)):
        min_val = min(min_val, a[i] * a[i - 1])
    return min_val


if __name__ == ("__main__"):
    arr = [6, 4, 5, 6, 2, 4, 1]
    print(findMinMax(arr))

# Expected output:
4
# find nth item distributed from infinite items of infinite types based on given conditions | function to find the type of the item given out according to the given rules ; stores the count of item given out at each step ; iterate over the days from 1 ; iterate over type of item on that day ; count of items given out should exceed n ; 
def itemType(n):
    count = 0
    day = 1
    while (True):
        for type in range(day, 0, -1):
            count += type
            if (count >= n):
                return type


N = 10
print(itemType(N))

# Expected output:
1
# find the sum of all array elements that are equidistant from two consecutive powers of 2 | python3 program for the above approach ; function to prthe sum of array elements that are equidistant from two consecutive powers of 2 ; stores the resultant sum of the array elements ; traverse the array arr [ ] ; stores the power of 2 of the number arr [ i ] ; stores the number which is power of 2 and lesser than or equal to arr [ i ] ; stores the number which is power of 2 and greater than or equal to arr [ i ] ; if arr [ i ] - lesservalue is the same as largervalue - arr [ i ] ; increment res by arr [ i ] ; return the resultant sum res ; 

from math import log2


def FindSum(arr, N):
    res = 0
    for i in range(N):
        power = int(log2(arr[i]))
        LesserValue = pow(2, power)
        LargerValue = pow(2, power + 1)
        if ((arr[i] - LesserValue) == (LargerValue - arr[i])):
            res += arr[i]
    return res


if __name__ == '__main__':
    arr = [10, 24, 17, 3, 8]
    N = len(arr)
    print(FindSum(arr, N))

# Expected output:
27
# find the person who will finish last | function to find the person who will finish last ; to keep track of rows and columns having 1 ; available rows and columns ; minimum number of choices we have ; if number of choices are odd ; p1 will finish last ; otherwise , p2 will finish last ; 
def findLast(mat):
    m = len(mat)
    n = len(mat[0])
    rows = set()
    cols = set()
    for i in range(m):
        for j in range(n):
            if mat[i][j]:
                rows .add(i)
                cols .add(j)
    avRows = m - len(list(rows))
    avCols = n - len(list(cols))
    choices = min(avRows, avCols)
    if choices & 1:
        print('P1')
    else:
        print('P2')


mat = [[1, 0, 0], [0, 0, 0], [0, 0, 1]]
findLast(mat)

# Expected output:
P1
# sum of decimals that are binary representations of first n natural numbers | python3 program for the above approach ; function to find the sum of first n natural numbers represented in binary representation ; stores the resultant sum ; iterate until the value of n is greater than 0 ; if n is less than 2 ; store the msb position of n ; iterate in the range [ 1 , x ] and add the contribution of the numbers from 1 to ( 2 ^ x - 1 ) ; update the value of the cur and add ; add the cur to ans ; store the remaining numbers ; add the contribution by msb by the remaining numbers ; the next iteration will be repeated for 2 ^ x - 1 ; print the result ; 

from math import log2, pow
MOD = 1000000007


def sumOfBinaryNumbers(n):
    ans = 0
    one = 1
    while (1):
        if (n <= 1):
            ans = (ans + n) % MOD
            break
        x = int(log2(n))
        cur = 0
        add = (one << (x - 1))
        for i in range(1, x + 1, 1):
            cur = (cur + add) % MOD
            add = (add * 10 % MOD)
        ans = (ans + cur) % MOD
        rem = n - (one << x) + 1
        p = pow(10, x)
        p = (p * (rem % MOD)) % MOD
        ans = (ans + p) % MOD
        n = rem - 1
    print(int(ans))


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

# Expected output:
22
# nearest fibonacci number to n | function to find the fibonacci number which is nearest to n ; base case ; initialize the first & second terms of the fibonacci series ; store the third term ; iterate until the third term is less than or equal to num ; update the first ; update the second ; update the third ; store the fibonacci number having smaller difference with n ; print the result ; 
def nearestFibonacci(num):
    if (num == 0):
        print(0)
        return
    first = 0
    second = 1
    third = first + second
    while (third <= num):
        first = second
        second = third
        third = first + second
    if (abs(third - num) >= abs(second - num)):
        ans = second
    else:
        ans = third
    print(ans)


if __name__ == '__main__':
    N = 17
    nearestFibonacci(N)

# Expected output:
13
# permutation of first n natural numbers having given array as the prefix maximum array | python3 program for the above approach ; function to check if the maximum prefix array of ans [ ] is equal to array arr [ ] ; initialize a variable , max ; traverse the array , ans [ ] ; store the maximum value upto index i ; if it is not equal to a [ i ] , then return false ; otherwise return false ; function to find the permutation of the array whose prefix maximum array is same as the given array a [ ] ; stores the required permutation ; stores the index of first occurrence of elements ; traverse the array a [ ] ; if a [ i ] is not present in um , then store it in um ; update the ans [ i ] to a [ i ] ; stores the unvisited numbers ; fill the array , v [ ] ; store the index ; traverse the array , ans [ ] ; fill v [ j ] at places where ans [ i ] is 0 ; check if the current permutation maximum prefix array is same as the given array a [ ] ; if true , the print the permutation ; otherwise , print - 1 ; 

import sys


def checkPermutation(ans, a, n):
    Max = -sys .maxsize - 1
    for i in range(n):
        Max = max(Max, ans[i])
        if (Max != a[i]):
            return False
    return True


def findPermutation(a, n):
    ans = [0] * n
    um = {}
    for i in range(n):
        if (a[i]not in um):
            ans[i] = a[i]
            um[a[i]] = i
    v = []
    j = 0
    for i in range(1, n + 1):
        if (i not in um):
            v .append(i)
    for i in range(n):
        if (ans[i] == 0):
            ans[i] = v[j]
            j += 1
    if (checkPermutation(ans, a, n)):
        for i in range(n):
            print(ans[i], end=" ")
    else:
        print("-1")


if __name__ == "__main__":
    arr = [1, 3, 4, 5, 5]
    N = len(arr)
    findPermutation(arr, N)

# Expected output:
1 3 4 5 2
# count pairs of equal elements possible by excluding each array element once | function to count the number of required pairs for every array element ; initialize a map ; update the frequency of every element ; stores the count of pairs ; traverse the map ; count the number of ways to select pairs consisting of equal elements only ; traverse the array ; print the count for every array element ; 
def countEqualElementPairs(arr, N):
    mp = {}
    for i in range(N):
        if arr[i] in mp:
            mp[arr[i]] += 1
        else:
            mp[arr[i]] = 1
    total = 0
    for key, value in mp .items():
        total += (value * (value - 1)) / 2
    for i in range(N):
        print(int(total - (mp[arr[i]] - 1)), end=" ")


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

# Expected output:
2 2 3 2 3