giulio98/xlcost-single-prompt huggingface dataset:
# program to check if n is a centered tridecagonal number | python3 program for the above approach ; function to check if the number n is a centered tridecagonal number ; condition to check if n is centered tridecagonal number ; 

import numpy as np


def isCenteredtridecagonal(N):
    n = (13 + np .sqrt(104 * N + 65)) / 26
    return (n - int(n)) == 0


N = 14
if (isCenteredtridecagonal(N)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# find the nth natural number which is not divisible by a | python3 code to find the nth number which is not divisible by a from the series ; find the quotient and the remainder when k is divided by n - 1 ; if remainder is 0 then multiply n by q and add the remainder ; print the answer ; 
def findNum(n, k):
    q = k // (n - 1)
    r = k % (n - 1)
    if (r != 0):
        a = (n * q) + r
    else:
        a = (n * q) - 1
    print(a)


A = 4
N = 6
findNum(A, N)

# Expected output:
7
# count the nodes in the given tree whose weight is a perfect number | python3 implementation to count the nodes in the 

graph = [[]for i in range(100)]
weight = [0] * 100
ans = 0


def isPerfect(n):
    sum = 1
    i = 2
    while (i * i < n):
        if (n % i == 0):
            if (i * i != n):
                sum = sum + i + n / i
            else:
                sum = sum + i
        i += 1
    if (sum == n and n != 1):
        return True
    return False


def dfs(Node, parent):
    global ans
    if (isPerfect(weight[Node])):
        ans += 1
    for to in graph[Node]:
        if (to == parent):
            continue
        dfs(to, Node)


weight[1] = 5
weight[2] = 10
weight[3] = 11
weight[4] = 8
weight[5] = 6
graph[1].append(2)
graph[2].append(3)
graph[2].append(4)
graph[1].append(5)
dfs(1, 1)
print(ans)

# Expected output:
1
# minimum cost to merge numbers from 1 to n | function returns the minimum cost ; min heap representation ; add all elements to heap ; first minimum ; second minimum ; multiply them ; add to the cost ; push the product into the heap again ; return the optimal cost ; 
def GetMinCost(N):
    pq = []
    for i in range(1, N + 1, 1):
        pq .append(i)
    pq .sort(reverse=False)
    cost = 0
    while (len(pq) > 1):
        mini = pq[0]
        pq .remove(pq[0])
        secondmini = pq[0]
        pq .remove(pq[0])
        current = mini * secondmini
        cost += current
        pq .append(current)
        pq .sort(reverse=False)
    return cost


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

# Expected output:
148
# permutation of first n positive integers such that prime numbers are at prime indices | set 2 | python3 program to count permutations from 1 to n such that prime numbers occur at prime indices ;  ; computing count of prime numbers using sieve ; computing permutations for primes ; computing permutations for non - primes ; 

import math


def numPrimeArrangements(n):
    prime = [True for i in range(n + 1)]
    prime[0] = False
    prime[1] = False
    for i in range(2, int(math .sqrt(n)) + 1):
        if prime[i]:
            factor = 2
            while factor * i <= n:
                prime[factor * i] = False
                factor += 1
    primeIndices = 0
    for i in range(1, n + 1):
        if prime[i]:
            primeIndices += 1
    mod = 1000000007
    res = 1
    for i in range(1, primeIndices + 1):
        res = (res * i) % mod
    for i in range(1, n - primeIndices + 1):
        res = (res * i) % mod
    return res


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

# Expected output:
12
# probability of getting k heads in n coin tosses | function to calculate factorial ; applying the formula ; 
def fact(n):
    res = 1
    for i in range(2, n + 1):
        res = res * i
    return res


def count_heads(n, r):
    output = fact(n) / (fact(r) * fact(n - r))
    output = output / (pow(2, n))
    return output


n = 4
r = 3
print(count_heads(n, r))

# Expected output:
0.25
# largest component size in a graph formed by connecting non |  ; mark this node as visited ; apply dfs and add nodes belonging to this component ; create graph and store in adjacency list form ; iterate over all pair of nodes ; if not co - prime ; build undirected graph ; visited array for dfs ; 

from math import gcd


def dfs(u, adj, vis):
    vis[u] = 1
    componentSize = 1
    for x in adj[u]:
        if (vis[x] == 0):
            componentSize += dfs(x, adj, vis)
    return componentSize


def maximumComponentSize(a, n):
    adj = [[]for i in range(n)]
    for i in range(n):
        for j in range(i + 1, n):
            if (gcd(a[i], a[j]) > 1):
                adj[i].append(j)
            adj[j].append(i)
    answer = 0
    vis = [0 for i in range(n)]
    for i in range(n):
        if (vis[i] == False):
            answer = max(answer, dfs(i, adj, vis))
    return answer


if __name__ == '__main__':
    n = 8
    A = [2, 3, 6, 7, 4, 12, 21, 39]
    print(maximumComponentSize(A, n))

# Expected output:
8
# largest component size in a graph formed by connecting non | smallest prime factor ; sieve of eratosthenes ; is spf [ i ] = 0 , then it 's prime ; smallest prime factor of all multiples is i ; prime factorise n , and store prime factors in set s ; for implementing dsu ; root of component of node i ; finding root as well as applying path compression ; merging two components ; find roots of both components ; if already belonging to the same compenent ; union by rank , the rank in this case is sizecontainer of the component . smaller sizecontainer will be merged into larger , so the larger 's root will be  final root ; function to find the maximum sized container ; intitalise the parents , and component sizecontainer ; intitally all component sizecontainers are 1 ans each node it parent of itself ; store prime factors of a [ i ] in s ; if this prime is seen as a factor for the first time ; if not then merge with that component in which this prime was previously seen ; maximum of sizecontainer of all components ; 

spf = [0 for i in range(100005)]


def sieve():
    for i in range(2, 100005):
        if (spf[i] == 0):
            spf[i] = i
            for j in range(2 * i, 100005, i):
                if (spf[j] == 0):
                    spf[j] = i


def factorize(n, s):
    while (n > 1):
        z = spf[n]
        s .add(z)
        while (n % z == 0):
            n //= z


id = [0 for i in range(100005)]
par = [0 for i in range(100005)]
sizeContainer = [0 for i in range(100005)]


def root(i):
    if (par[i] == i):
        return i
    else:
        return root(par[i])
        return par[i]


def merge(a, b):
    p = root(a)
    q = root(b)
    if (p == q):
        return
    if (sizeContainer[p] > sizeContainer[q]):
        p = p + q
        q = p - q
        p = p - q
    par[p] = q
    sizeContainer[q] += sizeContainer[p]


def maximumComponentsizeContainer(a, n):
    for i in range(100005):
        par[i] = i
        sizeContainer[i] = 1
    sieve()
    for i in range(n):
        s = set()
        factorize(a[i], s)
        for it in s:
            if (id[it] == 0):
                id[it] = i + 1
            else:
                merge(i + 1, id[it])
    answer = 0
    for i in range(n):
        answer = max(answer, sizeContainer[i])
    return answer


if __name__ == '__main__':
    n = 8
    A = [2, 3, 6, 7, 4, 12, 21, 39]
    print(maximumComponentsizeContainer(A, n))

# Expected output:
8
# check if original array is retained after performing xor with m exactly k times | function to check if original array can be retained by performing xor with m exactly k times ; check if o is present or not ; if k is odd and 0 is not present then the answer will always be no . ; else it will be yes ; 
def check(Arr, n, M, K):
    flag = 0
    for i in range(n):
        if (Arr[i] == 0):
            flag = 1
    if (K % 2 != 0 and flag == 0):
        return "No"
    else:
        return "Yes"


if __name__ == '__main__':
    Arr = [1, 1, 2, 4, 7, 8]
    M = 5
    K = 6
    n = len(Arr)
    print(check(Arr, n, M, K))

# Expected output:
Yes
# dixon 's factorization method with implementation | python 3 implementation of dixon factorization algo ; function to find the factors of a number using the dixon factorization algorithm ; factor base for the given number ; starting from the ceil of the root of the given number n ; storing the related squares ; for every number from the square root till n ; finding the related squares ; if the two numbers are the related squares , then append them to the array ; for every pair in the array , compute the gcd such that ; if we find a factor other than 1 , then appending it to the final factor array ; 

from math import sqrt, gcd
import numpy as np


def factor(n):
    base = [2, 3, 5, 7]
    start = int(sqrt(n))
    pairs = []
    for i in range(start, n):
        for j in range(len(base)):
            lhs = i ** 2 % n
            rhs = base[j]**2 % n
            if (lhs == rhs):
                pairs .append([i, base[j]])
    new = []
    for i in range(len(pairs)):
        factor = gcd(pairs[i][0] - pairs[i][1], n)
        if (factor != 1):
            new .append(factor)
    x = np .array(new)
    return (np .unique(x))


if __name__ == "__main__":
    print(factor(23449))

# Expected output:
[131 179]
# Sum of all armstrong numbers lying in the range [ L , R ] for Q queries | pref [ ] array to precompute the sum of all armstrong number ; Function that return number num if num is armstrong else return 0 ; Function to precompute the sum of all armstrong numbers upto 100000 ; checkarmstrong ( ) return the number i if i is armstrong else return 0 ; Function to print the sum for each query ; Function to prsum of all armstrong numbers between [ L , R ] ; Function that pre computes the sum of all armstrong numbers ; Iterate over all Queries to print the sum ; Queries ; Function that print the the sum of all armstrong number in Range [ L , R ]

pref = [0] * 100001


def checkArmstrong(x):
    n = len(str(x))
    sum1 = 0
    temp = x
    while temp > 0:
        digit = temp % 10
        sum1 += digit ** n
        temp //= 10
    if sum1 == x:
        return x
    return 0


def preCompute():
    for i in range(1, 100001):
        pref[i] = pref[i - 1] + checkArmstrong(i)


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


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


Q = 2
arr = [[1, 13], [121, 211]]
printSumarmstrong(arr, Q)

# Expected output:
45
153
# total number of subsets of size at most k | function to compute the value of binomial coefficient c ( n , k ) ; calculate value of binomial coefficient in bottom up manner ; base cases ; calculate value using previously stored values ; function to calculate sum of ncj from j = 1 to k ; calling the ncr function for each value of j ; 
def binomialCoeff(n, k):
    C = [[0 for i in range(k + 1)]for j in range(n + 1)]
    i, j = 0, 0
    for i in range(n + 1):
        for j in range(min(i, k) + 1):
            if (j == 0 or j == i):
                C[i][j] = 1
            else:
                C[i][j] = C[i - 1][j - 1] + C[i - 1][j]
    return C[n][k]


def count(n, k):
    sum = 0
    for j in range(1, k + 1):
        sum = sum + binomialCoeff(n, j)
    return sum


if __name__ == '__main__':
    n = 3
    k = 2
    print(count(n, k), end="")
    n1 = 5
    k1 = 2
    print(count(n1, k1))

# Expected output:
615
# Tree Isomorphism Problem | A Binary tree node ; Check if the binary tree is isomorphic or not ; Both roots are None , trees isomorphic by definition ; Exactly one of the n1 and n2 is None , trees are not isomorphic ; There are two possible cases for n1 and n2 to be isomorphic Case 1 : The subtrees rooted at these nodes have NOT been " Flipped " . Both of these subtrees have to be isomorphic , hence the && Case 2 : The subtrees rooted at these nodes have been " Flipped " ; Let us create trees shown in above diagram

class Node:
    def __init__(self, data):
        self .data = data
        self .left = None
        self .right = None


def isIsomorphic(n1, n2):
    if n1 is None and n2 is None:
        return True
    if n1 is None or n2 is None:
        return False
    if n1 .data != n2 .data:
        return False
    return ((isIsomorphic(n1 .left, n2 .left) and isIsomorphic(n1 .right, n2 .right)) or (
        isIsomorphic(n1 .left, n2 .right) and isIsomorphic(n1 .right, n2 .left)))


n1 = Node(1)
n1 .left = Node(2)
n1 .right = Node(3)
n1 .left .left = Node(4)
n1 .left .right = Node(5)
n1 .right .left = Node(6)
n1 .left .right .left = Node(7)
n1 .left .right .right = Node(8)
n2 = Node(1)
n2 .left = Node(3)
n2 .right = Node(2)
n2 .right .left = Node(4)
n2 .right .right = Node(5)
n2 .left .right = Node(6)
n2 .right .right .left = Node(8)
n2 .right .right .right = Node(7)
print("Yes") if (isIsomorphic(n1, n2)) else ("No")

# Expected output:
Yes
# find smallest number with given number of digits and sum of digits under given constraints | function to find the number having sum of digits as s and d number of digits such that the difference between the maximum and the minimum digit the minimum possible ; to store the final number ; to store the value that is evenly distributed among all the digits ; to store the remaining sum that still remains to be distributed among d digits ; rem stores the value that still remains to be distributed to keep the difference of digits minimum last rem digits are incremented by 1 ; in the last rem digits one is added to the value obtained by equal distribution ; 
def findNumber(s, d):
    num = ""
    val = s // d
    rem = s % d
    for i in range(1, d - rem + 1):
        num = num + str(val)
    if (rem):
        val += 1
        for i in range(d - rem + 1, d + 1):
            num = num + str(val)
    return num


if __name__ == "__main__":
    s = 25
    d = 4
    print(findNumber(s, d))

# Expected output:
6667
# find the sum of power of bit count raised to the power b | function to calculate a ^ b mod m using fast - exponentiation method ; function to calculate sum ; itereate for all values of array a ; calling fast - exponentiation and appending ans to sum ; 
def fastmod(base, exp, mod):
    if (exp == 0):
        return 1
    elif (exp % 2 == 0):
        ans = fastmod(base, exp / 2, mod)
        return (ans % mod * ans % mod) % mod
    else:
        return (fastmod(base, exp - 1, mod) % mod * base % mod) % mod


def findPowerSum(n, ar):
    mod = int(1e9) + 7
    sum = 0
    for i in range(n):
        base = bin(ar[i]).count('1')
        exp = ar[i]
        sum += fastmod(base, exp, mod)
        sum %= mod
    return sum


if __name__ == "__main__":
    n = 3
    ar = [1, 2, 3]
    print(findPowerSum(n, ar))

# Expected output:
10
# Count the total number of triangles after Nth operation | Function to return the total no . of Triangles ; For every subtriangle formed there are possibilities of generating ( curr * 3 ) + 2 ; Changing the curr value to Tri_count ;
def countTriangles(n):
    curr = 1
    Tri_count = 0
    for i in range(1, n + 1):
        Tri_count = (curr * 3) + 2
        curr = Tri_count
    return Tri_count


n = 10
print(countTriangles(n))

# Expected output:
118097
# in how many ways the ball will come back to the first boy after n turns | function to return the number of sequences that get back to bob ; 
def numSeq(n):
    return (pow(3, n) + 3 * pow(-1, n)) // 4


N = 10
print(numSeq(N))

# Expected output:
14763
# count subarrays such that remainder after dividing sum of elements by k gives count of elements | function to return the number of subarrays of the given array such that the remainder when dividing the sum of its elements by k is equal to the number of its elements ; to store prefix sum ; we are dealing with zero indexed array ; taking modulus value ; prefix sum ; to store the required answer , the left index and the right index ; to store si - i value ; include sum ; increment the right index ; if subarray has at least k elements ; return the required answer ; 
def sub_arrays(a, n, k):
    sum = [0 for i in range(n + 2)]
    for i in range(n):
        a[i] -= 1
        a[i] %= k
        sum[i + 1] += sum[i] + a[i]
        sum[i + 1] %= k
    ans = 0
    l = 0
    r = 0
    mp = dict()
    for i in range(n + 1):
        if sum[i] in mp:
            ans += mp[sum[i]]
        mp[sum[i]] = mp .get(sum[i], 0) + 1
        r += 1
        if (r - l >= k):
            mp[sum[l]] -= 1
            l += 1
    return ans


a = [1, 4, 2, 3, 5]
n = len(a)
k = 4
print(sub_arrays(a, n, k))

# Expected output:
4
# sum of all the prime numbers with the maximum position of set bit â ‰¤ d | python 3 implementation of the approach ; function for sieve of eratosthenes ; function to return the sum of the required prime numbers ; maximum number of the required range ; sieve of eratosthenes ; to store the required sum ; if current element is prime ; 

from math import sqrt, pow


def sieve(prime, n):
    prime[0] = False
    prime[1] = False
    for p in range(2, int(sqrt(n)) + 1, 1):
        if (prime[p]):
            for i in range(p * p, n + 1, p):
                prime[i] = False


def sumPrime(d):
    maxVal = int(pow(2, d)) - 1
    prime = [True for i in range(maxVal + 1)]
    sieve(prime, maxVal)
    sum = 0
    for i in range(2, maxVal + 1, 1):
        if (prime[i]):
            sum += i
    return sum


if __name__ == '__main__':
    d = 8
    print(sumPrime(d))

# Expected output:
6081
# check whether the exchange is possible or not | python3 implementation of the approach ; function that returns true if the exchange is possible ; find the gcd of the array elements ; if the exchange is possible ; 

from math import gcd as __gcd


def isPossible(arr, n, p):
    gcd = 0
    for i in range(n):
        gcd = __gcd(gcd, arr[i])
    if (p % gcd == 0):
        return True
    return False


if __name__ == "__main__":
    arr = [6, 9]
    n = len(arr)
    p = 3
    if (isPossible(arr, n, p)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# choose two elements from the given array such that their sum is not present in any of the arrays | function to find the numbers from the given arrays such that their sum is not present in any of the given array ; find the maximum element from both the arrays ; 
def findNum(a, n, b, m):
    x = max(a)
    y = max(b)
    print(x, y)


if __name__ == "__main__":
    a = [3, 2, 2]
    n = len(a)
    b = [1, 5, 7, 7, 9]
    m = len(b)
    findNum(a, n, b, m)

# Expected output:
3 9
# generate an array b [ ] from the given array a [ ] which satisfies the given conditions | python3 implementation of the approach ; utility function to print the array elements ; function to generate and print the required array ; to switch the ceil and floor function alternatively ; for every element of the array ; if the number is odd then print the ceil or floor value after division by 2 ; use the ceil and floor alternatively ; if arr [ i ] is even then it will be completely divisible by 2 ; 

from math import ceil, floor


def printArr(arr, n):
    for i in range(n):
        print(arr[i], end=" ")


def generateArr(arr, n):
    flip = True
    for i in range(n):
        if (arr[i] & 1):
            flip ^= True
            if (flip):
                print(int(ceil((arr[i]) / 2)), end=" ")
            else:
                print(int(floor((arr[i]) / 2)), end=" ")
        else:
            print(int(arr[i] / 2), end=" ")


arr = [3, -5, -7, 9, 2, -2]
n = len(arr)
generateArr(arr, n)

# Expected output:
1 -2 -4 5 1 -1
# number of k length subsequences with minimum sum | function to return the value of binomial coefficient c ( n , k ) ; calculate value of binomial coefficient in bottom up manner ; base cases ; calculate value using previously stored values ; function to return the count of valid subsequences ; sort the array ; maximum among the minimum k elements ; y will store the frequency of num in the minimum k elements ; cntx will store the frequency of num in the complete array ; 
def binomialCoeff(n, k):
    C = [[0 for i in range(n + 1)]for j in range(k + 1)]
    for i in range(0, n + 1):
        for j in range(0, min(i, k) + 1):
            if (j == 0 or j == i):
                C[i][j] = 1
            else:
                C[i][j] = C[i - 1][j - 1] + C[i - 1][j]
    return C[n][k]


def cntSubSeq(arr, n, k):
    arr .sort()
    num = arr[k - 1]
    Y = 0
    for i in range(k - 1, -1, 1):
        if (arr[i] == num):
            Y += 1
    cntX = Y
    for i in range(k, n):
        if (arr[i] == num):
            cntX += 1
    return binomialCoeff(cntX, Y)


arr = [1, 2, 3, 4]
n = len(arr)
k = 2
print(cntSubSeq(arr, n, k))

# Expected output:
1
# find all even length binary sequences with same sum of first and second half bits | iterative | function to convert the number into binary and store the number into an array ; function to check if the sum of the digits till the mid of the array and the sum of the digits from mid till n is the same , if they are same then print that binary ; calculating the sum from 0 till mid and store in sum1 ; calculating the sum from mid till n and store in sum2 ; if sum1 is same as sum2 print the binary ; function to prsequence ; creating the array ; '' looping over powers of 2  ; converting the number into binary first ; checking if the sum of the first half of the array is same as the sum of the next half ; 
def convertToBinary(num, a, n):
    pointer = n - 1
    while (num > 0):
        a[pointer] = num % 2
        num = num // 2
        pointer -= 1


def checkforsum(a, n):
    sum1 = 0
    sum2 = 0
    mid = n // 2
    for i in range(mid):
        sum1 = sum1 + a[i]
    for j in range(mid, n):
        sum2 = sum2 + a[j]
    if (sum1 == sum2):
        for i in range(n):
            print(a[i], end="")
        print()


def print_seq(m):
    n = (2 * m)
    a = [0 for i in range(n)]
    for i in range(pow(2, n)):
        convertToBinary(i, a, n)
        checkforsum(a, n)


m = 2
print_seq(m)

# Expected output:
0000
0101
0110
1001
1010
1111
# find out the prime numbers in the form of a + nb or b + na | python3 implementation of the above approach ; utility function to check whether two numbers is co - prime or not ; utility function to check whether a number is prime or not ; corner case ; check from 2 to sqrt ( n ) ; finding the prime numbers ; checking whether given numbers are co - prime or not ; to store the n primes ; if ' possible ' is true ; printing n numbers of prime ; checking the form of a + nb ; checking the form of b + na ; if ' possible ' is false return - 1 ; 

from math import gcd, sqrt


def coprime(a, b):
    if (gcd(a, b) == 1):
        return True
    else:
        return False


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


def findNumbers(a, b, n):
    possible = True
    if (not coprime(a, b)):
        possible = False
    c1 = 1
    c2 = 1
    num1 = 0
    num2 = 0
    st = set()
    if (possible):
        while (len(st) != n):
            num1 = a + (c1 * b)
            if (isPrime(num1)):
                st .add(num1)
            c1 += 1
            num2 = b + (c2 * a)
            if (isPrime(num2)):
                st .add(num2)
            c2 += 1
        for i in st:
            print(i, end=" ")
    else:
        print("-1")


if __name__ == "__main__":
    a = 3
    b = 5
    n = 4
    findNumbers(a, b, n)

# Expected output:
17 11 13 23