giulio98/xlcost-single-prompt huggingface dataset:
# long division method to find square root with examples | python3 program to find the square root of a number by using long division method ; function to find the square root of a number by using long division method ; udigit , j = 0 , 0 loop counters ; dividing the number into segments ; last index of the array of segments ; start long division from the last segment ( j = i ) ; initialising the remainder to the maximum value ; including the next segment in new dividend ; loop to check for the perfect square closest to each segment ; this condition is to find the divisor after adding a digit in the range 0 to 9 ; calculating the remainder ; updating the units digit of the quotient ; adding units digit to the quotient ; new divisor is two times quotient ; including the remainder in new dividend ; 

INFINITY_ = 9999999


def sqrtByLongDivision(n):
    i = 0
    cur_divisor = 0
    quotient_units_digit = 0
    cur_quotient = 0
    cur_dividend = 0
    cur_remainder = 0
    a = [0] * 10
    while (n > 0):
        a[i] = n % 100
        n = n // 100
        i += 1
    i -= 1
    for j in range(i, -1, -1):
        cur_remainder = INFINITY_
        cur_dividend = cur_dividend * 100 + a[j]
        for udigit in range(10):
            if (cur_remainder >= cur_dividend - ((cur_divisor * 10 + udigit) * udigit)
                    and cur_dividend - ((cur_divisor * 10 + udigit) * udigit) >= 0):
                cur_remainder = cur_dividend - \
                    ((cur_divisor * 10 + udigit) * udigit)
                quotient_units_digit = udigit
        cur_quotient = cur_quotient * 10 + quotient_units_digit
        cur_divisor = cur_quotient * 2
        cur_dividend = cur_remainder
    return cur_quotient


x = 1225
print(sqrtByLongDivision(x))

# Expected output:
35
# count of consecutive fibonacci pairs in the given array | python3 implementation to count the consecutive fibonacci pairs in the array ; function to find the previous fibonacci for the number n ; function to find the next fibonacci number for the number n ; function to check that a number is a perfect square or not ; function to check that a number is fibonacci number or not ; n is fibinacci if one of ( 5 * n * n + 4 ) or ( 5 * n * n - 4 ) is a perferct square ; function to count the fibonacci pairs in the array ; loop to iterate over the array to choose all pairs of the array ; condition to check if both the number of pair is a fibonacci number ; condition to check if both the number form consecutive fibonacci numbers ; 

from math import sqrt


def previousFibonacci(n):
    a = n / ((1 + sqrt(5)) / 2.0)
    return round(a)


def nextFibonacci(n):
    a = n * (1 + sqrt(5)) / 2.0
    return round(a)


def isPerfectSquare(x):
    s = sqrt(x)
    return (s * s == x)


def isFibonacci(n):
    return (isPerfectSquare(5 * n * n + 4) or isPerfectSquare(5 * n * n - 4))


def countFibonacciPairs(arr, n):
    res = 0
    for i in range(n):
        for j in range(i + 1, n):
            if (isFibonacci(arr[i]) and isFibonacci(arr[j])):
                prevFib = previousFibonacci(arr[i])
                nextFib = nextFibonacci(arr[i])
                if (prevFib == arr[j] or nextFib == arr[j]):
                    res += 1
    return res


a = [3, 5, 8, 11]
n = len(a)
print(countFibonacciPairs(a, n))

# Expected output:
2
# count all distinct pairs with product equal to k | function to count the number of pairs whose product is equal to k ; pick all elements one by one ; check if the product of this pair is equal to k ; 
def countPairsWithProdK(arr, n, k):
    count = 0
    for i in range(n):
        for j in range(i + 1, n):
            if (arr[i] * arr[j] == k):
                count += 1
    return count


arr = [1, 5, 3, 4, 2]
N = len(arr)
K = 3
print(countPairsWithProdK(arr, N, K))

# Expected output:
1
# count all distinct pairs with product equal to k | python3 program to count the number of pairs whose product is equal to k ; function to count the number of pairs whose product is equal to k ; initialize the count ; initialize empty hashmap . ; insert array elements to hashmap ; checking if the index is a whole number and present in the hashmap ; 

MAX = 100000


def countPairsWithProductK(arr, n, k):
    count = 0
    hashmap = [False] * MAX
    for i in range(n):
        hashmap[arr[i]] = True
    for i in range(n):
        x = arr[i]
        index = 1.0 * k / arr[i]
        if (index >= 0 and ((index - int(index)) == 0) and hashmap[k // x]):
            count += 1
        hashmap[x] = False
    return count


if __name__ == "__main__":
    arr = [1, 5, 3, 4, 2]
    N = len(arr)
    K = 3
    print(countPairsWithProductK(arr, N, K))

# Expected output:
1
# find any k distinct odd integers such that their sum is equal to n | function to find k odd integers such that their sum is n ; condition to check if there exist such k integers ; loop to find first k - 1 distinct odd integers ; final kth odd number ; 
def oddIntegers(n, k):
    if (n % 2 != k % 2):
        print("-1")
        return
    sum = 0
    i = 1
    j = 1
    while (j < k):
        sum += i
        print(i, end=" ")
        i += 2
        j += 1
    finalOdd = n - sum
    print(finalOdd)


if __name__ == "__main__":
    n = 10
    k = 2
    oddIntegers(n, k)

# Expected output:
1 9
# sum of product of proper divisors of all numbers lying in range [ l , r ] | python3 implementation to find the sum of the product of proper divisors of all the numbers lying in the range [ l , r ] ; vector to store the product of the proper divisors of a number ; variable to store the prefix sum of the product array ; function to precompute the product of proper divisors of a number at it 's corresponding index ; modificatino of sieve to store the product of the proper divisors ; multiplying the existing value with i because i is the proper divisor of ans [ j ] ; loop to store the prefix sum of the previously computed product array ; computing the prefix sum ; function to prthe sum for each query ; function to prte sum of product of proper divisors of a number in [ l , r ] ; calling the function that pre computes the sum of product of proper divisors ; iterate over all queries to prthe sum ; 

mod = 1000000007
ans = [1] * (100002)
pref = [0] * 100002


def preCompute():
    for i in range(2, 100000 // 2 + 1):
        for j in range(2 * i, 100000 + 1, i):
            ans[j] = (ans[j] * i) % mod
    for i in range(1, 100002):
        pref[i] = pref[i - 1] + ans[i]
        pref[i] %= mod


def printSum(L, R):
    print(pref[R] - pref[L - 1], end=" ")


def printSumProper(arr, Q):
    preCompute()
    for i in range(Q):
        printSum(arr[i][0], arr[i][1])


if __name__ == '__main__':
    Q = 2
    arr = [[10, 20], [12, 16]]
    printSumProper(arr, Q)

# Expected output:
975 238
# generate an array of size n according to the given rules | function to search the most recent location of element n if not present in the array it will return - 1 ; function to generate an array of size n by following the given rules ; loop to fill the array as per the given rules ; check for the occurrence of arr [ i - 1 ] ; 
def search(a, k, x):
    for j in range(k - 1, -1, -1):
        if (a[j] == x):
            return j
    return -1


def genArray(arr, N):
    for i in range(0, N - 1, 1):
        if (search(arr, i, arr[i]) == -1):
            arr[i + 1] = 0
        else:
            arr[i + 1] = (i - search(arr, i, arr[i]))


if __name__ == "__main__":
    N = 5
    size = N + 1
    a = [0] * N
    genArray(a, N)
    print(a)

# Expected output:
[0, 0, 1, 0, 2]
# count of pairs in an array whose sum is a perfect cube | function to return an arraylist containing all the perfect cubes upto n ; while current perfect cube is less than or equal to n ; function to print the sum of maximum two elements from the array ; function to return the count of numbers that when added with n give a perfect cube ; temp > n is checked so that pairs ( x , y ) and ( y , x ) don 't get counted twice ; function to count the pairs whose sum is a perfect cube ; sum of the maximum two elements from the array ; list of perfect cubes upto max ; contains all the array elements ; add count of the elements that when added with arr [ i ] give a perfect cube ; 
def getPerfectcubes(n):
    perfectcubes = []
    current = 1
    i = 1
    while (current <= n):
        perfectcubes .append(current)
        i += 1
        current = int(pow(i, 3))
    return perfectcubes


def maxPairSum(arr):
    n = len(arr)
    max = 0
    secondMax = 0
    if (arr[0] > arr[1]):
        max = arr[0]
        secondMax = arr[1]
    else:
        max = arr[1]
        secondMax = arr[0]
    for i in range(2, n):
        if (arr[i] > max):
            secondMax = max
            max = arr[i]
        elif (arr[i] > secondMax):
            secondMax = arr[i]
    return (max + secondMax)


def countPairsWith(n, perfectcubes, nums):
    count = 0
    for i in range(len(perfectcubes)):
        temp = perfectcubes[i] - n
        if (temp > n and (temp in nums)):
            count += 1
    return count


def countPairs(arr):
    n = len(arr)
    max = maxPairSum(arr)
    perfectcubes = getPerfectcubes(max)
    nums = []
    for i in range(n):
        nums .append(arr[i])
    count = 0
    for i in range(n):
        count += countPairsWith(arr[i], perfectcubes, nums)
    return count


arr = [2, 6, 18, 9, 999, 1]
print(countPairs(arr))

# Expected output:
3
# maximum squares possible parallel to both axes from n distinct points | function that returns the count of squares parallel to both x and y - axis from a given set of points ; initialize result ; initialize a set to store points ; initialize a map to store the points in the same vertical line ; store the points in a set ; store the points in the same vertical line i . e . with same x co - ordinates ; check for every two points in the same vertical line ; check if other two point are present or not ; 
def countSquares(X, Y, N):
    count = 0
    points = []
    vertical = dict .fromkeys(X, None)
    for i in range(N):
        points .append((X[i], Y[i]))
    for i in range(N):
        if vertical[X[i]] is None:
            vertical[X[i]] = [Y[i]]
        else:
            vertical[X[i]].append(Y[i])
    for line in vertical:
        X1 = line
        yList = vertical[line]
        for i in range(len(yList)):
            Y1 = yList[i]
            for j in range(i + 1, len(yList)):
                Y2 = yList[j]
                side = abs(Y1 - Y2)
                X2 = X1 + side
                if (X2, Y1) in points and (X2, Y2) in points:
                    count += 1
    return count


if __name__ == "__main__":
    X = [0, 2, 0, 2]
    Y = [0, 2, 2, 0]
    N = len(X)
    print(countSquares(X, Y, N))

# Expected output:
1
# number of perfect cubes between two given numbers | function to count cubes between two numbers ; traverse through all numbers ; check if current number ' i ' is perfect cube ; 
def countCubes(a, b):
    cnt = 0
    for i in range(a, b + 1):
        for j in range(i + 1):
            if j * j * j > i:
                break
            if j * j * j == i:
                cnt += 1
    return cnt


if __name__ == '__main__':
    a = 7
    b = 30
    print("Count of Cubes is ", countCubes(a, b))

# Expected output:
Count of Cubes is  2
# number of perfect cubes between two given numbers | an efficient method to count cubes between a and b ; function to count cubes between two numbers ; 

from math import *


def countCubes(a, b):
    return (floor(b ** (1. / 3.)) - ceil(a ** (1. / 3.)) + 1)


a = 7
b = 28
print("Count of cubes is", countCubes(a, b))

# Expected output:
Count of cubes is 2
# xor and or of all n | function to check if a number is armstrong or not ; function to find xor of all n - digits armstrong number ; to store the xor and or of all armstrong number ; starting n - digit armstrong number ; ending n - digit armstrong number ; iterate over starting and ending number ; to check if i is armstrong or not ; print the xor and or of all armstrong number ; 
def isArmstrong(x, n):
    sum1 = 0
    temp = x
    while temp > 0:
        digit = temp % 10
        sum1 += digit ** n
        temp //= 10
    return sum1 == x


def CalculateXORandOR(n):
    CalculateXOR = 0
    CalculateOR = 0
    start = 10 ** (n - 1)
    end = (10 ** n) - 1
    for i in range(start, end + 1):
        if (isArmstrong(i, n)):
            CalculateXOR = CalculateXOR ^ i
            CalculateOR = CalculateOR | i
    print("XOR = ", CalculateXOR)
    print("OR = ", CalculateOR)


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

# Expected output:
XOR =  880
OR =  10098
# perfect cube | function to check if a number is a perfect cube or not ; iterate from 1 - n ; find the cube of every number ; check if cube equals n or not ; 
def perfectCube(N):
    cube = 0
    for i in range(N + 1):
        cube = i * i * i
        if (cube == N):
            print("Yes")
            return
        elif (cube > N):
            print("NO")
            return


if __name__ == "__main__":
    N = 216
    perfectCube(N)

# Expected output:
Yes
# perfect cube | function to check if a number is a perfect cube using inbuilt function ; if cube of cube_root is equals to n , then print yes else print no ; 
def perfectCube(N):
    cube_root = round(N ** (1 / 3))
    if cube_root * cube_root * cube_root == N:
        print("Yes")
        return
    else:
        print("NO")
        return


if __name__ == "__main__":
    N = 125
    perfectCube(N)

# Expected output:
Yes
# check if given array can be made 0 with given operations performed any number of times | function to whether the array can be made zero or not ; count for even elements ; count for odd elements ; traverse the array to count the even and odd ; if arr [ i ] is odd ; if arr [ i ] is even ; check if count of even is zero or count of odd is zero ; 
def check(arr, N):
    even = 0
    odd = 0
    for i in range(N):
        if (arr[i] % 2 == 1):
            odd += 1
        else:
            even += 1
    if (even == N or odd == N):
        print("Yes")
    else:
        print("No")


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

# Expected output:
Yes
# calculate sum of all integers from 1 to n , excluding perfect power of 2 | python 3 implementation of the approach ; function to find the required summation ; find the sum of first n integers using the formula ; find the sum of numbers which are exact power of 2 by using the formula ; print the final sum ; 

from math import log2, pow


def findSum(N):
    sum = (N) * (N + 1) // 2
    r = log2(N) + 1
    expSum = pow(2, r) - 1
    print(int(sum - expSum))


if __name__ == '__main__':
    N = 2
    findSum(N)

# Expected output:
0
# largest even and odd n | function to print the largest n - digit even and odd numbers of base b ; initialise the number ; if base b is even , then b ^ n will give largest even number of n + 1 digit ; to get even number of n digit subtract 2 from b ^ n ; to get odd number of n digit subtract 1 from b ^ n ; if base b is odd , then b ^ n will give largest odd number of n + 1 digit ; to get even number of n digit subtract 1 from b ^ n ; to get odd number of n digit subtract 2 from b ^ n ; 
def findNumbers(n, b):
    even = 0
    odd = 0
    if (b % 2 == 0):
        even = pow(b, n) - 2
        odd = pow(b, n) - 1
    else:
        even = pow(b, n) - 1
        odd = pow(b, n) - 2
    print("Even Number = ", int(even))
    print("Odd Number = ", int(odd))


if __name__ == '__main__':
    N = 2
    B = 5
    findNumbers(N, B)

# Expected output:
Even Number =  24
Odd Number =  23
# minimum divisor of a number to make the number perfect cube | returns the minimum divisor ; since 2 is only even prime , compute its power seprately . ; if count is not divisible by 3 , it must be removed by dividing n by prime number power . ; if count is not divisible by 3 , it must be removed by dividing n by prime number power . ; if n is a prime number ; 
def findMinNumber(n):
    count = 0
    ans = 1
    while (n % 2 == 0):
        count += 1
        n /= 2
    if (count % 3 != 0):
        ans *= pow(2, (count % 3))
    for i in range(3, int(pow(n, 1 / 2)), 2):
        count = 0
        while (n % i == 0):
            count += 1
            n /= i
        if (count % 3 != 0):
            ans *= pow(i, (count % 3))
    if (n > 2):
        ans *= n
    return ans


if __name__ == '__main__':
    n = 128
    print(findMinNumber(n))

# Expected output:
2
# check whether the number can be made perfect square after adding k | python3 program to check whether the number can be made perfect square after adding k ; function to check whether the number can be made perfect square after adding k ; computing the square root of the number ; print yes if the number is a perfect square ; 

from math import sqrt


def isPerfectSquare(x):
    sr = int(sqrt(x))
    if (sr * sr == x):
        print("Yes")
    else:
        print("No")


if __name__ == "__main__":
    n = 7
    k = 2
    isPerfectSquare(n + k)

# Expected output:
Yes
# number of times the largest perfect cube can be subtracted from n | python3 implementation of the approach ; function to return the count of steps ; variable to store the count of steps ; iterate while n > 0 ; get the largest perfect cube and subtract it from n ; increment steps ; return the required count ; 

from math import floor


def countSteps(n):
    steps = 0
    while (n):
        largest = floor(n ** (1 / 3))
        n -= (largest * largest * largest)
        steps += 1
    return steps


n = 150
print(countSteps(n))

# Expected output:
5
# product of all subsets of a set formed by first n natural numbers | function to find the product of all elements in all subsets in natural numbers from 1 to n ; 
def product(N):
    ans = 1
    val = 2 ** (N - 1)
    for i in range(1, N + 1):
        ans *= (i ** val)
    return ans


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

# Expected output:
4
# radii of the three tangent circles of equal radius which are inscribed within a circle of given radius | python3 program to find the radii of the three tangent circles of equal radius when the radius of the circumscribed circle is given ; 
def threetangcircle(R):
    print("The radii of the tangent", "circles is ", end="")
    print(0.4645 * R)


R = 4
threetangcircle(R)

# Expected output:
The radii of the tangent circles is 1.858
# number of common tangents between two circles if their centers and radius is given | python3 program to find the number of common tangents between the two circles ; 
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, y1 = -10, 8
x2, y2 = 14, -24
r1, r2 = 30, 10
t = circle(x1, y1, x2, y2, r1, r2)
if (t == 1):
    print("There are 3 common tangents between the circles.")
elif (t < 0):
    print("There are 4 common tangents between the circles.")
else:
    print("There are 2 common tangents between the circles.")

# Expected output:
There are 3 common tangents between the circles.
# ratio of the distance between the centers of the circles and the point of intersection of two transverse common tangents to the circles | python3 program to find the ratio of the distance between the centres of the circles and the point of intersection of two transverse common tangents to the circles which do not touch each other ; function to find the ratio ; 
def GCD(a, b):
    if (b != 0):
        return GCD(b, a % b)
    else:
        return a


def ratiotang(r1, r2):
    print("The ratio is", r1 // GCD(r1, r2), ":", r2 // GCD(r1, r2))


r1 = 4
r2 = 8
ratiotang(r1, r2)

# Expected output:
The ratio is 1 : 2
# program to find the number of region in planar graph | function to return the number of regions in a planar graph ; 
def Regions(Vertices, Edges):
    R = Edges + 2 - Vertices
    return R


if __name__ == "__main__":
    V = 5
    E = 7
    print(Regions(V, E))

# Expected output:
4
# ratio of the distance between the centers of the circles and the point of intersection of two direct common tangents to the circles | function to find the gcd ; function to find the ratio ; 

from math import gcd


def ratiotang(r1, r2):
    print("The ratio is", int(r1 / gcd(r1, r2)), ":", int(r2 / gcd(r1, r2)))


if __name__ == '__main__':
    r1 = 4
    r2 = 6
    ratiotang(r1, r2)

# Expected output:
The ratio is 2 : 3
# length of the transverse common tangent between the two non intersecting circles | python 3 program to find the length of the transverse common tangent between two circles which do not touch each other ; function to find the length of the transverse common tangent ; 

from math import sqrt, pow


def lengthOfTangent(r1, r2, d):
    print("The length of the transverse", "common tangent is",
          '{0:.6g}'.format(sqrt(pow(d, 2) - pow((r1 + r2), 2))))


if __name__ == '__main__':
    r1 = 4
    r2 = 6
    d = 12
    lengthOfTangent(r1, r2, d)

# Expected output:
The length of the transverse common tangent is 6.63325
# area of plot remaining at the end | function to return the area of the remaining plot ; continue while plot has positive area and there are persons left ; if length > breadth then subtract breadth from length ; else subtract length from breadth ; 
def remainingArea(N, M, K):
    while (K > 0 and N > 0 and M > 0):
        if (N > M):
            N = N - M
        else:
            M = M - N
        K = K - 1
    if (N > 0 and M > 0):
        return N * M
    else:
        return 0


N = 5
M = 3
K = 2
print(remainingArea(N, M, K))

# Expected output:
2
# length of the direct common tangent between two externally touching circles | function to find the length of the direct common tangent ; 
def lengtang(r1, r2):
    print("The length of the direct",
          "common tangent is",
          2 * (r1 * r2)**(1 / 2))


r1 = 5
r2 = 9
lengtang(r1, r2)

# Expected output:
The length of the direct common tangent is 13.416407864998739
# shortest distance between a point and a circle | function to find the shortest distance ; 
def dist(x1, y1, x2, y2, r):
    print("The shortest distance between a point and a circle is ",
          ((((x2 - x1)**2) + ((y2 - y1)**2))**(1 / 2)) - r)


x1 = 4
y1 = 6
x2 = 35
y2 = 42
r = 5
dist(x1, y1, x2, y2, r)

# Expected output:
The shortest distance between a point and a circle is  42.50789408087881
# distance between two parallel lines | function to find the distance between parallel lines ; 
def dist(m, b1, b2):
    d = abs(b2 - b1) / ((m * m) - 1)
    return d


def main():
    m, b1, b2 = 2, 4, 3
    print(dist(m, b1, b2))


if __name__ == '__main__':
    main()

# Expected output:
0.3333333333333333
# length of the normal from origin on a straight line whose intercepts are given | python3 implementation of the approach ; function to find the normal of the straight line ; length of the normal ; 

import math


def normal(m, n):
    N = ((abs(m) * abs(n)) / math .sqrt((abs(m) * abs(m)) + (abs(n) * abs(n))))
    return N


m = -5
n = 3
print(normal(m, n))

# Expected output:
2.5724787771376323
# check if it is possible to create a polygon with given n sides | function to check whether it is possible to create a polygon with given sides length ; sum stores the sum of all the sides and maxs stores the length of the largest side ; if the length of the largest side is less than the sum of the other remaining sides ; 
def isPossible(a, n):
    sum = 0
    maxS = 0
    for i in range(n):
        sum += a[i]
        maxS = max(a[i], maxS)
    if ((sum - maxS) > maxS):
        return True
    return False


a = [2, 3, 4]
n = len(a)
if (isPossible(a, n)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# find the area of the shaded region formed by the intersection of four semicircles in a square | function to return the area of the shaded region ; area of the square ; area of the semicircle ; there are 4 semicircles shadedarea = area of 4 semicircles - area of square ; 
def findAreaShaded(a):
    sqArea = a * a
    semiCircleArea = (3.14 * (a * a) / 8)
    ShadedArea = 4 * semiCircleArea - sqArea
    return ShadedArea


if __name__ == '__main__':
    a = 10
    print(findAreaShaded(a))

# Expected output:
57.0