giulio98/xlcost-single-prompt huggingface dataset:
# swap bits in a given number |  ; setting bit at p1 position to 1 ; setting bit at p2 position to 1 ; value1 and value2 will have 0 if num at the respective positions - p1 and p2 is 0. ; check if value1 and value2 are different i . e . at one position bit is set and other it is not ; if bit at p1 position is set ; unset bit at p1 position ; set bit at p2 position ; if bit at p2 position is set ; set bit at p2 position ; unset bit at p2 position ; return final result ; 
def swapBits(num, p1, p2, n):
    shift1 = 0
    shift2 = 0
    value1 = 0
    value2 = 0
    while (n > 0):
        shift1 = 1 << p1
        shift2 = 1 << p2
        value1 = ((num & shift1))
        value2 = ((num & shift2))
        if ((value1 == 0 and value2 != 0) or (value2 == 0 and value1 != 0)):
            if (value1 != 0):
                num = num & (~shift1)
                num = num | shift2
            else:
                num = num & (~shift2)
                num = num | shift1
        p1 += 1
        p2 += 1
        n -= 1
    return num


res = swapBits(28, 0, 3, 2)
print("Result =", res)

# Expected output:
Result = 7
# smallest of three integers without comparison operators | python3 program to find smallest of three integers without comparison operators ; 
def smallest(x, y, z):
    c = 0
    while (x and y and z):
        x = x - 1
        y = y - 1
        z = z - 1
        c = c + 1
    return c


x = 12
y = 15
z = 5
print("Minimum of 3 numbers is", smallest(x, y, z))

# Expected output:
Minimum of 3 numbers is 5
# smallest of three integers without comparison operators | python3 implementation of above approach ; function to find minimum of x and y ; function to find minimum of 3 numbers x , y and z ; 

CHAR_BIT = 8


def min(x, y):
    return y + ((x - y) & ((x - y) >> (32 * CHAR_BIT - 1)))


def smallest(x, y, z):
    return min(x, min(y, z))


x = 12
y = 15
z = 5
print("Minimum of 3 numbers is ", smallest(x, y, z))

# Expected output:
Minimum of 3 numbers is  5
# smallest of three integers without comparison operators | using division operator to find minimum of three numbers ; same as " if ▁ ( y ▁ < ▁ x ) " ; 
def smallest(x, y, z):
    if (not (y / x)):
        return y if (not (y / z))else z
    return x if (not (x / z))else z


if __name__ == "__main__":
    x = 78
    y = 88
    z = 68
    print("Minimum of 3 numbers is", smallest(x, y, z))

# Expected output:
Minimum of 3 numbers is 68
# a boolean array puzzle |  ; 
def changeToZero(a):
    a[a[1]] = a[not a[1]]
    return a


if __name__ == '__main__':
    a = [1, 0]
    a = changeToZero(a)
    print(" arr[0] = " + str(a[0]))
    print(" arr[1] = " + str(a[1]))

# Expected output:
 arr[0] = 0
 arr[1] = 0
# next higher number with same number of set bits | this function returns next higher number with same number of set bits as x . ; right most set bit ; reset the pattern and set next higher bit left part of x will be here ; nexthigheronebit is now part [ d ] of the above explanation . isolate the pattern ; right adjust pattern ; correction factor ; rightonespattern is now part [ a ] of the above explanation . integrate new pattern ( add [ d ] and [ a ] ) ; 
def snoob(x):
    next = 0
    if (x):
        rightOne = x & -(x)
        nextHigherOneBit = x + int(rightOne)
        rightOnesPattern = x ^ int(nextHigherOneBit)
        rightOnesPattern = (int(rightOnesPattern) / int(rightOne))
        rightOnesPattern = int(rightOnesPattern) >> 2
        next = nextHigherOneBit | rightOnesPattern
    return next


x = 156
print("Next higher number with " + "same number of set bits is", snoob(x))

# Expected output:
Next higher number with same number of set bits is 163
# add 1 to a given number | python3 code to add 1 one to a given number ; flip all the set bits until we find a 0 ; flip the rightmost 0 bit ; 
def addOne(x):
    m = 1
    while (x & m):
        x = x ^ m
        m <<= 1
    x = x ^ m
    return x


n = 13
print(addOne(n))

# Expected output:
14
# add 1 to a given number | python3 code to add 1 to a given number ; 
def addOne(x):
    return (-(~x))


print(addOne(13))

# Expected output:
14
# multiply a given integer with 3.5 | python 3 program to multiply a number with 3.5 ; 
def multiplyWith3Point5(x):
    return (x << 1) + x + (x >> 1)


x = 4
print(multiplyWith3Point5(x))

# Expected output:
14
# turn off the rightmost set bit | unsets the rightmost set bit of n and returns the result ; 
def fun(n):
    return n & (n - 1)


n = 7
print("The number after unsetting the rightmost set bit", fun(n))

# Expected output:
The number after unsetting the rightmost set bit 6
# find whether a given number is a power of 4 or not | function to check if x is power of 4 ; 
def isPowerOfFour(n):
    if (n == 0):
        return False
    while (n != 1):
        if (n % 4 != 0):
            return False
        n = n // 4
    return True


test_no = 64
if (isPowerOfFour(64)):
    print(test_no, 'is a power of 4')
else:
    print(test_no, 'is not a power of 4')

# Expected output:
64 is a power of 4
# find whether a given number is a power of 4 or not | function to check if x is power of 4 ; check if there is only one bit set in n ; count 0 bits before set bit ; if count is even then return true else false ; 
def isPowerOfFour(n):
    count = 0
    if (n and (not (n & (n - 1)))):
        while (n > 1):
            n >>= 1
            count += 1
        if (count % 2 == 0):
            return True
        else:
            return False


test_no = 64
if (isPowerOfFour(64)):
    print(test_no, 'is a power of 4')
else:
    print(test_no, 'is not a power of 4')

# Expected output:
64 is a power of 4
# find whether a given number is a power of 4 or not | python3 program to check if given number is power of 4 or not ; 
def isPowerOfFour(n):
    return (n != 0 and ((n & (n - 1)) == 0) and not (n & 0xAAAAAAAA))


test_no = 64
if (isPowerOfFour(test_no)):
    print(test_no, "is a power of 4")
else:
    print(test_no, "is not a power of 4")

# Expected output:
64 is a power of 4
# find whether a given number is a power of 4 or not | python3 program to check if given number is power of 4 or not ; 0 is not considered as a power of 4 ; 

import math


def logn(n, r):
    return math .log(n) / math .log(r)


def isPowerOfFour(n):
    if (n == 0):
        return False
    return (math .floor(logn(n, 4)) == math .ceil(logn(n, 4)))


if __name__ == '__main__':
    test_no = 64
    if (isPowerOfFour(test_no)):
        print(test_no, " is a power of 4")
    else:
        print(test_no, " is not a power of 4")

# Expected output:
64  is a power of 4
# find whether a given number is a power of 4 or not | python3 program to check if given number is power of 4 or not ; 

import math


def isPowerOfFour(n):
    return (n > 0 and 4 ** int(math .log(n, 4)) == n)


if __name__ == '__main__':
    test_no = 64
    if (isPowerOfFour(test_no)):
        print(test_no, " is a power of 4")
    else:
        print(test_no, " is not a power of 4")

# Expected output:
64  is a power of 4
# find the two non | this function sets the values of * x and * y to non - repeating elements in an array arr [ ] of size n ; xor all the elements of the array all the elements occuring twice will cancel out each other remaining two unnique numbers will be xored ; bitwise & the sum with it ' s ▁ 2' s complement bitwise & will give us the sum containing only the rightmost set bit ; sum1 and sum2 will contains 2 unique elements elements initialized with 0 box number xored with 0 is number itself ; traversing the array again ; bitwise & the arr [ i ] with the sum two possibilities either result == 0 or result > 0 ; if result > 0 then arr [ i ] xored with the sum1 ; if result == 0 then arr [ i ] xored with sum2 ; print the the two unique numbers ; 
def UniqueNumbers2(arr, n):
    sums = 0
    for i in range(0, n):
        sums = (sums ^ arr[i])
    sums = (sums & -sums)
    sum1 = 0
    sum2 = 0
    for i in range(0, len(arr)):
        if (arr[i] & sums) > 0:
            sum1 = (sum1 ^ arr[i])
        else:
            sum2 = (sum2 ^ arr[i])
    print("The non-repeating elements are ", sum1, " and ", sum2)


if __name__ == "__main__":

    arr = [2, 3, 7, 9, 11, 2, 3, 11]
    n = len(arr)

    UniqueNumbers2(arr, n)

# Expected output:
The non-repeating elements are  7  and  9
# find the number occurring odd number of times | function to find the element occurring odd number of times ; 
def getOddOccurrence(arr, arr_size):
    for i in range(0, arr_size):
        count = 0
        for j in range(0, arr_size):
            if arr[i] == arr[j]:
                count += 1
        if (count % 2 != 0):
            return arr[i]
    return -1


arr = [2, 3, 5, 4, 5, 2, 4, 3, 5, 2, 4, 4, 2]
n = len(arr)
print(getOddOccurrence(arr, n))

# Expected output:
5
# find the number occurring odd number of times | function to find the element occurring odd number of times ; putting all elements into the hashmap ; iterate through hashmap to check an element occurring odd number of times and return it ; 
def getOddOccurrence(arr, size):
    Hash = dict()
    for i in range(size):
        Hash[arr[i]] = Hash .get(arr[i], 0) + 1
    for i in Hash:
        if (Hash[i] % 2 != 0):
            return i
    return -1


arr = [2, 3, 5, 4, 5, 2, 4, 3, 5, 2, 4, 4, 2]
n = len(arr)
print(getOddOccurrence(arr, n))

# Expected output:
5
# count number of bits to be flipped to convert a to b | function that count set bits ; function that return count of flipped number ; return count of set bits in a xor b ; 
def countSetBits(n):
    count = 0
    while n:
        count += 1
        n &= (n - 1)
    return count


def FlippedCount(a, b):
    return countSetBits(a ^ b)


a = 10
b = 20
print(FlippedCount(a, b))

# Expected output:
4
# position of rightmost set bit | python code for position of rightmost set bit ; 

import math


def getFirstSetBitPos(n):
    return math .log2(n & -n) + 1


n = 12
print(int(getFirstSetBitPos(n)))

# Expected output:
3
# position of rightmost set bit | function to find the rightmost set bit ; position variable initialize with 1 m variable is used to check the set bit ; left shift ; 
def PositionRightmostSetbit(n):
    position = 1
    m = 1
    while (not (n & m)):
        m = m << 1
        position += 1
    return position


n = 16
print(PositionRightmostSetbit(n))

# Expected output:
5
# position of rightmost set bit | python 3 implementation of above approach ; counting the position of first set bit ; 

INT_SIZE = 32


def Right_most_setbit(num):
    pos = 1
    for i in range(INT_SIZE):
        if not (num & (1 << i)):
            pos += 1
        else:
            break
    return pos


if __name__ == "__main__":
    num = 18
    pos = Right_most_setbit(num)
    print(pos)

# Expected output:
2
# position of rightmost set bit | program to find position of rightmost set bit ; iterate till number > 0 ; checking if last bit is set ; increment position and right shift number ; set bit not found . ; 
def PositionRightmostSetbit(n):
    p = 1
    while (n > 0):
        if (n & 1):
            return p
        p += 1
        n = n >> 1
    return -1


n = 18
pos = PositionRightmostSetbit(n)
if (pos != -1):
    print(pos)
else:
    print(0)

# Expected output:
2
# binary representation of a given number | bin function ; 
def bin(n):
    i = 1 << 31
    while (i > 0):
        if ((n & i) != 0):
            print("1", end="")
        else:
            print("0", end="")
        i = i // 2


bin(7)
print()
bin(4)

# Expected output:
00000000000000000000000000000111
00000000000000000000000000000100
# binary representation of a given number | function to convert decimal to binary number ; 
def bin(n):
    if (n > 1):
        bin(n >> 1)
    print(n & 1, end="")


bin(131)
print()
bin(3)

# Expected output:
10000011
11
# Swap all odd and even bits | Function for swapping even and odd bits ; Get all even bits of x ; Get all odd bits of x ; Right shift even bits ; Left shift odd bits ; Combine even and odd bits ; 00010111 ; Output is 43 ( 00101011 )
def swapBits(x):
    even_bits = x & 0xAAAAAAAA
    odd_bits = x & 0x55555555
    even_bits >>= 1
    odd_bits <<= 1
    return (even_bits | odd_bits)


x = 23
print(swapBits(x))

# Expected output:
43
# find position of the only set bit | a utility function to check whether n is power of 2 or not . ; returns position of the only set bit in 'n   ; iterate through bits of n till we find a set bit i & n will be non - zero only when ' i ' and ' n ' have a set bit at same position ; unset current bit and set the next bit in 'i   ; increment position ; 
def isPowerOfTwo(n):
    return (True if (n > 0 and ((n & (n - 1)) > 0))else False)


def findPosition(n):
    if (isPowerOfTwo(n)):
        return -1
    i = 1
    pos = 1
    while ((i & n) == 0):
        i = i << 1
        pos += 1
    return pos


n = 16
pos = findPosition(n)
if (pos == -1):
    print("n =", n, ", Invalid number")
else:
    print("n =", n, ", Position ", pos)
n = 12
pos = findPosition(n)
if (pos == -1):
    print("n =", n, ", Invalid number")
else:
    print("n =", n, ", Position ", pos)
n = 128
pos = findPosition(n)
if (pos == -1):
    print("n =", n, ", Invalid number")
else:
    print("n =", n, ", Position ", pos)

# Expected output:
n = 16 , Position  5
n = 12 , Invalid number
n = 128 , Position  8
# find position of the only set bit | a utility function to check whether n is power of 2 or not ; returns position of the only set bit in 'n   ; one by one move the only set bit to right till it reaches end ; increment count of shifts ; 
def isPowerOfTwo(n):
    return (n and (not (n & (n - 1))))


def findPosition(n):
    if not isPowerOfTwo(n):
        return -1
    count = 0
    while (n):
        n = n >> 1
        count += 1
    return count


if __name__ == "__main__":
    n = 0
    pos = findPosition(n)
    if pos == -1:
        print("n =", n, "Invalid number")
    else:
        print("n =", n, "Position", pos)
    n = 12
    pos = findPosition(n)
    if pos == -1:
        print("n =", n, "Invalid number")
    else:
        print("n =", n, "Position", pos)
    n = 128
    pos = findPosition(n)
    if pos == -1:
        print("n =", n, "Invalid number")
    else:
        print("n =", n, "Position", pos)

# Expected output:
n = 0 Invalid number
n = 12 Invalid number
n = 128 Position 8
# how to swap two numbers without using a temporary variable ? | swap function ; 
def swap(xp, yp):
    xp[0] = xp[0] ^ yp[0]
    yp[0] = xp[0] ^ yp[0]
    xp[0] = xp[0] ^ yp[0]


x = [10]
swap(x, x)
print("After swap(&x, &x): x = ", x[0])

# Expected output:
After swap(&x, &x): x =  0
# how to swap two numbers without using a temporary variable ? | function to swap the numbers ; same as a = a + b ; same as b = a - b ; same as a = a - b ; 
def swap(a, b):
    a = (a & b) + (a | b)
    b = a + (~b) + 1
    a = a + (~b) + 1
    print("After Swapping: a = ", a, ", b = ", b)


a = 5
b = 10
swap(a, b)

# Expected output:
After Swapping: a =  10 , b =  5
# check a given sentence for a given set of simple grammer rules | method to check a given sentence for given rules ; calculate the length of the string . ; check that the first character lies in [ a - z ] . otherwise return false . ; if the last character is not a full stop ( . ) no need to check further . ; maintain 2 states . previous and current state based on which vertex state you are . initialise both with 0 = start state . ; keep the index to the next character in the string . ; loop to go over the string . ; set states according to the input characters in the string and the rule defined in the description . if current character is [ a - z ] . set current state as 0. ; if current character is a space . set current state as 1. ; if current character is a space . set current state as 2. ; if current character is a space . set current state as 3. ; validates all current state with previous state for the rules in the description of the problem . ; if we have reached last state and previous state is not 1 , then check next character . if next character is ' \0' , then return true , else false ; set previous state as current state before going over to the next character . ; 
def checkSentence(string):
    length = len(string)
    if string[0] < 'A' or string[0] > 'Z':
        return False
    if string[length - 1] != '.':
        return False
    prev_state = 0
    curr_state = 0
    index = 1
    while (string[index]):
        if string[index] >= 'A' and string[index] <= 'Z':
            curr_state = 0
        elif string[index] == ' ':
            curr_state = 1
        elif string[index] >= 'a' and string[index] <= 'z':
            curr_state = 2
        elif string[index] == '.':
            curr_state = 3
        if prev_state == curr_state and curr_state != 2:
            return False
        if prev_state == 2 and curr_state == 0:
            return False
        if curr_state == 3 and prev_state != 1:
            return True
        index += 1
        prev_state = curr_state
    return False


string = [
    "I love cinema.",
    "The vertex is S.",
    "I am single.",
    "My name is KG.",
    "I lovE cinema.",
    "GeeksQuiz. is a quiz site.",
    "I love Geeksquiz and Geeksforgeeks.",
    " You are my friend.",
    "I love cinema"]
string_size = len(string)
for i in range(string_size):
    if checkSentence(string[i]):
        print("" " + string[i] + "" is correct")
    else:
        print("" " + string[i] + "" is incorrect")

# Expected output:
 + string[i] +  is correct
 + string[i] +  is correct
 + string[i] +  is correct
 + string[i] +  is incorrect
 + string[i] +  is incorrect
 + string[i] +  is incorrect
 + string[i] +  is correct
 + string[i] +  is incorrect
 + string[i] +  is incorrect
# find index of 0 to be replaced with 1 to get longest continuous sequence of 1 s in a binary array | returns index of 0 to be replaced with 1 to get longest continuous sequence of 1 s . if there is no 0 in array , then it returns - 1. ; for maximum number of 1 around a zero ; for storing result ; index of previous zero ; index of previous to previous zero ; traverse the input array ; if current element is 0 , then calculate the difference between curr and prev_prev_zero ; update result if count of 1 s around prev_zero is more ; update for next iteration ; check for the last encountered zero ; 
def maxOnesIndex(arr, n):
    max_count = 0
    max_index = 0
    prev_zero = -1
    prev_prev_zero = -1
    for curr in range(n):
        if (arr[curr] == 0):
            if (curr - prev_prev_zero > max_count):
                max_count = curr - prev_prev_zero
                max_index = prev_zero
            prev_prev_zero = prev_zero
            prev_zero = curr
    if (n - prev_prev_zero > max_count):
        max_index = prev_zero
    return max_index


arr = [1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1]
n = len(arr)
print("Index of 0 to be replaced is ", maxOnesIndex(arr, n))

# Expected output:
Index of 0 to be replaced is  9
# length of the largest subarray with contiguous elements | set 1 | utility functions to find minimum and maximum of two elements ; returns length of the longest contiguous subarray ; initialize result ; initialize min and max for all subarrays starting with i ; consider all subarrays starting with i and ending with j ; update min and max in this subarray if needed ; if current subarray has all contiguous elements ; return result ; 
def min(x, y):
    return x if (x < y)else y


def max(x, y):
    return x if (x > y)else y


def findLength(arr, n):
    max_len = 1
    for i in range(n - 1):
        mn = arr[i]
        mx = arr[i]
        for j in range(i + 1, n):
            mn = min(mn, arr[j])
            mx = max(mx, arr[j])
            if ((mx - mn) == j - i):
                max_len = max(max_len, mx - mn + 1)
    return max_len


arr = [1, 56, 58, 57, 90, 92, 94, 93, 91, 45]
n = len(arr)
print("Length of the longest contiguous subarray is ", findLength(arr, n))

# Expected output:
Length of the longest contiguous subarray is  5
# print all increasing sequences of length k from first n natural numbers | a utility function to print contents of arr [ 0. . k - 1 ] ; a recursive function to print all increasing sequences of first n natural numbers . every sequence should be length k . the array arr [ ] is used to store current sequence . ; if length of current increasing sequence becomes k , print it ; decide the starting number to put at current position : if length is 0 , then there are no previous elements in arr [ ] . so start putting new numbers with 1. if length is not 0 , then start from value of previous element plus 1. ; increase length ; put all numbers ( which are greater than the previous element ) at new position . ; this is important . the variable ' len ' is shared among all function calls in recursion tree . its value must be brought back before next iteration of while loop ; this function prints all increasing sequences of first n natural numbers . the length of every sequence must be k . this function mainly uses printsequtil ( ) ; an array to store individual sequences ; initial length of current sequence ; 
def printArr(arr, k):
    for i in range(k):
        print(arr[i], end=" ")
    print()


def printSeqUtil(n, k, len1, arr):
    if (len1 == k):
        printArr(arr, k)
        return
    i = 1 if (len1 == 0)else (arr[len1 - 1] + 1)
    len1 += 1
    while (i <= n):
        arr[len1 - 1] = i
        printSeqUtil(n, k, len1, arr)
        i += 1
    len1 -= 1


def printSeq(n, k):
    arr = [0] * k
    len1 = 0
    printSeqUtil(n, k, len1, arr)


k = 3
n = 7
printSeq(n, k)

# Expected output:
1 2 3 
1 2 4 
1 2 5 
1 2 6 
1 2 7 
1 3 4 
1 3 5 
1 3 6 
1 3 7 
1 4 5 
1 4 6 
1 4 7 
1 5 6 
1 5 7 
1 6 7 
2 3 4 
2 3 5 
2 3 6 
2 3 7 
2 4 5 
2 4 6 
2 4 7 
2 5 6 
2 5 7 
2 6 7 
3 4 5 
3 4 6 
3 4 7 
3 5 6 
3 5 7 
3 6 7 
4 5 6 
4 5 7 
4 6 7 
5 6 7
# given two strings , find if first string is a subsequence of second | returns true if str1 [ ] is a subsequence of str2 [ ] . ; base cases ; if last characters of two strings are matching ; if last characters are not matching ; 
def isSubSequence(string1, string2, m, n):
    if m == 0:
        return True
    if n == 0:
        return False
    if string1[m - 1] == string2[n - 1]:
        return isSubSequence(string1, string2, m - 1, n - 1)
    return isSubSequence(string1, string2, m, n - 1)


string1 = "gksrek"
string2 = "geeksforgeeks"
if isSubSequence(string1, string2, len(string1), len(string2)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# find a sorted subsequence of size 3 in linear time | python3 program for above approach ; function to find the triplet ; if number of elements < 3 then no triplets are possible ; track best sequence length ( not current sequence length ) ; min number in array ; least max number in best sequence i . e . track arr [ j ] ( e . g . in array { 1 , 5 , 3 } our best sequence would be { 1 , 3 } with arr [ j ] = 3 ) ; save arr [ i ] ; iterate from 1 to nums . size ( ) ; this condition is only hit when current sequence size is 2 ; update best sequence max number to a smaller value ( i . e . we 've found a smaller value for arr[j]) ; store best sequence start value i . e . arr [ i ] ; increase best sequence length & save next number in our triplet ; we 've found our arr[k]!  print the output ; no triplet found ; 

import sys


def find3Numbers(nums):
    if (len(nums) < 3):
        print("No such triplet found", end='')
        return
    seq = 1
    min_num = nums[0]
    max_seq = -sys.maxsize - 1
    store_min = min_num
    for i in range(1, len(nums)):
        if (nums[i] == min_num):
            continue
        elif (nums[i] < min_num):
            min_num = nums[i]
            continue
        elif (nums[i] < max_seq):
            max_seq = nums[i]

            store_min = min_num
        elif (nums[i] > max_seq):
            if seq == 1:
                store_min = min_num
            seq += 1
            if (seq == 3):
                print("Triplet: " + str(store_min) +
                      ", " + str(max_seq) + ", " +
                      str(nums[i]))

                return

            max_seq = nums[i]
    print("No such triplet found", end='')


if __name__ == '__main__':
    nums = [1, 2, -1, 7, 5]
    find3Numbers(nums)

# Expected output:
Triplet: 1, 2, 7