giulio98/xlcost-single-prompt huggingface dataset:
# leftover element after performing alternate bitwise or and bitwise xor operations on adjacent pairs | python3 program to print the leftover element after performing alternate bitwise or and bitwise xor operations to the pairs . ; count the step number ; if one element is there , it will be the answer ; at first step we do a bitwise or ; keep on doing bitwise operations till the last element is left ; perform operations ; if step is the odd step ; else : even step ; answer when one element is left ; 

N = 1000


def lastElement(a, n):
    steps = 1
    v = [[] for i in range(n)]
    if n == 1:
        return a[0]
    for i in range(0, n, 2):
        v[steps].append(a[i] | a[i+1])
    while len(v[steps]) > 1:

        steps += 1
        for i in range(0, len(v[steps-1]), 2):
            if steps & 1:
                v[steps].append(v[steps-1][i] | v[steps-1][i+1])
            else:
                v[steps].append(v[steps-1][i] ^ v[steps-1][i+1])
    return v[steps][0]


if __name__ == "__main__":

    a = [1, 4, 5, 6]
    n = len(a)
    index, value, a[0] = 0, 2, 2
    print(lastElement(a, n))
    index, value = 3, 5
    value = 5
    a[index] = value
    print(lastElement(a, n))

# Expected output:
1
3
# find the winner in nim | function to find winner of nim - game ; case when alice is winner ; when bob is winner ; 
def findWinner(A, n):
    res = 0
    for i in range(n):
        res ^= A[i]
    if (res == 0 or n % 2 == 0):
        return "Alice"
    else:
        return "Bob"


A = [1, 4, 3, 5]
n = len(A)
print("Winner = ", findWinner(A, n))

# Expected output:
Winner =  Alice
# fibbinary numbers ( no consecutive 1 s in binary ) | function to check whether a number is fibbinary or not ; if the number does not contain adjacent ones then ( n & ( n >> 1 ) ) operation results to 0 ; not a fibbinary number ; 
def isFibbinaryNum(n):
    if ((n & (n >> 1)) == 0):
        return 1
    return 0


n = 10
if (isFibbinaryNum(n)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# maximum xor | python3 program to obtain maximum xor value sub - array ; function to calculate maximum xor value ; return ( 2 ^ c - 1 ) ; 

import math


def maxXOR(n, k):
    c = int(math .log(n, 2)) + 1
    return ((1 << c) - 1)


n = 12
k = 3
print(maxXOR(n, k))

# Expected output:
15
# divide two integers without using multiplication , division and mod operator | function to divide a by b and return floor value it ; calculate sign of divisor i . e . , sign will be negative either one of them is negative only iff otherwise it will be positive ; remove sign of operands ; initialize the quotient ; test down from the highest bit and accumulate the tentative value for valid bit ; if the sign value computed earlier is - 1 then negate the value of quotient ; 
def divide(dividend, divisor):
    sign = (-1 if ((dividend < 0) ^ (divisor < 0))else 1)
    dividend = abs(dividend)
    divisor = abs(divisor)
    quotient = 0
    temp = 0
    for i in range(31, -1, -1):
        if (temp + (divisor << i) <= dividend):
            temp += divisor << i
            quotient |= 1 << i
    if sign == -1:
        quotient = -quotient
    return quotient


a = 10
b = 3
print(divide(a, b))
a = 43
b = -8
print(divide(a, b))

# Expected output:
3
-5
# xor of two numbers after making length of their binary representations equal | function to count the number of bits in binary representation of an integer ; initialize count ; count till n is non zero ; right shift by 1 i . e , divide by 2 ; function to calculate the xor of two numbers by adding trailing zeros to the number having less number of bits in its binary representation . ; stores the minimum and maximum ; left shift if the number of bits are less in binary representation ; 
def count(n):
    c = 0
    while (n != 0):
        c += 1
        n = n >> 1
    return c


def XOR(a, b):
    c = min(a, b)
    d = max(a, b)
    if (count(c) < count(d)):
        c = c << (count(d) - count(c))
    return (c ^ d)


a = 13
b = 5
print(XOR(a, b))

# Expected output:
7
# check if binary string multiple of 3 using dfa | function to check if the binary string is divisible by 3. ; checking if the bit is nonzero ; checking if the nonzero bit is at even position ; checking if the difference of non - zero oddbits and evenbits is divisible by 3. ; 
def CheckDivisibilty(A):
    oddbits = 0
    evenbits = 0
    for counter in range(len(A)):
        if (A[counter] == '1'):
            if (counter % 2 == 0):
                evenbits += 1
            else:
                oddbits += 1
    if (abs(oddbits - evenbits) % 3 == 0):
        print("Yes" + "")
    else:
        print("No" + "")


if __name__ == '__main__':
    A = "10101"
    CheckDivisibilty(A)

# Expected output:
Yes
# swap every two bits in bytes | python program to swap every two bits in a byte . ; extracting the high bit shift it to lowbit extracting the low bit shift it to highbit ; 

import math


def swapBitsInPair(x):
    return ((x & 0b10101010) >> 1) or ((x & 0b01010101) << 1)


x = 4
print(swapBitsInPair(x))

# Expected output:
8
# alternate bits of two numbers to create a new number | set even bit of number n ; res for store 101010. . number ; generate number form of 101010. . ... till temp size ; if bit is even then generate number and or with res ; return set even bit number ; set odd bit of number m ; res for store 101010. . number ; generate number form of 101010. . . . till temp size ; if bit is even then generate number and or with res ; return set odd bit number ; set even bit of number n ; set odd bit of number m ; take or with these number ; 
def setevenbits(n):
    temp = n
    count = 0
    res = 0
    while temp > 0:
        if count % 2:
            res |= (1 << count)
        count += 1
        temp >>= 1
    return (n & res)


def setoddbits(m):
    temp = m
    count = 0
    res = 0
    while temp > 0:
        if not count % 2:
            res |= (1 << count)
        count += 1
        temp >>= 1
    return (m & res)


def getAlternateBits(n, m):
    tempn = setevenbits(n)
    tempm = setoddbits(m)
    return (tempn | tempm)


n = 10
m = 11
print(getAlternateBits(n, m))

# Expected output:
11
# for every set bit of a number toggle bits of other | function for the nega_bit ; 
def toggleBits(n1, n2):
    return (n1 ^ n2)


n1 = 2
n2 = 5
print(toggleBits(n1, n2))

# Expected output:
7
# toggle all even bits of a number | returns a number which has all even bits of n toggled . ; generate number form of 101010 . . till of same order as n ; if bit is even then generate number and or with res ; return toggled number ; 
def evenbittogglenumber(n):
    res = 0
    count = 0
    temp = n
    while (temp > 0):
        if (count % 2 == 1):
            res = res | (1 << count)
        count = count + 1
        temp >>= 1
    return n ^ res


n = 11
print(evenbittogglenumber(n))

# Expected output:
1
# toggle first and last bits of a number | returns a number which has same bit count as n and has only first and last bits as set . ; set all the bit of the number ; adding one to n now unsets all bits and moves msb to one place . now we shift the number by 1 and add 1. ; if number is 1 ; take xor with first and last set bit number ; 
def takeLandFsetbits(n):
    n = n | n >> 1
    n = n | n >> 2
    n = n | n >> 4
    n = n | n >> 8
    n = n | n >> 16
    return ((n + 1) >> 1) + 1


def toggleFandLbits(n):
    if (n == 1):
        return 0
    return n ^ takeLandFsetbits(n)


n = 10
print(toggleFandLbits(n))

# Expected output:
3
# odious number | function to get no of set bits in binary representation of passed binary no . please refer below for details of this function : https : www . geeksforgeeks . org / count - set - bits - in - an - integer ; check if number is odious or not ; 
def countSetBits(n):
    count = 0
    while (n):
        n = n & (n - 1)
        count = count + 1
    return count


def checkOdious(n):
    return (countSetBits(n) % 2 == 1)


num = 32
if (checkOdious(num)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# set the left most unset bit | set left most unset bit ; if number contain all 1 then return n ; find position of leftmost unset bit ; if temp l . s . b is zero then unset bit pos is change ; return or of number and unset bit pos ; 
def setleftmostunsetbit(n):
    if not (n & (n + 1)):
        return n
    pos, temp, count = 0, n, 0
    while temp:
        if not (temp & 1):
            pos = count
        count += 1
        temp >>= 1
    return (n | (1 << (pos)))


n = 10
print(setleftmostunsetbit(n))

# Expected output:
14
# maximum xor using k numbers from 1 to n | to return max xor sum of 1 to n using at most k numbers ; if k is 1 then maximum possible sum is n ; finding number greater than or equal to n with most significant bit same as n . for example , if n is 4 , result is 7. if n is 5 or 6 , result is 7 ; return res - 1 which denotes a number with all bits set to 1 ; 
def maxXorSum(n, k):
    if k == 1:
        return n
    res = 1
    while res <= n:
        res <<= 1
    return res - 1


n = 4
k = 3
print(maxXorSum(n, k))

# Expected output:
7
# increment a number by one by manipulating the bits | python 3 implementation to increment a number by one by manipulating the bits ; function to find the position of rightmost set bit ; function to toggle the last m bits ; calculating a number ' num ' having ' m ' bits and all are set ; toggle the last m bits and return the number ; function to increment a number by one by manipulating the bits ; get position of rightmost unset bit if all bits of ' n ' are set , then the bit left to the msb is the rightmost unset bit ; kth bit of n is being set by this operation ; from the right toggle all the bits before the k - th bit ; required number ; 

import math


def getPosOfRightmostSetBit(n):
    return math .log2(n & -n)


def toggleLastKBits(n, k):
    num = (1 << (int)(k)) - 1
    return (n ^ num)


def incrementByOne(n):
    k = getPosOfRightmostSetBit(~n)
    n = ((1 << (int)(k)) | n)
    if (k != 0):
        n = toggleLastKBits(n, k)
    return n


n = 15
print(incrementByOne(n))

# Expected output:
16
# xnor of two numbers | python3 program to find xnor of two numbers ; log ( n ) solution ; make sure a is larger ; for last bit of a ; for last bit of b ; counter for count bit and set bit in xnor num ; for make new xnor number ; for set bits in new xnor number ; get last bit of a ; get last bit of b ; check if current two bits are same ; counter for count bit ; 

import math


def swap(a, b):
    temp = a
    a = b
    b = temp


def xnor(a, b):
    if (a < b):
        swap(a, b)
    if (a == 0 and b == 0):
        return 1
    a_rem = 0
    b_rem = 0
    count = 0
    xnornum = 0
    while (a != 0):
        a_rem = a & 1
        b_rem = b & 1
        if (a_rem == b_rem):
            xnornum |= (1 << count)
        count = count + 1
        a = a >> 1
        b = b >> 1
    return xnornum


a = 10
b = 50
print(xnor(a, b))

# Expected output:
7
# xnor of two numbers | python program to find xnor of two numbers ; please refer below post for details of this function https : www . geeksforgeeks . org / toggle - bits - significant - bit / ; make a copy of n as we are going to change it . ; suppose n is 273 ( binary is 100010001 ) . it does following 100010001 | 010001000 = 110011001 ; this makes sure 4 bits ( from msb and including msb ) are set . it does following 110011001 | 001100110 = 111111111 ; returns xnor of num1 and num2 ; make sure num1 is larger ; 

import math


def togglebit(n):
    if (n == 0):
        return 1
    i = n
    n = n | (n >> 1)
    n |= n >> 2
    n |= n >> 4
    n |= n >> 8
    n |= n >> 16
    return i ^ n


def xnor(num1, num2):
    if (num1 < num2):
        temp = num1
        num1 = num2
        num2 = temp
    num1 = togglebit(num1)
    return num1 ^ num2


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

# Expected output:
1
# maximum or sum of sub | function to find maximum or sum ; or sum of all the elements in both arrays ; 
def MaximumSum(a, b, n):
    sum1 = 0
    sum2 = 0
    for i in range(0, n):
        sum1 |= a[i]
        sum2 |= b[i]
    print(sum1 + sum2)


A = [1, 2, 4, 3, 2]
B = [2, 3, 3, 12, 1]
n = len(A)
MaximumSum(A, B, n)

# Expected output:
22
# position of rightmost bit with first carry in sum of two binary | python3 implementation to find the position of rightmost bit where a carry is generated first ; function to find the position of rightmost set bit in 'n ; function to find the position of rightmost bit where a carry is generated first ; 

import math


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


def posOfCarryBit(a, b):
    return posOfRightmostSetBit(a & b)


a = 10
b = 2
print(posOfCarryBit(a, b))

# Expected output:
2
# check whether the two numbers differ at one bit position only | function to check if x is power of 2 ; first x in the below expression is for the case when x is 0 ; function to check whether the two numbers differ at one bit position only ; 
def isPowerOfTwo(x):
    return x and (not (x & (x - 1)))


def differAtOneBitPos(a, b):
    return isPowerOfTwo(a ^ b)


a = 13
b = 9
if (differAtOneBitPos(a, b)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# multiplication with a power of 2 | returns 2 raised to power n ; 
def power2(n):
    if (n == 0):
        return 1
    if (n == 1):
        return 2
    return power2(n / 2)*power2(n / 2)


def multiply(x, n):
    return x * power2(n)


x = 70
n = 2
print(multiply(x, n))

# Expected output:
280
# multiplication with a power of 2 | efficient python3 code to compute x * ( 2 ^ n ) ; 
def multiply(x, n):
    return x << n


x = 70
n = 2
print(multiply(x, n))

# Expected output:
280
# check if n is divisible by power of 2 without using arithmetic operators | function to check whether n is divisible by pow ( 2 , m ) ; if expression results to 0 , then n is divisible by pow ( 2 , m ) ; n is not divisible ; 
def isDivBy2PowerM(n, m):
    if (n & ((1 << m) - 1)) == 0:
        return True
    return False


n = 8
m = 2
if isDivBy2PowerM(n, m):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# game of nim with removal of one stone allowed | return true if player a wins , return false if player b wins . ; checking the last bit of n . ; 
def findWinner(N):
    return N & 1


N = 15
print("Player A"if findWinner(N)else "Player B")

# Expected output:
Player A
# toggle all odd bits of a number | returns a number which has all odd bits of n toggled . ; generate number form of 101010. . . . . till of same order as n ; if bit is odd , then generate number and or with res ; return toggled number ; 
def evenbittogglenumber(n):
    res = 0
    count = 0
    temp = n
    while (temp > 0):
        if (count % 2 == 0):
            res = res | (1 << count)
        count = count + 1
        temp >>= 1
    return n ^ res


if __name__ == '__main__':
    n = 11
    print(evenbittogglenumber(n))

# Expected output:
14
# quotient and remainder dividing by 2 ^ k ( a power of 2 ) | python 3 to find remainder and quotient ; function to print remainder and quotient ; print remainder by n and ( m - 1 ) ; print quotient by right shifting n by ( log2 ( m ) ) times ; 

import math


def divide(n, m):
    print("Remainder = ", ((n) & (m - 1)))
    print("Quotient = ", (n >> (int)(math .log2(m))))


n = 43
m = 8
divide(n, m)

# Expected output:
Remainder =  3
Quotient =  5
# maximum and value of a pair in an array | function for finding maximum and value pair ; 
def maxAND(arr, n):
    res = 0
    for i in range(0, n):
        for j in range(i + 1, n):
            res = max(res, arr[i] & arr[j])
    return res


arr = [4, 8, 6, 2]
n = len(arr)
print("Maximum AND Value = ", maxAND(arr, n))

# Expected output:
Maximum AND Value =  4
# check if a number is positive , negative or zero using bit operators | function to return 1 if it is zero returns 0 if it is negative returns 2 if it is positive ; string array to store all kinds of number ; function call to check the sign of number ; 
def index(i):
    return 1 + (i >> 31) - (-i >> 31)


def check(n):
    s = "negative", "zero", "positive"
    val = index(n)
    print(n, "is", s[val])


check(30)
check(-20)
check(0)

# Expected output:
30 is positive
-20 is negative
0 is zero
# find two numbers from their sum and xor | function that takes in the sum and xor of two numbers and generates the two numbers such that the value of x is minimized ; traverse through all bits ; let us leave bits as 0. ; else : ( xi == 1 and ai == 1 ) ; 
def compute(S, X):
    A = (S - X)//2
    a = 0
    b = 0
    for i in range(64):
        Xi = (X & (1 << i))
        Ai = (A & (1 << i))
        if (Xi == 0 and Ai == 0):
            pass

        elif (Xi == 0 and Ai > 0):
            a = ((1 << i) | a)
            b = ((1 << i) | b)

        elif (Xi > 0 and Ai == 0):
            a = ((1 << i) | a)

        else:
            print("Not Possible")
            return

    print("a = ", a)
    print("b =", b)


S = 17
X = 13
compute(S, X)

# Expected output:
a =  15
b = 2
# divisibility by 64 with removal of bits allowed | function to check if it is possible to make it a multiple of 64. ; counter to count 0 's ; loop which traverses right to left and calculates the number of zeros before 1. ; 
def checking(s):
    c = 0
    n = len(s)
    i = n - 1
    while (i >= 0):
        if (s[i] == '0'):
            c += 1
        if (c >= 6 and s[i] == '1'):
            return True
        i -= 1
    return False


if __name__ == '__main__':
    s = "100010001"
    if (checking(s)):
        print("Possible")
    else:
        print("Not possible")

# Expected output:
Possible
# modify a bit at a given position | returns modified n . ; 
def modifyBit(n, p, b):
    mask = 1 << p
    return (n & ~mask) | ((b << p) & mask)


def main():
    print(modifyBit(6, 2, 0))
    print(modifyBit(6, 5, 1))


if __name__ == '__main__':
    main()

# Expected output:
2
38
# count set bits in a range | function to get no of set bits in the binary representation of 'n ; function to count set bits in the given range ; calculating a number ' num ' having ' r ' number of bits and bits in the range l to r are the only set bits ; returns number of set bits in the range ' l ' to ' r ' in 'n ; 
def countSetBits(n):
    count = 0
    while (n):
        n &= (n - 1)
        count = count + 1

    return count


def countSetBitsInGivenRange(n, l,  r):
    num = ((1 << r) - 1) ^ ((1 << (l - 1)) - 1)
    return countSetBits(n & num)


n = 42
l = 2
r = 5
ans = countSetBitsInGivenRange(n, l, r)
print(ans)

# Expected output:
2
# check if one of the numbers is one 's complement of the other | function to check if all the bits are set or not in the binary representation of 'n ; all bits are not set ; if true , then all bits are set ; else all bits are not set ; function to check if one of the two numbers is one 's complement of the other ; 
def areAllBitsSet(n):
    if (n == 0):
        return False
    if (((n + 1) & n) == 0):
        return True
    return False


def isOnesComplementOfOther(a, b):
    return areAllBitsSet(a ^ b)


a = 1
b = 14
if (isOnesComplementOfOther(a, b)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# unique element in an array where all elements occur k times except one | python 3 program to find unique element where every element appears k times except one ; create a count array to store count of numbers that have a particular bit set . count [ i ] stores count of array elements with i - th bit set . ; and ( bitwise ) each element of the array with each set digit ( one at a time ) to get the count of set bits at each position ; now consider all bits whose count is not multiple of k to form the required number . ; 

import sys


def findUnique(a, n, k):
    INT_SIZE = 8 * sys .getsizeof(int)
    count = [0] * INT_SIZE
    for i in range(INT_SIZE):
        for j in range(n):
            if ((a[j] & (1 << i)) != 0):
                count[i] += 1
    res = 0
    for i in range(INT_SIZE):
        res += (count[i] % k) * (1 << i)
    return res


if __name__ == '__main__':
    a = [6, 2, 5, 2, 2, 6, 6]
    n = len(a)
    k = 3
    print(findUnique(a, n, k))

# Expected output:
5
# check whether the number has only first and last bits set | function to check whether ' n ' is a power of 2 or not ; function to check whether number has only first and last bits set ; 
def powerOfTwo(n):
    return (not (n & n - 1))


def onlyFirstAndLastAreSet(n):
    if (n == 1):
        return True
    return powerOfTwo(n - 1)


n = 9
if (onlyFirstAndLastAreSet(n)):
    print('Yes')
else:
    print('No')

# Expected output:
Yes
# check if a number has bits in alternate pattern | set | function to check if all the bits are set or not in the binary representation of 'n ; if true , then all bits are set ; else all bits are not set ; function to check if a number has bits in alternate pattern ; to check if all bits are set in 'num ; 
def allBitsAreSet(n):
    if (((n + 1) & n) == 0):
        return True
    return False


def bitsAreInAltOrder(n):
    num = n ^ (n >> 1)
    return allBitsAreSet(num)


n = 10

if (bitsAreInAltOrder(n)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# minimum flips required to maximize a number with k set bits | python3 for finding min flip for maximizing given n ; function for finding set bit ; return count of set bit ; function for finding min flip ; number of bits in n ; find the largest number of same size with k set bits ; count bit differences to find required flipping . ; 

import math


def setBit(xorValue):
    count = 0
    while (xorValue):
        if (xorValue % 2):
            count += 1
        xorValue = int(xorValue / 2)
    return count


def minFlip(n, k):
    size = int(math .log(n) / math .log(2) + 1)
    max = pow(2, k) - 1
    max = max << (size - k)
    xorValue = (n ^ max)
    return (setBit(xorValue))


n = 27
k = 3
print("Min Flips = ", minFlip(n, k))

# Expected output:
Min Flips =  3
# set all the bits in given range of a number | function to toggle bits in the given range ; calculating a number ' range ' having set bits in the range from l to r and all other bits as 0 ( or unset ) . ; 
def setallbitgivenrange(n, l, r):
    range = (((1 << (l - 1)) - 1) ^ ((1 << (r)) - 1))
    return (n | range)


n, l, r = 17, 2, 3
print(setallbitgivenrange(n, l, r))

# Expected output:
23
# count total bits in a number | python3 program to find total bit in given number ; log function in base 2 take only integer part ; 

import math


def countBits(number):
    return int((math .log(number) / math .log(2)) + 1)


num = 65
print(countBits(num))

# Expected output:
7
# check whether all the bits are unset in the given range or not | function to check whether all the bits are unset in the given range or not ; calculating a number ' num ' having ' r ' number of bits and bits in the range l to r are the only set bits ; new number which will only have one or more set bits in the range l to r and nowhere else ; if new num is 0 , then all bits are unset in the given range ; else all bits are not unset ; 
def allBitsSetInTheGivenRange(n, l, r):
    num = ((1 << r) - 1) ^ ((1 << (l - 1)) - 1)
    new_num = n & num
    if (new_num == 0):
        return "Yes"
    return "No"


if __name__ == "__main__":
    n = 17
    l = 2
    r = 4
    print(allBitsSetInTheGivenRange(n, l, r))

# Expected output:
Yes
# toggle all bits after most significant bit | returns a number which has all set bits starting from msb of n ; this makes sure two bits ( from msb and including msb ) are set ; this makes sure 4 bits ( from msb and including msb ) are set ; 
def setAllBitsAfterMSB(n):
    n |= n >> 1
    n |= n >> 2
    n |= n >> 4
    n |= n >> 8
    n |= n >> 16
    return n


def toggle(n):
    n = n ^ setAllBitsAfterMSB(n)
    return n


n = 10
n = toggle(n)
print(n)

# Expected output:
5
# check if a number has two adjacent set bits | python 3 program to check if there are two adjacent set bits . ; 
def adjacentSet(n):
    return (n & (n >> 1))


if __name__ == '__main__':
    n = 3
    if (adjacentSet(n)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes