giulio98/xlcost-single-prompt huggingface dataset:
# find sum of xor of all unordered triplets of the array | iterative function to calculate ( x ^ y ) % p in o ( log y ) ; initialize result ; update x if it is more than or equal to p ; if y is odd , multiply x with result ; y must be even now y = y >> 1 y = y / 2 ; returns n ^ ( - 1 ) mod p ; returns ncr % p using fermat 's little theorem. ; base case ; fill factorial array so that we can find all factorial of r , n and n - r ; function returns sum of xor of all unordered triplets of the array ; iterating over the bits ; number of elements whith k 'th bit  1 and 0 respectively ; checking if k 'th bit is 1 ; adding this bit 's part to the answer ; 
def power(x, y, p):
    res = 1
    x = x % p

    while (y > 0):
        if (y & 1):
            res = (res * x) % p
        y = y >> 1
        x = (x * x) % p
    return res


def modInverse(n, p):
    return power(n, p - 2, p)


def nCrModPFermat(n, r, p):
    if (r == 0):
        return 1
    if (n < r):
        return 0
    fac = [0]*(n + 1)
    fac[0] = 1
    for i in range(1, n + 1):
        fac[i] = fac[i - 1] * i % p

    return (fac[n] * modInverse(fac[r], p) % p *
            modInverse(fac[n - r], p) % p) % p


def SumOfXor(a, n):

    mod = 10037

    answer = 0
    for k in range(32):

        x = 0
        y = 0

        for i in range(n):
            if (a[i] & (1 << k)):
                x += 1
            else:
                y += 1
        answer += ((1 << k) % mod * (nCrModPFermat(x, 3, mod)
                                     + x * nCrModPFermat(y, 2, mod))
                   % mod) % mod

    return answer


if __name__ == '__main__':
    n = 5
    A = [3, 5, 2, 18, 7]

    print(SumOfXor(A, n))

# Expected output:
132
# probability of not getting two consecutive heads together in n tosses of coin | python3 implementation to find the probability of not getting two consecutive heads together when n coins are tossed ; function to compute the n - th fibonacci number in the sequence where a = 2 and b = 3 ; the first two numbers in the sequence are initialized ; base cases ; loop to compute the fibonacci sequence based on the first two initialized numbers ; function to find the probability of not getting two consecutive heads when n coins are tossed ; computing the number of favourable cases ; computing the number of all possible outcomes for n tosses ; 

import math


def probability(N):
    a = 2
    b = 3
    if N == 1:
        return a
    elif N == 2:
        return b
    else:
        for i in range(3, N + 1):
            c = a + b
            a = b
            b = c
        return b


def operations(N):
    x = probability(N)
    y = math .pow(2, N)
    return round(x / y, 2)


if __name__ == '__main__':
    N = 10
    print(operations(N))

# Expected output:
0.14
# check if number formed by joining two numbers is perfect cube | function to check if a number is a perfect cube or not ; function to check if concatenation of two numbers is a perfect cube or not ; convert numbers to string using to_string ( ) ; concatenate the numbers and convert it into integer ; check if concatenated value is perfect cube or not ; 
def isPerfectCube(x):
    x = abs(x)
    return int(round(x ** (1. / 3)))**3 == x


def checkCube(a, b):
    s1 = str(a)
    s2 = str(b)
    c = int(s1 + s2)
    if (isPerfectCube(c)):
        print("Yes")
    else:
        print("No")


if __name__ == '__main__':
    a = 6
    b = 4
    checkCube(a, b)

# Expected output:
Yes
# largest number in given array formed by repeatedly combining two same elements | function to return the largest sum ; variable to store the largest sum ; map to store the frequencies of each element ; store the frequencies ; loop to combine duplicate elements and update the sum in the map ; if j is a duplicate element ; update the frequency of 2 * j ; if the new sum is greater than maximum value , update the maximum ; returns the largest sum ; 
def largest_sum(arr, n):
    maximum = -1
    m = dict()
    for i in arr:
        m[i] = m .get(i, 0) + 1
    for j in list(m):
        if ((j in m) and m[j] > 1):
            x, y = 0, 0
            if 2 * j in m:
                m[2 * j] = m[2 * j] + m[j] // 2
            else:
                m[2 * j] = m[j] // 2
            if (2 * j > maximum):
                maximum = 2 * j
    return maximum


if __name__ == '__main__':
    arr = [1, 1, 2, 4, 7, 8]
    n = len(arr)
    print(largest_sum(arr, n))

# Expected output:
16
# check if it is possible to make x and y zero at same time with given operation | function to check if it is possible to make x and y can become 0 at same time ; check the given conditions ; 
def canBeReduced(x, y):
    maxi = max(x, y)
    mini = min(x, y)
    if (((x + y) % 3) == 0 and maxi <= 2 * mini):
        print("YES")
    else:
        print("NO")


if __name__ == '__main__':
    x = 6
    y = 9
    canBeReduced(x, y)

# Expected output:
YES
# wheel factorization algorithm | python3 program to check if the given number is prime using wheel factorization method ; function to check if a given number x is prime or not ; the wheel for checking prime number ; base case ; check for the number taken as basis ; check for wheel here i , acts as the layer of the wheel ; check for the list of sieve in arr [ ] ; if number is greater than sqrt ( n ) break ; check if n is a multiple of prime number in the wheel ; if at any iteration isprime is false , break from the loop ; 

import math


def isPrime(N):
    isPrime = True
    arr = [7, 11, 13, 17, 19, 23, 29, 31]
    if (N < 2):
        isPrime = False
    if (N % 2 == 0 or N % 3 == 0 or N % 5 == 0):
        isPrime = False
    for i in range(0, int(math .sqrt(N)), 30):
        for c in arr:
            if (c > int(math .sqrt(N))):
                break
            else:
                if (N % (c + i) == 0):
                    isPrime = False
                    break
            if (not isPrime):
                break
    if (isPrime):
        print("Prime Number")
    else:
        print("Not a Prime Number")


if __name__ == "__main__":
    N = 121
    isPrime(N)

# Expected output:
Not a Prime Number
# find all pairs possible from the given array | function to prall possible pairs from the array ; nested loop for all possible pairs ; 
def printPairs(arr, n):
    for i in range(n):
        for j in range(n):
            print("(", arr[i], ",", arr[j], ")", end=", ")


arr = [1, 2]
n = len(arr)
printPairs(arr, n)

# Expected output:
( 1 , 1 ), ( 1 , 2 ), ( 2 , 1 ), ( 2 , 2 ),
# sum of cubes of all subsets of given array | python3 implementation of the approach ; function to return ( 2 ^ p % mod ) ; function to return the sum of cubes of subsets ; cubing the elements and adding it to ans ; 

mod = int(1e9) + 7


def power(p):
    res = 1
    for i in range(1, p + 1):
        res *= 2
        res %= mod
    return res % mod


def subset_cube_sum(A):
    n = len(A)
    ans = 0
    for i in A:
        ans += (i * i * i) % mod
        ans %= mod
    return (ans * power(n - 1)) % mod


if __name__ == "__main__":
    A = [1, 2]
    print(subset_cube_sum(A))

# Expected output:
18
# check if a circle lies inside another circle or not | python3 program to check if one circle lies inside another circle or not . ; 
def circle(x1, y1, x2, y2, r1, r2):
    distSq = (((x1 - x2) * (x1 - x2)) + ((y1 - y2) * (y1 - y2)))**(.5)
    if (distSq + r2 == r1):
        print(
            "The smaller circle lies completely"
            " inside the bigger circle with "
            "touching each other "
            "at a point of circumference. ")
    elif (distSq + r2 < r1):
        print(
            "The smaller circle lies completely"
            " inside the bigger circle without"
            " touching each other "
            "at a point of circumference. ")
    else:
        print(
            "The smaller does not lies inside"
            " the bigger circle completely.")


x1, y1 = 10, 8
x2, y2 = 1, 2
r1, r2 = 30, 10
circle(x1, y1, x2, y2, r1, r2)

# Expected output:
The smaller circle lies completely inside the bigger circle without touching each other at a point of circumference.
# length of direct common tangent between two intersecting circles | function to find the length of the direct common tangent ; 
def lengtang(r1, r2, d):
    print("The length of the direct common tangent is ",
          ((d ** 2) - ((r1 - r2)**2))**(1 / 2))


r1 = 4
r2 = 6
d = 3
lengtang(r1, r2, d)

# Expected output:
The length of the direct common tangent is  2.23606797749979
# radius of the circle when the width and height of an arc is given | function to find the radius ; 
def rad(d, h):
    print("The radius of the circle is", ((d * d) / (8 * h) + h / 2))


d = 4
h = 1
rad(d, h)

# Expected output:
The radius of the circle is 2.5
# shortest distance from the centre of a circle to a chord | function to find the shortest distance ; 
def shortdis(r, d):
    print("The shortest distance ", end="")
    print("from the chord to centre ", end="")
    print(((r * r) - ((d * d) / 4))**(1 / 2))


r = 4
d = 3
shortdis(r, d)

# Expected output:
The shortest distance from the chord to centre 3.7080992435478315
# length of direct common tangent between the two non | python3 program to find the length of the direct common tangent between two circles which do not touch each other ; function to find the length of the direct common tangent ; 

import math


def lengtang(r1, r2, d):
    print("The length of the direct common tangent is",
          (((d ** 2) - ((r1 - r2)**2))**(1 / 2)))


r1 = 4
r2 = 6
d = 12
lengtang(r1, r2, d)

# Expected output:
The length of the direct common tangent is 11.832159566199232
# biggest square that can be inscribed within an equilateral triangle | function to find the side of the square ; the side cannot be negative ; side of the square ; 
def square(a):
    if (a < 0):
        return -1
    x = 0.464 * a
    return x


a = 5
print(square(a))

# Expected output:
2.3200000000000003
# apothem of a n | python 3 program to find the apothem of a regular polygon with given side length ; function to find the apothem of a regular polygon ; side and side length cannot be negative ; degree converted to radians ; 

from math import tan


def polyapothem(n, a):
    if (a < 0 and n < 0):
        return -1
    return a / (2 * tan((180 / n) * 3.14159 / 180))


if __name__ == '__main__':
    a = 9
    n = 6
    print('{0:.6}'.format(polyapothem(n, a)))

# Expected output:
7.79424
# area of a n | python 3 program to find the area of a regular polygon with given side length ; function to find the area of a regular polygon ; side and side length cannot be negative ; area degree converted to radians ; 

from math import tan


def polyarea(n, a):
    if (a < 0 and n < 0):
        return -1
    A = (a * a * n) / (4 * tan((180 / n) * 3.14159 / 180))
    return A


if __name__ == '__main__':
    a = 9
    n = 6
    print('{0:.6}'.format(polyarea(n, a)))

# Expected output:
210.444
# side of a regular n | python 3 implementation of the approach ; function to calculate the side of the polygon circumscribed in a circle ; 

from math import sin


def calculateSide(n, r):
    theta = 360 / n
    theta_in_radians = theta * 3.14 / 180
    return 2 * r * sin(theta_in_radians / 2)


if __name__ == '__main__':
    n = 3
    r = 5
    print('{0:.5}'.format(calculateSide(n, r)))

# Expected output:
8.6576
# largest right circular cylinder within a frustum | function to find the biggest right circular cylinder ; radii and height cannot be negative ; radius of right circular cylinder ; height of right circular cylinder ; volume of right circular cylinder ; 
def cyl(r, R, h):
    if (h < 0 and r < 0 and R < 0):
        return -1
    r1 = r
    h1 = h
    V = 3.14 * pow(r1, 2) * h1
    return round(V, 2)


if __name__ == "__main__":
    r, R, h = 7, 11, 6
    print(cyl(r, R, h))

# Expected output:
923.16
# program to find the perimeter of a regular polygon | function to calculate the perimeter ; calculate perimeter ; 
def Perimeter(s, n):
    perimeter = 1
    perimeter = n * s
    return perimeter


if __name__ == '__main__':
    n = 5
    s = 2.5
    peri = Perimeter(s, n)
    print("Perimeter of Regular Polygon with",
          n, "sides of length", s, "=", peri)

# Expected output:
Perimeter of Regular Polygon with 5 sides of length 2.5 = 12.5
# area of the biggest possible rhombus that can be inscribed in a rectangle | function to find the area of the biggest rhombus ; the length and breadth cannot be negative ; area of the rhombus ; 
def rhombusarea(l, b):
    if (l < 0 or b < 0):
        return -1
    return (l * b) / 2


if __name__ == '__main__':
    l = 16
    b = 6
    print(rhombusarea(l, b))

# Expected output:
48.0
# check if a point lies inside a rectangle | set | function to find if given point lies inside a given rectangle or not . ; 
def FindPoint(x1, y1, x2, y2, x, y):
    if (x > x1 and x < x2 and y > y1 and y < y2):
        return True
    else:
        return False


if __name__ == "__main__":
    x1, y1, x2, y2 = 0, 0, 10, 8
    x, y = 1, 5
    if FindPoint(x1, y1, x2, y2, x, y):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# distance between a point and a plane in 3 d | python program to find the perpendicular ( shortest ) distance between a point and a plane in 3 d . ; function to find distance ; 

import math


def shortest_distance(x1, y1, z1, a, b, c, d):
    d = abs((a * x1 + b * y1 + c * z1 + d))
    e = (math .sqrt(a * a + b * b + c * c))
    print("Perpendicular distance is", d / e)


x1 = 4
y1 = -4
z1 = 3
a = 2
b = -2
c = 5
d = 8
shortest_distance(x1, y1, z1, a, b, c, d)

# Expected output:
Perpendicular distance is 6.789028582272215
# program to find the volume of a triangular prism | function to find the volume of triangular prism ; formula to find volume ; 
def findVolume(l, b, h):
    return ((l * b * h) / 2)


l = 18
b = 12
h = 9
print("Volume of triangular prism: ", findVolume(l, b, h))

# Expected output:
Volume of triangular prism:  972.0
# check if given four integers ( or sides ) make rectangle | function to check if the given integers value make a rectangle ; check all sides of rectangle combinations ; 
def isRectangle(a, b, c, d):
    if (a == b and d == c) or (a == c and b == d) or (a == d and b == c):
        return True
    else:
        return False


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

# Expected output:
No
# program to find the mid | function to find the midpoint of a line ; 
def midpoint(x1, x2, y1, y2):
    print((x1 + x2) // 2, " , ", (y1 + y2) // 2)


x1, y1, x2, y2 = -1, 2, 3, -6
midpoint(x1, x2, y1, y2)

# Expected output:
1  ,  -2
# arc length from given angle | python3 code to calculate length of an arc ; function to calculate arc length ; 

import math


def arcLength(diameter, angle):
    if angle >= 360:
        print("Angle cannot be formed")
        return 0
    else:
        arc = (3.142857142857143 * diameter) * (angle / 360.0)
        return arc


diameter = 25.0
angle = 45.0
arc_len = arcLength(diameter, angle)
print(arc_len)

# Expected output:
9.821428571428571
# check if a line touches or intersects a circle | python program to check if a line touches or intersects or outside a circle . ; finding the distance of line from center . ; checking if the distance is less than , greater than or equal to radius . ; 

import math


def checkCollision(a, b, c, x, y, radius):
    dist = ((abs(a * x + b * y + c)) / math .sqrt(a * a + b * b))
    if (radius == dist):
        print("Touch")
    elif (radius > dist):
        print("Intersect")
    else:
        print("Outside")


radius = 5
x = 0
y = 0
a = 3
b = 4
c = 25
checkCollision(a, b, c, x, y, radius)

# Expected output:
Touch
# program to find circumcenter of a triangle | function to find the line given two points ; function which converts the input line to its perpendicular bisector . it also inputs the points whose mid - point lies on the bisector ; c = - bx + ay ; returns the intersection point of two lines ; the lines are parallel . this is simplified by returning a pair of ( 10.0 ) * * 19 ; line pq is represented as ax + by = c ; line qr is represented as ex + fy = g ; converting lines pq and qr to perpendicular vbisectors . after this , l = ax + by = c m = ex + fy = g ; the point of intersection of l and m gives the circumcenter ; 
def lineFromPoints(P, Q, a, b, c):
    a = Q[1] - P[1]
    b = P[0] - Q[0]
    c = a * (P[0]) + b * (P[1])
    return a, b, c


def perpendicularBisectorFromLine(P, Q, a, b, c):
    mid_point = [(P[0] + Q[0]) // 2, (P[1] + Q[1]) // 2]
    c = -b * (mid_point[0]) + a * (mid_point[1])
    temp = a
    a = -b
    b = temp
    return a, b, c


def lineLineIntersection(a1, b1, c1, a2, b2, c2):
    determinant = a1 * b2 - a2 * b1
    if (determinant == 0):
        return [(10.0)**19, (10.0)**19]
    else:
        x = (b2 * c1 - b1 * c2) // determinant
        y = (a1 * c2 - a2 * c1) // determinant
        return [x, y]


def findCircumCenter(P, Q, R):
    a, b, c = 0.0, 0.0, 0.0
    a, b, c = lineFromPoints(P, Q, a, b, c)
    e, f, g = 0.0, 0.0, 0.0
    e, f, g = lineFromPoints(Q, R, e, f, g)
    a, b, c = perpendicularBisectorFromLine(P, Q, a, b, c)
    e, f, g = perpendicularBisectorFromLine(Q, R, e, f, g)
    circumcenter = lineLineIntersection(a, b, c, e, f, g)
    if (circumcenter[0] == (10.0)**19 and circumcenter[1] == (10.0)**19):
        print("The two perpendicular bisectors found come parallel")
        print("Thus, the given points do not form a triangle and are collinear")
    else:
        print("The circumcenter of the triangle PQR is: ", end="")
        print("(", circumcenter[0], ",", circumcenter[1], ")")


if __name__ == '__main__':
    P = [6, 0]
    Q = [0, 0]
    R = [0, 8]
    findCircumCenter(P, Q, R)

# Expected output:
The circumcenter of the triangle PQR is: ( 3 , 4 )
# program to find area of a triangle | ( 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 = area + (X[j] + X[i]) * (Y[j] - Y[i])
    return abs(area // 2.0)


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

# Expected output:
24.0
# maximize sum of lsbs of bitwise or of all possible n / 2 pairs from given array | function top get lsb value of v ; binary conversion ; function to find the sum of lsbs of all possible pairs of the given array ; stores the lsb of array elements ; storing the lsb values ; sort the array lab_arr [ ] ; taking pairwise sum to get the maximum sum of lsb ; print the result ; 
def chk(v):
    v = list(bin(v)[2:])
    v .reverse()
    if ('1' in v):
        v = v .index('1')
        return (2 ** v)
    else:
        return 0


def sumOfLSB(arr, N):
    lsb_arr = []
    for i in range(N):
        lsb_arr .append(chk(arr[i]))
    lsb_arr .sort(reverse=True)
    ans = 0
    for i in range(0, N - 1, 2):
        ans += (lsb_arr[i + 1])
    print(ans)


N = 5
arr = [1, 2, 3, 4, 5]
sumOfLSB(arr, N)

# Expected output:
3
# count of subsequences having odd bitwise and values in the given array | function to find count of subsequences having odd bitwise and value ; stores count of odd elements ; traverse the array arr [ ] ; if x is odd increment count ; return answer ; 
def countSubsequences(arr):
    odd = 0
    for x in arr:
        if (x & 1):
            odd = odd + 1
    return (1 << odd) - 1


if __name__ == "__main__":
    arr = [1, 3, 3]
    print(countSubsequences(arr))

# Expected output:
7
# count pairs from an array with absolute difference not less than the minimum element in the pair | function to find the number of pairs ( i , j ) such that abs ( a [ i ] - a [ j ] ) is at least the minimum of ( a [ i ] , a [ j ] ) ; stores the resultant count of pairs ; iterate over the range [ 0 , n ] ; iterate from arr [ i ] - ( i % arr [ i ] ) till n with an increment of arr [ i ] ; count the possible pairs ; return the total count ; 
def getPairsCount(arr, n):
    count = 0
    for i in range(n):
        for j in range(arr[i] - (i % arr[i]), n, arr[i]):
            if (i < j and abs(arr[i] - arr[j]) >= min(arr[i], arr[j])):
                count += 1
    return count


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

# Expected output:
3
# minimum steps to change n to 1 by changing it to 2 * n or n / 10 at any step | function to check if n can be changed to 1 or not . ; count the number of 2 in the prime factorisation of n ; count the number of 5 in the prime factorisation of n ; 
def check(N):
    twos = 0
    fives = 0
    while (N % 2 == 0):
        N /= 2
        twos += 1
    while (N % 5 == 0):
        N /= 5
        fives += 1
    if (N == 1 and twos <= fives):
        print(2 * fives - twos)
    else:
        print(-1)


if __name__ == '__main__':
    N = 50
    check(N)

# Expected output:
3
# sum of elements in given range from array formed by infinitely concatenating given array | function to find the sum of elements in a given range of an infinite array ; stores the sum of array elements from l to r ; traverse from l to r ; print the resultant sum ; 
def rangeSum(arr, N, L, R):
    sum = 0
    for i in range(L - 1, R, 1):
        sum += arr[i % N]
    print(sum)


if __name__ == '__main__':
    arr = [5, 2, 6, 9]
    L = 10
    R = 13
    N = len(arr)
    rangeSum(arr, N, L, R)

# Expected output:
22
# sum of elements in given range from array formed by infinitely concatenating given array | function to find the sum of elements in a given range of an infinite array ; stores the prefix sum ; calculate the prefix sum ; stores the sum of elements from 1 to l - 1 ; stores the sum of elements from 1 to r ; print the resultant sum ; 
def rangeSum(arr, N, L, R):
    prefix = [0 for i in range(N + 1)]
    prefix[0] = 0
    for i in range(1, N + 1, 1):
        prefix[i] = prefix[i - 1] + arr[i - 1]
    leftsum = ((L - 1) // N) * prefix[N] + prefix[(L - 1) % N]
    rightsum = (R // N) * prefix[N] + prefix[R % N]
    print(rightsum - leftsum)


if __name__ == '__main__':
    arr = [5, 2, 6, 9]
    L = 10
    R = 13
    N = len(arr)
    rangeSum(arr, N, L, R)

# Expected output:
22
# exponential factorial of n | function to find exponential factorial of a 
def ExpoFactorial(N):
    res = 1
    mod = (int)(1000000007)
    for i in range(2, N + 1):
        res = pow(i, res, mod)
    return res


N = 4
print(ExpoFactorial(N))

# Expected output:
262144
# maximum subarray sum in an array created after repeated concatenation | set | function to find contiguous subarray with maximum sum if array is repeated k times ; store the sum of the array arr [ ] ; traverse the array and find sum ; store the answer ; if k = 1 ; apply kadane algorithm to find sum ; return the answer ; stores the twice repeated array ; traverse the range [ 0 , 2 * n ] ; stores the maximum suffix sum ; stores the maximum prefix sum ; apply kadane algorithm for 2 repetition of the array ; if the sum of the array is greater than 0 ; return the answer ; 
def maxSubArraySumRepeated(arr, N, K):
    sum = 0
    for i in range(N):
        sum += arr[i]
    curr = arr[0]
    ans = arr[0]
    if (K == 1):
        for i in range(1, N, 1):
            curr = max(arr[i], curr + arr[i])
            ans = max(ans, curr)
        return ans
    V = []
    for i in range(2 * N):
        V .append(arr[i % N])
    maxSuf = V[0]
    maxPref = V[2 * N - 1]
    curr = V[0]
    for i in range(1, 2 * N, 1):
        curr += V[i]
        maxPref = max(maxPref, curr)
    curr = V[2 * N - 1]
    i = 2 * N - 2
    while (i >= 0):
        curr += V[i]
        maxSuf = max(maxSuf, curr)
        i -= 1
    curr = V[0]
    for i in range(1, 2 * N, 1):
        curr = max(V[i], curr + V[i])
        ans = max(ans, curr)
    if (sum > 0):
        temp = sum * (K - 2)
        ans = max(ans, max(temp + maxPref, temp + maxSuf))
    return ans


if __name__ == '__main__':
    arr = [10, 20, -30, -1, 40]
    N = len(arr)
    K = 10
    print(maxSubArraySumRepeated(arr, N, K))

# Expected output:
391