giulio98/xlcost-single-prompt huggingface dataset:
# linear search | python3 program for linear search ; run loop from 0 to right ; if search_element is found with left variable ; if search_element is found with right variable ; if element not found ; 
def search(arr, search_Element):
    left = 0
    length = len(arr)
    position = -1
    right = length - 1
    for left in range(0, right, 1):
        if (arr[left] == search_Element):
            position = left
            print(
                "Element found in Array at ",
                position + 1,
                " Position with ",
                left + 1,
                " Attempt")
            break
        if (arr[right] == search_Element):
            position = right
            print(
                "Element found in Array at ",
                position + 1,
                " Position with ",
                length - right,
                " Attempt")
            break
        left += 1
        right -= 1
    if (position == -1):
        print("Not found in Array with ", left, " Attempt")


arr = [1, 2, 3, 4, 5]
search_element = 5
search(arr, search_element)

# Expected output:
Element found in Array at  5  Position with  1  Attempt
# counting sort | the function that sorts the given arr [ ] ; 
def count_sort(arr):
    max_element = int(max(arr))
    min_element = int(min(arr))
    range_of_elements = max_element - min_element + 1
    count_arr = [0 for _ in range(range_of_elements)]
    output_arr = [0 for _ in range(len(arr))]
    for i in range(0, len(arr)):
        count_arr[arr[i] - min_element] += 1
    for i in range(1, len(count_arr)):
        count_arr[i] += count_arr[i - 1]
    for i in range(len(arr) - 1, -1, -1):
        output_arr[count_arr[arr[i] - min_element] - 1] = arr[i]
        count_arr[arr[i] - min_element] -= 1
    for i in range(0, len(arr)):
        arr[i] = output_arr[i]
    return arr


arr = [-5, -10, 0, -3, 8, 5, -1, 10]
ans = count_sort(arr)
print("Sorted character array is " + str(ans))

# Expected output:
Sorted character array is [-10, -5, -3, -1, 0, 5, 8, 10]
# binomial coefficient | dp | returns value of binomial coefficient c ( n , k ) ; base cases ; recursive call ; 
def binomialCoeff(n, k):
    if k > n:
        return 0
    if k == 0 or k == n:
        return 1
    return binomialCoeff(n - 1, k - 1) + binomialCoeff(n - 1, k)


n = 5
k = 2
print("Value of C(%d,%d) is (%d)" % (n, k, binomialCoeff(n, k)))

# Expected output:
Value of C(5,2) is (10)
# binomial coefficient | dp | python program for optimized dynamic programming solution to binomail coefficient . this one uses the concept of pascal triangle and less memory ; since nc0 is 1 ; compute next row of pascal triangle using the previous row ; 
def binomialCoeff(n, k):
    C = [0 for i in range(k + 1)]
    C[0] = 1
    for i in range(1, n + 1):
        j = min(i, k)
        while (j > 0):
            C[j] = C[j] + C[j - 1]
            j -= 1
    return C[k]


n = 5
k = 2
print("Value of C(%d,%d) is %d" % (n, k, binomialCoeff(n, k)))

# Expected output:
Value of C(5,2) is 10
# binomial coefficient | dp | function to find binomial coefficient ; getting the modular inversion for all the numbers from 2 to r with respect to m here m = 1000000007 ; for 1 / ( r ! ) part ; for ( n ) * ( n - 1 ) * ( n - 2 ) * ... * ( n - r + 1 ) part ; 
def binomialCoeff(n, r):
    if (r > n):
        return 0
    m = 1000000007
    inv = [0 for i in range(r + 1)]
    inv[0] = 1
    if (r + 1 >= 2):
        inv[1] = 1
    for i in range(2, r + 1):
        inv[i] = m - (m // i) * inv[m % i] % m
    ans = 1
    for i in range(2, r + 1):
        ans = ((ans % m) * (inv[i] % m)) % m
    for i in range(n, n - r, -1):
        ans = ((ans % m) * (i % m)) % m
    return ans


n = 5
r = 2
print("Value of C(", n, ", ", r, ") is ", binomialCoeff(n, r))

# Expected output:
Value of C( 5 ,  2 ) is  10
# partition problem | dp | returns true if arr [ ] can be partitioned in two subsets of equal sum , otherwise false ; calculate sum of all elements ; initialze the part array as 0 ; fill the partition table in bottom up manner ; the element to be included in the sum cannot be greater than the sum ; check if sum - arr [ i ] could be formed from a subset using elements before index i ; 
def findPartiion(arr, n):
    Sum = 0
    for i in range(n):
        Sum += arr[i]
    if (Sum % 2 != 0):
        return 0
    part = [0] * ((Sum // 2) + 1)
    for i in range((Sum // 2) + 1):
        part[i] = 0
    for i in range(n):
        for j in range(Sum // 2, arr[i] - 1, -1):
            if (part[j - arr[i]] == 1 or j == arr[i]):
                part[j] = 1
    return part[Sum // 2]


arr = [1, 3, 3, 2, 3, 2]
n = len(arr)
if (findPartiion(arr, n) == 1):
    print("Can be divided into two subsets of equal sum")
else:
    print("Can not be divided into two subsets of equal sum")

# Expected output:
Can be divided into two subsets of equal sum
# dynamic programming | returns true if there is a subset of set [ ] with sun equal to given sum ; the value of subset [ i ] [ j ] will be true if there is a subset of set [ 0. . j - 1 ] with sum equal to i ; if sum is 0 , then answer is true ; if sum is not 0 and set is empty , then answer is false ; fill the subset table in botton up manner ; print table ; 
def isSubsetSum(set, n, sum):
    subset = ([[False for i in range(sum + 1)]for i in range(n + 1)])
    for i in range(n + 1):
        subset[i][0] = True
    for i in range(1, sum + 1):
        subset[0][i] = False
    for i in range(1, n + 1):
        for j in range(1, sum + 1):
            if j < set[i - 1]:
                subset[i][j] = subset[i - 1][j]
            if j >= set[i - 1]:
                subset[i][j] = (subset[i - 1][j]
                                or subset[i - 1][j - set[i - 1]])
    for i in range(n + 1):
        for j in range(sum + 1):
            print(subset[i][j], end=" ")
            print()
    return subset[n][sum]


if __name__ == '__main__':
    set = [3, 34, 4, 12, 5, 2]
    sum = 9
    n = len(set)
    if (isSubsetSum(set, n, sum)):
        print("Found a subset with given sum")
    else:
        print("No subset with given sum")

# Expected output:
True 
False 
False 
False 
False 
False 
False 
False 
False 
False 
True 
False 
False 
True 
False 
False 
False 
False 
False 
False 
True 
False 
False 
True 
False 
False 
False 
False 
False 
False 
True 
False 
False 
True 
True 
False 
False 
True 
False 
False 
True 
False 
False 
True 
True 
False 
False 
True 
False 
False 
True 
False 
False 
True 
True 
True 
False 
True 
True 
True 
True 
False 
True 
True 
True 
True 
True 
True 
True 
True 
Found a subset with given sum
# how to print maximum number of a 's using given four keys | a recursive function that returns the optimal length string for n keystrokes ; the optimal string length is n when n is smaller than ; initialize result ; try all possible break - points for any keystroke n , we need to loop from n - 3 keystrokes back to 1 keystroke to find a breakpoint ' b ' after which we will have ctrl - a , ctrl - c and then only ctrl - v all the way . ; if the breakpoint is s at b 'th  keystroke then the optimal string  would have length  (n-b-1)*screen[b-1]; ; 
def findoptimal(N):
    if N <= 6:
        return N
    maxi = 0
    for b in range(N - 3, 0, -1):
        curr = (N - b - 1) * findoptimal(b)
        if curr > maxi:
            maxi = curr
    return maxi


if __name__ == '__main__':
    for n in range(1, 21):
        print("Maximum Number of As with", n, "keystrokes is", findoptimal(n))

# Expected output:
Maximum Number of As with 1 keystrokes is 1
Maximum Number of As with 2 keystrokes is 2
Maximum Number of As with 3 keystrokes is 3
Maximum Number of As with 4 keystrokes is 4
Maximum Number of As with 5 keystrokes is 5
Maximum Number of As with 6 keystrokes is 6
Maximum Number of As with 7 keystrokes is 9
Maximum Number of As with 8 keystrokes is 12
Maximum Number of As with 9 keystrokes is 16
Maximum Number of As with 10 keystrokes is 20
Maximum Number of As with 11 keystrokes is 27
Maximum Number of As with 12 keystrokes is 36
Maximum Number of As with 13 keystrokes is 48
Maximum Number of As with 14 keystrokes is 64
Maximum Number of As with 15 keystrokes is 81
Maximum Number of As with 16 keystrokes is 108
Maximum Number of As with 17 keystrokes is 144
Maximum Number of As with 18 keystrokes is 192
Maximum Number of As with 19 keystrokes is 256
Maximum Number of As with 20 keystrokes is 324
# how to print maximum number of a 's using given four keys | this function returns the optimal length string for n keystrokes ; the optimal string length is n when n is smaller than 7 ; an array to store result of subproblems ; initializing the optimal lengths array for uptil 6 input strokes . ; solve all subproblems in bottom manner ; initialize length of optimal string for n keystrokes ; for any keystroke n , we need to loop from n - 3 keystrokes back to 1 keystroke to find a breakpoint ' b ' after which we will have ctrl - a , ctrl - c and then only ctrl - v all the way . ; if the breakpoint is at b 'th keystroke then  the optimal string would have length  (n-b-1)*screen[b-1]; ; 
def findoptimal(N):
    if (N <= 6):
        return N
    screen = [0] * N
    for n in range(1, 7):
        screen[n - 1] = n
    for n in range(7, N + 1):
        screen[n - 1] = 0
        for b in range(n - 3, 0, -1):
            curr = (n - b - 1) * screen[b - 1]
            if (curr > screen[n - 1]):
                screen[n - 1] = curr
    return screen[N - 1]


if __name__ == "__main__":
    for N in range(1, 21):
        print("Maximum Number of A's with ", N,
              " keystrokes is ", findoptimal(N))

# Expected output:
Maximum Number of A's with  1  keystrokes is  1
Maximum Number of A's with  2  keystrokes is  2
Maximum Number of A's with  3  keystrokes is  3
Maximum Number of A's with  4  keystrokes is  4
Maximum Number of A's with  5  keystrokes is  5
Maximum Number of A's with  6  keystrokes is  6
Maximum Number of A's with  7  keystrokes is  9
Maximum Number of A's with  8  keystrokes is  12
Maximum Number of A's with  9  keystrokes is  16
Maximum Number of A's with  10  keystrokes is  20
Maximum Number of A's with  11  keystrokes is  27
Maximum Number of A's with  12  keystrokes is  36
Maximum Number of A's with  13  keystrokes is  48
Maximum Number of A's with  14  keystrokes is  64
Maximum Number of A's with  15  keystrokes is  81
Maximum Number of A's with  16  keystrokes is  108
Maximum Number of A's with  17  keystrokes is  144
Maximum Number of A's with  18  keystrokes is  192
Maximum Number of A's with  19  keystrokes is  256
Maximum Number of A's with  20  keystrokes is  324
# how to print maximum number of a 's using given four keys | this function returns the optimal length string for n keystrokes ; the optimal string length is n when n is smaller than 7 ; an array to store result of subproblems ; initializing the optimal lengths array for uptil 6 input strokes . ; solve all subproblems in bottom manner ; for any keystroke n , we will need to choose between : - 1. pressing ctrl - v once after copying the a ' s ▁ obtained ▁ by ▁ n - 3 ▁ keystrokes . ▁ ▁ 2 . ▁ pressing ▁ ctrl - v ▁ twice ▁ after ▁ copying ▁ the ▁ a ' s obtained by n - 4 keystrokes . 3. pressing ctrl - v thrice after copying the a 's  obtained by n-5 keystrokes. ; 
def findoptimal(N):
    if (N <= 6):
        return N
    screen = [0] * N
    for n in range(1, 7):
        screen[n - 1] = n
    for n in range(7, N + 1):
        screen[n - 1] = max(2 * screen[n - 4],
                            max(3 * screen[n - 5], 4 * screen[n - 6]))
    return screen[N - 1]


if __name__ == "__main__":
    for N in range(1, 21):
        print("Maximum Number of A's with ", N,
              " keystrokes is ", findoptimal(N))

# Expected output:
Maximum Number of A's with  1  keystrokes is  1
Maximum Number of A's with  2  keystrokes is  2
Maximum Number of A's with  3  keystrokes is  3
Maximum Number of A's with  4  keystrokes is  4
Maximum Number of A's with  5  keystrokes is  5
Maximum Number of A's with  6  keystrokes is  6
Maximum Number of A's with  7  keystrokes is  9
Maximum Number of A's with  8  keystrokes is  12
Maximum Number of A's with  9  keystrokes is  16
Maximum Number of A's with  10  keystrokes is  20
Maximum Number of A's with  11  keystrokes is  27
Maximum Number of A's with  12  keystrokes is  36
Maximum Number of A's with  13  keystrokes is  48
Maximum Number of A's with  14  keystrokes is  64
Maximum Number of A's with  15  keystrokes is  81
Maximum Number of A's with  16  keystrokes is  108
Maximum Number of A's with  17  keystrokes is  144
Maximum Number of A's with  18  keystrokes is  192
Maximum Number of A's with  19  keystrokes is  256
Maximum Number of A's with  20  keystrokes is  324
# write a program to calculate pow ( x , n ) | function to calculate x raised to the power y ; 
def power(x, y):
    if (y == 0):
        return 1
    elif (int(y % 2) == 0):
        return (power(x, int(y / 2)) * power(x, int(y / 2)))
    else:
        return (x * power(x, int(y / 2)) * power(x, int(y / 2)))


x = 2
y = 3
print(power(x, y))

# Expected output:
8
# write a program to calculate pow ( x , n ) | python3 code for extended version of power function that can work for float x and negative y ; 
def power(x, y):
    if (y == 0):
        return 1
    temp = power(x, int(y / 2))
    if (y % 2 == 0):
        return temp * temp
    else:
        if (y > 0):
            return x * temp * temp
        else:
            return (temp * temp) / x


x, y = 2, -3
print('%.6f' % (power(x, y)))

# Expected output:
0.125000
# write a program to calculate pow ( x , n ) | python3 program for the above approach ; if x ^ 0 return 1 ; if we need to find of 0 ^ y ; for all other cases ; 
def power(x, y):
    if (y == 0):
        return 1
    if (x == 0):
        return 0
    return x * power(x, y - 1)


x = 2
y = 3
print(power(x, y))

# Expected output:
8
# babylonian method for square root | returns the square root of n . note that the function ; we are using n itself as initial approximation this can definitely be improved ; e decides the accuracy level ; 
def squareRoot(n):
    x = n
    y = 1
    e = 0.000001
    while (x - y > e):
        x = (x + y) / 2
        y = n / x
    return x


n = 50
print("Square root of", n, "is", round(squareRoot(n), 6))

# Expected output:
Square root of 50 is 7.071068
# average of a stream of numbers | returns the new average after including x ; prints average of a stream of numbers ; 
def getAvg(prev_avg, x, n):
    return ((prev_avg * n + x) / (n + 1))


def streamAvg(arr, n):
    avg = 0
    for i in range(n):
        avg = getAvg(avg, arr[i], i)
        print("Average of ", i + 1, " numbers is ", avg)


arr = [10, 20, 30, 40, 50, 60]
n = len(arr)
streamAvg(arr, n)

# Expected output:
Average of  1  numbers is  10.0
Average of  2  numbers is  15.0
Average of  3  numbers is  20.0
Average of  4  numbers is  25.0
Average of  5  numbers is  30.0
Average of  6  numbers is  35.0
# average of a stream of numbers | returns the new average after including x ; prints average of a stream of numbers ; 
def getAvg(x, n, sum):
    sum = sum + x
    return float(sum) / n


def streamAvg(arr, n):
    avg = 0
    sum = 0
    for i in range(n):
        avg = getAvg(arr[i], i + 1, sum)
        sum = avg * (i + 1)
        print("Average of ", end="")
        print(i + 1, end="")
        print(" numbers is ", end="")
        print(avg)
    return


arr = [10, 20, 30, 40, 50, 60]
n = len(arr)
streamAvg(arr, n)

# Expected output:
Average of 1 numbers is 10.0
Average of 2 numbers is 15.0
Average of 3 numbers is 20.0
Average of 4 numbers is 25.0
Average of 5 numbers is 30.0
Average of 6 numbers is 35.0
# space and time efficient binomial coefficient | returns value of binomial coefficient c ( n , k ) ; since c ( n , k ) = c ( n , n - k ) ; calculate value of [ n * ( n - 1 ) * -- - * ( n - k + 1 ) ] / [ k * ( k - 1 ) * -- -- * 1 ] ; 
def binomialCoefficient(n, k):
    res = 1
    if (k > n - k):
        k = n - k
    for i in range(k):
        res = res * (n - i)
        res = res / (i + 1)
    return res


n = 8
k = 2
res = binomialCoefficient(n, k)
print("Value of C(% d, % d) is % d" % (n, k, res))

# Expected output:
Value of C( 8,  2) is  28
# efficient program to print all prime factors of a given number | python program to print prime factors ; a function to print all prime factors of a given number n ; print the number of two 's that divide n ; n must be odd at this point so a skip of 2 ( i = i + 2 ) can be used ; while i divides n , print i ad divide n ; condition if n is a prime number greater than 2 ; 

import math


def primeFactors(n):
    while n % 2 == 0:
        print(2,)
        n = n / 2
    for i in range(3, int(math .sqrt(n)) + 1, 2):
        while n % i == 0:
            print(i,)
            n = n / i
    if n > 2:
        print(n)


n = 315
primeFactors(n)

# Expected output:
3
3
5
7
# print all possible combinations of r elements in a given array of size n | the main function that prints all combinations of size r in arr [ ] of size n . this function mainly uses combinationutil ( ) ; a temporary array to store all combination one by one ; print all combination using temprary array 'data[]   ; arr [ ] -- -> input array data [ ] -- -> temporary array to store current combination start & end -- -> staring and ending indexes in arr [ ] index -- -> current index in data [ ] r -- -> size of a combination to be printed ; current combination is ready to be printed , print it ; replace index with all possible elements . the condition " end - i + 1 ▁ > = ▁ ▁ r - index " makes sure that including one element at index will make a combination with remaining elements at remaining positions ; 
def combinationUtil(arr, data, start, end, index, r):
    if (index == r):
        for j in range(r):
            print(data[j], end=" ")
        print()
        return
    i = start
    while (i <= end and end - i + 1 >= r - index):
        data[index] = arr[i]
        combinationUtil(arr, data, i + 1, end, index + 1, r)
        i += 1

def printCombination(arr, n, r):
    data = [0] * r
    combinationUtil(arr, data, 0, n - 1, 0, r)


arr = [1, 2, 3, 4, 5]
r = 3
n = len(arr)
printCombination(arr, n, r)

# Expected output:
1 2 3 
1 2 4 
1 2 5 
1 3 4 
1 3 5 
1 4 5 
2 3 4 
2 3 5 
2 4 5 
3 4 5
# print all possible combinations of r elements in a given array of size n | the main function that prints all combinations of size r in arr [ ] of size n . this function mainly uses combinationutil ( ) ; a temporary array to store all combination one by one ; print all combination using temprary array 'data[]   ; arr [ ] -- -> input array n -- -> size of input array r -- -> size of a combination to be printed index -- -> current index in data [ ] data [ ] -- -> temporary array to store current combination i -- -> index of current element in arr [ ] ; current cobination is ready , print it ; when no more elements are there to put in data [ ] ; current is included , put next at next location ; current is excluded , replace it with next ( note that i + 1 is passed , but index is not changed ) ; 
def combinationUtil(arr, n, r, index, data, i):
    if (index == r):
        for j in range(r):
            print(data[j], end=" ")
        print()
        return
    if (i >= n):
        return
    data[index] = arr[i]
    combinationUtil(arr, n, r, index + 1, data, i + 1)
    combinationUtil(arr, n, r, index, data, i + 1)

def printCombination(arr, n, r):
    data = [0] * r
    combinationUtil(arr, n, r, 0, data, 0)


if __name__ == "__main__":
    arr = [1, 2, 3, 4, 5]
    r = 3
    n = len(arr)
    printCombination(arr, n, r)

# Expected output:
1 2 3 
1 2 4 
1 2 5 
1 3 4 
1 3 5 
1 4 5 
2 3 4 
2 3 5 
2 4 5 
3 4 5
# count all possible groups of size 2 or 3 that have sum as multiple of 3 | returns count of all possible groups that can be formed from elements of a [ ] . ; create an array c [ 3 ] to store counts of elements with remainder 0 , 1 and 2. c [ i ] would store count of elements with remainder i ; to store the result ; count elements with remainder 0 , 1 and 2 ; case 3. a : count groups of size 2 from 0 remainder elements ; case 3. b : count groups of size 2 with one element with 1 remainder and other with 2 remainder ; case 4. a : count groups of size 3 with all 0 remainder elements ; case 4. b : count groups of size 3 with all 1 remainder elements ; case 4. c : count groups of size 3 with all 2 remainder elements ; case 4. c : count groups of size 3 with different remainders ; return total count stored in res ; 
def findgroups(arr, n):
    c = [0, 0, 0]
    res = 0
    for i in range(0, n):
        c[arr[i] % 3] += 1
    res += ((c[0] * (c[0] - 1)) >> 1)
    res += c[1] * c[2]
    res += (c[0] * (c[0] - 1) * (c[0] - 2)) / 6
    res += (c[1] * (c[1] - 1) * (c[1] - 2)) / 6
    res += ((c[2] * (c[2] - 1) * (c[2] - 2)) / 6)
    res += c[0] * c[1] * c[2]
    return res


arr = [3, 6, 7, 2, 9]
n = len(arr)
print("Required number of groups are", int(findgroups(arr, n)))

# Expected output:
Required number of groups are 8
# smallest power of 2 greater than or equal to n |  ; first n in the below condition is for the case where n is 0 ; 
def nextPowerOf2(n):
    count = 0
    if (n and not (n & (n - 1))):
        return n
    while (n != 0):
        n >>= 1
        count += 1
    return 1 << count


n = 0
print(nextPowerOf2(n))

# Expected output:
1
# smallest power of 2 greater than or equal to n |  ; 
def nextPowerOf2(n):
    p = 1
    if (n and not (n & (n - 1))):
        return n
    while (p < n):
        p <<= 1
    return p


n = 5
print(nextPowerOf2(n))

# Expected output:
8
# smallest power of 2 greater than or equal to n | finds next power of two for n . if n itself is a power of two then returns n ; 
def nextPowerOf2(n):
    n -= 1
    n |= n >> 1
    n |= n >> 2
    n |= n >> 4
    n |= n >> 8
    n |= n >> 16
    n += 1
    return n


n = 5
print(nextPowerOf2(n))

# Expected output:
8
# segregate 0 s and 1 s in an array | function to put all 0 s on left and all 1 s on right ; initialize left and right indexes ; increment left index while we see 0 at left ; decrement right index while we see 1 at right ; if left is smaller than right then there is a 1 at left and a 0 at right . exchange arr [ left ] and arr [ right ] ; 
def segregate0and1(arr, size):
    left, right = 0, size - 1
    while left < right:
        while arr[left] == 0 and left < right:
            left += 1
        while arr[right] == 1 and left < right:
            right -= 1
        if left < right:
            arr[left] = 0
            arr[right] = 1
            left += 1
            right -= 1
    return arr


arr = [0, 1, 0, 1, 1, 1]
arr_size = len(arr)
print("Array after segregation")
print(segregate0and1(arr, arr_size))

# Expected output:
Array after segregation
[0, 0, 1, 1, 1, 1]
# segregate 0 s and 1 s in an array | function to put all 0 s on left and all 1 s on right ; 
def segregate0and1(arr, size):
    type0 = 0
    type1 = size - 1
    while (type0 < type1):
        if (arr[type0] == 1):
            (arr[type0], arr[type1]) = (arr[type1], arr[type0])
            type1 -= 1
        else:
            type0 += 1


arr = [0, 1, 0, 1, 1, 1]
arr_size = len(arr)
segregate0and1(arr, arr_size)
print("Array after segregation is", end=" ")
for i in range(0, arr_size):
    print(arr[i], end=" ")

# Expected output:
Array after segregation is 0 0 1 1 1 1