giulio98/xlcost-single-prompt huggingface dataset:
# check whether given circle resides in boundary maintained by two other circles | python3 program to check whether circle with given co - ordinates reside within the boundary of outer circle and inner circle ; function to check if 

import math


def fitOrNotFit(R, r, x, y, rad):
    val = math .sqrt(math .pow(x, 2) + math .pow(y, 2))
    if (val + rad <= R and val - rad >= R - r):
        print("Fits")
    else:
        print("Doesn't Fit")


R = 8
r = 4
x = 5
y = 3
rad = 3
fitOrNotFit(R, r, x, y, rad)

# Expected output:
Doesn't Fit
# program to find line passing through 2 points | function to find the line given two points ; 
def lineFromPoints(P, Q):
    a = Q[1] - P[1]
    b = P[0] - Q[0]
    c = a * (P[0]) + b * (P[1])
    if (b < 0):
        print(
            "The line passing through points P and Q is:",
            a,
            "x - ",
            b,
            "y = ",
            c,
            "")
    else:
        print(
            "The line passing through points P and Q is: ",
            a,
            "x + ",
            b,
            "y = ",
            c,
            "")


if __name__ == '__main__':
    P = [3, 2]
    Q = [2, 6]
    lineFromPoints(P, Q)

# Expected output:
The line passing through points P and Q is:  4 x +  1 y =  14
# regular polygon using only 1 s in a binary numbered circle | python3 program to find whether a regular polygon is possible in circle with 1 s as vertices ; method returns true if polygon is possible with ' midpoints ' number of midpoints ; loop for getting first vertex of polygon ; loop over array values at ' midpoints ' distance ; and ( & ) all those values , if even one of them is 0 , val will be 0 ; if val is still 1 and ( n / midpoints ) or ( number of vertices ) are more than two ( for a polygon minimum ) print result and return true ; method prints sides in the polygon or print not possible in case of no possible polygon ; limit for iterating over divisors ; if i divides n then i and ( n / i ) will be divisors ; check polygon for both divisors ; 

from math import sqrt


def checkPolygonWithMidpoints(arr, N, midpoints):
    for j in range(midpoints):
        val = 1
        for k in range(j, N, midpoints):
            val &= arr[k]
        if (val and N // midpoints > 2):
            print("Polygon possible with side length", (N // midpoints))
            return True
    return False


def isPolygonPossible(arr, N):
    limit = sqrt(N)
    for i in range(1, int(limit) + 1):
        if (N % i == 0):
            if (checkPolygonWithMidpoints(arr, N, i)
                    or checkPolygonWithMidpoints(arr, N, (N // i))):
                return
    print("Not possiblen")


if __name__ == "__main__":
    arr = [1, 0, 1, 0, 1, 0, 1, 0, 1, 1]
    N = len(arr)
    isPolygonPossible(arr, N)

# Expected output:
Polygon possible with side length 5
# minimum lines to cover all points | utility method to get gcd of a and b ; method returns reduced form of dy / dx as a pair ; get sign of result ; method returns minimum number of lines to cover all points where all lines goes through ( xo , yo ) ; set to store slope as a pair ; loop over all points once ; get x and y co - ordinate of current point ; if this slope is not there in set , increase ans by 1 and insert in set ; 
def gcd(a, b):
    if (b == 0):
        return a
    return gcd(b, a % b)


def getReducedForm(dy, dx):
    g = gcd(abs(dy), abs(dx))
    sign = (dy < 0) ^ (dx < 0)
    if (sign):
        return (-abs(dy) // g, abs(dx) // g)
    else:
        return (abs(dy) // g, abs(dx) // g)


def minLinesToCoverPoints(points, N, xO, yO):
    st = dict()
    minLines = 0
    for i in range(N):
        curX = points[i][0]
        curY = points[i][1]
        temp = getReducedForm(curY - yO, curX - xO)
        if (temp not in st):
            st[temp] = 1
            minLines += 1
    return minLines


xO = 1
yO = 0
points = [[-1, 3], [4, 3], [2, 1], [-1, -2], [3, -3]]
N = len(points)
print(minLinesToCoverPoints(points, N, xO, yO))

# Expected output:
2
# maximum height when coins are arranged in a triangle | returns the square root of n . note that the function ; we are using n itself as initial approximation this can definitely be improved ; e = 0.000001 e decides the accuracy level ; method to find maximum height of arrangement of coins ; calculating portion inside the square root ; 
def squareRoot(n):
    x = n
    y = 1
    e = 0.000001
    while (x - y > e):
        x = (x + y) / 2
        y = n / x
    return x


def findMaximumHeight(N):
    n = 1 + 8 * N
    maxH = (-1 + squareRoot(n)) / 2
    return int(maxH)


N = 12
print(findMaximumHeight(N))

# Expected output:
4
# number of integral points between two points | class to represent an integral point on xy plane . ; utility function to find gcd of two numbers gcd of a and b ; finds the no . of integral points between two given points . ; if line joining p and q is parallel to x axis , then count is difference of y values ; if line joining p and q is parallel to y axis , then count is difference of x values ; 

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


def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)


def getCount(p, q):
    if p .x == q .x:
        return abs(p .y - q .y) - 1
    if p .y == q .y:
        return abs(p .x - q .x) - 1
    return gcd(abs(p .x - q .x), abs(p .y - q .y)) - 1


if __name__ == "__main__":
    p = Point(1, 9)
    q = Point(8, 16)
    print(
        "The number of integral points",
        "between ({}, {}) and ({}, {}) is {}".format(
            p .x,
            p .y,
            q .x,
            q .y,
            getCount(
                p,
                q)))

# Expected output:
The number of integral points between (1, 9) and (8, 16) is 6
# how to check if given four points form a square | a python3 program to check if four given points form a square or not . ; structure of a point in 2d space ; a utility function to find square of distance from point ' p ' to point 'q ; this function returns true if ( p1 , p2 , p3 , p4 ) form a square , otherwise false ; d2 = distsq ( p1 , p2 ) from p1 to p2 d3 = distsq ( p1 , p3 ) from p1 to p3 d4 = distsq ( p1 , p4 ) from p1 to p4 ; if lengths if ( p1 , p2 ) and ( p1 , p3 ) are same , then following conditions must be met to form a square . 1 ) square of length of ( p1 , p4 ) is same as twice the square of ( p1 , p2 ) 2 ) square of length of ( p2 , p3 ) is same as twice the square of ( p2 , p4 ) ; the below two cases are similar to above case ; 

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


def distSq(p, q):
    return (p .x - q .x) * (p .x - q .x) + (p .y - q .y) * (p .y - q .y)


def isSquare(p1, p2, p3, p4):

    d2 = distSq(p1, p2)
    d3 = distSq(p1, p3)
    d4 = distSq(p1, p4)
    if d2 == 0 or d3 == 0 or d4 == 0:
        return False
    if d2 == d3 and 2 * d2 == d4 and 2 * distSq(p2, p4) == distSq(p2, p3):
        return True
    if d3 == d4 and 2 * d3 == d2 and 2 * distSq(p3, p2) == distSq(p3, p4):
        return True
    if d2 == d4 and 2 * d2 == d3 and 2 * distSq(p2, p3) == distSq(p2, p4):
        return True
    return False


if __name__ == "__main__":
    p1 = Point(20, 10)
    p2 = Point(10, 20)
    p3 = Point(20, 20)
    p4 = Point(10, 10)
    if isSquare(p1, p2, p3, p4):
        print('Yes')
    else:
        print('No')

# Expected output:
Yes
# count triplets such that product of two numbers added with third number is n | python program for the above approach ; function to find the divisors of the number ( n - i ) ; stores the resultant count of divisors of ( n - i ) ; iterate over range [ 1 , sqrt ( n ) ] ; return the total divisors ; def to find the number of triplets such that a * b - c = n ; loop to fix the value of c ; adding the number of divisors in count ; return count of triplets ; driver code 

import math


def countDivisors(n):
    divisors = 0
    for i in range(1, math .ceil(math .sqrt(n)) + 1):
        if n % i == 0:
            divisors = divisors + 1
        if (i - (n / i) == 1):
            i = i - 1
    for i in range(math .ceil(math .sqrt(n)) + 1, 1, -1):
        if (n % i == 0):
            divisors = divisors + 1
    return divisors


def possibleTriplets(N):
    count = 0
    for i in range(1, N):
        count = count + countDivisors(N - i)
    return count


if __name__ == "__main__":
    N = 10
    print(possibleTriplets(N))

# Expected output:
23
# maximize count of planes that can be stopped per second with help of given initial position and speed | function to find maximum number of planes that can be stopped from landing ; stores the times needed for landing for each plane ; iterate over the arrays ; stores the time needed for landing of current plane ; update the value of t ; append the t in set st ; return the answer ; 
def maxPlanes(A, B, N):
    St = set()
    for i in range(N):
        t = 1 if (A[i] % B[i] > 0)else 0
        t += (A[i] // B[i]) + t
        St .add(t)
    return len(St)


A = [1, 3, 5, 4, 8]
B = [1, 2, 2, 1, 2]
N = len(A)
print(maxPlanes(A, B, N))

# Expected output:
3
# find the player who will win by choosing a number in range [ 1 , k ] with sum total n | function to predict the winner ; 
def predictTheWinner(K, N):
    if (N % (K + 1) == 0):
        print("Bob")
    else:
        print("Alice")


if __name__ == '__main__':
    K = 7
    N = 50
    predictTheWinner(K, N)

# Expected output:
Alice
# maximize the rightmost element of an array in k operations in linear time | function to calculate maximum value of rightmost element ; initializing ans to store maximum valued rightmost element ; calculating maximum value of rightmost element ; printing rightmost element ; 
def maxRightmostElement(N, k, arr):
    ans = arr[N - 1]
    i = N - 2
    while (i >= 0):
        d = min(arr[i] // 2, k // (N - 1 - i))
        k -= d * (N - 1 - i)
        ans += d * 2
        i -= 1
    print(ans, end=" ")


if __name__ == '__main__':
    N = 4
    k = 5
    arr = [3, 8, 1, 4]
    maxRightmostElement(N, k, arr)

# Expected output:
8
# minimize the maximum element in constructed array with sum divisible by k | function to find smallest maximum number in an array whose sum is divisible by k . ; minimum possible sum possible for an array of size n such that its sum is divisible by k ; if sum is not divisible by n ; if sum is divisible by n ; 
def smallestMaximum(N, K):
    sum = ((N + K - 1) // K) * K
    if (sum % N != 0):
        return (sum // N) + 1
    else:
        return sum // N


if __name__ == "__main__":
    N = 4
    K = 3
    print(smallestMaximum(N, K))

# Expected output:
2
# check if it is possible to construct an array of size n having sum as s and xor value as x | function to find if any sequence is possible or not . ; since , s is greater than equal to x , and either both are odd or even there always exists a sequence ; only one case possible is s == x or not ; considering the above conditions true , check if xor of s ^ ( s - x ) is x or not ; 
def findIfPossible(N, S, X):
    if (S >= X and S % 2 == X % 2):
        if (N >= 3):
            return "Yes"
        if (N == 1):
            if (S == X):
                return "Yes"
            else:
                return "No"
        if (N == 2):
            C = (S - X) // 2
            A = C
            B = C
            A = A + X
            if (((A ^ B) == X)):
                return "Yes"
            else:
                return "No"
    else:
        return "No"


N = 3
S = 10
X = 4
print(findIfPossible(N, S, X))

# Expected output:
Yes
# check whether each array element can be reduced to minimum element by replacing it with remainder with some x | python 3 program for the above approach ; function to check if every integer in the array can be reduced to the minimum array element ; stores the minimum array element ; find the minimum element ; traverse the array arr [ ] ; stores the maximum value in the range ; check whether mini lies in the range or not ; otherwise , return yes ; 

import sys


def isPossible(arr, n):
    mini = sys .maxsize
    for i in range(n):
        mini = min(mini, arr[i])
    for i in range(n):
        if (arr[i] == mini):
            continue
        Max = (arr[i] + 1) // 2 - 1
        if (mini < 0 or mini > Max):
            return "No"
    return "Yes"


if __name__ == '__main__':
    arr = [1, 1, 3, 4]
    N = len(arr)
    print(isPossible(arr, N))

# Expected output:
Yes
# maximum number which can divide all array element after one replacement | function to return gcd1 of two numbers ; if one of numbers is 0 then gcd1 is other number ; if both are equal then that value is gcd1 ; one is greater ; function to return minimum sum ; initialize min_sum with large value ; initialize variable gcd1 ; storing value of arr [ i ] in c ; update maxgcd1 if gcd1 is greater than maxgcd1 ; returning the maximum divisor of all elements ; 
def gcd1OfTwoNos(num1, num2):
    if (num1 == 0):
        return num2
    if (num2 == 0):
        return num1
    if (num1 == num2):
        return num1
    if (num1 > num2):
        return gcd1OfTwoNos(num1 - num2, num2)
    return gcd1OfTwoNos(num1, num2 - num1)


def Min_sum(arr, N):
    min_sum = 1000000
    maxgcd1 = 1
    for i in range(N):
        gcd1 = 1
        if (i == 0):
            gcd1 = arr[1]
        else:
            gcd1 = arr[i - 1]
        for j in range(N):
            if (j != i):
                gcd1 = gcd1OfTwoNos(gcd1, arr[j])
        c = arr[i]
        if (gcd1 > maxgcd1):
            maxgcd1 = gcd1
    return maxgcd1


if __name__ == '__main__':
    arr = [16, 5, 10, 25]
    N = len(arr)
    print(Min_sum(arr, N))

# Expected output:
5
# count of distinct n | python program for the above approach ; function to find the count of distinct odd integers with n digits using the given digits in the array arr [ ] ; stores the factorial of a number calculate the factorial of all numbers from 1 to n ; stores the frequency of each digit ; stores the final answer ; loop to iterate over all values of nth digit i and 1 st digit j ; if digit i does not exist in the given array move to next i ; fixing i as nth digit ; stores the answer of a specific value of i and j ; if digit j does not exist move to the next j ; fixing j as 1 st digit ; calculate number of ways to arrange remaining n - 2 digits ; including j back into the set of digits ; including i back into the set of the digits ; return answer ; 

from array import *
from math import *


def countOddIntegers(arr, N):
    Fact = [0] * N
    Fact[0] = 1
    for i in range(1, N):
        Fact[i] = i * Fact[i - 1]
    freq = [0] * 10
    for i in range(len(freq)):
        freq[i] = 0
    for i in range(N):
        freq[arr[i]] = freq[arr[i]] + 1
    ans = 0
    for i in range(1, 10, 2):
        if (freq[i] == 0):
            continue
        freq[i] = freq[i] - 1
        for j in range(1, 10, 1):
            cur_ans = 0
            if (freq[j] == 0):
                continue
            freq[j] = freq[j] - 1
            cur_ans = Fact[N - 2]
            for k in range(10):
                cur_ans = cur_ans / Fact[freq[k]]
            ans += cur_ans
            freq[j] = freq[j] + 1
        freq[i] = freq[i] + 1
    return ceil(ans)


if __name__ == "__main__":
    A = [2, 3, 4, 1, 2, 3]
    N = len(A)
    print(countOddIntegers(A, N))

# Expected output:
90
# count pairs in an array having sum of elements with their respective sum of digits equal | function to find the sum of digits of the number n ; stores the sum of digits ; if the number n is greater than 0 ; return the sum ; function to find the count of pairs such that arr [ i ] + sumofdigits ( arr [ i ] ) is equal to ( arr [ j ] + sumofdigits ( arr [ j ] ) ; stores the frequency of value of arr [ i ] + sumofdigits ( arr [ i ] ) ; traverse the given array ; find the value ; increment the frequency ; stores the total count of pairs ; traverse the map mp ; update the count of pairs ; return the total count of pairs ; 
def sumOfDigits(N):
    sum = 0
    while (N):
        sum += (N % 10)
        N = N // 10
    return sum


def CountPair(arr, n):
    mp = {}
    for i in range(n):
        val = arr[i] + sumOfDigits(arr[i])
        if val in mp:
            mp[val] += 1
        else:
            mp[val] = 1
    count = 0
    for key, value in mp .items():
        val = key
        times = value
        count += ((times * (times - 1)) // 2)
    return count


if __name__ == '__main__':
    arr = [105, 96, 20, 2, 87, 96]
    N = len(arr)
    print(CountPair(arr, N))

# Expected output:
3
# longest subarray with gcd greater than 1 | function to build the segment tree from the given array to process range queries in log ( n ) time ; termination condition ; find the mid value ; left and right recursive call ; update the segment tree node ; function to return the gcd of the elements of the array from index l to index r ; base case ; find the middle range ; find the gcd and return ; function to print maximum length of the subarray having gcd > one ; stores the segment tree ; function call to build the segment tree from array [ ] arr ; store maximum length of subarray ; starting and ending pointer of the current window ; case where the gcd of the current window is 1 ; update the maximum length ; print answer ; 
def build_tree(b, seg_tree, l, r, vertex):
    if (l == r):
        seg_tree[vertex] = b[l]
        return
    mid = int((l + r) / 2)
    build_tree(b, seg_tree, l, mid, 2 * vertex)
    build_tree(b, seg_tree, mid + 1, r, 2 * vertex + 1)
    seg_tree[vertex] = __gcd(seg_tree[2 * vertex], seg_tree[2 * vertex + 1])


def __gcd(a, b):
    if b == 0:
        return b
    else:
        return __gcd(b, a % b)


def range_gcd(seg_tree, v, tl, tr, l, r):
    if (l > r):
        return 0
    if (l == tl and r == tr):
        return seg_tree[v]
    tm = int((tl + tr) / 2)
    return __gcd(range_gcd(seg_tree, 2 * v, tl, tm, l, min(tm, r)),
                 range_gcd(seg_tree, 2 * v + 1, tm + 1, tr, max(tm + 1, l), r))


def maxSubarrayLen(arr, n):
    seg_tree = [0] * (4 * n + 1)
    build_tree(arr, seg_tree, 0, n - 1, 1)
    maxLen = 0
    l, r = 0, 0
    while (r < n and l < n):
        if (range_gcd(seg_tree, 1, 0, n - 1, l, r) == 1):
            l += 1
        maxLen = max(maxLen, r - l - 1)
        r += 1
    print(maxLen, end="")


arr = [410, 52, 51, 180, 222, 33, 33]
N = len(arr)
maxSubarrayLen(arr, N)

# Expected output:
5
# smallest pair of integers with minimum difference whose bitwise xor is n | python3 program for the above approach ; function to find the numbers a and b whose bitwise xor is n and the difference between them is minimum ; find the msb of the n ; find the value of b ; find the value of a ; print the result ; 

from math import log2


def findAandB(N):
    K = int(log2(N))
    B = (1 << K)
    A = B ^ N
    print(A, B)


if __name__ == '__main__':
    N = 26
    findAandB(N)

# Expected output:
10 16
# find all possible values of k such that the sum of first n numbers starting from k is g | python 3 program for the above approach ; function to find the count the value of k such that sum of the first n numbers from k is g ; stores the total count of k ; iterate till square root of g ; if the number is factor of g ; if the second factor is not equal to first factor ; check if two factors are odd or not ; if second factor is the same as the first factor then check if the first factor is odd or not ; print the resultant count ; 

from math import sqrt


def findValuesOfK(g):
    count = 0
    for i in range(1, int(sqrt(g)) + 1, 1):
        if (g % i == 0):
            if (i != g // i):
                if (i & 1):
                    count += 1
                if ((g // i) & 1):
                    count += 1
            elif (i & 1):
                count += 1
    print(count)


if __name__ == '__main__':
    G = 125
    findValuesOfK(G)

# Expected output:
4
# difference between maximum and minimum average of all k | function to find the difference between the maximum and minimum subarrays of length k ; stores the sum of subarray over the range [ 0 , k ] ; iterate over the range [ 0 , k ] ; store min and max sum ; iterate over the range [ k , n - k ] ; increment sum by arr [ i ] - arr [ i - k ] ; update max and min moving sum ; return difference between max and min average ; 
def Avgdifference(arr, N, K):
    sum = 0
    for i in range(K):
        sum += arr[i]
    min = sum
    max = sum
    for i in range(K, N - K + 2, 1):
        sum += arr[i] - arr[i - K]
        if (min > sum):
            min = sum
        if (max < sum):
            max = sum
    return (max - min) / K


if __name__ == '__main__':
    arr = [3, 8, 9, 15]
    N = len(arr)
    K = 2
    print(Avgdifference(arr, N, K))

# Expected output:
6.5
# count of distinct integers in range [ 1 , n ] that do not have any subset sum as k | function to find maximum number of distinct integers in [ 1 , n ] having no subset with sum equal to k ; declare a vector to store the required numbers ; store all the numbers in [ 1 , n ] except k ; store the maximum number of distinct numbers ; reverse the array ; print the required numbers ; 
def findSet(N, K):
    a = []
    for i in range(1, N + 1):
        if (i != K):
            a .append(i)
    MaxDistinct = (N - K) + (K // 2)
    a = a[::-1]
    for i in range(MaxDistinct):
        print(a[i], end=" ")


if __name__ == '__main__':
    N = 5
    K = 3
    findSet(N, K)

# Expected output:
5 4 2
# solve linear congruences ax = b ( mod n ) for values of x in range [ 0 , n | function to stores the values of x and y and find the value of gcd ( a , b ) ; base case ; store the result of recursive call ; update x and y using results of recursive call ; function to give the distinct solutions of ax = b ( mod n ) ; function call to find the value of d and u ; no solution exists ; else , initialize the value of x0 ; pr all the answers ; 
def ExtendedEuclidAlgo(a, b):
    if a == 0:
        return b, 0, 1
    gcd, x1, y1 = ExtendedEuclidAlgo(b % a, a)
    x = y1 - (b // a) * x1
    y = x1
    return gcd, x, y


def linearCongruence(A, B, N):
    A = A % N
    B = B % N
    u = 0
    v = 0
    d, u, v = ExtendedEuclidAlgo(A, N)
    if (B % d != 0):
        print(-1)
        return
    x0 = (u * (B // d)) % N
    if (x0 < 0):
        x0 += N
    for i in range(d):
        print((x0 + i * (N // d)) % N, end=" ")


A = 15
B = 9
N = 18
linearCongruence(A, B, N)

# Expected output:
15 3 9
# factorial of a number without using multiplication | function to calculate factorial of the number without using multiplication operator ; variable to store the final factorial ; outer loop ; inner loop ; 
def factorialWithoutMul(N):
    ans = N
    i = N - 1
    while (i > 0):
        sum = 0
        for j in range(i):
            sum += ans
        ans = sum
        i -= 1
    return ans


if __name__ == '__main__':
    N = 5
    print(factorialWithoutMul(N))

# Expected output:
120
# sum of bitwise and of all unordered triplets of an array | function to calculate sum of bitwise and of all unordered triplets from a given array such that ( i < j < k ) ; stores the resultant sum of bitwise and of all triplets ; generate all triplets of ( arr [ i ] , arr [ j ] , arr [ k ] ) ; add bitwise and to ans ; print the result ; 
def tripletAndSum(arr, n):
    ans = 0
    for i in range(n):
        for j in range(i + 1, n, 1):
            for k in range(j + 1, n, 1):
                ans += arr[i] & arr[j] & arr[k]
    print(ans)


if __name__ == '__main__':
    arr = [3, 5, 4, 7]
    N = len(arr)
    tripletAndSum(arr, N)

# Expected output:
5