giulio98/xlcost-single-prompt huggingface dataset:
# maximize the sum of products of the degrees between any two vertices of the tree | function to return the maximum possible sum ; initialize degree for node u to 2 ; if u is the leaf node or the root node ; initialize degree for node v to 2 ; if v is the leaf node or the root node ; update the sum ; 
def maxSum(N):
    ans = 0
    for u in range(1, N + 1):
        for v in range(1, N + 1):
            if (u == v):
                continue
            degreeU = 2
            if (u == 1 or u == N):
                degreeU = 1
            degreeV = 2
            if (v == 1 or v == N):
                degreeV = 1
            ans += (degreeU * degreeV)
    return ans


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

# Expected output:
82
# find integers that divides maximum number of elements of the array | function to print the integers that divide the maximum number of elements from the array ; initialize two lists to store rank and factors ; start from 2 till the maximum element in arr ; initialize a variable to count the number of elements it is a factor of ; maximum rank in the rank list ; print all the elements with rank m ; 
def maximumFactor(arr):
    rank, factors = [], []
    for i in range(2, max(arr) + 1):
        count = 0
        for j in arr:
            if j % i == 0:
                count += 1
        rank .append(count)
        factors .append(i)
    m = max(rank)
    for i in range(len(rank)):
        if rank[i] == m:
            print(factors[i], end=" ")


arr = [120, 15, 24, 63, 18]
maximumFactor(arr)

# Expected output:
3
# natural numbers | returns sum of first n natural numbers ; 
def findSum(n):
    sum = 0
    x = 1
    while x <= n:
        sum = sum + x
        x = x + 1
    return sum


n = 5
print(findSum(n))

# Expected output:
15
# median | function for calculating median ; first we sort the array ; check for even case ; 
def findMedian(a, n):
    sorted(a)
    if n % 2 != 0:
        return float(a[n // 2])
    return float((a[int((n - 1) / 2)] + a[int(n / 2)]) / 2.0)


a = [1, 3, 4, 2, 7, 5, 8, 6]
n = len(a)
print("Median =", findMedian(a, n))

# Expected output:
Median = 4.5
# mean | function for calculating mean ; 
def findMean(a, n):
    sum = 0
    for i in range(0, n):
        sum += a[i]
    return float(sum / n)


a = [1, 3, 4, 2, 7, 5, 8, 6]
n = len(a)
print("Mean =", findMean(a, n))

# Expected output:
Mean = 4.5
# check if the array has an element which is equal to product of remaining elements | python3 implementation of the above approach ; function to check if the array has an element which is equal to product of all the remaining elements ; storing frequency in map ; calculate the product of all the elements ; if the prod is a perfect square ; then check if its square root exist in the array or not ; 

import math


def CheckArray(arr, n):
    prod = 1
    freq = []
    for i in range(n):
        freq .append(arr[i])
        prod *= arr[i]
    root = math .sqrt(prod)
    if (root * root == prod):
        if root in freq:
            return True
    return False


if __name__ == "__main__":
    arr = [1, 2, 12, 3, 2]
    n = len(arr)
    if (CheckArray(arr, n)):
        print("YES")
    else:
        print("NO")

# Expected output:
YES
# find minimum area of rectangle with given set of coordinates | ''python implementation of above approach ; function to find minimum area of rectangle ; creating empty columns ; fill columns with coordinates ; check if rectangle can be formed ; 

import collections


def minAreaRect(A):
    columns = collections .defaultdict(list)
    for x, y in A:
        columns[x].append(y)
    lastx = {}
    ans = float('inf')
    for x in sorted(columns):
        column = sorted(columns[x])
        for j, y2 in enumerate(column):
            for i in range(j):
                y1 = column[i]
                if (y1, y2) in lastx:
                    ans = min(ans, (x - lastx[y1, y2]) * (y2 - y1))
                lastx[y1, y2] = x
    if ans < float('inf'):
        return ans
    else:
        return 0


A = [[1, 1], [1, 3], [3, 1], [3, 3], [2, 2]]
print(minAreaRect(A))

# Expected output:
4
# check whether a number has consecutive 0 's in the given base or not | we first convert to given base , then check if the converted number has two consecutive 0 s or not ; function to convert n into base k ; weight of each digit ; function to check for consecutive 0 ; flag to check if there are consecutive zero or not ; if there are two consecutive zero then returning false ; 
def toK(N, K):
    w = 1
    s = 0
    while (N != 0):
        r = N % K
        N = N // K
        s = r * w + s
        w *= 10
    return s


def check(N):
    fl = False
    while (N != 0):
        r = N % 10
        N = N // 10
        if (fl and r == 0):
            return False
        if (r > 0):
            fl = False
            continue
        fl = True
    return True


def hasConsecutiveZeroes(N, K):
    z = toK(N, K)
    if (check(z)):
        print("Yes")
    else:
        print("No")


N, K = 15, 8
hasConsecutiveZeroes(N, K)

# Expected output:
Yes
# find the super power of a given number | python3 for finding super power of n ; global hash for prime ; sieve method for storing a list of prime ; function to return super power ; find the super power ; 

MAX = 100000
prime = [True] * 100002


def SieveOfEratosthenes():
    p = 2
    while (p * p <= MAX):
        if (prime[p]):
            i = p * 2
            while (i <= MAX):
                prime[i] = False
                i += p
        p += 1


def superpower(n):
    SieveOfEratosthenes()
    superPower = 0
    factor = 0
    i = 2
    while (n > 1 and i <= MAX):
        if (prime[i]):
            factor = 0
            while (n % i == 0 and n > 1):
                factor += 1
                n = int(n / i)
            if (superPower < factor):
                superPower = factor
        i += 1
    return superPower


n = 256
print(superpower(n))

# Expected output:
8
# smallest prime divisor of a number | function to find the smallest divisor ; if divisible by 2 ; iterate from 3 to sqrt ( n ) ; 
def smallestDivisor(n):
    if (n % 2 == 0):
        return 2
    i = 3
    while (i * i <= n):
        if (n % i == 0):
            return i
        i += 2
    return n


n = 31
print(smallestDivisor(n))

# Expected output:
31
# count number of animals in a zoo from given number of head and legs | function that calculates rabbits ; 
def countRabbits(Heads, Legs):
    count = 0
    count = (Legs) - 2 * (Heads)
    count = count / 2
    return count


if __name__ == '__main__':
    Heads = 100
    Legs = 300
    Rabbits = countRabbits(Heads, Legs)
    print("Rabbits =", Rabbits)
    print("Pigeons =", Heads - Rabbits)

# Expected output:
Rabbits = 50.0
Pigeons = 50.0
# find the sum of series 3 , | calculate sum upto n term of series ; 
def Sum_upto_nth_Term(n):
    return (1 - pow(-2, n))


N = 5
print(Sum_upto_nth_Term(N))

# Expected output:
33
# count numbers whose xor with n is equal to or with n | function to calculate count of numbers with xor equals or ; variable to store count of unset bits ; 
def xorEqualsOrCount(N):
    count = 0
    while (N > 0):
        bit = N % 2
        if bit == 0:
            count += 1
        N //= 2
    return int(pow(2, count))


if __name__ == "__main__":
    N = 7
    print(xorEqualsOrCount(N))

# Expected output:
1
# program to find sum of 1 + x / 2 ! + x ^ 2 / 3 ! + ... + x ^ n / ( n + 1 ) ! | method to find the factorial of a number ; method to compute the sum ; iterate the loop till n and compute the formula ; 
def fact(n):
    if n == 1:
        return 1
    else:
        return n * fact(n - 1)


def sum(x, n):
    total = 1.0
    for i in range(1, n + 1, 1):
        total = total + (pow(x, i) / fact(i + 1))
    return total


if __name__ == '__main__':
    x = 5
    n = 4
    print("Sum is: {0:.4f}".format(sum(x, n)))

# Expected output:
Sum is: 18.0833
# find sum of series 1 ^ 2 | function to find sum of series ; if i is even ; if i is odd ; return the result ; 
def sum_of_series(n):
    result = 0
    for i in range(1, n + 1):
        if (i % 2 == 0):
            result = result - pow(i, 2)
        else:
            result = result + pow(i, 2)
    return result


if __name__ == "__main__":
    n = 3
    print(sum_of_series(n))
    n = 10
    print(sum_of_series(n))

# Expected output:
6
-55
# program to find the sum of the series 23 + 45 + 75 + ... . . upto n terms | calculate nth term of series ; 
def findSum(N):
    return (2 * N * (N + 1) * (4 * N + 17) + 54 * N) / 6


if __name__ == '__main__':
    N = 4
    print(findSum(N))

# Expected output:
256.0
# Program to find Nth term of series 9 , 23 , 45 , 75 , 113. . . | Python program to find N - th term of the series : 9 , 23 , 45 , 75 , 113. . . ; calculate Nth term of series ; Get the value of N ; Find the Nth term and print it
def nthTerm(N):
    return ((2 * N + 3) * (2 * N + 3) - 2 * N)


n = 4
print(nthTerm(n))

# Expected output:
113
# find sum of the series 1 + 22 + 333 + 4444 + ... ... upto n terms | function to calculate sum ; return sum ; 
def solve_sum(n):
    return (pow(10, n + 1) * (9 * n - 1) + 10) / pow(9, 3) - n * (n + 1) / 18


n = 3
print(int(solve_sum(n)))

# Expected output:
356
# find sum of the series 1 | function to calculate sum ; when n is odd ; when n is not odd ; 
def solve_sum(n):
    if (n % 2 == 1):
        return (n + 1) / 2
    return -n / 2


n = 8
print(int(solve_sum(n)))

# Expected output:
-4
# check if a number can be expressed as a ^ b | set 2 | python 3 program to check if a number can be expressed as a ^ b ; 

from math import *


def isPower(a):
    if a == 1:
        return True
    for i in range(2, int(sqrt(a)) + 1):
        val = log(a) / log(i)
        if (round((val - int(val)), 8) < 0.00000001):
            return True
    return False


if __name__ == "__main__":
    n = 16
    if isPower(n):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# program to calculate root mean square | python3 program to calculate root mean square ; function that calculate root mean square ; calculate square ; calculate mean ; calculate root ; 

import math


def rmsValue(arr, n):
    square = 0
    mean = 0.0
    root = 0.0
    for i in range(0, n):
        square += (arr[i]**2)
    mean = (square / (float)(n))
    root = math .sqrt(mean)
    return root


if __name__ == '__main__':
    arr = [10, 4, 6, 8]
    n = len(arr)
    print(rmsValue(arr, n))

# Expected output:
7.3484692283495345
# program to find the quantity after mixture replacement | function to calculate the remaining amount . ; calculate right hand side ( rhs ) . ; calculate amount left by multiply it with original value . ; 
def Mixture(X, Y, Z):
    result = 0.0
    result1 = 0.0
    result1 = ((X - Y) / X)
    result = pow(result1, Z)
    result = result * X
    return result


if __name__ == "__main__":
    X = 10
    Y = 2
    Z = 2
    print("{:.1f}".format(Mixture(X, Y, Z)) + " litres")

# Expected output:
6.4 litres
# sum of sum of all subsets of a set formed by first n natural numbers | modulo value ; iterative function to calculate ( x ^ y ) % p in o ( log y ) ; x = x % p update x if it is more than or equal to p ; if y is odd , multiply x with the result ; y must be even now y = y >> 1 y = y / 2 ; function to find ff ( n ) ; in formula n is starting from zero ; calculate answer using formula 2 ^ n * ( n ^ 2 + n + 2 ) - 1 ; whenever answer is greater than or equals to mod then modulo it . ; adding modulo while subtraction is very necessary otherwise it will cause wrong answer ; 

mod = (int)(1e9 + 7)


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


def check(n):
    n = n - 1
    ans = n * n
    if (ans >= mod):
        ans %= mod
    ans += n + 2
    if (ans >= mod):
        ans %= mod
    ans = (pow(2, n, mod) % mod * ans % mod) % mod
    ans = (ans - 1 + mod) % mod
    return ans


if __name__ == '__main__':
    n = 4
    print(check(n))

# Expected output:
111
# program to find lcm of 2 numbers without using gcd | python 3 program to find lcm of 2 numbers without using gcd ; function to return lcm of two numbers ; 

import sys


def findLCM(a, b):
    lar = max(a, b)
    small = min(a, b)
    i = lar
    while (1):
        if (i % small == 0):
            return i
        i += lar


a = 5
b = 7
print("LCM of ", a, " and ", b, " is ", findLCM(a, b), sep="")

# Expected output:
LCM of 5 and 7 is 35
# smarandache | python program to print the first ' n ' terms of the smarandache - wellin sequence ; function to collect first ' n ' prime numbers ; list to store first ' n ' primes ; function to generate smarandache - wellin sequence ; storing the first ' n ' prime numbers in a list ; 

from __future__ import print_function


def primes(n):
    i, j = 2, 0
    result = []
    while j < n:
        flag = True
        for item in range(2, int(i ** 0.5) + 1):
            if i % item == 0 and i != item:
                flag = False
                break
        if flag:
            result .append(i)
            j += 1
        i += 1
    return result


def smar_wln(n):
    arr = primes(n)
    for i in range(0, len(arr)):
        for j in range(0, i + 1):
            print(arr[j], end='')
        print(end=' ')


if __name__ == '__main__':
    n = 5
    print('First{}termsoftheSequenceare'.format(n))
    smar_wln(n)

# Expected output:
First5termsoftheSequenceare
2 23 235 2357 235711
# pentatope number | function to calculate pentatope number ; formula to calculate nth pentatope number ; 
def Pentatope_number(n):
    return (n * (n + 1) * (n + 2) * (n + 3) // 24)


n = 7
print("%sth Pentatope number : " % n, Pentatope_number(n))
n = 12
print("%sth Pentatope number : " % n, Pentatope_number(n))

# Expected output:
7th Pentatope number :  210
12th Pentatope number :  1365
# program for centered icosahedral number | function to calculate centered icosahedral number ; formula to calculate nth centered icosahedral number ; 
def centeredIcosahedralNum(n):
    return ((2 * n + 1) * (5 * n * n + 5 * n + 3) // 3)


n = 10
print(centeredIcosahedralNum(n))
n = 12
print(centeredIcosahedralNum(n))

# Expected output:
3871
6525
# centered square number | function to calculate centered square number function ; formula to calculate nth centered square number ; 
def centered_square_num(n):
    return n * n + ((n - 1) * (n - 1))


n = 7
print("%sth Centered square number: " % n, centered_square_num(n))

# Expected output:
7th Centered square number:  85
# sum of first n natural numbers | function to find the sum of series ; 
def seriesSum(n):
    return int((n * (n + 1) * (n + 2)) / 6)


n = 4
print(seriesSum(n))

# Expected output:
20
# dodecagonal number | function to calculate dodecagonal number ; formula to calculate nth dodecagonal number ; 
def Dodecagonal_number(n):
    return 5 * n * n - 4 * n


n = 7
print(Dodecagonal_number(n))
n = 12
print(Dodecagonal_number(n))

# Expected output:
217
672
# finding n | a formula based python program to find sum of series with cubes of first n natural numbers ; 
def magicOfSequence(N):
    return (N * (N + 1) / 2) + 2 * N


N = 6
print(int(magicOfSequence(N)))

# Expected output:
33
# form a number using corner digits of powers | storing n raised to power 0 ; find next power by multiplying n with current power ; store digits of power one by one . ; calculate carry . ; store carry in power array . ; prints number formed by corner digits of powers of n . ; initializing empty result ; one by one compute next powers and add their corner digits . ; call function that store power in power array . ; store unit and last digits of power in res . ; 

power = []


def nextPower(N):
    global power
    carry = 0
    for i in range(0, len(power)):
        prod = (power[i] * N) + carry
        power[i] = prod % 10
        carry = (int)(prod / 10)
    while (carry):
        power .append(carry % 10)
        carry = (int)(carry / 10)


def printPowerNumber(X, N):
    global power
    power .append(1)
    res = []
    for i in range(1, X + 1):
        nextPower(N)
        res .append(power[-1])
        res .append(power[0])
    for i in range(0, len(res)):
        print(res[i], end="")


N = 19
X = 4
printPowerNumber(X, N)

# Expected output:
19316911
# first digit in factorial of a number | python3 program for finding the first digit of the large factorial number ; removing trailing 0 s as this does not change first digit . ; loop for divide the fact until it become the single digit and return the fact ; 

import math


def firstDigit(n):
    fact = 1
    for i in range(2, n + 1):
        fact = fact * i
        while (fact % 10 == 0):
            fact = int(fact / 10)
    while (fact >= 10):
        fact = int(fact / 10)
    return math .floor(fact)


n = 5
print(firstDigit(n))

# Expected output:
1
# sum of the series 1.2 . 3 + 2.3 . 4 + ... + n ( n + 1 ) ( n + 2 ) | python 3 program to find sum of the series 1.2 . 3 + 2.3 . 4 + 3.4 . 5 + ... ; 
def sumofseries(n):
    res = 0
    for i in range(1, n + 1):
        res += (i) * (i + 1) * (i + 2)
    return res


print(sumofseries(3))

# Expected output:
90
# find n geometric means between a and b | python3 program to find n geometric means between a and b ; prints n geometric means between a and b . ; calculate common ratio ( r ) ; for finding n the geometric mean between a and b ; 

import math


def printGMeans(A, B, N):
    R = (math .pow((B / A), 1.0 / (N + 1)))
    for i in range(1, N + 1):
        print(int(A * math .pow(R, i)), end=" ")


A = 3
B = 81
N = 2
printGMeans(A, B, N)

# Expected output:
9 27
# numbers having difference with digit sum more than s | function for digit sum ; function to calculate count of integer s . t . integer - digsum > s ; if n < s no integer possible ; iterate for s range and then calculate total count of such integer if starting integer is found ; if no integer found return 0 ; 
def digitSum(n):
    digSum = 0
    while (n > 0):
        digSum += n % 10
        n //= 10
    return digSum


def countInteger(n, s):
    if (n < s):
        return 0
    for i in range(s, min(n, s + 163) + 1):
        if ((i - digitSum(i)) > s):
            return (n - i + 1)
    return 0


n = 1000
s = 100
print(countInteger(n, s))

# Expected output:
891
# division without using ' / ' operator | function to find division without using ' / ' operator ; handling negative numbers ; if num1 is greater than equal to num2 subtract num2 from num1 and increase quotient by one . ; checking if neg equals to 1 then making quotient negative ; 
def division(num1, num2):
    if (num1 == 0):
        return 0
    if (num2 == 0):
        return INT_MAX
    negResult = 0
    if (num1 < 0):
        num1 = -num1
        if (num2 < 0):
            num2 = -num2
        else:
            negResult = true
    elif (num2 < 0):
        num2 = -num2
        negResult = true
    quotient = 0
    while (num1 >= num2):
        num1 = num1 - num2
        quotient += 1
    if (negResult):
        quotient = -quotient
    return quotient


num1 = 13
num2 = 2
print(division(num1, num2))

# Expected output:
6
# nonagonal number | function to find nonagonal number series . ; 
def Nonagonal(n):
    for i in range(1, n + 1):
        print(int(i * (7 * i - 5) / 2), end=" ")


n = 10
Nonagonal(n)

# Expected output:
1 9 24 46 75 111 154 204 261 325
# find n | func for calualtion ; for summation of square of first n - natural nos . ; summation of first n natural nos . ; return result ; 
def seriesFunc(n):
    sumSquare = (n * (n + 1) * (2 * n + 1)) / 6
    sumNatural = (n * (n + 1) / 2)
    return (sumSquare + sumNatural + 1)


n = 8
print(int(seriesFunc(n)))
n = 13
print(int(seriesFunc(n)))

# Expected output:
241
911
# program to check plus perfect number | python 3 implementation to check if the number is plus perfect or not ; function to check plus perfect number ; calculating number of digits ; calculating plus perfect number ; checking whether number is plus perfect or not ; 

import math


def checkplusperfect(x):
    temp = x
    n = 0
    while (x != 0):
        x = x // 10
        n = n + 1
    x = temp
    sm = 0
    while (x != 0):
        sm = sm + (int)(math .pow(x % 10, n))
        x = x // 10
    return (sm == temp)


x = 9474
if (checkplusperfect(x)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# number of distinct subsets of a set | python3 program to count number of distinct subsets in an array of distinct numbers ; returns 2 ^ n ; 

import math


def subsetCount(arr, n):
    return 1 << n


A = [1, 2, 3]
n = len(A)
print(subsetCount(A, n))

# Expected output:
8
# program to calculate gst from original and net prices | python3 program to compute gst from original and net prices . ; return value after calculate gst % ; 
def Calculate_GST(org_cost, N_price):
    return (((N_price - org_cost) * 100) / org_cost)


org_cost = 100
N_price = 120
print("GST = ", end='')
print(round(Calculate_GST(org_cost, N_price)), end='')
print("%")

# Expected output:
GST = 20%
# centered hexagonal number | function to find centered hexagonal number ; formula to calculate nth centered hexagonal ; 
def centeredHexagonalNumber(n):
    return 3 * n * (n - 1) + 1


if __name__ == '__main__':
    n = 10
    print(n, "th centered hexagonal number: ", centeredHexagonalNumber(n))

# Expected output:
10 th centered hexagonal number:  271
# find the distance covered to collect items at equal distances | function to calculate the distance ; 
def find_distance(n):
    return n * ((3 * n) + 7)


n = 5
ans = find_distance(n)
print(ans)

# Expected output:
110
# twin prime numbers | python3 code to check twin prime ; function to check whether a number is prime or not ; corner cases ; this is checked so that we can skip middle five numbers in below loop ; returns true if n1 and n2 are twin primes ; 

import math


def isPrime(n):
    if n <= 1:
        return False
    if n <= 3:
        return True
    if n % 2 == 0 or n % 3 == 0:
        return False
    for i in range(5, int(math .sqrt(n) + 1), 6):
        if n % i == 0 or n % (i + 2) == 0:
            return False
    return True


def twinPrime(n1, n2):
    return (isPrime(n1) and isPrime(n2) and abs(n1 - n2) == 2)


n1 = 137
n2 = 139
if twinPrime(n1, n2):
    print("Twin Prime")
else:
    print("Not Twin Prime")

# Expected output:
Twin Prime
# sum of the sequence 2 , 22 , 222 , ... ... ... | python3 code to find sum of series 2 , 22 , 222 , . . ; function which return the sum of series ; 

import math


def sumOfSeries(n):
    return 0.0246 * (math .pow(10, n) - 1 - (9 * n))


n = 3
print(sumOfSeries(n))

# Expected output:
23.9112
# find sum of even index binomial coefficients | python program to find sum even indexed binomial coefficient ; returns value of even indexed binomial coefficient sum which is 2 raised to power n - 1. ; 

import math


def evenbinomialCoeffSum(n):
    return (1 << (n - 1))


if __name__ == '__main__':
    n = 4
    print(evenbinomialCoeffSum(n))

# Expected output:
8
# program to print triangular number series till n | function to find triangular number ; for each iteration increase j by 1 and add it into k ; increasing j by 1 ; add value of j into k and update k ; 
def triangular_series(n):
    j = 1
    k = 1
    for i in range(1, n + 1):
        print(k, end=' ')
        j = j + 1
        k = k + j


n = 5
triangular_series(n)

# Expected output:
1 3 6 10 15
# sum of the series 1 + ( 1 + 3 ) + ( 1 + 3 + 5 ) + ( 1 + 3 + 5 + 7 ) + ã ¢ â ‚¬¦ ã ¢ â ‚¬¦ + ( 1 + 3 + 5 + 7 + ã ¢ â ‚¬¦ + ( 2 n | functionn to find the sum of the given series ; required sum ; 
def sumOfTheSeries(n):
    return int((n * (n + 1) / 2) * (2 * n + 1) / 3)


n = 5
print("Sum =", sumOfTheSeries(n))

# Expected output:
Sum = 55
# sum of the series 1 + ( 1 + 2 ) + ( 1 + 2 + 3 ) + ( 1 + 2 + 3 + 4 ) + ... ... + ( 1 + 2 + 3 + 4 + ... + n ) | function to find sum of series ; 
def sumOfSeries(n):
    return sum([i * (i + 1) / 2 for i in range(1, n + 1)])


if __name__ == "__main__":
    n = 10
    print(sumOfSeries(n))

# Expected output:
220.0
# number of triangles after n moves | function to calculate number of triangles in nth step ; 
def numberOfTriangles(n):
    ans = 2 * (pow(3, n)) - 1
    return ans


n = 2
print(numberOfTriangles(n))

# Expected output:
17