giulio98/xlcost-single-prompt huggingface dataset:
# number of horizontal or vertical line segments to connect 3 points | python program to find number of horizontal ( or vertical line segments needed to connect three points . ; function to check if the third point forms a rectangle with other two points at corners ; returns true if point k can be used as a joining point to connect using two line segments ; check for the valid polyline with two segments ; check whether the x - coordinates or y - cocordinates are same . ; iterate over all pairs to check for two line segments ; otherwise answer is three . ; 

import math


def isBetween(a, b, c):
    return min(a, b) <= c and c <= max(a, b)


def canJoin(x, y, i, j, k):
    return (
        x[k] == x[i] or x[k] == x[j]) and isBetween(
        y[i],
        y[j],
        y[k]) or (
            y[k] == y[i] or y[k] == y[j]) and isBetween(
                x[i],
                x[j],
        x[k])


def countLineSegments(x, y):
    if ((x[0] == x[1] and x[1] == x[2]) or (y[0] == y[1] and y[1] == y[2])):
        return 1
    elif (canJoin(x, y, 0, 1, 2) or canJoin(x, y, 0, 2, 1) or canJoin(x, y, 1, 2, 0)):
        return 2
    else:
        return 3


x = [-1, -1, 4]
y = [-1, 3, 3]
print(countLineSegments(x, y))

# Expected output:
2
# pythagorean quadruple | python code to detect pythagorean quadruples . ; function for checking ; 

import math


def pythagorean_quadruple(a, b, c, d):
    sum = a * a + b * b + c * c
    if (d * d == sum):
        return True
    else:
        return False


a = 1
b = 2
c = 2
d = 3
if (pythagorean_quadruple(a, b, c, d)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# program to find perimeter / circumference of square and rectangle | python3 program to find circumference of a square ; 
def Circumference(a):
    return (4 * a)


a = 5
c = Circumference(a)
print("Circumference of a " + "square is % d" % (c))

# Expected output:
Circumference of a square is  20
# maximum area of quadrilateral | python3 program to find maximum area of a quadrilateral ; calculating the semi - perimeter of the given quadrilateral ; applying brahmagupta 's formula to  get maximum area of quadrilateral ; 

import math


def maxArea(a, b, c, d):
    semiperimeter = (a + b + c + d) / 2
    return math .sqrt((semiperimeter - a) * (semiperimeter - b)
                      * (semiperimeter - c) * (semiperimeter - d))


a = 1
b = 2
c = 1
d = 2
print("%.2f" % maxArea(a, b, c, d))

# Expected output:
2.00
# direction of a point from a line segment | structure for point in cartesian plane . ; constant integers for directions ; subtracting co - ordinates of point a from b and p , to make a as origin ; determining cross product ; return right if cross product is positive ; return left if cross product is negative ; return zero if cross product is zero ; 

class point:
    def __init__(self):
        self .x = 0
        self .y = 0


RIGHT = 1
LEFT = -1
ZERO = 0


def directionOfPoint(A, B, P):
    global RIGHT, LEFT, ZERO
    B .x -= A .x
    B .y -= A .y
    P .x -= A .x
    P .y -= A .y
    cross_product = B .x * P .y - B .y * P .x
    if (cross_product > 0):
        return RIGHT
    if (cross_product < 0):
        return LEFT
    return ZERO


if __name__ == "__main__":
    A = point()
    B = point()
    P = point()
    A .x = -30
    B .x = 29
    P .x = 15
    direction = directionOfPoint(A, B, P)
    if (direction == 1):
        print("Right Direction")
    elif (direction == -1):
        print("Left Direction")
    else:
        print("Point is on the Line")

# Expected output:
Point is on the Line
# find minimum radius such that atleast k point lie inside the circle | return minimum distance required so that aleast k point lie inside the circle . ; finding distance between of each point from origin ; sorting the distance ; 
def minRadius(k, x, y, n):
    dis = [0] * n
    for i in range(0, n):
        dis[i] = x[i] * x[i] + y[i] * y[i]
    dis .sort()
    return dis[k - 1]


k = 3
x = [1, -1, 1]
y = [1, -1, -1]
n = len(x)
print(minRadius(k, x, y, n))

# Expected output:
2
# program for area of square | python3 code to find the area of the square ; 
def areaSquare(side):
    area = side * side
    return area


side = 4
print(areaSquare(side))

# Expected output:
16
# minimum perimeter of n blocks | python3 program to find minimum perimeter using n blocks . ; if n is a perfect square ; number of rows ; perimeter of the rectangular grid ; if there are blocks left ; 

import math


def minPerimeter(n):
    l = math .sqrt(n)
    sq = l * l
    if (sq == n):
        return l * 4
    else:
        row = n / l
        perimeter = 2 * (l + row)
        if (n % l != 0):
            perimeter += 2
        return perimeter


n = 10
print(int(minPerimeter(n)))

# Expected output:
14
# Find if it 's possible to rotate the page by an angle or not. | Function to find if it 's possible to rotate page or not ; Calculating distance b / w points ; If distance is not equal ; If the points are in same line ; Points a , b , and c
def possibleOrNot(a1, a2, b1, b2, c1, c2):
    dis1 = (pow(b1 - a1, 2) + pow(b2 - a2, 2))
    dis2 = (pow(c1 - b1, 2) + pow(c2 - b2, 2))
    if (dis1 != dis2):
        print("No")
    elif (b1 == ((a1 + c1) // 2.0) and b2 == ((a2 + c2) // 2.0)):
        print("No")
    else:
        print("Yes")


a1, b1, c1 = 1, 2, 3
a2 = b2 = c2 = 0
possibleOrNot(a1, a2, b1, b2, c1, c2)

# Expected output:
No
# check if two given circles touch or intersect each other | python3 program to check if two circles touch each other or not . ; 
def circle(x1, y1, x2, y2, r1, r2):
    distSq = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)
    radSumSq = (r1 + r2) * (r1 + r2)
    if (distSq == radSumSq):
        return 1
    elif (distSq > radSumSq):
        return -1
    else:
        return 0


x1 = -10
y1 = 8
x2 = 14
y2 = -24
r1 = 30
r2 = 10
t = circle(x1, y1, x2, y2, r1, r2)
if (t == 1):
    print("Circle touch to each other.")
elif (t < 0):
    print("Circle not touch to each other.")
else:
    print("Circle intersect to each other.")

# Expected output:
Circle touch to each other.
# count of obtuse angles in a circle with ' k ' equidistant points between 2 given points | c ++ program to count number of obtuse angles for given two points . ; there are two arcs connecting a and b . let us count points on both arcs . ; both arcs have same number of points ; points on smaller arc is answer ; 
def countObtuseAngles(a, b, k):
    c1 = (b - a) - 1
    c2 = (k - b) + (a - 1)
    if (c1 == c2):
        return 0
    return min(c1, c2)


k, a, b = 6, 1, 3
print(countObtuseAngles(a, b, k))

# Expected output:
1
# count of acute , obtuse and right triangles with given sides | find the number of acute , right , obtuse triangle that can be formed from given array . ; finding the square of each element of array ; sort the sides of array and their squares . ; x for acute triangles y for right triangles z for obtuse triangles ; finding the farthest point p where a ^ 2 + b ^ 2 >= c ^ 2. ; finding the farthest point q where a + b > c . ; if point p make right triangle . ; all triangle between j and p are acute triangles . so add p - j - 1 in x . ; increment y by 1. ; all triangle between q and p are acute triangles . so add q - p in z . ; if no right triangle ; all triangle between j and p are acute triangles . so add p - j in x . ; all triangle between q and p are acute triangles . so add q - p in z . ; 
def findTriangle(a, n):
    b = []
    for i in range(n):
        b .append(a[i] * a[i])
    a .sort()
    b .sort()
    x, y, z = 0, 0, 0
    for i in range(n):
        p = i + 1
        q = i + 1
        for j in range(i + 1, n):
            while (p < n - 1 and b[i] + b[j] >= b[p + 1]):
                p += 1
            q = max(q, p)
            while (q < n - 1 and a[i] + a[j] > a[q + 1]):
                q += 1
            if (b[i] + b[j] == b[p]):
                x += max(p - j - 1, 0)
                y += 1
                z += q - p
            else:
                x += max(p - j, 0)
                z += q - p
    print("Acute Triangle:", x)
    print("Right Triangle:", y)
    print("Obtuse Triangle:", z)


if __name__ == "__main__":
    arr = [2, 3, 9, 10, 12, 15]
    n = len(arr)
    findTriangle(arr, n)

# Expected output:
Acute Triangle: 2
Right Triangle: 1
Obtuse Triangle: 4
# check whether a point exists in circle sector or not . | python3 program to check if a point lies inside a circle sector . ; calculate endangle ; calculate polar co - ordinates ; check whether polarradius is less then radius of circle or not and angle is between startangle and endangle or not ; 

import math


def checkPoint(radius, x, y, percent, startAngle):
    endAngle = 360 / percent + startAngle
    polarradius = math .sqrt(x * x + y * y)
    Angle = math .atan(y / x)
    if (Angle >= startAngle and Angle <= endAngle and polarradius < radius):
        print("Point (", x, ",", y, ") " "exist in the circle sector")
    else:
        print("Point (", x, ",", y, ") " "does not exist in the circle sector")


radius, x, y = 8, 3, 4
percent, startAngle = 12, 0
checkPoint(radius, x, y, percent, startAngle)

# Expected output:
Point ( 3 , 4 ) exist in the circle sector
# area of a polygon with given n ordered vertices | ( x [ i ] , y [ i ] ) are coordinates of i 'th point. ; initialize area ; calculate value of shoelace formula ; return absolute value ; 
def polygonArea(X, Y, n):
    area = 0.0
    j = n - 1
    for i in range(0, n):
        area += (X[j] + X[i]) * (Y[j] - Y[i])
    return int(abs(area / 2.0))


X = [0, 2, 4]
Y = [1, 3, 7]
n = len(X)
print(polygonArea(X, Y, n))

# Expected output:
24
# difference of count of distinct elements present to left and right for each array element | function to find the difference of count of distince elements to the left and right for each array elements ; stores distinct array element in the left and right ; traverse the array ; insert all element to the left in the set s1 ; insert all element to the right in the set s2 ; print the difference ; 
def findDifferenceArray(arr, N):
    S1 = set()
    S2 = set()
    for i in range(N):
        for j in range(i):
            S1 .add(arr[j])
        for j in range(i + 1, N):
            S2 .add(arr[j])
        print(abs(len(S1) - len(S2)), end=' ')
        S1 .clear()
        S2 .clear()


if __name__ == "__main__":
    arr = [7, 7, 3, 2, 3]
    N = len(arr)
    findDifferenceArray(arr, N)

# Expected output:
3 1 1 1 3
# maximum subarray sum with same first and last element formed by removing elements | python3 program for the above approach ; function to find the maximum sum of sub - array ; initialize the variables ; traverse over the range ; add the current value to the variable currsum for prefix sum ; calculate the result ; return the answer ; 

import sys


def maximumSubarraySum(arr):
    N = len(arr)
    memo = {}
    res = -(sys .maxsize - 1)
    currsum = 0
    currval = 0
    for i in range(N):
        currval = arr[i]
        currsum = currsum + currval
        if currval in memo:
            if (currval > 0):
                res = max(res, currsum - memo[currval] + currval)
            else:
                res = max(res, currsum - memo[currval] + 2 * currval)
        else:
            memo[currval] = currsum
        if (currval < 0):
            currsum = currsum - currval
    return res


if __name__ == "__main__":
    arr = [-1, -3, 4, 0, -1, -2]
    print(maximumSubarraySum(arr))

# Expected output:
2
# find nth root of a number using bisection method | function that returns the value of the function at a given value of x ; calculating the value of the differential of the function ; the function that returns the root of given number ; defining range on which answer can be found ; finding mid value ; 
def f(x, p, num):
    return pow(x, p) - num


def f_prime(x, p):
    return p * pow(x, p - 1)


def root(num, p):
    left = -num
    right = num
    x = 0
    while (True):
        x = (left + right) / 2.0
        value = f(x, p, num)
        prime = f_prime(x, p)
        if (value * prime <= 0):
            left = x
        else:
            right = x
        if (value < 0.000001 and value >= 0):
            return x


if __name__ == "__main__":
    P = 1234321
    N = 2
    ans = root(P, N)
    print(ans)

# Expected output:
1111.0000000004495
# count of permutations of an array having maximum mexs sum of prefix arrays | to calculate the factorial ; to return the number of permutations of an array with maximum mexs sum of prefix array ; map to store the frequency of each element ; running a loop from i = 0 to i < n ; if continuity breaks , then break the loop ; considering choices available to be filled at this position , i . e . mp [ i ] ; decrement the count of remaining right elements ; adding all permutations of the elements present to the right of the point where continuity breaks . ; 
def factorial(n):
    res = 1
    for i in range(2, n + 1):
        res *= i
    return res


def countPermutations(ar, n):
    mp = dict()
    ans = 1
    cnt = n
    for i in range(n):
        if (ar[i] in mp):
            mp[ar[i]] += 1
        else:
            mp[ar[i]] = 1
    for i in range(n):
        if (i not in mp):
            break
        ans = (ans * mp[i])
        cnt -= 1
    ans = ans * factorial(cnt)
    return ans


arr = [1, 0, 1]
N = len(arr)
print(countPermutations(arr, N))

# Expected output:
2
# convert 0 to n by adding 1 or multiplying by 2 in minimum steps | function to count number of set bits in n ; stores the count of set bits ; if n is odd , then it a set bit ; return the result ; 
def minimumAdditionOperation(N):
    count = 0
    while (N):
        if (N & 1 == 1):
            count += 1
        N = N >> 1
    return count


if __name__ == "__main__":
    N = 6
    print(minimumAdditionOperation(N))

# Expected output:
2
# minimum divide by 2 operations required to make gcd odd for given array | python program for the above approac ; function to find the minimum number of operations to make the gcd of the array odd ; stores the minimum operations required ; stores the powers of two for the current array element ; dividing by 2 ; increment the count ; update the minimum operation required ; return the result required ; 

INT_MAX = 2147483647


def minimumOperations(arr, N):
    mini = INT_MAX
    for i in range(0, N):
        count = 0
        while (arr[i] % 2 == 0):
            arr[i] = arr[i] // 2
            count += 1
        if (mini > count):
            mini = count
    return mini


if __name__ == "__main__":
    arr = [4, 6]
    N = len(arr)
    print(minimumOperations(arr, N))

# Expected output:
1
# minimum size of the array with mex as a and xor of the array elements as b | function to find the minimum size of array with given mex and xor ; find the xor of values from 0 to a - 1 ; if a is a multiple of 4 ; if a % 4 gives remainder 1 ; if a % 4 gives remainder 2 ; initializing array size by a ; if xor of all values of array is equal to b ; if the required integer to be added is equal to a ; else any integer can be added ; 
def minimumSizeArr(A, B):
    currXor = 0
    reminder = (A - 1) % 4
    if (reminder == 0):
        currXor = A - 1
    elif (reminder == 1):
        currXor = 1
    elif (reminder == 2):
        currXor = A
    minSize = A
    if (currXor == B):
        return minSize
    elif (currXor ^ B == A):
        return minSize + 2
    else:
        return minSize + 1


if __name__ == "__main__":
    A = 1
    B = 999
    print(minimumSizeArr(A, B))

# Expected output:
2
# count of triplets till n whose product is at most n | function to find number of triplets ( a , b , c ) having a * b * c <= n ; stores the count of triplets ; iterate a loop fixing the value of a ; iterate a loop fixing the value of a ; find the total count of triplets and add it to cnt ; return the total triplets formed ; 
def countTriplets(N):
    cnt = 0
    for A in range(1, N + 1):
        for B in range(1, N // A + 1):
            cnt += N // (A * B)
    return cnt


if __name__ == "__main__":
    N = 2
    print(countTriplets(N))

# Expected output:
4
# largest integer upto n having greatest prime factor greater than its square root | python program for the above approach ; stores the greatest prime factor ; modified sieve to find the greatest prime factor of all integers in the range [ 1 , maxn ] ; initialize the array with 0 ; iterate through all values of i ; if i is not a prime number ; update the multiples of i ; function to find integer in the range [ 1 , n ] such that its greatest prime factor is greater than its square root ; iterate through all values of i in the range [ n , 1 ] ; if greatest prime factor of i is greater than its sqare root ; return answer ; if no valid integer exist ; 

import math
maxn = 100001
gpf = [0 for _ in range(maxn)]


def modifiedSieve():
    gpf[0] = 0
    gpf[1] = 1
    for i in range(2, maxn):
        if (gpf[i] > 0):
            continue
        for j in range(i, maxn, i):
            gpf[j] = max(i, gpf[j])


def greatestValidInt(N):
    modifiedSieve()
    for i in range(N, 0, -1):
        if (gpf[i] > math .sqrt(i)):
            return i
    return -1


if __name__ == "__main__":
    N = 25
    print(greatestValidInt(N))

# Expected output:
23
# find subfactorial of a number | function to find the subfactorial of the number ; initialize variables ; iterating over range n ; fact variable store factorial of the i ; if count is even ; increase the value of count by 1 ; 
def subfactorial(N):
    res = 0
    fact = 1
    count = 0
    for i in range(1, N + 1):
        fact = fact * i
        if (count % 2 == 0):
            res = res - (1 / fact)
        else:
            res = res + (1 / fact)
        count += 1
    return fact * (1 + res)


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

# Expected output:
9.0
# minimize operations to delete all elements of permutation a by removing a subsequence having order as array b | function to find the minimum number of operations to delete all elements of permutation a in order described by b ; stores the count of operations ; stores the index of current integer in b to be deleted ; loop to iterate over all values of b ; stores the current index in a ; iterate over all values a ; if current integer of b and a equal , increment the index of the current integer of b ; as the permutation a has been traversed completely , increment the count of operations by 1 ; return answer ; 
def minOperations(A, B, N):
    cnt = 0
    i = 0
    while (i < N):
        j = 0
        while (j < N and i < N):
            if (B[i] == A[j]):
                i += 1
            j += 1
        cnt += 1
    return cnt


if __name__ == '__main__':
    A = [2, 4, 6, 1, 5, 3]
    B = [6, 5, 4, 2, 3, 1]
    N = len(A)
    print(minOperations(A, B, N))

# Expected output:
4
# count of pair of integers ( x , y ) such that difference between square of x and y is a perfect square | python program for the above approach ; function to find number of pairs ( x , y ) such that x ^ 2 - y is a square number ; stores the count of total pairs ; iterate q value 1 to sqrt ( n ) ; maximum possible value of p is min ( 2 * n - q , n / q ) ; p must be greater than or equal to q ; total number of pairs are ; adding all valid pairs to res ; return total no of pairs ( x , y ) ; 

import math


def countPairs(N):
    res = 0
    for q in range(1, int(math .sqrt(N)) + 1):
        maxP = min(2 * N - q, N // q)
        if (maxP < q):
            continue
        cnt = maxP - q + 1
        res += (cnt // 2 + (cnt & 1))
    return res


if __name__ == "__main__":
    N = 3
    print(countPairs(N))

# Expected output:
2
# maximum size of subset such that product of all subset elements is a factor of n | function to find the maximum size of the subset such that the product of subset elements is a factor of n ; base case ; stores maximum size of valid subset ; traverse the given array ; if n % arr [ i ] = 0 , include arr [ i ] in a subset and recursively call for the remaining array integers ; return answer ; 
def maximizeSubset(N, arr, M, x=0):
    if (x == M):
        return 0
    ans = 0
    for i in range(x, M):
        if (N % arr[i] == 0):
            ans = max(ans, maximizeSubset(N // arr[i], arr, M, x + 1) + 1)
    return ans


N = 64
arr = [1, 2, 4, 8, 16, 32]
M = len(arr)
print(maximizeSubset(N, arr, M))

# Expected output:
4
# count of distinct sums formed by n numbers taken form range [ l , r ] | function to find total number of different sums of n numbers in the range [ l , r ] ; to store minimum possible sum with n numbers with all as l ; to store maximum possible sum with n numbers with all as r ; all other numbers in between maxsum and minsum can also be formed so numbers in this range is the final answer ; 
def countDistinctSums(N, L, R):
    minSum = L * N
    maxSum = R * N
    return maxSum - minSum + 1


if __name__ == "__main__":
    N = 2
    L = 1
    R = 3
    print(countDistinctSums(N, L, R))

# Expected output:
5
# make the array sum 0 by using either ceil or floor on each element | python 3 program for the above approach ; function to modify the array element such that the sum is close to 0 ; stores the modified elements ; stores the sum of array element ; stores minimum size of the array ; traverse the array and find the sum ; if sum is positive ; find the minimum number of elements that must be changed ; iterate until m elements are modified or the array end ; update the current array elements to its floor ; print the resultant array ; 

import sys
from math import ceil, floor


def setSumtoZero(arr, N):
    A = [0 for i in range(N)]
    sum = 0
    m = -sys .maxsize - 1
    for i in range(N):
        sum += ceil(arr[i])
        A[i] = ceil(arr[i])
    if (sum > 0):
        m = min(sum, N)
        i = 0
        while (i < N and m > 0):
            A[i] = floor(arr[i])
            if (A[i] != floor(arr[i])):
                m -= 1
            i += 1
    for i in range(N):
        print(A[i], end=" ")


if __name__ == '__main__':
    arr = [-2, -2, 4.5]
    N = len(arr)
    setSumtoZero(arr, N)

# Expected output:
-2 -2 4
# find first undeleted integer from k to n in given unconnected graph after performing q queries | function to perform th get operation of disjoint set union ; function to perform the union operation of dijoint set union ; update the graph [ a ] as b ; function to perform given queries on set of vertices initially not connected ; stores the vertices ; mark every vertices rightmost vertex as i ; traverse the queries array ; check if it is first type of the givan query ; get the parent of a ; print the answer for the second query ; 
def Get(graph, a):
    if graph[graph[a]] != graph[a]:
        graph[a] = Get(graph, graph[a])
    else:
        return graph[a]


def Union(graph, a, b):
    a = Get(graph, a)
    b = Get(graph, b)
    graph[a] = b


def Queries(queries, N, M):
    graph = [0 for i in range(N + 2)]
    for i in range(1, N + 2, 1):
        graph[i] = i
    for query in queries:
        if (query[0] == 1):
            Union(graph, query[1], query[1] + 1)
        else:
            a = Get(graph, query[1])
            if (a == N + 1):
                print(-1)
            else:
                print(graph[a], end=" ")


if __name__ == '__main__':
    N = 5
    queries = [[2, 1], [1, 1], [2, 1], [2, 3]]
    Q = len(queries)
    Queries(queries, N, Q)

# Expected output:
1 2 3