giulio98/xlcost-single-prompt huggingface dataset:
# minimum number of moves required to reach the destination by the king in a chess board | function to find the minimum number of moves required to reach the destination by the king in a chess board ; minimum number of steps ; while the king is not in the same row or column as the destination ; go up ; go down ; go left ; go right ; 
def MinSteps(SourceX, SourceY, DestX, DestY):
    print(max(abs(SourceX - DestX), abs(SourceY - DestY)))
    while ((SourceX != DestX) or (SourceY != DestY)):
        if (SourceX < DestX):
            print('U', end="")
            SourceX += 1
        if (SourceX > DestX):
            print('D', end="")
            SourceX -= 1
        if (SourceY > DestY):
            print('L')
            SourceY -= 1
        if (SourceY < DestY):
            print('R', end="")
            SourceY += 1


if __name__ == '__main__':
    sourceX = 4
    sourceY = 4
    destinationX = 7
    destinationY = 0
    MinSteps(sourceX, sourceY, destinationX, destinationY)

# Expected output:
4
UL
UL
UL
L
# count of pairs in an array whose sum is a perfect square | function to return an arraylist containing all the perfect squares upto n ; while current perfect square 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 square ; 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 square ; sum of the maximum two elements from the array ; list of perfect squares upto max ; contains all the array elements ; add count of the elements that when added with arr [ i ] give a perfect square ; 
def getPerfectSquares(n):
    perfectSquares = []
    current = 1
    i = 1
    while (current <= n):
        perfectSquares .append(current)
        i += 1
        current = int(pow(i, 2))
    return perfectSquares


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, perfectSquares, nums):
    count = 0
    for i in range(len(perfectSquares)):
        temp = perfectSquares[i] - n
        if (temp > n and (temp in nums)):
            count += 1
    return count


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


arr = [2, 3, 6, 9, 10, 20]
print(countPairs(arr))

# Expected output:
2
# element equal to the sum of all the remaining elements | function to find the element ; sum is use to store sum of all elements of array ; iterate over all elements ; 
def findEle(arr, n):
    sum = 0
    for i in range(n):
        sum += arr[i]
    for i in range(n):
        if arr[i] == sum - arr[i]:
            return arr[i]
    return -1


if __name__ == "__main__":
    arr = [1, 2, 3, 6]
    n = len(arr)
    print(findEle(arr, n))

# Expected output:
6
# sum of all natural numbers in range l to r | function to return the sum of all natural numbers ; function to return the sum of all numbers in range l and r ; 
def sumNatural(n):
    sum = (n * (n + 1)) // 2
    return sum


def suminRange(l, r):
    return sumNatural(r) - sumNatural(l - 1)


l = 2
r = 5
print("Sum of Natural numbers from L to R is ", suminRange(l, r))

# Expected output:
Sum of Natural numbers from L to R is  14
# check if a large number is divisible by 75 or not | check divisibleby3 ; to store sum of digit ; traversing through each digit ; summing up digit ; check if sumofdigit is divisibleby3 ; check divisibleby25 ; if a single digit number ; length of the number ; taking the last two digit ; checking if the lasttwo digit is divisibleby25 ; function to check divisibleby75 or not ; check if divisibleby3 and divisibleby25 ; 
def divisibleBy3(number):
    sumOfDigit = 0
    for i in range(0, len(number), 1):
        sumOfDigit += ord(number[i]) - ord('0')
    if (sumOfDigit % 3 == 0):
        return True
    return False


def divisibleBy25(number):
    if (len(number) < 2):
        return False
    length = len(number)
    lastTwo = ((ord(number[length - 2]) - ord('0')) *
               10 + (ord(number[length - 1]) - ord('0')))
    if (lastTwo % 25 == 0):
        return True
    return False


def divisibleBy75(number):
    if (divisibleBy3(number) and divisibleBy25(number)):
        return True
    return False


if __name__ == '__main__':
    number = "754586672150"
    divisible = divisibleBy75(number)
    if (divisible):
        print("Yes")
    else:
        print("No")

# Expected output:
No
# find the other number when lcm and hcf given | function that will calculates the zeroes at the end ; 
def otherNumber(a, Lcm, Hcf):
    return (Lcm * Hcf) // A


A = 8
Lcm = 8
Hcf = 1
result = otherNumber(A, Lcm, Hcf)
print("B =", result)

# Expected output:
B = 1
# overall percentage change from successive changes | python implementation of above approach ; calculate successive change of 1 st 2 change ; calculate successive change for rest of the value ; 
def successiveChange(arr, N):
    result = 0
    var1 = arr[0]
    var2 = arr[1]
    result = float(var1 + var2 + (float(var1 * var2) / 100))
    for i in range(2, N):
        result = (result + arr[i] + (float(result * arr[i]) / 100))
    return result


arr = [10, 20, 30, 10]
N = len(arr)
result = successiveChange(arr, N)
print("Percentage change is = %.2f" % (result), "%")

# Expected output:
Percentage change is = 88.76 %
# minimum numbers ( smaller than or equal to n ) with sum s | function to find the minimum numbers required to get to s ; 
def minimumNumbers(n, s):
    if (s % n):
        return s / n + 1
    else:
        return s / n


n = 5
s = 11
print(int(minimumNumbers(n, s)))

# Expected output:
3
# sum of multiples of a and b less than n | python 3 program to find the sum of all multiples of a and b below n ; function to find sum of ap series ; number of terms ; function to find the sum of all multiples of a and b below n ; since , we need the sum of multiples less than n ; common factors of a and b ; 

from math import gcd, sqrt


def sumAP(n, d):
    n = int(n / d)
    return (n) * (1 + n) * d / 2


def sumMultiples(A, B, n):
    n -= 1
    common = int((A * B) / gcd(A, B))
    return (sumAP(n, A) + sumAP(n, B) - sumAP(n, common))


if __name__ == '__main__':
    n = 100
    A = 5
    B = 10
    print("Sum =", int(sumMultiples(A, B, n)))

# Expected output:
Sum = 950
# check if a prime number can be expressed as sum of two prime numbers | python3 program to check if a prime number can be expressed as sum of two prime numbers ; function to check whether a number is prime or not ; function to check if a prime number can be expressed as sum of two prime numbers ; if the number is prime , and number - 2 is also prime ; 

import math


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


def isPossible(n):
    if isPrime(n) and isPrime(n - 2):
        return True
    else:
        return False


n = 13
if isPossible(n):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# count pairs from two arrays whose modulo operation yields k | function to return the total pairs of elements whose modulo yield k ; set is used to avoid duplicate pairs ; check which element is greater and proceed according to it ; check if modulo is equal to k ; return size of the set ; 
def totalPairs(arr1, arr2, K, n, m):
    s = {}
    for i in range(n):
        for j in range(m):
            if (arr1[i] > arr2[j]):
                if (arr1[i] % arr2[j] == K):
                    s[(arr1[i], arr2[j])] = 1
            else:
                if (arr2[j] % arr1[i] == K):
                    s[(arr2[j], arr1[i])] = 1
    return len(s)


arr1 = [8, 3, 7, 50]
arr2 = [5, 1, 10, 4]
K = 3
n = len(arr1)
m = len(arr2)
print(totalPairs(arr1, arr2, K, n, m))

# Expected output:
3
# largest sub | python 3 program to find the length of the largest sub - array of an array every element of whose is a perfect square ; function to return the length of the largest sub - array of an array every element of whose is a perfect square ; if both a and b are equal then arr [ i ] is a perfect square ; 

from math import sqrt


def contiguousPerfectSquare(arr, n):
    current_length = 0
    max_length = 0
    for i in range(0, n, 1):
        b = sqrt(arr[i])
        a = int(b)
        if (a == b):
            current_length += 1
        else:
            current_length = 0
        max_length = max(max_length, current_length)
    return max_length


if __name__ == '__main__':
    arr = [9, 75, 4, 64, 121, 25]
    n = len(arr)
    print(contiguousPerfectSquare(arr, n))

# Expected output:
4
# count pairs of numbers from 1 to n with product divisible by their sum | function to count pairs ; variable to store count ; generate all possible pairs such that 1 <= x < y < n ; 
def countPairs(n):
    count = 0
    for x in range(1, n):
        for y in range(x + 1, n + 1):
            if ((y * x) % (y + x) == 0):
                count += 1
    return count


n = 15
print(countPairs(n))

# Expected output:
4
# find the index of the left pointer after possible moves in the array | function that returns the index of the left pointer ; there 's only one element  in the array ; initially both are at end ; 
def getIndex(a, n):
    if (n == 1):
        return 0
    ptrL = 0
    ptrR = n - 1
    sumL = a[0]
    sumR = a[n - 1]
    while (ptrR - ptrL > 1):
        if (sumL < sumR):
            ptrL += 1
            sumL += a[ptrL]
        elif (sumL > sumR):
            ptrR -= 1
            sumR += a[ptrR]
        else:
            break
    return ptrL


if __name__ == "__main__":
    a = [2, 7, 9, 8, 7]
    n = len(a)
    print(getIndex(a, n))

# Expected output:
2
# find the position of the last removed element from the array | python3 program to find the position of the last removed element from the array ; function to find the original position of the element which will be removed last ; take ceil of every number ; since position is index + 1 ; 

import math as mt


def getPosition(a, n, m):
    for i in range(n):
        a[i] = (a[i] // m + (a[i] % m != 0))
    ans, maxx = -1, -1
    for i in range(n - 1, -1, -1):
        if (maxx < a[i]):
            maxx = a[i]
            ans = i
    return ans + 1


a = [2, 5, 4]
n = len(a)
m = 2
print(getPosition(a, n, m))

# Expected output:
2
# find the value of f ( n ) / f ( r ) * f ( n | function to find value of given f ( n ) ; iterate over n ; calculate result ; return the result ; 
def calcFunction(n, r):
    finalDenominator = 1
    mx = max(r, n - r)
    for i in range(mx + 1, n + 1):
        denominator = pow(i, i)
        numerator = pow(i - mx, i - mx)
        finalDenominator = (finalDenominator * denominator) // numerator
    return finalDenominator


if __name__ == "__main__":
    n = 6
    r = 2
    print("1/", end="")
    print(calcFunction(n, r))

# Expected output:
1/36450000
# check if the array has an element which is equal to sum of all the remaining elements | function to check if such element exists or not ; storing frequency in dict ; stores the sum ; traverse the array and count the array element ; only possible if sum is even ; if half sum is available ; 
def isExists(a, n):
    freq = {i: 0 for i in a}
    Sum = 0
    for i in range(n):
        freq[a[i]] += 1
        Sum += a[i]
    if Sum % 2 == 0:
        if freq[Sum // 2]:
            return True
    return False


a = [5, 1, 2, 2]
n = len(a)
if isExists(a, n):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# print all numbers less than n with at | python 3 program to print all the numbers less than n which have at most 2 unique digits ; function to generate all possible numbers ; if the number is less than n ; if the number exceeds ; check if it is not the same number ; function to print all numbers ; all combination of digits ; print all numbers ; 

st = set()


def generateNumbers(n, num, a, b):
    if (num > 0 and num < n):
        st .add(num)
    if (num >= n):
        return
    if (num * 10 + a > num):
        generateNumbers(n, num * 10 + a, a, b)
    generateNumbers(n, num * 10 + b, a, b)


def printNumbers(n):
    for i in range(10):
        for j in range(i + 1, 10, 1):
            generateNumbers(n, 0, i, j)
    print("The numbers are:", end=" ")
    l = list(st)
    for i in l:
        print(i, end=" ")


if __name__ == '__main__':
    n = 12
    printNumbers(n)

# Expected output:
The numbers are: 1 2 3 4 5 6 7 8 9 10 11
# smallest prime number missing in an array | this function finds all prime numbers upto 10 ^ 5 ; use sieve to find prime ; if number is prime , then store it in prime list ; function to find the smallest prime missing ; first of all find all prime ; now store all number as index of freq arr so that we can improve searching ; now check for each prime ; 
def findPrime(MAX):
    pm = [True] * (MAX + 1)
    pm[0], pm[1] = False, False
    for i in range(2, MAX + 1):
        if pm[i]:
            for j in range(2 * i, MAX + 1, i):
                pm[j] = False
    prime = []
    for i in range(0, MAX + 1):
        if pm[i]:
            prime .append(i)
    return prime


def findSmallest(arr, n):
    MAX = max(arr)
    prime = findPrime(MAX)
    s = set()
    for i in range(0, n):
        s .add(arr[i])
    ans = -1
    for i in range(0, len(prime)):
        if prime[i]not in s:
            ans = prime[i]
            break
    return ans


if __name__ == "__main__":
    arr = [3, 0, 1, 2, 7]
    n = len(arr)
    if (findSmallest(arr, n) == -1):
        print("No prime number missing")
    else:
        print(findSmallest(arr, n))

# Expected output:
5
# find the number after successive division | function to find the number ; 
def findNum(div, rem, N):
    num = rem[N - 1]
    i = N - 2
    while (i >= 0):
        num = num * div[i] + rem[i]
        i -= 1
    return num


if __name__ == '__main__':
    div = [8, 3]
    rem = [2, 2]
    N = len(div)
    print(findNum(div, rem, N))

# Expected output:
18
# program to find the profit or loss when cp of n items is equal to sp of m items | function to calculate profit or loss ; 
def profitLoss(N, M):
    if (N == M):
        print("No Profit nor Loss")
    else:
        result = 0.0
        result = float(abs(N - M)) / M
        if (N - M < 0):
            print("Loss = -", '{0:.6}'.format(result * 100), "%")
        else:
            print("Profit = ", '{0:.6}'.format(result * 100), "%")


if __name__ == '__main__':
    N = 8
    M = 9
    profitLoss(N, M)

# Expected output:
Loss = - 11.1111 %
# find all good indices in the given array | python3 program to find all good indices in the given array ; function to find all good indices in the given array ; hash to store frequency of each element ; storing frequency of each element and calculating sum simultaneously ; check if array is good after removing i - th index element ; print good indices ; 

from collections import defaultdict


def niceIndices(A, n):
    Sum = 0
    m = defaultdict(lambda: 0)
    for i in range(n):
        m[A[i]] += 1
        Sum += A[i]
    for i in range(n):
        k = Sum - A[i]
        if k % 2 == 0:
            k = k >> 1
            if k in m:
                if ((A[i] == k and m[k] > 1) or (A[i] != k)):
                    print((i + 1), end=" ")


if __name__ == "__main__":
    A = [8, 3, 5, 2]
    n = len(A)
    niceIndices(A, n)

# Expected output:
1 4
# count pieces of circle after n cuts | function to find number of pieces of circle after n cuts ; 
def countPieces(N):
    return 2 * N


N = 100
print(countPieces(N))

# Expected output:
200
# sum of all the multiples of 3 and 7 below n | function to find sum of ap series ; number of terms ; function to find the sum of all multiples of 3 and 7 below n ; since , we need the sum of multiples less than n ; 
def sumAP(n, d):
    n = int(n / d)
    return (n) * (1 + n) * (d / 2)


def sumMultiples(n):
    n -= 1
    return int(sumAP(n, 3) + sumAP(n, 7) - sumAP(n, 21))


n = 24
print(sumMultiples(n))

# Expected output:
105
# check whether product of digits at even places is divisible by sum of digits at odd place of a number | below function checks whether product of digits at even places is divisible by sum of digits at odd places ; if size is even ; if size is odd ; 
def productSumDivisible(n, size):
    sum = 0
    product = 1
    while (n > 0):
        if (size % 2 == 0):
            product *= n % 10
        else:
            sum += n % 10
        n = n // 10
        size -= 1
    if (product % sum == 0):
        return True
    return False


if __name__ == "__main__":
    n = 1234
    len = 4
    if (productSumDivisible(n, len)):
        print("TRUE")
    else:
        print("FALSE")

# Expected output:
TRUE
# check whether product of digits at even places is divisible by sum of digits at odd place of a number | below function checks whether product of digits at even places is divisible by sum of digits at odd places ; converting integer to string ; traveersing the string ; 
def productSumDivisible(n):
    sum = 0
    product = 1
    num = str(n)
    for i in range(len(num)):
        if (i % 2 != 0):
            product = product * int(num[i])
        else:
            sum = sum + int(num[i])
    if (product % sum == 0):
        return True
    return False


if __name__ == "__main__":
    n = 1234
    if (productSumDivisible(n)):
        print("TRUE")
    else:
        print("FALSE")

# Expected output:
TRUE
# gcd of a number raised to some power and another number | python 3 program to find gcd of a ^ n and b . ; returns gcd of a ^ n and b . ; 
def gcd(a, b):
    if (a == 0):
        return b
    return gcd(b % a, a)


def powGCD(a, n, b):
    for i in range(0, n + 1, 1):
        a = a * a
    return gcd(a, b)


if __name__ == '__main__':
    a = 10
    b = 5
    n = 2
    print(powGCD(a, n, b))

# Expected output:
5
# position after taking n steps to the right and left in an alternate manner | function to return the last destination ; 
def lastCoordinate(n, a, b):
    return (((n + 1) // 2) * a - (n // 2) * b)


n = 3
a = 5
b = 2
print(lastCoordinate(n, a, b))

# Expected output:
8
# smallest number greater than or equal to n divisible by k | function to find the smallest number greater than or equal to n that is divisible by k ; 
def findNum(N, K):
    rem = (N + K) % K
    if (rem == 0):
        return N
    else:
        return (N + K - rem)


N = 45
K = 6
print('Smallest number greater than', 'or equal to', N,
      'that is divisible by', K, 'is', findNum(45, 6))

# Expected output:
Smallest number greater than or equal to 45 that is divisible by 6 is 48
# sum and product of digits in a number that divide the number | print the sum and product of digits that divides the number . ; fetching each digit of the number ; checking if digit is greater than 0 and can divides n . ; 
def countDigit(n):
    temp = n
    sum = 0
    product = 1
    while (temp != 0):
        d = temp % 10
        temp //= 10
        if (d > 0 and n % d == 0):
            sum += d
            product *= d
    print("Sum =", sum)
    print("Product =", product)


if __name__ == '__main__':
    n = 1012
    countDigit(n)

# Expected output:
Sum = 4
Product = 2
# largest number smaller than or equal to n divisible by k | function to find the largest number smaller than or equal to n that is divisible by k ; 
def findNum(N, K):
    rem = N % K
    if (rem == 0):
        return N
    else:
        return N - rem


if __name__ == '__main__':
    N = 45
    K = 6
    print("Largest number smaller than or equal to" + str(N) +
          "that is divisible by" + str(K) + "is", findNum(N, K))

# Expected output:
Largest number smaller than or equal to45that is divisible by6is 42
# check if any permutation of a number is divisible by 3 and is palindromic | function to check if any permutation of a number is divisible by 3 and is palindromic ; hash array to store frequency of digits of n ; traverse the digits of integer and store their frequency ; calculate the sum of digits simultaneously ; check if number is not divisible by 3 ; if more than one digits have odd frequency , palindromic permutation not possible ; 
def isDivisiblePalindrome(n):
    hash = [0] * 10
    digitSum = 0
    while (n):
        digitSum += n % 10
        hash[n % 10] += 1
        n //= 10
    if (digitSum % 3 != 0):
        return False
    oddCount = 0
    for i in range(10):
        if (hash[i] % 2 != 0):
            oddCount += 1
    if (oddCount > 1):
        return False
    else:
        return True


n = 34734
if (isDivisiblePalindrome(n)):
    print("True")
else:
    print("False")

# Expected output:
True
# minimum time to return array to its original state after given modifications | python implementation of above approach ; function to return lcm of two numbers ; make a graph ; add edge ; count reachable nodes from every node . ; 

import math


def lcm(a, b):
    return (a * b) // (math .gcd(a, b))


def dfs(src, adj, visited):
    visited[src] = True
    count = 1
    if adj[src] != 0:
        for i in range(len(adj[src])):
            if (not visited[adj[src][i]]):
                count += dfs(adj[src][i], adj, visited)
    return count


def findMinTime(arr, P, n):
    adj = [0] * (n + 1)
    for i in range(n):
        if adj[arr[i]] == 0:
            adj[arr[i]] = []
        adj[arr[i]].append(P[i])
    visited = [0] * (n + 1)
    ans = 1
    for i in range(n):
        if (not visited[i]):
            ans = lcm(ans, dfs(i, adj, visited))
    return ans


arr = [1, 2, 3]
P = [3, 2, 1]
n = len(arr)
print(findMinTime(arr, P, n))

# Expected output:
2
# check whether product of digits at even places of a number is divisible by k | below function checks whether product of digits at even places is divisible by k ; if position is even ; 
def productDivisible(n, k):
    product = 1
    position = 1
    while n > 0:
        if position % 2 == 0:
            product *= n % 10
        n = n / 10
        position += 1
    if product % k == 0:
        return True
    return False


n = 321922
k = 3
if productDivisible(n, k):
    print("YES")
else:
    print("NO")

# Expected output:
YES
# check whether product of digits at even places of a number is divisible by k | function checks whether product of digits at even places is divisible by k ; converting integer to string ; traversing the string ; 
def productDivisible(n, k):
    product = 1
    num = str(n)
    for i in range(len(num)):
        if (i % 2 == 0):
            product = product * int(num[i])
    if product % k == 0:
        return True
    return False


n = 321922
k = 3
if productDivisible(n, k):
    print("YES")
else:
    print("NO")

# Expected output:
YES
# permutations of n things taken r at a time with k things together | def to find factorial of a number ; def to calculate p ( n , r ) ; def to find the number of permutations of n different things taken r at a time with k things grouped together ; 
def factorial(n):
    fact = 1
    for i in range(2, n + 1):
        fact = fact * i
    return fact


def npr(n, r):
    pnr = factorial(n) / factorial(n - r)
    return pnr


def countPermutations(n, r, k):
    return int(factorial(k) * (r - k + 1) * npr(n - k, r - k))


n = 8
r = 5
k = 2
print(countPermutations(n, r, k))

# Expected output:
960
# greatest integer function | python3 program to illustrate greatest integer function ; function to calculate the gif value of a number ; gif is the floor of a number ; 

import math


def GIF(n):
    return int(math .floor(n))


n = 2.3
print(GIF(n))

# Expected output:
2