giulio98/xlcost-single-prompt huggingface dataset:
# position of rightmost common bit in two numbers | python3 implementation to find the position of rightmost same bit ; function to find the position of rightmost set bit in 'n ; function to find the position of rightmost same bit in the binary representations of ' m ' and 'n ; position of rightmost same bit ; 

import math


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


def posOfRightMostSameBit(m, n):
    return getRightMostSetBit(~(m ^ n))


m, n = 16, 7
print("Position = ", posOfRightMostSameBit(m, n))

# Expected output:
Position =  4
# position of rightmost common bit in two numbers | function to find the position of rightmost same bit in the binary representations of ' m ' and 'n ; initialize loop counter ; check whether the value ' m ' is odd ; check whether the value ' n ' is odd ; below ' if ' checks for both values to be odd or even ; right shift value of m ; right shift value of n ; when no common set is found ; 
def posOfRightMostSameBit(m, n):
    loopCounter = 1

    while (m > 0 or n > 0):
        a = m % 2 == 1
        b = n % 2 == 1
        if (not (a ^ b)):
            return loopCounter
        m = m >> 1
        n = n >> 1
        loopCounter += 1
    return -1


if __name__ == '__main__':

    m, n = 16, 7

    print("Position = ",
          posOfRightMostSameBit(m, n))

# Expected output:
Position =  4
# check whether all the bits are set in the given range | function to check whether all the bits are set 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 both are equal , then all bits are set in the given range ; else all bits are not set ; 
def allBitsSetInTheGivenRange(n, l, r):
    num = ((1 << r) - 1) ^ ((1 << (l - 1)) - 1)
    new_num = n & num
    if (num == new_num):
        return "Yes"
    return "No"


n, l, r = 22, 2, 3
print(allBitsSetInTheGivenRange(n, l, r))

# Expected output:
Yes
# 1 to n bit numbers with no consecutive 1 s in binary representation | python3 program to print all numbers upto n bits with no consecutive set bits . ; let us first compute 2 raised to power n . ; loop 1 to n to check all the numbers ; a number i doesn ' t ▁ contain ▁ ▁ consecutive ▁ set ▁ bits ▁ if ▁ ▁ bitwise ▁ and ▁ of ▁ i ▁ and ▁ left ▁ ▁ shifted ▁ i ▁ do ' t contain a common set bit . ; 
def printNonConsecutive(n):
    p = (1 << n)
    for i in range(1, p):
        if ((i & (i << 1)) == 0):
            print(i, end=" ")


n = 3
printNonConsecutive(n)

# Expected output:
1 2 4 5
# find the n | efficient python3 program to find n - th palindrome ; construct the nth binary palindrome with the given group number , aux_number and operation type ; no need to insert any bit in the middle ; length of the final binary representation ; fill first and last bit as 1 ; start filling the a [ ] from middle , with the aux_num binary representation ; get the auxiliary number 's ith  bit and fill around middle ; insert bit 0 in the middle ; length of the final binary representation ; fill first and last bit as 1 ; start filling the a [ ] from middle , with the aux_num binary representation ; get the auxiliary number 's ith  bit and fill around middle ; length of the final binary representation ; fill first and last bit as 1 ; start filling the a [ ] from middle , with the aux_num binary representation ; get the auxiliary number 's ith  bit and fill around middle ; convert the number to decimal from binary ; will return the nth binary palindrome number ; add number of elements in all the groups , until the group of the nth number is found ; total number of elements until this group ; element 's offset position in the group ; finding which bit to be placed in the middle and finding the number , which we will fill from the middle in both directions ; we need to fill this auxiliary number in binary form the middle in both directions ; op = 0 need to insert 0 at middle ; op = 1 need to insert 1 at middle ; 

INT_SIZE = 32


def constructNthNumber(group_no, aux_num, op):

    a = [0] * INT_SIZE
    num, i = 0, 0
    if op == 2:
        len_f = 2 * group_no
        a[len_f - 1] = a[0] = 1
        while aux_num:
            a[group_no + i] = a[group_no - 1 - i] = aux_num & 1
            aux_num = aux_num >> 1
            i += 1
    elif op == 0:
        len_f = 2 * group_no + 1
        a[len_f - 1] = a[0] = 1
        a[group_no] = 0
        while aux_num:
            a[group_no + 1 + i] = a[group_no - 1 - i] = aux_num & 1
            aux_num = aux_num >> 1
            i += 1

    else:
        len_f = 2 * group_no + 1
        a[len_f - 1] = a[0] = 1
        a[group_no] = 1
        while aux_num:
            a[group_no + 1 + i] = a[group_no - 1 - i] = aux_num & 1
            aux_num = aux_num >> 1
            i += 1
    for i in range(0, len_f):
        num += (1 << i) * a[i]
    return num


def getNthNumber(n):

    group_no = 0
    count_upto_group, count_temp = 0, 1
    while count_temp < n:

        group_no += 1
        count_upto_group = count_temp
        count_temp += 3 * (1 << (group_no - 1))
    group_offset = n - count_upto_group - 1
    if (group_offset + 1) <= (1 << (group_no - 1)):

        op = 2
        aux_num = group_offset

    else:

        if (((group_offset + 1) -
             (1 << (group_no - 1))) % 2):
            op = 0
        else:
            op = 1
        aux_num = (((group_offset) -
                    (1 << (group_no - 1))) // 2)

    return constructNthNumber(group_no, aux_num, op)


if __name__ == "__main__":
    n = 9
    print(getNthNumber(n))

# Expected output:
27
# shuffle a pack of cards and answer the query | function to find card at 
def shuffle(N, key):
    NO_OF_BITS = N
    reverse_num = 0
    for i in range(NO_OF_BITS):
        temp = (key & (1 << i))
        if (temp):
            reverse_num |= (1 << ((NO_OF_BITS - 1) - i))
    print(reverse_num)


N = 3
key = 3
shuffle(N, key)

# Expected output:
6
# sum of numbers with exactly 2 bits set | to count number of set bits ; to calculate sum of numbers ; to count sum of number whose 2 bit are set ; 
def countSetBits(n):
    count = 0
    while (n):
        n = n & (n - 1)
        count = count + 1
    return count


def findSum(n):
    sum = 0
    for i in range(1, n + 1):
        if (countSetBits(i) == 2):
            sum = sum + i
    return sum


n = 10
print(findSum(n))

# Expected output:
33
# toggle the last m bits | 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 ; 
def toggleLastMBits(n, m):
    num = (1 << m) - 1
    return (n ^ num)


n = 107
m = 4
print(toggleLastMBits(n, m))

# Expected output:
100
# set the rightmost unset bit | python3 implementation to set the rightmost unset bit ; function to find the position of rightmost set bit ; if n = 0 , return 1 ; if all bits of ' n ' are set ; position of rightmost unset bit in ' n ' passing ~ n as argument ; set the bit at position 'pos ; 

import math


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


def setRightmostUnsetBit(n):
    if (n == 0):
        return 1
    if ((n & (n + 1)) == 0):
        return n
    pos = getPosOfRightmostSetBit(~n)
    return ((1 << (pos - 1)) | n)


n = 21
print(setRightmostUnsetBit(n))

# Expected output:
23
# previous smaller integer having one less number of set bits | python3 implementation to find the previous smaller integer with one less number of set bits ; function to find the position of rightmost set bit . ; function to find the previous smaller integer ; position of rightmost set bit of n ; turn off or unset the bit at position 'pos ; 

import math


def getFirstSetBitPos(n):
    return (int)(math .log(n & -n) / math .log(2)) + 1


def previousSmallerInteger(n):
    pos = getFirstSetBitPos(n)
    return (n & ~(1 << (pos - 1)))


n = 25
print("Previous small Integer = ", previousSmallerInteger(n))

# Expected output:
Previous small Integer =  24
# check if all bits of a number are set | function to check if all the bits are set or not in the binary representation of 'n ; all bits are not set ; loop till n becomes '0 ; if the last bit is not set ; right shift ' n ' by 1 ; all bits are set ; 
def areAllBitsSet(n):
    if (n == 0):
        return "No"
    while (n > 0):
        if ((n & 1) == 0):
            return "No"
        n = n >> 1
    return "Yes"


n = 7
print(areAllBitsSet(n))

# Expected output:
Yes
# check if all bits of a number are set | 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 ; 
def areAllBitsSet(n):
    if (n == 0):
        return "No"
    if (((n + 1) & n) == 0):
        return "Yes"
    return "No"


n = 7
print(areAllBitsSet(n))

# Expected output:
Yes
# next greater integer having one more number of set bits | python3 implementation to find the next greater integer with one more number of set bits ; function to find the position of rightmost set bit . returns - 1 if there are no set bits ; function to find the next greater integer ; position of rightmost unset bit of n by passing ~ n as argument ; if n consists of unset bits , then set the rightmost unset bit ; n does not consists of unset bits ; 

import math


def getFirstSetBitPos(n):
    return ((int)(math .log(n & -n) / math .log(2)) + 1) - 1


def nextGreaterWithOneMoreSetBit(n):
    pos = getFirstSetBitPos(~n)
    if (pos > -1):
        return (1 << pos) | n
    return ((n << 1) + 1)


n = 10
print("Next greater integer = ", nextGreaterWithOneMoreSetBit(n))

# Expected output:
Next greater integer =  11
# toggle all the bits of a number except k | returns a number with all bit toggled in n except k - th bit ; 1 ) toggle k - th bit by doing n ^ ( 1 << k ) 2 ) toggle all bits of the modified number ; 
def toggleAllExceptK(n, k):
    temp = bin(n ^ (1 << k))[2:]
    ans = ""
    for i in temp:
        if i == '1':
            ans += '0'
        else:
            ans += '1'
    return int(ans, 2)


if __name__ == '__main__':
    n = 4294967295
    k = 0
    print(toggleAllExceptK(n, k))

# Expected output:
1
# count numbers whose sum with x is equal to xor with x | function to find total 0 bit in a number ; function to find count of non - negative numbers less than or equal to x , whose bitwise xor and sum with x are equal . ; count number of zero bit in x ; power of 2 to count ; 
def CountZeroBit(x):
    count = 0
    while (x):
        if ((x & 1) == 0):
            count += 1
        x >>= 1
    return count


def CountXORandSumEqual(x):
    count = CountZeroBit(x)
    return (1 << count)


if __name__ == '__main__':
    x = 10
    print(CountXORandSumEqual(x))

# Expected output:
4
# find missing number in another array which is shuffled copy | returns the missing number size of arr2 [ ] is n - 1 ; missing number 'mnum ; 1 st array is of size 'n ; 2 nd array is of size 'n - 1 ; required missing number ; 
def missingNumber(arr1, arr2, n):
    mnum = 0
    for i in range(n):
        mnum = mnum ^ arr1[i]
    for i in range(n - 1):
        mnum = mnum ^ arr2[i]
    return mnum


arr1 = [4, 8, 1, 3, 7]
arr2 = [7, 4, 3, 1]
n = len(arr1)
print("Missing number = ",
      missingNumber(arr1, arr2, n))

# Expected output:
Missing number =  8
# find duplicates of array using bit array | a class to represent array of bits using array of integers ; constructor ; divide by 32. to store n bits , we need n / 32 + 1 integers ( assuming int is stored using 32 bits ) ; get value of a bit at given position ; divide by 32 to find position of integer . ; now find bit number in arr [ index ] ; find value of given bit number in arr [ index ] ; sets a bit at given position ; find index of bit position ; set bit number in arr [ index ] ; main function to print all duplicates ; create a bit with 32000 bits ; traverse array elements ; index in bit array ; if num is already present in bit array ; else insert num ; 

class BitArray:
    def __init__(self, n):
        self .arr = [0] * ((n >> 5) + 1)

    def get(self, pos):
        self .index = pos >> 5
        self .bitNo = pos & 0x1F
        return (self .arr[self .index] & (1 << self .bitNo)) != 0

    def set(self, pos):
        self .index = pos >> 5
        self .bitNo = pos & 0x1F
        self .arr[self .index] |= (1 << self .bitNo)


def checkDuplicates(arr):
    ba = BitArray(320000)
    for i in range(len(arr)):
        num = arr[i]
        if ba .get(num):
            print(num, end=" ")
        else:
            ba .set(num)


if __name__ == "__main__":
    arr = [1, 5, 1, 10, 12, 10]
    checkDuplicates(arr)

# Expected output:
1 10
# count smaller values whose xor with x is greater than x | python3 program to find count of values whose xor with x is greater than x and values are smaller than x ; initialize result ; traversing through all bits of x ; if current last bit of x is set then increment count by n . here n is a power of 2 corresponding to position of bit ; simultaneously calculate the 2 ^ n ; replace x with x / 2 ; ; 
def countValues(x):
    count = 0
    n = 1
    while (x > 0):
        if (x % 2 == 0):
            count += n
        n *= 2
        x /= 2
        x = int(x)
    return count


x = 10
print(countValues(x))

# Expected output:
5
# construct an array from xor of all elements of array except element at same index | function to construct new array ; calculate xor of array ; update array ; 
def constructXOR(A, n):
    XOR = 0
    for i in range(0, n):
        XOR ^= A[i]
    for i in range(0, n):
        A[i] = XOR ^ A[i]


A = [2, 4, 1, 3, 5]
n = len(A)
constructXOR(A, n)
for i in range(0, n):
    print(A[i], end=" ")

# Expected output:
3 5 0 2 4
# count all pairs of an array which differ in k bits | utility function to count total ones in a number ; function to count pairs of k different bits ; initialize final answer ; check for k differ bit ; 
def bitCount(n):
    count = 0
    while (n):
        if (n & 1):
            count += 1
        n >>= 1
    return count


def countPairsWithKDiff(arr, n, k):
    ans = 0
    for i in range(0, n - 1, 1):
        for j in range(i + 1, n, 1):
            xoredNum = arr[i] ^ arr[j]
            if (k == bitCount(xoredNum)):
                ans += 1
    return ans


if __name__ == '__main__':
    k = 2
    arr = [2, 4, 1, 3, 1]
    n = len(arr)
    print("Total pairs for k =", k, "are", countPairsWithKDiff(arr, n, k))

# Expected output:
Total pairs for k = 2 are 5
# count all pairs of an array which differ in k bits | function to calculate k bit different pairs in array ; get the maximum value among all array elemensts ; set the count array to 0 , count [ ] stores the total frequency of array elements ; initialize result ; for 0 bit answer will be total count of same number ; if count [ i ] is 0 , skip the next loop as it will not contribute the answer ; update answer if k differ bit found ; 
def kBitDifferencePairs(arr, n, k):
    MAX = max(arr)
    count = [0 for i in range(MAX + 1)]
    for i in range(n):
        count[arr[i]] += 1
    ans = 0
    if (k == 0):
        for i in range(MAX + 1):
            ans += (count[i] * (count[i] - 1)) // 2
        return ans
    for i in range(MAX + 1):
        if (count[i] == 0):
            continue
        for j in range(i + 1, MAX + 1):
            if (bin(i ^ j).count('1') == k):
                ans += count[i] * count[j]
    return ans


k = 2
arr = [2, 4, 1, 3, 1]
n = len(arr)
print("Total pairs for k =", k, "are", kBitDifferencePairs(arr, n, k))
k = 3
print("Total pairs for k =", k, "are", kBitDifferencePairs(arr, n, k))

# Expected output:
Total pairs for k = 2 are 5
Total pairs for k = 3 are 1
# ways to represent a number as a sum of 1 ' s ▁ and ▁ 2' s | function to multiply matrix . ; power function in log n ; function that returns ( n + 1 ) th fibonacci number or number of ways to represent n as sum of 1 ' s ▁ 2' s ; 
def multiply(F, M):
    x = F[0][0] * M[0][0] + F[0][1] * M[1][0]
    y = F[0][0] * M[0][1] + F[0][1] * M[1][1]
    z = F[1][0] * M[0][0] + F[1][1] * M[1][0]
    w = F[1][0] * M[0][1] + F[1][1] * M[1][1]
    F[0][0] = x
    F[0][1] = y
    F[1][0] = z
    F[1][1] = w


def power(F, n):
    if (n == 0 or n == 1):
        return
    M = [[1, 1], [1, 0]]
    power(F, n // 2)
    multiply(F, F)
    if (n % 2 != 0):
        multiply(F, M)


def countWays(n):
    F = [[1, 1], [1, 0]]
    if (n == 0):
        return 0
    power(F, n)
    return F[0][0]


n = 5
print(countWays(n))

# Expected output:
8
# multiplication of two numbers with shift operator | function for multiplication ; check for set bit and left shift n , count times ; increment of place value ( count ) ; 
def multiply(n, m):
    ans = 0
    count = 0
    while (m):
        if (m % 2 == 1):
            ans += n << count
        count += 1
        m = int(m / 2)
    return ans


if __name__ == '__main__':
    n = 20
    m = 13
    print(multiply(n, m))

# Expected output:
260
# compare two integers without using any comparison operator | function return true if a ^ b > 0 else false ; 
def EqualNumber(A, B):
    return (A ^ B)


A = 5
B = 6
print(int(not (EqualNumber(A, B))))

# Expected output:
0
# check if bits of a number has count of consecutive set bits in increasing order | returns true if n has increasing count of continuous - 1 else false ; store the bit - pattern of n into bit bitset - bp ; set prev_count = 0 and curr_count = 0. ; increment current count of continuous - 1 ; traverse all continuous - 0 ; check prev_count and curr_count on encounter of first zero after continuous - 1 s ; check for last sequence of continuous - 1 ; 
def findContinuous1(n):
    bp = list(bin(n))
    bits = len(bp)
    prev_count = 0
    curr_count = 0
    i = 0
    while (i < bits):
        if (bp[i] == '1'):
            curr_count += 1
            i += 1
        elif (bp[i - 1] == '0'):
            i += 1
            curr_count = 0
            continue
        else:
            if (curr_count < prev_count):
                return 0
            i += 1
            prev_count = curr_count
            curr_count = 0
    if (prev_count > curr_count and (curr_count != 0)):
        return 0
    return 1


n = 179
if (findContinuous1(n)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# check if bits of a number has count of consecutive set bits in increasing order | python3 program to check if counts of consecutive 1 s are increasing order . ; returns true if n has counts of consecutive 1 's are increasing order. ; initialize previous count ; we traverse bits from right to left and check if counts are decreasing order . ; ignore 0 s until we reach a set bit . ; count current set bits ; compare current with previous and update previous . ; 

import sys


def areSetBitsIncreasing(n):
    prev_count = sys .maxsize
    while (n > 0):
        while (n > 0 and n % 2 == 0):
            n = int(n / 2)
        curr_count = 1
        while (n > 0 and n % 2 == 1):
            n = n / 2
            curr_count += 1
        if (curr_count >= prev_count):
            return False
        prev_count = curr_count
    return True


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

# Expected output:
No
# check if a number has bits in alternate pattern | set 1 | returns true if n has alternate bit pattern else false ; store last bit ; traverse through remaining bits ; if current bit is same as previous ; 
def findPattern(n):
    prev = n % 2
    n = n // 2
    while (n > 0):
        curr = n % 2
        if (curr == prev):
            return False
        prev = curr
        n = n // 2
    return True


n = 10
print("Yes")if (findPattern(n))else print("No")

# Expected output:
Yes
# xor counts of 0 s and 1 s in binary representation | returns xor of counts 0 s and 1 s in binary representation of n . ; calculating count of zeros and ones ; 
def countXOR(n):
    count0, count1 = 0, 0
    while (n != 0):
        if (n % 2 == 0):
            count0 += 1
        else:
            count1 += 1
        n //= 2
    return (count0 ^ count1)


n = 31
print(countXOR(n))

# Expected output:
5
# count all pairs with given xor | returns count of pairs in arr [ 0. . n - 1 ] with xor value equals to x . ; create empty map that stores counts of individual elements of array . ; if there exist an element in map m with xor equals to x ^ arr [ i ] , that means there exist an element such that the xor of element with arr [ i ] is equal to x , then increment count . ; increment count of current element ; return total count of pairs with xor equal to x ; 
def xorPairCount(arr, n, x):
    result = 0
    m = dict()
    for i in range(n):
        curr_xor = x ^ arr[i]
        if (curr_xor in m .keys()):
            result += m[curr_xor]
        if arr[i] in m .keys():
            m[arr[i]] += 1
        else:
            m[arr[i]] = 1
    return result


arr = [2, 5, 2]
n = len(arr)
x = 0
print("Count of pairs with given XOR = ", xorPairCount(arr, n, x))

# Expected output:
Count of pairs with given XOR =  1
# bitwise and ( or & ) of a range | find position of msb in n . for example if n = 17 , then position of msb is 4. if n = 7 , value of msb is 3 ; function to find bit - wise & of all numbers from x to y . ; res = 0 initialize result ; find positions of msb in x and y ; if positions are not same , return ; add 2 ^ msb_p1 to result ; subtract 2 ^ msb_p1 from x and y . ; 
def msbPos(n):
    msb_p = -1
    while (n > 0):
        n = n >> 1
        msb_p += 1
    return msb_p


def andOperator(x, y):
    res = 0
    while (x > 0 and y > 0):
        msb_p1 = msbPos(x)
        msb_p2 = msbPos(y)
        if (msb_p1 != msb_p2):
            break
        msb_val = (1 << msb_p1)
        res = res + msb_val
        x = x - msb_val
        y = y - msb_val
    return res


x, y = 10, 15
print(andOperator(x, y))

# Expected output:
8
# multiply a number with 10 without using multiplication operator | function to find multiplication of n with 10 without using multiplication operator ; 
def multiplyTen(n):
    return (n << 1) + (n << 3)


n = 50
print(multiplyTen(n))

# Expected output:
500
# equal sum and xor | function to count number of values less than equal to n that satisfy the given condition ; traverse all numbers from 0 to n and increment result only when given condition is satisfied . ; 
def countValues(n):
    countV = 0
    for i in range(n + 1):
        if ((n + i) == (n ^ i)):
            countV += 1
    return countV


n = 12
print(countValues(n))

# Expected output:
4
# equal sum and xor | function to count number of values less than equal to n that satisfy the given condition ; unset_bits keeps track of count of un - set bits in binary representation of n ; return 2 ^ unset_bits ; 
def countValues(n):
    unset_bits = 0
    while (n):
        if n & 1 == 0:
            unset_bits += 1
        n = n >> 1
    return 1 << unset_bits


if __name__ == '__main__':
    n = 12
    print(countValues(n))

# Expected output:
4
# find profession in a special family | returns ' e ' if profession of node at given level and position is engineer . else doctor . the function assumes that given position and level have valid values . ; base case ; recursively find parent 's profession. if parent  is a doctor, this node will be a doctor if it is  at odd position and an engineer if at even position ; if parent is an engineer , then current node will be an engineer if at add position and doctor if even position . ; 
def findProffesion(level, pos):
    if (level == 1):
        return 'e'
    if (findProffesion(level - 1, (pos + 1) // 2) == 'd'):
        if (pos % 2):
            return 'd'
        else:
            return 'e'
    if (pos % 2):
        return 'e'
    else:
        return 'd'


if __name__ == '__main__':
    level = 3
    pos = 4
    if (findProffesion(level, pos) == 'e'):
        print("Engineer")
    else:
        print("Doctor")

# Expected output:
Engineer