giulio98/xlcost-single-prompt huggingface dataset:
# k | python3 program to find kth element from two sorted arrays ; in case we have reached end of array 1 ; in case we have reached end of array 2 ; k should never reach 0 or exceed sizes of arrays ; compare first elements of arrays and return ; size of array 1 is less than k / 2 ; last element of array 1 is not kth we can directly return the ( k - m ) th element in array 2 ; size of array 2 is less than k / 2 ; normal comparison , move starting index of one array k / 2 to the right ; 
def kth(arr1, arr2, m, n, k, st1=0, st2=0):
    if (st1 == m):
        return arr2[st2 + k - 1]
    if (st2 == n):
        return arr1[st1 + k - 1]
    if (k == 0 or k > (m - st1) + (n - st2)):
        return -1
    if (k == 1):
        if (arr1[st1] < arr2[st2]):
            return arr1[st1]
        else:
            return arr2[st2]
    curr = int(k / 2)
    if (curr - 1 >= m - st1):
        if (arr1[m - 1] < arr2[st2 + curr - 1]):
            return arr2[st2 + (k - (m - st1) - 1)]
        else:
            return kth(arr1, arr2, m, n, k - curr, st1, st2 + curr)
    if (curr - 1 >= n - st2):
        if (arr2[n - 1] < arr1[st1 + curr - 1]):
            return arr1[st1 + (k - (n - st2) - 1)]
        else:
            return kth(arr1, arr2, m, n, k - curr, st1 + curr, st2)
    else:
        if (arr1[curr + st1 - 1] < arr2[curr + st2 - 1]):
            return kth(arr1, arr2, m, n, k - curr, st1 + curr, st2)
        else:
            return kth(arr1, arr2, m, n, k - curr, st1, st2 + curr)


arr1 = [2, 3, 6, 7, 9]
arr2 = [1, 4, 8, 10]
k = 5
print(kth(arr1, arr2, 5, 4, k))

# Expected output:
6
# search element in a sorted matrix | python3 implementation to search an element in a sorted matrix ; this function does binary search for x in i - th row . it does the search from mat [ i ] [ j_low ] to mat [ i ] [ j_high ] ; element found ; element not found ; function to perform binary search on the mid values of row to get the desired pair of rows where the element can be found ; single row matrix ; do binary search in middle column . condition to terminate the loop when the 2 desired rows are found ; element found ; if element is present on the mid of the two rows ; search element on 1 st half of 1 st row ; search element on 2 nd half of 1 st row ; search element on 1 st half of 2 nd row ; search element on 2 nd half of 2 nd row ; 

MAX = 100


def binarySearch(mat, i, j_low, j_high, x):
    while (j_low <= j_high):
        j_mid = (j_low + j_high) // 2
        if (mat[i][j_mid] == x):
            print("Found at (", i, ", ", j_mid, ")")
            return
        elif (mat[i][j_mid] > x):
            j_high = j_mid - 1
        else:
            j_low = j_mid + 1
    print("Element no found")


def sortedMatrixSearch(mat, n, m, x):
    if (n == 1):
        binarySearch(mat, 0, 0, m - 1, x)
        return
    i_low = 0
    i_high = n - 1
    j_mid = m // 2
    while ((i_low + 1) < i_high):
        i_mid = (i_low + i_high) // 2
        if (mat[i_mid][j_mid] == x):
            print("Found at (", i_mid, ", ", j_mid, ")")
            return
        elif (mat[i_mid][j_mid] > x):
            i_high = i_mid
        else:
            i_low = i_mid
    if (mat[i_low][j_mid] == x):
        print("Found at (", i_low, ",", j_mid, ")")
    elif (mat[i_low + 1][j_mid] == x):
        print("Found at (", (i_low + 1), ", ", j_mid, ")")
    elif (x <= mat[i_low][j_mid - 1]):
        binarySearch(mat, i_low, 0, j_mid - 1, x)
    elif (x >= mat[i_low][j_mid + 1] and x <= mat[i_low][m - 1]):
        binarySearch(mat, i_low, j_mid + 1, m - 1, x)
    elif (x <= mat[i_low + 1][j_mid - 1]):
        binarySearch(mat, i_low + 1, 0, j_mid - 1, x)
    else:
        binarySearch(mat, i_low + 1, j_mid + 1, m - 1, x)


if __name__ == "__main__":
    n = 4
    m = 5
    x = 8
    mat = [[0, 6, 8, 9, 11], [20, 22, 28, 29, 31], [
        36, 38, 50, 61, 63], [64, 66, 100, 122, 128]]
    sortedMatrixSearch(mat, n, m, x)

# Expected output:
Found at ( 0 , 2 )
# minimum difference between adjacent elements of array which contain elements from each row of a matrix | return smallest element greater than or equal to the current element . ; return the minimum absolute difference adjacent elements of array ; arr = [ 0 for i in range ( r ) ] [ for j in range ( c ) ] sort each row of the matrix . ; for each matrix element ; search smallest element in the next row which is greater than or equal to the current element ; largest element which is smaller than the current element in the next row must be just before smallest element which is greater than or equal to the current element because rows are sorted . ; 
def bsearch(low, high, n, arr):
    mid = (low + high)//2

    if (low <= high):
        if (arr[mid] < n):
            return bsearch(mid + 1, high, n, arr)
        return bsearch(low, mid - 1, n, arr)

    return low


def mindiff(arr, n, m):
    for i in range(n):
        sorted(arr)

    ans = 2147483647
    for i in range(n-1):
        for j in range(m):
            p = bsearch(0, m-1, arr[i][j], arr[i + 1])
            ans = min(ans, abs(arr[i + 1][p] - arr[i][j]))
            if (p-1 >= 0):
                ans = min(ans, abs(arr[i + 1][p - 1] - arr[i][j]))
    return ans


m = [8, 5], [6, 8]
print(mindiff(m, 2, 2))

# Expected output:
0
# find bitonic point in given bitonic sequence | function to find bitonic point using binary search ; base condition to check if arr [ mid ] is bitonic point or not ; we assume that sequence is bitonic . we go to right subarray if middle point is part of increasing subsequence . else we go to left subarray . ; 
def binarySearch(arr, left, right):
    if (left <= right):
        mid = (left + right) // 2
        if (arr[mid - 1] < arr[mid] and arr[mid] > arr[mid + 1]):
            return mid
        if (arr[mid] < arr[mid + 1]):
            return binarySearch(arr, mid + 1, right)
        else:
            return binarySearch(arr, left, mid - 1)
    return -1


arr = [6, 7, 8, 11, 9, 5, 2, 1]
n = len(arr)
index = binarySearch(arr, 1, n - 2)
if (index != -1):
    print(arr[index])

# Expected output:
11
# find the only repeating element in a sorted array of size n | returns index of second appearance of a repeating element the function assumes that array elements are in range from 1 to n - 1. ; low = 0 , high = n - 1 ; check if the mid element is the repeating one ; if mid element is not at its position that means the repeated element is in left ; if mid is at proper position then repeated one is in right . ; 
def findRepeatingElement(arr, low, high):
    if low > high:
        return -1

    mid = (low + high) // 2
    if (arr[mid] != mid + 1):

        if (mid > 0 and arr[mid] == arr[mid-1]):
            return mid
        return findRepeatingElement(arr, low, mid-1)
    return findRepeatingElement(arr, mid+1, high)


arr = [1, 2, 3, 3, 4, 5]
n = len(arr)
index = findRepeatingElement(arr, 0, n - 1)
if (index != -1):
    print(arr[index])

# Expected output:
3
# find cubic root of a number | returns the absolute value of n - mid * mid * mid ; returns cube root of a no n ; set start and end for binary search ; set precision ; if error is less than e then mid is our answer so return mid ; if mid * mid * mid is greater than n set end = mid ; if mid * mid * mid is less than n set start = mid ; 
def diff(n, mid):
    if (n > (mid * mid * mid)):
        return (n - (mid * mid * mid))
    else:
        return ((mid * mid * mid) - n)


def cubicRoot(n):
    start = 0
    end = n
    e = 0.0000001
    while (True):
        mid = (start + end) / 2
        error = diff(n, mid)
        if (error <= e):
            return mid
        if ((mid * mid * mid) > n):
            end = mid
        else:
            start = mid


n = 3
print("Cubic root of", n, "is", round(cubicRoot(n), 6))

# Expected output:
Cubic root of 3 is 1.44225
# find frequency of each element in a limited range array in less than o ( n ) time | a recursive function to count number of occurrences for each element in the array without traversing the whole array ; if element at index low is equal to element at index high in the array ; increment the frequency of the element by count of elements between high and low ; find mid and recurse for left and right subarray ; a wrapper over recursive function findfrequencyutil ( ) . it print number of occurrences of each element in the array . ; create a empty vector to store frequencies and initialize it by 0. size of vector is maximum value ( which is last value in sorted array ) plus 1. ; fill the vector with frequency ; print the frequencies ; 
def findFrequencyUtil(arr, low, high, freq):
    if (arr[low] == arr[high]):
        freq[arr[low]] += high - low + 1
    else:
        mid = int((low + high) / 2)
        findFrequencyUtil(arr, low, mid, freq)
        findFrequencyUtil(arr, mid + 1, high, freq)


def findFrequency(arr, n):
    freq = [0 for i in range(n - 1 + 1)]
    findFrequencyUtil(arr, 0, n - 1, freq)
    for i in range(0, arr[n - 1] + 1, 1):
        if (freq[i] != 0):
            print("Element", i, "occurs", freq[i], "times")


if __name__ == '__main__':
    arr = [1, 1, 1, 2, 3, 3, 5, 5, 8, 8, 8, 9, 9, 10]
    n = len(arr)
    findFrequency(arr, n)

# Expected output:
Element 1 occurs 3 times
Element 2 occurs 1 times
Element 3 occurs 2 times
Element 5 occurs 2 times
Element 8 occurs 3 times
Element 9 occurs 2 times
Element 10 occurs 1 times
# square root of an integer | returns floor of square root of x ; base cases ; starting from 1 , try all numbers until i * i is greater than or equal to x . ; 
def floorSqrt(x):
    if (x == 0 or x == 1):
        return x
    i = 1
    result = 1
    while (result <= x):
        i += 1
        result = i * i
    return i - 1


x = 11
print(floorSqrt(x))

# Expected output:
3
# maximum number of overlapping rectangles with at least one common point | function to find the maximum number of overlapping rectangles ; stores the maximum count of overlapping rectangles ; stores the x and y coordinates ; iterate over all pairs of xs and ys ; store the count for the current x and y ; update the maximum count of rectangles ; returns the total count ; 
def maxOverlappingRectangles(x1, y1, x2, y2, N):
    max_rectangles = 0
    X = []
    Y = []
    for i in range(0, N):
        X .append(x1[i])
        X .append(x2[i] - 1)
        Y .append(y1[i])
        Y .append(y2[i] - 1)
    for i in range(0, len(X)):
        for j in range(0, len(Y)):
            cnt = 0
            for k in range(0, N):
                if (X[i] >= x1[k] and X[i] + 1 <= x2[k]
                        and Y[j] >= y1[k] and Y[j] + 1 <= y2[k]):
                    cnt += 1
            max_rectangles = max(max_rectangles, cnt)
    print(max_rectangles)


if __name__ == "__main__":
    x1 = [0, 50]
    y1 = [0, 50]
    x2 = [100, 60]
    y2 = [100, 60]
    N = len(x1)
    maxOverlappingRectangles(x1, y1, x2, y2, N)

# Expected output:
2
# check if it is possible to reach the point ( x , y ) using distances given in an array | python program for the above approach ; function to check if the po ( x , y ) is reachable from ( 0 , 0 ) or not ; find the euclidian distance ; calculate the maximum distance ; case 1. ; case 2. ; otherwise , check for the polygon condition for each side ; otherwise , pryes ; 

import math


def isPossibleToReach(A, N, X, Y):
    distance = math .sqrt(X * X + Y * Y)
    mx = 0
    for i in range(N):
        mx += A[i]
    if (mx < distance):
        print("NO")
        return 0
    if ((mx - distance) < 0.000001):
        print("YES")
        return 0
    for i in range(N):
        if (distance + mx < (2) * (A[i])):
            print("No")
            return 0
    print("Yes")
    return 0


A = [2, 5]
X = 5
Y = 4
N = len(A)
isPossibleToReach(A, N, X, Y)

# Expected output:
Yes
# check if it is possible to reach ( x , y ) from origin such that in each ith move increment x or y coordinate with 3 ^ i | function to find whether ( 0 , 0 ) can be reached from ( x , y ) by decrementing 3 ^ i at each ith step ; termination condition ; otherwise , recursively call by decrementing 3 ^ i at each step ; 
def canReach(X, Y, steps):
    if (X == 0 and Y == 0):
        return True
    if (X < 0 or Y < 0):
        return False
    return (canReach(X - int(pow(3, steps)), Y, steps + 1) |
            canReach(X, Y - int(pow(3, steps)), steps + 1))


if __name__ == '__main__':
    X = 10
    Y = 30
    if (canReach(X, Y, 0)):
        print("YES")
    else:
        print("NO")

# Expected output:
YES
# check if it is possible to reach ( x , y ) from origin such that in each ith move increment x or y coordinate with 3 ^ i | function to find whether ( 0 , 0 ) can be reached from ( x , y ) by decrementing 3 ^ i at each ith step ; stores the number of steps performed to reach ( x , y ) ; value of x in base 3 ; value of y in base 3 ; check if any has value 2 ; if both have value 1 ; if both have value 0 ; otherwise , return true ; 
def canReach(X, Y):
    steps = 0
    while (X != 0 or Y != 0):
        pos1 = X % 3
        pos2 = Y % 3
        if (pos1 == 2 or pos2 == 2):
            return False
        if (pos1 == 1 and pos2 == 1):
            return False
        if (pos1 == 0 and pos2 == 0):
            return False
        X /= 3
        Y /= 3
        steps += 1
    return True


X = 10
Y = 30
if (canReach(X, Y)):
    print("YES")
else:
    print("NO")

# Expected output:
YES
# program to calculate surface area of ellipsoid | python3 program for the above approach ; function to find the surface area of the given ellipsoid ; formula to find surface area of an ellipsoid ; print the area ; 

from math import pow


def findArea(a, b, c):
    area = (4 * 3.141592653 * pow((pow(a * b, 1.6) +
            pow(a * c, 1.6) + pow(b * c, 1.6)) / 3, 1 / 1.6))
    print("{:.2f}".format(round(area, 2)))


if __name__ == '__main__':
    A = 11
    B = 12
    C = 13
    findArea(A, B, C)

# Expected output:
1807.89
# descartes ' circle theorem with implementation | python 3 implementation of the above formulae ; function to find the fourth circle 's when three radius are given ; 

from math import sqrt


def findRadius(r1, r2, r3):
    r4 = (r1 * r2 * r3) / (r1 * r2 + r2 * r3 + r1 *
                           r3 + 2.0 * sqrt(r1 * r2 * r3 * (r1 + r2 + r3)))
    return r4


if __name__ == '__main__':
    r1 = 1
    r2 = 1
    r3 = 1
    r4 = findRadius(r1, r2, r3)
    print("The radius of fourth circle:", r4)

# Expected output:
The radius of fourth circle: 0.15470053837925155
# make n pairs from array as ( x , y ) coordinate point that are enclosed inside a minimum area rectangle | function to make n pairs of coordinates such that they are enclosed in a minimum area rectangle with sides parallel to the x and y axes ; a variable to store the answer ; for the case where the maximum and minimum are in different partitions ; for the case where the maximum and minimum are in the same partition ; return the answer ; 
def minimumRectangleArea(A, N):
    ans = 0
    A .sort()
    ans = (A[N - 1] - A[0]) * (A[2 * N - 1] - A[N])
    for i in range(1, N, 1):
        ans = min(ans, (A[2 * N - 1] - A[0]) * (A[i + N - 1] - A[i]))
    return ans


if __name__ == '__main__':
    A = [2, 4, 1, 5, 3, 6, 7, 8]
    N = len(A)
    N //= 2
    print(minimumRectangleArea(A, N))

# Expected output:
9
# program to find the length of latus rectum of a hyperbola | function to calculate the length of the latus rectum of a hyperbola ; store the length of major axis ; store the length of minor axis ; store the length of the latus rectum ; return the length of the latus rectum ; 
def lengthOfLatusRectum(A, B):
    major = 2.0 * A
    minor = 2.0 * B
    latus_rectum = (minor * minor) / major
    return latus_rectum


A = 3.0
B = 2.0
print(round(lengthOfLatusRectum(A, B), 5))

# Expected output:
2.66667
# length of intercept cut off from a line by a circle | python3 program for the above approach ; function to find the radius of a circle ; g and f are the coordinates of the center ; case of invalid circle ; apply the radius formula ; function to find the perpendicular distance between circle center and the line ; store the coordinates of center ; stores the perpendicular distance between the line and the point ; invalid case ; return the distance ; function to find the length of intercept cut off from a line by a circle ; calculate the value of radius ; calculate the perpendicular distance between line and center ; invalid case ; if line do not cut circle ; print the intercept length ; 

import math


def radius(a, b, c):
    g = a / 2
    f = b / 2
    if (g * g + f * f - c < 0):
        return (-1)
    return (math .sqrt(g * g + f * f - c))


def centerDistanceFromLine(a, b, i, j, k):
    g = a / 2
    f = b / 2
    distance = (abs(i * g + j * f + k) / (math .sqrt(i * i + j * j)))
    if (distance < 0):
        return (-1)
    return distance


def interceptLength(a, b, c, i, j, k):
    rad = radius(a, b, c)
    dist = centerDistanceFromLine(a, b, i, j, k)
    if (rad < 0 or dist < 0):
        print("circle not possible")
        return
    if (dist > rad):
        print("Line not cutting circle")
    else:
        print(2 * math .sqrt(rad * rad - dist * dist))


if __name__ == "__main__":
    a = 0
    b = 0
    c = -4
    i = 2
    j = -1
    k = 1
    interceptLength(a, b, c, i, j, k)

# Expected output:
3.8987177379235853
# determine position of two points with respect to a 3d plane | function to check position of two points with respect to a plane in 3d ; put coordinates in plane equation ; if both values have same sign ; if both values have different sign ; if both values are zero ; if either of the two values is zero ; 
def check_position(a, b, c, d, x1, y1, z1, x2, y2, z2):
    value_1 = a * x1 + b * y1 + c * z1 + d
    value_2 = a * x2 + b * y2 + c * z2 + d
    if ((value_1 > 0 and value_2 > 0) or (value_1 < 0 and value_2 < 0)):
        print("On same side")
    if ((value_1 > 0 and value_2 < 0) or (value_1 < 0 and value_2 > 0)):
        print("On different sides")
    if (value_1 == 0 and value_2 == 0):
        print("Both on the plane")
    if (value_1 == 0 and value_2 != 0):
        print("Point 1 on the plane")
    if (value_1 != 0 and value_2 == 0):
        print("Point 2 on the plane")


if __name__ == '__main__':
    a, b, c, d = 1, 2, 3, 4
    x1, y1, z1 = -2, -2, 1
    x2, y2, z2 = -4, 11, -1
    check_position(a, b, c, d, x1, y1, z1, x2, y2, z2)

# Expected output:
On same side
# check if any pair of semicircles intersect or not | function to check if any pairs of the semicircles intersects or not ; stores the coordinates of all the semicircles ; x and y are coordinates ; store the minimum and maximum value of the pair ; push the pair in vector ; compare one pair with other pairs ; generating the second pair ; extract all the second pairs one by one ; 1 st condition ; 2 nd condition ; if any one condition is true ; if any pair of semicircles doesn 't exists ; 
def checkIntersection(arr, N):
    vec = []
    for i in range(N - 1):
        x = arr[i]
        y = arr[i + 1]
        minn = min(x, y)
        maxx = max(x, y)
        vec .append([minn, maxx])
    for i in range(len(vec)):
        x = vec[i]
        for j in range(len(vec)):
            y = vec[j]
            cond1 = (x[0] < y[0] and x[1] < y[1] and y[0] < x[1])
            cond2 = (y[0] < x[0] and y[1] < x[1] and x[0] < y[1])
            if (cond1 or cond2):
                return True
    return False


if __name__ == '__main__':
    arr = [0, 15, 5, 10]
    N = len(arr)
    if (checkIntersection(arr, N)):
        print("Yes")
    else:
        print("No")

# Expected output:
No
# find the angle between tangents drawn from a given external point to a circle | python 3 program for the above approach ; function to find the distance between center and the exterior point ; find the difference between the x and y coordinates ; using the distance formula ; function to find the angle between the pair of tangents drawn from the point ( x2 , y2 ) to the circle . ; calculate the distance between the center and exterior point ; invalid case ; find the angle using the formula ; print the resultant angle ; 

import math


def point_distance(x1, y1, x2, y2):
    p = (x2 - x1)
    q = (y2 - y1)
    distance = math .sqrt(p * p + q * q)
    return distance


def tangentAngle(x1, y1, x2, y2, radius):
    distance = point_distance(x1, y1, x2, y2)
    if (radius / distance > 1 or radius / distance < -1):
        print(-1)
    result = 2 * math .asin(radius / distance) * 180 / 3.1415
    print(result, " degrees")


if __name__ == "__main__":
    radius = 4
    x1 = 7
    y1 = 12
    x2 = 3
    y2 = 4
    tangentAngle(x1, y1, x2, y2, radius)

# Expected output:
53.131669342763075  degrees
# ratio of area of two nested polygons formed by connecting midpoints of sides of a regular n | python3 code for the above approach ; function to calculate the ratio of area of n - th and ( n + 1 ) - th nested polygons formed by connecting midpoints ; stores the value of pi ; calculating area the factor ; printing the ratio precise upto 6 decimal places ; 

import math


def AreaFactor(n):
    pi = 3.14159265
    areaf = 1 / (math .cos(pi / n) * math .cos(pi / n))
    print('%.6f' % areaf)


if __name__ == "__main__":
    n = 4
    AreaFactor(n)

# Expected output:
2.000000
# find the length of kth n | python3 program for the above approach ; function to calculate the interior angle of a n - sided regular polygon ; function to find the k - th polygon formed inside the ( k - 1 ) th polygon ; stores the interior angle ; stores the side length of k - th regular polygon ; return the length ; 

import math
PI = 3.14159265


def findInteriorAngle(n):
    return (n - 2) * PI / n


def calculateSideLength(L, N, K):
    angle = findInteriorAngle(N)
    length = L * pow(math .sin(angle / 2), (K - 1))
    return length


if __name__ == "__main__":
    N = 5
    L = 21
    K = 7
    print(calculateSideLength(L, N, K))

# Expected output:
5.887964192826562
# angle between a pair of lines | python3 program for the above approach ; function to find the angle between two lines ; store the tan value of the angle ; calculate tan inverse of the angle ; convert the angle from radian to degree ; print the result ; 

from math import atan


def findAngle(M1, M2):
    PI = 3.14159265
    angle = abs((M2 - M1) / (1 + M1 * M2))
    ret = atan(angle)
    val = (ret * 180) / PI
    print(round(val, 4))


if __name__ == '__main__':
    M1 = 1.75
    M2 = 0.27
    findAngle(M1, M2)

# Expected output:
45.1455
# angle of intersection of two circles having their centers d distance apart | function to find the cosine of the angle of the intersection of two circles with radius r1 and r2 ; return the cosine of the angle ; 
def angle(R1, R2, D):
    ans = ((R1 * R1 + R2 * R2 - D * D) / (2 * R1 * R2))
    return ans


if __name__ == '__main__':
    R1 = 3
    R2 = 4
    D = 5
    print(angle(R1, R2, D))

# Expected output:
0.0
# check if any point exists in a plane whose manhattan distance is at most k from n given points | function to check if there exists any point with at most k distance from n given points ; traverse the given n points ; stores the count of pairs of coordinates having manhattan distance <= k ; for the same coordinate ; calculate manhattan distance ; if manhattan distance <= k ; if all coordinates can meet ; if all coordinates can 't meet ; 
def find(a, b, N, K):
    for i in range(N):
        count = 0
        for j in range(N):
            if (i == j):
                continue
            dis = abs(a[i] - a[j]) + abs(b[i] - b[j])
            if (dis <= K):
                count = count + 1
            if (count == N - 1):
                return "Yes"
        return "No"


N = 5
A = [1, 0, 2, 1, 1]
B = [1, 1, 1, 0, 2]
K = 1
print(find(A, B, N, K))

# Expected output:
Yes
# minimum area of the triangle formed by any tangent to an ellipse with the coordinate axes | function to find the minimum area of triangle formed by any tangent to ellipse with the coordinate axes ; stores the minimum area ; print the calculated area ; 
def minimumTriangleArea(a, b):
    area = a * b
    print(area)


a = 1
b = 2
minimumTriangleArea(a, b)

# Expected output:
2