giulio98/xlcost-single-prompt huggingface dataset:
# rat in a maze problem when movement in all possible directions is allowed | python3 implementation of the above approach ; function returns true if the move taken is valid else it will return false . ; function to print all the possible paths from ( 0 , 0 ) to ( n - 1 , n - 1 ) . ; this will check the initial point ( i . e . ( 0 , 0 ) ) to start the paths . ; if reach the last cell ( n - 1 , n - 1 ) then store the path and return ; mark the cell as visited ; check if downward move is valid ; check if the left move is valid ; check if the right move is valid ; check if the upper move is valid ; mark the cell as unvisited for other possible paths ; function to store and print all the valid paths ; vector to store all the possible paths ; call the utility function to find the valid paths ; print all possible paths ; 

from typing import List
MAX = 5


def isSafe(row: int,
           col: int,
           m: List[List[int]],
           n: int,
           visited: List[List[bool]]) -> bool:
    if (row == -1 or row == n or col == -1 or col ==
            n or visited[row][col] or m[row][col] == 0):
        return False
    return True


def printPathUtil(row: int,
                  col: int,
                  m: List[List[int]],
                  n: int,
                  path: str,
                  possiblePaths: List[str],
                  visited: List[List[bool]]) -> None:
    if (row == -1 or row == n or col == -1 or col ==
            n or visited[row][col] or m[row][col] == 0):
        return
    if (row == n - 1 and col == n - 1):
        possiblePaths .append(path)
        return
    visited[row][col] = True
    if (isSafe(row + 1, col, m, n, visited)):
        path += 'D'
        printPathUtil(row + 1, col, m, n, path, possiblePaths, visited)
        path = path[:-1]
    if (isSafe(row, col - 1, m, n, visited)):
        path += 'L'
        printPathUtil(row, col - 1, m, n, path, possiblePaths, visited)
        path = path[:-1]
    if (isSafe(row, col + 1, m, n, visited)):
        path += 'R'
        printPathUtil(row, col + 1, m, n, path, possiblePaths, visited)
        path = path[:-1]
    if (isSafe(row - 1, col, m, n, visited)):
        path += 'U'
        printPathUtil(row - 1, col, m, n, path, possiblePaths, visited)
        path = path[:-1]
    visited[row][col] = False


def printPath(m: List[List[int]], n: int) -> None:
    possiblePaths = []
    path = ""
    visited = [[False for _ in range(MAX)]for _ in range(n)]
    printPathUtil(0, 0, m, n, path, possiblePaths, visited)
    for i in range(len(possiblePaths)):
        print(possiblePaths[i], end=" ")


if __name__ == "__main__":
    m = [[1, 0, 0, 0, 0], [1, 1, 1, 1, 1], [
        1, 1, 1, 0, 1], [0, 0, 0, 0, 1], [0, 0, 0, 0, 1]]
    n = len(m)
    printPath(m, n)

# Expected output:
DDRRURRDDD DDRURRRDDD DRDRURRDDD DRRRRDDD
# collect all coins in minimum number of steps | recursive method to collect coins from height array l to r , with height h already collected ; if l is more than r , no steps needed ; loop over heights to get minimum height index ; choose minimum from , 1 ) collecting coins using all vertical lines ( total r - l ) 2 ) collecting coins using lower horizontal lines and recursively on left and right segments ; method returns minimum number of step to collect coin from stack , with height in height [ ] array ; 
def minStepsRecur(height, l, r, h):
    if l >= r:
        return 0
    m = l
    for i in range(l, r):
        if height[i] < height[m]:
            m = i
    return min(
        r -
        l,
        minStepsRecur(
            height,
            l,
            m,
            height[m]) +
        minStepsRecur(
            height,
            m +
            1,
            r,
            height[m]) +
        height[m] -
        h)


def minSteps(height, N):
    return minStepsRecur(height, 0, N, 0)


height = [2, 1, 2, 5, 1]
N = len(height)
print(minSteps(height, N))

# Expected output:
4
# count of unique direct path between n points on a plane | function to count the total number of direct paths ; 
def countDirectPath(N):
    return N + (N * (N - 3)) // 2


if __name__ == "__main__":
    N = 5
    print(countDirectPath(N))

# Expected output:
10
# find the coordinate that does not belong to any square | function to find the point that is not a part of the side of a square ; traverse each pair of coordinates ; minimize x - coordinate in all the points except current point ; maximize x - coordinate in all the points except the current point ; minimize y - coordinate in all the points except current point ; maximize y - coordinate in all the points except current point ; if x - coordinate matches with other same line ; if y coordinate matches with other same line ; check if the condition for square exists or not ; print the output ; 
def findPoint(n, p):
    for i in range(n * 4 + 1):
        x1 = 2e9
        x2 = -2e9
        y1 = 2e9
        y2 = -2e9
        for j in range(n * 4 + 1):
            if (i != j):
                x1 = min(x1, p[j][0])
                x2 = max(x2, p[j][0])
                y1 = min(y1, p[j][1])
                y2 = max(y2, p[j][1])
        ok = 1
        c1 = 0
        c2 = 0
        c3 = 0
        c4 = 0
        for j in range(1, n * 4 + 1, 1):
            if (i != j):
                if ((p[j][0] == x1 or p[j][0] == x2) or (
                        p[j][1] == y1 or p[j][1] == y2)):
                    if (p[j][0] == x1):
                        c1 += 1
                    if (p[j][0] == x2):
                        c2 += 1
                    if (p[j][1] == y1):
                        c3 += 1
                    if (p[j][1] == y2):
                        c4 += 1
                else:
                    ok = 0
        if (ok and c1 >= n and c2 >= n and c3 >=
                n and c4 >= n and x2 - x1 == y2 - y1):
            print(p[i][0], p[i][1])


if __name__ == '__main__':
    N = 2
    arr = [
        [
            0, 0], [
            0, 1], [
                0, 2], [
                    1, 0], [
                        1, 1], [
                            1, 2], [
                                2, 0], [
                                    2, 1], [
                                        2, 2]]
    findPoint(N, arr)

# Expected output:
1 1
# triacontagon number | finding the nth triacontagonal number ; 
def triacontagonalNum(n):
    return (28 * n * n - 26 * n) // 2


n = 3
print("3rd triacontagonal Number is = ", triacontagonalNum(n))

# Expected output:
3rd triacontagonal Number is =  87
# hexacontagon number | finding the nth hexacontagon number ; 
def hexacontagonNum(n):
    return (58 * n * n - 56 * n) // 2


n = 3
print("3rd hexacontagon Number is = ", hexacontagonNum(n))

# Expected output:
3rd hexacontagon Number is =  177
# enneacontagon number | finding the nth enneacontagon number ; 
def enneacontagonNum(n):
    return (88 * n * n - 86 * n) // 2


n = 3
print("3rd enneacontagon Number is = ", enneacontagonNum(n))

# Expected output:
3rd enneacontagon Number is =  267
# triacontakaidigon number | finding the nth triacontakaidigon number ; 
def triacontakaidigonNum(n):
    return (30 * n * n - 28 * n) // 2


n = 3
print("3rd triacontakaidigon Number is = ", triacontakaidigonNum(n))

# Expected output:
3rd triacontakaidigon Number is =  93
# icosihexagonal number | finding the nth icosihexagonal number ; 
def IcosihexagonalNum(n):
    return (24 * n * n - 22 * n) // 2


n = 3
print("3rd Icosihexagonal Number is = ", IcosihexagonalNum(n))

# Expected output:
3rd Icosihexagonal Number is =  75
# icosikaioctagon or icosioctagon number | finding the nth icosikaioctagonal number ; 
def icosikaioctagonalNum(n):
    return (26 * n * n - 24 * n) // 2


n = 3
print("3rd icosikaioctagonal Number is = ", icosikaioctagonalNum(n))

# Expected output:
3rd icosikaioctagonal Number is =  81
# octacontagon number | finding the nth octacontagon number ; 
def octacontagonNum(n):
    return (78 * n * n - 76 * n) // 2


n = 3
print("3rd octacontagon Number is = ", octacontagonNum(n))

# Expected output:
3rd octacontagon Number is =  237
# hectagon number | finding the nth hectagon number ; 
def hectagonNum(n):
    return (98 * n * n - 96 * n) // 2


n = 3
print("3rd hectagon Number is = ", hectagonNum(n))

# Expected output:
3rd hectagon Number is =  297
# tetracontagon number | finding the nth tetracontagon number ; 
def tetracontagonNum(n):
    return (38 * n * n - 36 * n) // 2


n = 3
print("3rd tetracontagon Number is = ", tetracontagonNum(n))

# Expected output:
3rd tetracontagon Number is =  117
# perimeter of an ellipse | python3 program to find perimeter of an ellipse ; function to find the perimeter of an ellipse ; compute perimeter ; 

from math import sqrt


def Perimeter(a, b):
    perimeter = 0
    perimeter = (2 * 3.14 * sqrt((a * a + b * b) / (2 * 1.0)))
    print(perimeter)


a = 3
b = 2
Perimeter(a, b)

# Expected output:
16.010921272681344
# biggest reuleaux triangle within a square | function to find the area of the reuleaux triangle ; side cannot be negative ; area of the reuleaux triangle ; 
def ReuleauxArea(a):
    if (a < 0):
        return -1
    A = 0.70477 * pow(a, 2)
    return A


if __name__ == "__main__":
    a = 6
    print(ReuleauxArea(a))

# Expected output:
25.37172
# largest hexagon that can be inscribed within a square | function to return the side of the hexagon ; side cannot be negative ; side of the hexagon ; 
def hexagonside(a):
    if (a < 0):
        return -1
    x = 0.5176 * a
    return x


a = 6
print(hexagonside(a))

# Expected output:
3.1056
# largest hexagon that can be inscribed within an equilateral triangle | function to find the side of the hexagon ; side cannot be negative ; side of the hexagon ; 
def hexagonside(a):
    if a < 0:
        return -1
    x = a // 3
    return x


a = 6
print(hexagonside(a))

# Expected output:
2
# find middle point segment from given segment lengths | function that returns the segment for the middle point ; the middle point ; stores the segment index ; increment sum by length of the segment ; if the middle is in between two segments ; if sum is greater than middle point ; 
def findSegment(n, m, segment_length):
    meet_point = (1.0 * n) / 2.0
    sum = 0
    segment_number = 0
    for i in range(0, m, 1):
        sum += segment_length[i]
        if (sum == meet_point):
            segment_number = -1
            break
        if (sum > meet_point):
            segment_number = i + 1
            break
    return segment_number


if __name__ == '__main__':
    n = 13
    m = 3
    segment_length = [3, 2, 8]
    ans = findSegment(n, m, segment_length)
    print(ans)

# Expected output:
3
# maximum points of intersection n lines | nc2 = ( n ) * ( n - 1 ) / 2 ; 
def countMaxIntersect(n):
    return int(n * (n - 1) / 2)


if __name__ == '__main__':
    n = 8
    print(countMaxIntersect(n))

# Expected output:
28
# check if a point is inside , outside or on the parabola | function to check the point ; checking the equation of parabola with the given point ; 
def checkpoint(h, k, x, y, a):
    p = pow((y - k), 2) - 4 * a * (x - h)
    return p


if __name__ == "__main__":
    h = 0
    k = 0
    x = 2
    y = 1
    a = 4
    if checkpoint(h, k, x, y, a) > 0:
        print("Outside")
    elif checkpoint(h, k, x, y, a) == 0:
        print("Ontheparabola")
    else:
        print("Inside")

# Expected output:
Inside
# check if a point is inside , outside or on the ellipse | python 3 program to check if the point lies within the ellipse or not ; function to check the point ; checking the equation of ellipse with the given point ; 

import math


def checkpoint(h, k, x, y, a, b):
    p = ((math .pow((x - h), 2) // math .pow(a, 2)) +
         (math .pow((y - k), 2) // math .pow(b, 2)))
    return p


if __name__ == "__main__":
    h = 0
    k = 0
    x = 2
    y = 1
    a = 4
    b = 5
    if (checkpoint(h, k, x, y, a, b) > 1):
        print("Outside")
    elif (checkpoint(h, k, x, y, a, b) == 1):
        print("On the ellipse")
    else:
        print("Inside")

# Expected output:
Inside
# area of circle inscribed within rhombus | function to find the area of the inscribed circle ; the diagonals cannot be negative ; area of the circle ; 
def circlearea(a, b):
    if (a < 0 or b < 0):
        return -1
    A = ((3.14 * pow(a, 2) * pow(b, 2)) / (4 * (pow(a, 2) + pow(b, 2))))
    return A


if __name__ == "__main__":
    a = 8
    b = 10
    print(circlearea(a, b))

# Expected output:
30.634146341463413
# the biggest possible circle that can be inscribed in a rectangle | function to find the area of the biggest circle ; the length and breadth cannot be negative ; area of the circle ; 
def circlearea(l, b):
    if (l < 0 or b < 0):
        return -1
    if (l < b):
        return 3.14 * pow(l // 2, 2)
    else:
        return 3.14 * pow(b // 2, 2)


if __name__ == "__main__":
    l = 4
    b = 8
    print(circlearea(l, b))

# Expected output:
12.56
# centered cube number | centered cube number function ; formula to calculate nth centered cube number return it into main function . ; 
def centered_cube(n):
    return (2 * n + 1) * (n * n + n + 1)


if __name__ == '__main__':
    n = 3
    print(n, "th Centered cube " + "number : ", centered_cube(n))
    n = 10
    print(n, "th Centered cube " + "number : ", centered_cube(n))

# Expected output:
3 th Centered cube number :  91
10 th Centered cube number :  2331
# find the center of the circle using endpoints of diameter | function to find the center of the circle ; 
def center(x1, x2, y1, y2):
    print(int((x1 + x2) / 2), end="")
    print(",", int((y1 + y2) / 2))


x1 = -9
y1 = 3
x2 = 5
y2 = -7
center(x1, x2, y1, y2)

# Expected output:
-2, -2
# program to calculate volume of ellipsoid | python3 program to volume of ellipsoid ; function to calculate volume ; 

import math


def volumeOfEllipsoid(r1, r2, r3):
    return 1.33 * math .pi * r1 * r2 * r3


r1 = float(2.3)
r2 = float(3.4)
r3 = float(5.7)
print("Volume of ellipsoid is : ", volumeOfEllipsoid(r1, r2, r3))

# Expected output:
Volume of ellipsoid is :  186.24435675167823
# find mirror image of a point in 2 | python function which finds coordinates of mirror image . this function return a pair of double ; 
def mirrorImage(a, b, c, x1, y1):
    temp = -2 * (a * x1 + b * y1 + c) / (a * a + b * b)
    x = temp * a + x1
    y = temp * b + y1
    return (x, y)


a = -1.0
b = 1.0
c = 0.0
x1 = 1.0
y1 = 0.0
x, y = mirrorImage(a, b, c, x1, y1)
print("Image of point (" + str(x1) + ", " + str(y1) + ") ")
print(
    "by mirror (" +
    str(a) +
    ")x + (" +
    str(b) +
    ")y + (" +
    str(c) +
    ") = 0, is :")
print("(" + str(x) + ", " + str(y) + ")")

# Expected output:
Image of point (1.0, 0.0) 
by mirror (-1.0)x + (1.0)y + (0.0) = 0, is :
(0.0, 1.0)
# minimum revolutions to move center of a circle to a target | python program to find minimum number of revolutions to reach a target center ; minimum revolutions to move center from ( x1 , y1 ) to ( x2 , y2 ) ; 

import math


def minRevolutions(r, x1, y1, x2, y2):
    d = math .sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2))
    return math .ceil(d / (2 * r))


r = 2
x1 = 0
y1 = 0
x2 = 0
y2 = 4
print(minRevolutions(r, x1, y1, x2, y2))

# Expected output:
1
# find all sides of a right angled triangle from given hypotenuse and area | set 1 | limit for float comparison define eps 1e-6 ; utility method to get area of right angle triangle , given base and hypotenuse ; prints base and height of triangle using hypotenuse and area information ; maximum area will be obtained when base and height are equal ( = sqrt ( h * h / 2 ) ) ; if given area itself is larger than maxarea then no solution is possible ; binary search for base ; get height by pythagorean rule ; 

import math


def getArea(base, hypotenuse):
    height = math .sqrt(hypotenuse * hypotenuse - base * base)
    return 0.5 * base * height


def printRightAngleTriangle(hypotenuse, area):
    hsquare = hypotenuse * hypotenuse
    sideForMaxArea = math .sqrt(hsquare / 2.0)
    maxArea = getArea(sideForMaxArea, hypotenuse)
    if (area > maxArea):
        print("Not possiblen")
        return
    low = 0.0
    high = sideForMaxArea
    while (abs(high - low) > 1e-6):
        base = (low + high) / 2.0
        if (getArea(base, hypotenuse) >= area):
            high = base
        else:
            low = base
    height = math .ceil(math .sqrt(hsquare - base * base))
    base = math .floor(base)
    print(base, height)


if __name__ == '__main__':
    hypotenuse = 5
    area = 6
    printRightAngleTriangle(hypotenuse, area)

# Expected output:
3 4
# circle and lattice points | python3 program to find countlattice podefs on a circle ; function to count lattice podefs on a circle ; initialize result as 4 for ( r , 0 ) , ( - r . 0 ) , ( 0 , r ) and ( 0 , - r ) ; check every value that can be potential x ; find a potential y ; checking whether square root is an defeger or not . count increments by 4 for four different quadrant values ; 

import math


def countLattice(r):
    if (r <= 0):
        return 0
    result = 4
    for x in range(1, r):
        ySquare = r * r - x * x
        y = int(math .sqrt(ySquare))
        if (y * y == ySquare):
            result += 4
    return result


r = 5
print(countLattice(r))

# Expected output:
12
# count of subarrays of size k with average at least m | function to count the subarrays of size k having average at least m ; stores the resultant count of subarray ; stores the sum of subarrays of size k ; add the values of first k elements to the sum ; increment the count if the current subarray is valid ; traverse the given array ; find the updated sum ; check if current subarray is valid or not ; return the count of subarrays ; 
def countSubArrays(arr, N, K, M):
    count = 0
    sum = 0
    for i in range(K):
        sum += arr[i]
    if sum >= K * M:
        count += 1
    for i in range(K, N):
        sum += (arr[i] - arr[i - K])
        if sum >= K * M:
            count += 1
    return count


if __name__ == '__main__':
    arr = [3, 6, 3, 2, 1, 3, 9]
    K = 2
    M = 4
    N = len(arr)
    count = countSubArrays(arr, N, K, M)
    print(count)

# Expected output:
3
# count of ks in the array for a given range of indices after array updates for q queries | ''function to perform all the queries ; ''stores the count of 0s ; ''count the number of 0s for  query of type 1 ; '' update the array element for  query of type 2 ; ''
def performQueries(n, q, k, arr, query):
    for i in range(1, q + 1, 1):
        count = 0
        if (query[i - 1][0] == 1):
            for j in range(query[i - 1][1], query[i - 1][2]+1, 1):
                if (arr[j] == k):
                    count += 1
            print(count)
        else:
            arr[query[i - 1][1]] = query[i - 1][2]


if __name__ == '__main__':
    arr = [9, 5, 7, 6, 9, 0, 0, 0, 0, 5, 6, 7, 3, 9, 0, 7, 0, 9, 0]
    Q = 5
    query = [[1, 5, 14], [2, 6, 1], [1, 0, 8], [2, 13, 0], [1, 6, 18]]
    N = len(arr)
    K = 0
    performQueries(N, Q, K, arr, query)

# Expected output:
5
3
6
# minimum count of consecutive integers till n whose bitwise and is 0 with n | function to count the minimum count of integers such that bitwise and of that many consecutive elements is equal to 0 ; stores the binary representation of n ; excludes first two characters "0b " ; stores the msb bit ; stores the count of numbers ; return res ; 
def count(N):
    a = bin(N)
    a = a[2:]
    m = len(a) - 1
    res = N - (2 ** m - 1)
    return res


N = 18
print(count(N))

# Expected output:
3
# find smallest value of k such that bitwise and of numbers in range [ n , n | function is to find the largest no which gives the sequence n & ( n - 1 ) & ( n - 2 ) & ... . . & ( n - k ) = 0. ; since , we need the largest no , we start from n itself , till 0 ; 
def findSmallestNumK(n):
    cummAnd = n
    i = n - 1
    while (cummAnd != 0):
        cummAnd = cummAnd & i
        if (cummAnd == 0):
            return i
        i -= 1
    return -1


if __name__ == '__main__':
    N = 17
    lastNum = findSmallestNumK(N)
    K = lastNum if lastNum == -1 else N - lastNum
    print(K)

# Expected output:
2
# program to find the value of p ( n + r ) for a polynomial of a degree n such that p ( i ) = 1 for 1 ã ¢ â €°¤ i ã ¢ â €°¤ n and p ( n + 1 ) = a | ''function to calculate factorial of n ; '' base case ; '' otherwise, recursively  calculate the factorial ; ''function to find the value of p(n + r) for polynomial p(x) ; ''stores the value of k ; '' store the required answer ; '' iterate in the range [1, n] and  multiply (n + r - i) with answer ; '' add the constant value c as 1 ; '' return the result ; ''
def fact(n):
    if n == 1 or n == 0:
        return 1
    else:
        return n * fact(n - 1)


def findValue(n, r, a):
    k = (a - 1) // fact(n)
    answer = k
    for i in range(1, n + 1):
        answer = answer * (n + r - i)
        answer = answer + 1
    return answer


N = 1
A = 2
R = 3
print(findValue(N, R, A))

# Expected output:
4
# find winner in game of n balls , in which a player can remove any balls in range [ a , b ] in a single move | function to find the winner of the game ; stores sum of a and b ; if n is of the form m * ( a + b ) + y ; otherwise , ; 
def NimGame(N, A, B):
    sum = A + B
    if (N % sum <= A - 1):
        return "Bob"
    else:
        return "Alice"


N = 3
A = 1
B = 2
print(NimGame(N, A, B))

# Expected output:
Bob
# find nth number in a sequence which is not a multiple of a 

import math


def countNo(A, N, L, R):
    ans = L - 1 + N + math .floor((N - 1) / (A - 1))
    if ans % A == 0:
        ans = ans + 1
    print(ans)


A, N, L, R = 5, 10, 4, 20
countNo(A, N, L, R)

# Expected output:
16
# count of paths in 
def compute(query):
    v = [None] * 100001
    dp = [None] * 100001
    v[1] = 1
    v[2] = 0
    dp[1] = 0
    dp[2] = 0
    for i in range(3, 100001):
        if (i % 2 != 0):
            if ((i // 2) % 2 == 0):
                v[i] = 1
                dp[i] = dp[i - 1]
            else:
                v[i] = v[i // 2] + 1
                dp[i] = dp[i - 1] + v[i] - 1
        else:
            v[i] = 0
            dp[i] = dp[i - 1]
    for x in query:
        print(dp[x])


query = [5, 2]
compute(query)

# Expected output:
1
0
# number of cycles formed by joining vertices of n sided polygon at the center | function to calculate number of cycles ; biginteger is used here if n = 10 ^ 9 then multiply will result into value greater than 10 ^ 18 ; biginteger multiply function ; return the final result ; 
def findCycles(N):
    res = 0
    finalResult = 0
    val = 2 * N - 1
    s = val
    res = (N - 1) * (N - 2)
    finalResult = res + s
    return finalResult


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

# Expected output:
21
# split a given array into k subarrays minimizing the difference between their maximum and minimum | function to find the subarray ; add the difference to vectors ; sort vector to find minimum k ; initialize result ; adding first k - 1 values ; return the minimized sum ; 
def find(a, n, k):
    v = []
    for i in range(1, n):
        v .append(a[i - 1] - a[i])
    v .sort()
    res = a[n - 1] - a[0]
    for i in range(k - 1):
        res += v[i]
    return res


arr = [4, 8, 15, 16, 23, 42]
N = len(arr)
K = 3
print(find(arr, N, K))

# Expected output:
12