giulio98/xlcost-single-prompt huggingface dataset:
# program to check for isbn | python code to check if a given isbn is valid or not . ; check for length ; computing weighted sum of first 9 digits ; checking last digit ; if last digit is ' x ' , add 10 to sum , else add its value . ; return true if weighted sum of digits is divisible by 11 ; 
def isValidISBN(isbn):
    if len(isbn) != 10:
        return False
    _sum = 0
    for i in range(9):
        if 0 <= int(isbn[i]) <= 9:
            _sum += int(isbn[i]) * (10 - i)
        else:
            return False
    if (isbn[9] != 'X' and 0 <= int(isbn[9]) <= 9):
        return False
    _sum += 10 if isbn[9] == 'X'else int(isbn[9])
    return (_sum % 11 == 0)


isbn = "007462542X"
if isValidISBN(isbn):
    print('Valid')
else:
    print("Invalid")

# Expected output:
Valid
# reverse vowels in a given string | utility function to check for vowel ; function to reverse order of vowels ; storing the vowels separately ; placing the vowels in the reverse order in the string ; 
def isVowel(c):
    if (c == 'a' or c == 'A' or c == 'e' or c == 'E' or c == 'i' or c ==
            'I' or c == 'o' or c == 'O' or c == 'u' or c == 'U'):
        return True
    return False


def reverserVowel(string):
    j = 0
    vowel = [0] * len(string)
    string = list(string)
    for i in range(len(string)):
        if isVowel(string[i]):
            vowel[j] = string[i]
            j += 1
    for i in range(len(string)):
        if isVowel(string[i]):
            j -= 1
            string[i] = vowel[j]
    return ''.join(string)


if __name__ == "__main__":
    string = "hello world"
    print(reverserVowel(string))

# Expected output:
hollo werld
# string containing first letter of every word in a given string with spaces | function to find string which has first character of each word . ; traverse the string . ; if it is space , set v as true . ; else check if v is true or not . if true , copy character in output string and set v as false . ; 
def firstLetterWord(str):
    result = ""
    v = True
    for i in range(len(str)):
        if (str[i] == ' '):
            v = True
        elif (str[i] != ' ' and v):
            result += (str[i])
            v = False
    return result


if __name__ == "__main__":
    str = "geeks for geeks"
    print(firstLetterWord(str))

# Expected output:
gfg
# unique paths covering every non | function for dfs . i , j == > current cell indexes vis == > to mark visited cells ans == > result z == > current count 0 s visited z_count == > total 0 s present ; mark the block as visited ; update the count ; if end block reached ; if path covered all the non - obstacle blocks ; up ; down ; left ; right ; unmark the block ( unvisited ) ; function to return the count of the unique paths ; total 0 s present ; count non - obstacle blocks ; starting position ; 
def dfs(i, j, grid, vis, ans, z, z_count):
    n = len(grid)
    m = len(grid[0])
    vis[i][j] = 1
    if (grid[i][j] == 0):
        z += 1
    if (grid[i][j] == 2):
        if (z == z_count):
            ans += 1
        vis[i][j] = 0
        return grid, vis, ans
    if (i >= 1 and not vis[i - 1][j] and grid[i - 1][j] != -1):
        grid, vis, ans = dfs(i - 1, j, grid, vis, ans, z, z_count)
    if (i < n - 1 and not vis[i + 1][j] and grid[i + 1][j] != -1):
        grid, vis, ans = dfs(i + 1, j, grid, vis, ans, z, z_count)
    if (j >= 1 and not vis[i][j - 1] and grid[i][j - 1] != -1):
        grid, vis, ans = dfs(i, j - 1, grid, vis, ans, z, z_count)
    if (j < m - 1 and not vis[i][j + 1] and grid[i][j + 1] != -1):
        grid, vis, ans = dfs(i, j + 1, grid, vis, ans, z, z_count)
    vis[i][j] = 0
    return grid, vis, ans


def uniquePaths(grid):
    z_count = 0
    n = len(grid)
    m = len(grid[0])
    ans = 0
    vis = [[0 for j in range(m)]for i in range(n)]
    x = 0
    y = 0
    for i in range(n):
        for j in range(m):
            if grid[i][j] == 0:
                z_count += 1
            elif (grid[i][j] == 1):
                x = i
                y = j
    grid, vis, ans = dfs(x, y, grid, vis, ans, 0, z_count)
    return ans


if __name__ == '__main__':
    grid = [[1, 0, 0, 0], [0, 0, 0, 0], [0, 0, 2, -1]]
    print(uniquePaths(grid))

# Expected output:
2
# count of unordered pairs ( x , y ) of array which satisfy given equation | return the number of unordered pairs satisfying the conditions ; ans stores the number of unordered pairs ; making each value of array to positive ; sort the array ; for each index calculating the right boundary for the unordered pairs ; return the final result ; 
def numPairs(a, n):
    ans = 0
    for i in range(n):
        a[i] = abs(a[i])
    a .sort()
    for i in range(n):
        index = 0
        for j in range(i + 1, n):
            if (2 * a[i] >= a[j - 1] and 2 * a[i] < a[j]):
                index = j
        if index == 0:
            index = n
        ans += index - i - 1
    return ans


a = [3, 6]
n = len(a)
print(numPairs(a, n))

# Expected output:
1
# area of a square | using side , diagonal and perimeter | function to find the area of a square ; use above formula ; 
def areaOfSquare(S):
    area = S * S
    return area


if __name__ == '__main__':
    S = 5
    print(areaOfSquare(S))

# Expected output:
25
# maximum points of intersections possible among x circles and y straight lines | python3 program to implement the above approach ; number of circles ; number of straight lines ; 
def maxPointOfIntersection(x, y):
    k = y * (y - 1) // 2
    k = k + x * (2 * y + x - 1)
    return k


x = 3
y = 4
print(maxPointOfIntersection(x, y))

# Expected output:
36
# icosihenagonal number | function to find icosihenagonal number ; formula to calculate nth icosihenagonal number ; 
def Icosihenagonal_num(n):
    return (19 * n * n - 17 * n) / 2


n = 3
print(int(Icosihenagonal_num(n)))
n = 10
print(int(Icosihenagonal_num(n)))

# Expected output:
60
865
# Find the centroid of a non | Python3 program to implement the above approach ; For all vertices ; Calculate value of A using shoelace formula ; Calculating coordinates of centroid of polygon ; Coordinate of the vertices
def find_Centroid(v):
    ans = [0, 0]
    n = len(v)
    signedArea = 0
    for i in range(len(v)):
        x0 = v[i][0]
        y0 = v[i][1]
        x1 = v[(i + 1) % n][0]
        y1 = v[(i + 1) % n][1]
        A = (x0 * y1) - (x1 * y0)
        signedArea += A
        ans[0] += (x0 + x1) * A
        ans[1] += (y0 + y1) * A
    signedArea *= 0.5
    ans[0] = (ans[0]) / (6 * signedArea)
    ans[1] = (ans[1]) / (6 * signedArea)
    return ans


vp = [[1, 2], [3, -4], [6, -7]]
ans = find_Centroid(vp)
print(round(ans[0], 12), ans[1])

# Expected output:
3.333333333333 -3.0
# distance between two parallel planes in 3 | python program to find the distance between two parallel planes in 3 d . ; function to find distance ; 

import math


def distance(a1, b1, c1, d1, a2, b2, c2, d2):
    if (a1 / a2 == b1 / b2 and b1 / b2 == c1 / c2):
        x1 = y1 = 0
        z1 = -d1 / c1
        d = abs((c2 * z1 + d2)) / (math .sqrt(a2 * a2 + b2 * b2 + c2 * c2))
        print("Perpendicular distance is"), d
    else:
        print("Planes are not parallel")


a1 = 1
b1 = 2
c1 = -1
d1 = 1
a2 = 3
b2 = 6
c2 = -3
d2 = -4
distance(a1, b1, c1, d1, a2, b2, c2, d2)

# Expected output:
Perpendicular distance is
# count of ways to form 2 necklace from n beads containing n / 2 beads each | function to calculate factorial ; function to count number of ways to make 2 necklace having exactly n / 2 beads if each bead is considered different ; number of ways to choose n / 2 beads from n beads ; number of ways to permute n / 2 beads ; divide ans by 2 to remove repetitions ; return ans ; 
def factorial(n):
    if (n == 0):
        return 1
    return n * factorial(n - 1)


def numOfNecklace(N):
    ans = factorial(N) // (factorial(N // 2) * factorial(N // 2))
    ans = ans * factorial(N // 2 - 1)
    ans = ans * factorial(N // 2 - 1)
    ans //= 2
    return ans


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

# Expected output:
3
# check if a number s can be made divisible by d by repeatedly adding the remainder to s | function to check if s is divisible by d while changing s to ( s + s % d ) ; v ( 0 ) = s % d ; stores the encountered values ; v ( i ) = ( v ( i - 1 ) + v ( i - 1 ) % d ) % d ; check if the value has already been encountered ; edge case ; otherwise , insert it into the hashmap ; 
def isDivisibleByDivisor(S, D):
    S %= D
    hashMap = set()
    hashMap .add(S)
    for i in range(D + 1):
        S += (S % D)
        S %= D
        if (S in hashMap):
            if (S == 0):
                return "Yes"
            return "No"
        else:
            hashMap .add(S)
    return "Yes"


if __name__ == '__main__':
    S = 3
    D = 6
    print(isDivisibleByDivisor(S, D))

# Expected output:
Yes
# minimum number of given moves required to reach ( 1 , 1 ) from ( x , y ) | function to count the number of steps required to convert ( x , y ) to ( 1 , 1 ) ; store the required result ; iterate while both x and y are not equal to 0 ; if x is greater than y ; update count and value of x ; otherwise ; update count and value of y ; if both x and y > 1 ; print the result ; 
def minimumSteps(x, y):
    cnt = 0
    while (x != 0 and y != 0):
        if (x > y):
            cnt += x / y
            x %= y
        else:
            cnt += y / x
            y %= x
    cnt -= 1
    if (x > 1 or y > 1):
        cnt = -1
    print(int(cnt))


if __name__ == '__main__':
    x = 3
    y = 1
    minimumSteps(x, y)

# Expected output:
2
# minimum number of bracket reversals needed to make an expression balanced | returns count of minimum reversals for making expr balanced . returns - 1 if expr cannot be balanced . ; length of expression must be even to make it balanced by using reversals . ; after this loop , stack contains unbalanced part of expression , i . e . , expression of the form " . . . . " ; length of the reduced expression red_len = ( m + n ) ; count opening brackets at the end of stack ; return ceil ( m / 2 ) + ceil ( n / 2 ) which is actually equal to ( m + n ) / 2 + n % 2 when m + n is even . ; 
def countMinReversals(expr):
    lenn = len(expr)
    if (lenn % 2):
        return -1
    s = []
    for i in range(lenn):
        if (expr[i] == '' and len(s)):
            if (s[0] == ''):
                s .pop(0)
            else:
                s .insert(0, expr[i])
        else:
            s .insert(0, expr[i])
    red_len = len(s)
    n = 0
    while (len(s) and s[0] == ''):
        s .pop(0)
        n += 1
    return (red_len // 2 + n % 2)


if __name__ == '__main__':
    expr = "}}{{"
    print(countMinReversals(expr .strip()))

# Expected output:
2
# form minimum number from given sequence | prints the minimum number that can be formed from input sequence of i ' s ▁ and ▁ d ' s ; initialize current_max ( to make sure that we don 't use repeated character ; initialize last_entry ( keeps track for last printed digit ) ; iterate over input array ; initialize ' noofnextd ' to get count of next d 's available ; if letter is ' i ' calculate number of next consecutive d 's  available ; if ' i ' is first letter , print incremented sequence from 1 ; set max digit reached ; if not first letter get next digit to print ; print digit for i ; for all next consecutive ' d ' print decremented sequence ; if letter is 'd   ; if ' d ' is first letter in sequence find number of next d 's available ; calculate first digit to print based on number of consecutive d 's ; print twice for the first time ; store last entry ; if current ' d ' is not first letter decrement last_entry ; 
def PrintMinNumberForPattern(arr):
    curr_max = 0
    last_entry = 0
    i = 0
    while i < len(arr):
        noOfNextD = 0
        if arr[i] == "I":
            j = i + 1
            while j < len(arr) and arr[j] == "D":
                noOfNextD += 1
                j += 1
            if i == 0:
                curr_max = noOfNextD + 2
                last_entry += 1
                print("", last_entry, end="")
                print("", curr_max, end="")
                last_entry = curr_max
            else:
                curr_max += noOfNextD + 1
                last_entry = curr_max
                print("", last_entry, end="")
            for k in range(noOfNextD):
                last_entry -= 1
                print("", last_entry, end="")
                i += 1
        elif arr[i] == "D":
            if i == 0:
                j = i + 1
                while j < len(arr) and arr[j] == "D":
                    noOfNextD += 1
                    j += 1
                curr_max = noOfNextD + 2
                print("", curr_max, curr_max - 1, end="")
                last_entry = curr_max - 1
            else:
                print("", last_entry - 1, end="")
                last_entry -= 1
        i += 1
    print()


if __name__ == "__main__":
    PrintMinNumberForPattern("IDID")
    PrintMinNumberForPattern("I")
    PrintMinNumberForPattern("DD")
    PrintMinNumberForPattern("II")
    PrintMinNumberForPattern("DIDI")
    PrintMinNumberForPattern("IIDDD")
    PrintMinNumberForPattern("DDIDDIID")

# Expected output:
 1 3 2 5 4
 1 2
 3 2 1
 1 2 3
 2 1 4 3 5
 1 2 6 5 4 3
 3 2 1 6 5 4 7 9 8
# form minimum number from given sequence | python3 program to print minimum number that can be formed from a given sequence of is and ds ; min_avail represents the minimum number which is still available for inserting in the output vector . pos_of_i keeps track of the most recent index where ' i ' was encountered w . r . t the output vector ; vector to store the output ; cover the base cases ; traverse rest of the input ; print the number ; 
def printLeast(arr):
    min_avail = 1
    pos_of_I = 0
    v = []
    if (arr[0] == 'I'):
        v .append(1)
        v .append(2)
        min_avail = 3
        pos_of_I = 1
    else:
        v .append(2)
        v .append(1)
        min_avail = 3
        pos_of_I = 0
    for i in range(1, len(arr)):
        if (arr[i] == 'I'):
            v .append(min_avail)
            min_avail += 1
            pos_of_I = i + 1
        else:
            v .append(v[i])
            for j in range(pos_of_I, i + 1):
                v[j] += 1
            min_avail += 1
    print(*v, sep=' ')


printLeast("IDID")
printLeast("I")
printLeast("DD")
printLeast("II")
printLeast("DIDI")
printLeast("IIDDD")
printLeast("DDIDDIID")

# Expected output:
1 3 2 5 4
1 2
3 2 1
1 2 3
2 1 4 3 5
1 2 6 5 4 3
3 2 1 6 5 4 7 9 8
# form minimum number from given sequence | function to decode the given sequence to construct minimum number without repeated digits ; string for storing result ; take a list to work as stack ; run n + 1 times where n is length of input sequence , as length of result string is always 1 greater ; push number i + 1 into the stack ; if all characters of the input sequence are processed or current character is 'i ; run while loop untill stack is empty ; pop the element on top of stack and store it in result string ; 
def PrintMinNumberForPattern(Strr):
    res = ''
    stack = []
    for i in range(len(Strr) + 1):
        stack .append(i + 1)
        if (i == len(Strr) or Strr[i] == 'I'):
            while len(stack) > 0:
                res += str(stack .pop())
                res += ' '
    print(res)


PrintMinNumberForPattern("IDID")
PrintMinNumberForPattern("I")
PrintMinNumberForPattern("DD")
PrintMinNumberForPattern("II")
PrintMinNumberForPattern("DIDI")
PrintMinNumberForPattern("IIDDD")
PrintMinNumberForPattern("DDIDDIID")

# Expected output:
1 3 2 5 4 
1 2 
3 2 1 
1 2 3 
2 1 4 3 5 
1 2 6 5 4 3 
3 2 1 6 5 4 7 9 8
# form minimum number from given sequence | returns minimum number made from given sequence without repeating digits ; the loop runs for each input character as well as one additional time for assigning rank to remaining characters ; 
def getMinNumberForPattern(seq):
    n = len(seq)
    if (n >= 9):
        return "-1"
    result = [None] * (n + 1)
    count = 1
    for i in range(n + 1):
        if (i == n or seq[i] == 'I'):
            for j in range(i - 1, -2, -1):
                result[j + 1] = int('0' + str(count))
                count += 1
                if (j >= 0 and seq[j] == 'I'):
                    break
    return result


if __name__ == '__main__':
    inputs = ["IDID", "I", "DD", "II", "DIDI", "IIDDD", "DDIDDIID"]
    for Input in inputs:
        print(*(getMinNumberForPattern(Input)))

# Expected output:
1 3 2 5 4
1 2
3 2 1
1 2 3
2 1 4 3 5
1 2 6 5 4 3
3 2 1 6 5 4 7 9 8
# check if the first and last digit of the smallest number forms a prime | python3 implementation of above approach ; function to check prime ; function to generate smallest possible number with given digits ; declare a hash array of size 10 and initialize all the elements to zero ; store the number of occurrences of the digits in the given array into the hash table ; traverse the hash in ascending order to print the required number ; print the number of times a digits occurs ; extracting the first digit ; extracting the last digit ; printing the prime combinations ; 

import math as mt


def isPrime(n):
    i, c = 0, 0
    for i in range(1, n // 2):
        if (n % i == 0):
            c += 1
    if (c == 1):
        return 1
    else:
        return 0


def findMinNum(arr, n):
    first, last = 0, 0
    Hash = [0 for i in range(10)]
    for i in range(n):
        Hash[arr[i]] += 1
    print("Minimum number: ", end="")
    for i in range(0, 10):
        for j in range(Hash[i]):
            print(i, end="")
    print()
    for i in range(10):
        if (Hash[i] != 0):
            first = i
            break
    for i in range(9, -1, -1):
        if (Hash[i] != 0):
            last = i
            break
    num = first * 10 + last
    rev = last * 10 + first
    print("Prime combinations: ", end="")
    if (isPrime(num) and isPrime(rev)):
        print(num, " ", rev)
    elif (isPrime(num)):
        print(num)
    elif (isPrime(rev)):
        print(rev)
    else:
        print("No combinations exist")


arr = [1, 2, 4, 7, 8]
findMinNum(arr, 5)

# Expected output:
Minimum number: 12478
Prime combinations: No combinations exist
# Finding a Non Transitive Co | Function to return gcd of a and b ; function to check for gcd ; a and b are coprime if their gcd is 1. ; Checks if any possible triplet ( a , b , c ) satifying the condition that ( a , b ) is coprime , ( b , c ) is coprime but ( a , c ) isnt ; Generate and check for all possible triplets between L and R ; if we find any such triplets set flag to true ; flag = True indicates that a pair exists between L and R ; finding possible Triplet between 2 and 10 ; finding possible Triplet between 23 and 46
def gcd(a, b):
    if (a == 0):
        return b
    return gcd(b % a, a)


def coprime(a, b):
    return (gcd(a, b) == 1)


def possibleTripletInRange(L, R):
    flag = False
    possibleA = 0
    possibleB = 0
    possibleC = 0
    for a in range(L, R + 1):
        for b in range(a + 1, R + 1):
            for c in range(b + 1, R + 1):
                if (coprime(a, b) and coprime(b, c) and coprime(a, c) == False):
                    flag = True
                    possibleA = a
                    possibleB = b
                    possibleC = c
                    break
    if (flag):
        print(
            "(",
            possibleA,
            ",",
            possibleB,
            ",",
            possibleC,
            ") is one such",
            "possible triplet between",
            L,
            "and",
            R)
    else:
        print("No Such Triplet exists between", L, "and", R)


L = 2
R = 10
possibleTripletInRange(L, R)
L = 23
R = 46
possibleTripletInRange(L, R)

# Expected output:
( 8 , 9 , 10 ) is one such possible triplet between 2 and 10
( 44 , 45 , 46 ) is one such possible triplet between 23 and 46
# reach a and b by multiplying them with k and k ^ 2 at every step | python 3 program to determine if a and b can be reached starting from 1 , 1 following the given steps . ; function to check is it is possible to reach a and b starting from 1 and 1 ; find the cuberoot of the number ; divide the number by cuberoot ; if it is a perfect cuberoot and divides a and b ; 

import numpy as np


def possibleToReach(a, b):
    c = np .cbrt(a * b)
    re1 = a // c
    re2 = b // c
    if ((re1 * re1 * re2 == a) and (re2 * re2 * re1 == b)):
        return True
    else:
        return False


if __name__ == "__main__":
    A = 60
    B = 450
    if (possibleToReach(A, B)):
        print("yes")
    else:
        print("no")

# Expected output:
yes
# undulating numbers | python3 program to check whether a number is undulating or not ; considering the definition with restriction that there should be at least 3 digits ; check if all alternate digits are same or not . ; 
def isUndulating(n):
    if (len(n) <= 2):
        return False
    for i in range(2, len(n)):
        if (n[i - 2] != n[i]):
            return False
    return True


n = "1212121"
if (isUndulating(n)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# program to find the sum of a series ( 1 * 1 ) + ( 2 * 2 ) + ( 3 * 3 ) + ( 4 * 4 ) + ( 5 * 5 ) + ... + ( n * n ) | function to calculate the following series ; 
def Series(n):
    sums = 0
    for i in range(1, n + 1):
        sums += (i * i)
    return sums


n = 3
res = Series(n)
print(res)

# Expected output:
14
# count numbers with unit digit k in given range | efficient python program to count numbers with last digit as k in given range . ; returns count of numbers with k as last digit . ; 

import math


def counLastDigitK(low, high, k):
    mlow = 10 * math .ceil(low / 10.0)
    mhigh = 10 * int(high / 10.0)
    count = (mhigh - mlow) / 10
    if (high % 10 >= k):
        count += 1
    if (low % 10 <= k and (low % 10) > 0):
        count += 1
    return int(count)


low = 3
high = 35
k = 3
print(counLastDigitK(low, high, k))

# Expected output:
4
# sum of all numbers divisible by 6 in a given range | function to calculate the sum of all numbers divisible by 6 in range l - r . . ; no of multiples of 6 upto r ; no of multiples of 6 upto l - 1 ; summation of all multiples of 6 upto r ; summation of all multiples of 6 upto l - 1 ; returns the answer ; 
def sumDivisible(L, R):
    p = int(R / 6)
    q = int((L - 1) / 6)
    sumR = 3 * (p * (p + 1))
    sumL = (q * (q + 1)) * 3
    return sumR - sumL


L = 1
R = 20
print(sumDivisible(L, R))

# Expected output:
36
# largest smaller number possible using only one swap operation | python3 program to find the largest smaller number by swapping one digit . ; returns largest possible number with one swap such that the number is smaller than str . it is assumed that there are leading 0 s . ; traverse from right until we find a digit which is greater than its next digit . for example , in 34125 , our index is 4. ; we can also use binary search here as digits after index are sorted in increasing order . find the biggest digit in the right of arr [ index ] which is smaller than arr [ index ] ; if index is - 1 i . e . digits are in increasing order . ; swap both values ; 

import sys


def prevNum(string, n):
    index = -1
    for i in range(n - 2, -1, -1):
        if int(string[i]) > int(string[i + 1]):
            index = i
            break
    smallGreatDgt = -1
    for i in range(n - 1, index, -1):
        if (smallGreatDgt == -1 and int(string[i]) < int(string[index])):
            smallGreatDgt = i
        elif (index > -1 and int(string[i]) >= int(string[smallGreatDgt]) and int(string[i]) < int(string[index])):
            smallGreatDgt = i
    if index == -1:
        return "".join("-1")
    else:
        (string[index], string[smallGreatDgt]) = (
            string[smallGreatDgt], string[index])
    return "".join(string)


if __name__ == '__main__':
    n_str = "34125"
    ans = prevNum(list(n_str), len(n_str))
    print(ans)

# Expected output:
32145