dhyuti-n/xlcost-variable-roles huggingface dataset:
Python program:
import math
def sumOfDigitsFrom1ToN ( n ) :
    if ( n < 10 ) :
        return ( n * ( n + 1 ) / 2 )
    d = ( int ) ( math . log10 ( n ) )
    a = [ 0 ] * ( d + 1 )
    a [ 0 ] = 0
    a [ 1 ] = 45
    for i in range ( 2 , d + 1 ) :
        a [ i ] = a [ i - 1 ] * 10 + 45 * ( int ) ( math . ceil ( math . pow ( 10 , i - 1 ) ) )
    p = ( int ) ( math . ceil ( math . pow ( 10 , d ) ) )
    msd = n // p
    return ( int ) ( msd * a [ d ] + ( msd * ( msd - 1 ) // 2 ) * p + msd * ( 1 + n % p ) + sumOfDigitsFrom1ToN ( n % p ) )
n = 328
print ( " Sum ▁ of ▁ digits ▁ in ▁ numbers ▁ from ▁ 1 ▁ to " , n , " is " , sumOfDigitsFrom1ToN ( n ) )

Index/key variables: d, i
Iterator variables: i
Python program:
import math
def sumOfDigitsFrom1ToNUtil ( n , a ) :
    if ( n < 10 ) :
        return ( n * ( n + 1 ) ) // 2
    d = int ( math . log ( n , 10 ) )
    p = int ( math . ceil ( pow ( 10 , d ) ) )
    msd = n // p
    return ( msd * a [ d ] + ( msd * ( msd - 1 ) // 2 ) * p + msd * ( 1 + n % p ) + sumOfDigitsFrom1ToNUtil ( n % p , a ) )
def sumOfDigitsFrom1ToN ( n ) :
    d = int ( math . log ( n , 10 ) )
    a = [ 0 ] * ( d + 1 )
    a [ 0 ] = 0
    a [ 1 ] = 45
    for i in range ( 2 , d + 1 ) :
        a [ i ] = a [ i - 1 ] * 10 + 45 * int ( math . ceil ( pow ( 10 , i - 1 ) ) )
    return sumOfDigitsFrom1ToNUtil ( n , a )
n = 328
print ( " Sum ▁ of ▁ digits ▁ in ▁ numbers ▁ from ▁ 1 ▁ to " , n , " is " , sumOfDigitsFrom1ToN ( n ) )

Index/key variables: d, i
Iterator variables: i
Python program:
def countWays ( N ) :
    if ( N == 1 ) :
        return 4
    countB = 1
    countS = 1
    for i in range ( 2 , N + 1 ) :
        prev_countB = countB
        prev_countS = countS
        countS = prev_countB + prev_countS
        countB = prev_countS
    result = countS + countB
    return ( result * result )
if __name__ == " _ _ main _ _ " :
    N = 3
    print ( " Count ▁ of ▁ ways ▁ for ▁ " , N , " ▁ sections ▁ is ▁ " , countWays ( N ) )

Iterator variables: i
Python program:
def count ( n ) :
    table = [ 0 for i in range ( n + 1 ) ]
    table [ 0 ] = 1
    for i in range ( 3 , n + 1 ) :
        table [ i ] += table [ i - 3 ]
    for i in range ( 5 , n + 1 ) :
        table [ i ] += table [ i - 5 ]
    for i in range ( 10 , n + 1 ) :
        table [ i ] += table [ i - 10 ]
    return table [ n ]
n = 20
print ( ' Count ▁ for ' , n , ' is ' , count ( n ) )
n = 13
print ( ' Count ▁ for ' , n , ' is ' , count ( n ) )

Index/key variables: i, n
Iterator variables: i
Python program:
def search ( pat , txt ) :
    M = len ( pat )
    N = len ( txt )
    for i in range ( N - M + 1 ) :
        j = 0
        while ( j < M ) :
            if ( txt [ i + j ] != pat [ j ] ) :
                break
            j += 1
        if ( j == M ) :
            print ( " Pattern ▁ found ▁ at ▁ index ▁ " , i )
if __name__ == ' _ _ main _ _ ' :
    txt = " AABAACAADAABAAABAA "
    pat = " AABA "
    search ( pat , txt )

Index/key variables: j
Iterator variables: i
Python program:
NO_OF_CHARS = 256
def badCharHeuristic ( string , size ) :
    badChar = [ - 1 ] * NO_OF_CHARS
    for i in range ( size ) :
        badChar [ ord ( string [ i ] ) ] = i ;
    return badChar
def search ( txt , pat ) :
    m = len ( pat )
    n = len ( txt )
    badChar = badCharHeuristic ( pat , m )
    s = 0
    while ( s <= n - m ) :
        j = m - 1
        while j >= 0 and pat [ j ] == txt [ s + j ] :
            j -= 1
        if j < 0 :
            print ( " Pattern ▁ occur ▁ at ▁ shift ▁ = ▁ { } " . format ( s ) )
            s += ( m - badChar [ ord ( txt [ s + m ] ) ] if s + m < n else 1 )
        else :
            s += max ( 1 , j - badChar [ ord ( txt [ s + j ] ) ] )
def main ( ) :
    txt = " ABAAABCD "
    pat = " ABC "
    search ( txt , pat )
if __name__ == ' _ _ main _ _ ' :
    main ( )

Index/key variables: i, j
Accumulator variables: s
Iterator variables: i
Python program:
MAX = 256
def compare ( arr1 , arr2 ) :
    for i in range ( MAX ) :
        if arr1 [ i ] != arr2 [ i ] :
            return False
    return True
def search ( pat , txt ) :
    M = len ( pat )
    N = len ( txt )
    countP = [ 0 ] * MAX
    countTW = [ 0 ] * MAX
    for i in range ( M ) :
        ( countP [ ord ( pat [ i ] ) ] ) += 1
        ( countTW [ ord ( txt [ i ] ) ] ) += 1
    for i in range ( M , N ) :
        if compare ( countP , countTW ) :
            print ( " Found ▁ at ▁ Index " , ( i - M ) )
        ( countTW [ ord ( txt [ i ] ) ] ) += 1
        ( countTW [ ord ( txt [ i - M ] ) ] ) -= 1
    if compare ( countP , countTW ) :
        print ( " Found ▁ at ▁ Index " , N - M )
txt = " BACDGABCDA "
pat = " ABCD "
search ( pat , txt )

Index/key variables: i
Iterator variables: i
Python program:
def getMedian ( ar1 , ar2 , n ) :
    i = 0
    j = 0
    m1 = - 1
    m2 = - 1
    count = 0
    while count < n + 1 :
        count += 1
        if i == n :
            m1 = m2
            m2 = ar2 [ 0 ]
            break
        elif j == n :
            m1 = m2
            m2 = ar1 [ 0 ]
            break
        if ar1 [ i ] <= ar2 [ j ] :
            m1 = m2
            m2 = ar1 [ i ]
            i += 1
        else :
            m1 = m2
            m2 = ar2 [ j ]
            j += 1
    return ( m1 + m2 ) / 2
ar1 = [ 1 , 12 , 15 , 26 , 38 ]
ar2 = [ 2 , 13 , 17 , 30 , 45 ]
n1 = len ( ar1 )
n2 = len ( ar2 )
if n1 == n2 :
    print ( " Median ▁ is ▁ " , getMedian ( ar1 , ar2 , n1 ) )
else :
    print ( " Doesn ' t ▁ work ▁ for ▁ arrays ▁ of ▁ unequal ▁ size " )

Index/key variables: i, j
Accumulator variables: count
Python program:
def getMedian ( ar1 , ar2 , n ) :
    i , j = n - 1 , 0
    while ( ar1 [ i ] > ar2 [ j ] and i > - 1 and j < n ) :
        ar1 [ i ] , ar2 [ j ] = ar2 [ j ] , ar1 [ i ]
        i -= 1
        j += 1
    ar1 . sort ( )
    ar2 . sort ( )
    return ( ar1 [ - 1 ] + ar2 [ 0 ] ) >> 1
if __name__ == ' _ _ main _ _ ' :
    ar1 = [ 1 , 12 , 15 , 26 , 38 ]
    ar2 = [ 2 , 13 , 17 , 30 , 45 ]
    n1 , n2 = len ( ar1 ) , len ( ar2 )
    if ( n1 == n2 ) :
        print ( ' Median ▁ is ' , getMedian ( ar1 , ar2 , n1 ) )
    else :
        print ( " Doesn ' t ▁ work ▁ for ▁ arrays ▁ of ▁ unequal ▁ size " )

Index/key variables: i, j
Python program:
import math
import copy
class Point ( ) :
    def __init__ ( self , x , y ) :
        self . x = x
        self . y = y
def dist ( p1 , p2 ) :
    return math . sqrt ( ( p1 . x - p2 . x ) * ( p1 . x - p2 . x ) + ( p1 . y - p2 . y ) * ( p1 . y - p2 . y ) )
def bruteForce ( P , n ) :
    min_val = float ( ' inf ' )
    for i in range ( n ) :
        for j in range ( i + 1 , n ) :
            if dist ( P [ i ] , P [ j ] ) < min_val :
                min_val = dist ( P [ i ] , P [ j ] )
    return min_val
def stripClosest ( strip , size , d ) :
    min_val = d
    for i in range ( size ) :
        j = i + 1
        while j < size and ( strip [ j ] . y - strip [ i ] . y ) < min_val :
            min_val = dist ( strip [ i ] , strip [ j ] )
            j += 1
    return min_val
def closestUtil ( P , Q , n ) :
    if n <= 3 :
        return bruteForce ( P , n )
    mid = n // 2
    midPoint = P [ mid ]
    Pl = P [ : mid ]
    Pr = P [ mid : ]
    dl = closestUtil ( Pl , Q , mid )
    dr = closestUtil ( Pr , Q , n - mid )
    d = min ( dl , dr )
    stripP = [ ]
    stripQ = [ ]
    lr = Pl + Pr
    for i in range ( n ) :
        if abs ( lr [ i ] . x - midPoint . x ) < d :
            stripP . append ( lr [ i ] )
        if abs ( Q [ i ] . x - midPoint . x ) < d :
            stripQ . append ( Q [ i ] )
    stripP . sort ( key = lambda point : point . y )
    min_a = min ( d , stripClosest ( stripP , len ( stripP ) , d ) )
    min_b = min ( d , stripClosest ( stripQ , len ( stripQ ) , d ) )
    return min ( min_a , min_b )
def closest ( P , n ) :
    P . sort ( key = lambda point : point . x )
    Q = copy . deepcopy ( P )
    Q . sort ( key = lambda point : point . y )
    return closestUtil ( P , Q , n )
P = [ Point ( 2 , 3 ) , Point ( 12 , 30 ) , Point ( 40 , 50 ) , Point ( 5 , 1 ) , Point ( 12 , 10 ) , Point ( 3 , 4 ) ]
n = len ( P )
print ( " The ▁ smallest ▁ distance ▁ is " , closest ( P , n ) )

Index/key variables: i, j, mid
Accumulator variables: stripP, stripQ
Iterator variables: i, j
Class/struct names: Point
Python program:
class Point :
    def __init__ ( self , x , y ) :
        self . x = x
        self . y = y
def onSegment ( p , q , r ) :
    if ( ( q . x <= max ( p . x , r . x ) ) and ( q . x >= min ( p . x , r . x ) ) and ( q . y <= max ( p . y , r . y ) ) and ( q . y >= min ( p . y , r . y ) ) ) :
        return True
    return False
def orientation ( p , q , r ) :
    val = ( float ( q . y - p . y ) * ( r . x - q . x ) ) - ( float ( q . x - p . x ) * ( r . y - q . y ) )
    if ( val > 0 ) :
        return 1
    elif ( val < 0 ) :
        return 2
    else :
        return 0
def doIntersect ( p1 , q1 , p2 , q2 ) :
    o1 = orientation ( p1 , q1 , p2 )
    o2 = orientation ( p1 , q1 , q2 )
    o3 = orientation ( p2 , q2 , p1 )
    o4 = orientation ( p2 , q2 , q1 )
    if ( ( o1 != o2 ) and ( o3 != o4 ) ) :
        return True
    if ( ( o1 == 0 ) and onSegment ( p1 , p2 , q1 ) ) :
        return True
    if ( ( o2 == 0 ) and onSegment ( p1 , q2 , q1 ) ) :
        return True
    if ( ( o3 == 0 ) and onSegment ( p2 , p1 , q2 ) ) :
        return True
    if ( ( o4 == 0 ) and onSegment ( p2 , q1 , q2 ) ) :
        return True
    return False
p1 = Point ( 1 , 1 )
q1 = Point ( 10 , 1 )
p2 = Point ( 1 , 2 )
q2 = Point ( 10 , 2 )
if doIntersect ( p1 , q1 , p2 , q2 ) :
    print ( " Yes " )
else :
    print ( " No " )
p1 = Point ( 10 , 0 )
q1 = Point ( 0 , 10 )
p2 = Point ( 0 , 0 )
q2 = Point ( 10 , 10 )
if doIntersect ( p1 , q1 , p2 , q2 ) :
    print ( " Yes " )
else :
    print ( " No " )
p1 = Point ( - 5 , - 5 )
q1 = Point ( 0 , 0 )
p2 = Point ( 1 , 1 )
q2 = Point ( 10 , 10 )
if doIntersect ( p1 , q1 , p2 , q2 ) :
    print ( " Yes " )
else :
    print ( " No " )

Class/struct names: Point
Python program:
def pow ( a , b ) :
    if ( b == 0 ) :
        return 1
    answer = a
    increment = a
    for i in range ( 1 , b ) :
        for j in range ( 1 , a ) :
            answer += increment
        increment = answer
    return answer
print ( pow ( 5 , 3 ) )

Accumulator variables: answer
Iterator variables: i, j
Python program:
def maximumNumberDistinctPrimeRange ( m , n ) :
    factorCount = [ 0 ] * ( n + 1 )
    prime = [ False ] * ( n + 1 )
    for i in range ( n + 1 ) :
        factorCount [ i ] = 0
        prime [ i ] = True
    for i in range ( 2 , n + 1 ) :
        if ( prime [ i ] == True ) :
            factorCount [ i ] = 1
            for j in range ( i * 2 , n + 1 , i ) :
                factorCount [ j ] += 1
                prime [ j ] = False
    max = factorCount [ m ]
    num = m
    for i in range ( m , n + 1 ) :
        if ( factorCount [ i ] > max ) :
            max = factorCount [ i ]
            num = i
    return num
if __name__ == " _ _ main _ _ " :
    m = 4
    n = 6
    print ( maximumNumberDistinctPrimeRange ( m , n ) )

Index/key variables: i, j, m
Iterator variables: i, j
Python program:
def fact ( n ) :
    f = 1
    while n >= 1 :
        f = f * n
        n = n - 1
    return f
def findSmallerInRight ( st , low , high ) :
    countRight = 0
    i = low + 1
    while i <= high :
        if st [ i ] < st [ low ] :
            countRight = countRight + 1
        i = i + 1
    return countRight
def findRank ( st ) :
    ln = len ( st )
    mul = fact ( ln )
    rank = 1
    i = 0
    while i < ln :
        mul = mul / ( ln - i )
        countRight = findSmallerInRight ( st , i , ln - 1 )
        rank = rank + countRight * mul
        i = i + 1
    return rank
st = " string "
print ( findRank ( st ) )

Index/key variables: i, low
Python program:
MAX_CHAR = 256 ;
count = [ 0 ] * ( MAX_CHAR + 1 ) ;
def fact ( n ) :
    return 1 if ( n <= 1 ) else ( n * fact ( n - 1 ) ) ;
def populateAndIncreaseCount ( str ) :
    for i in range ( len ( str ) ) :
        count [ ord ( str [ i ] ) ] += 1 ;
    for i in range ( 1 , MAX_CHAR ) :
        count [ i ] += count [ i - 1 ] ;
def updatecount ( ch ) :
    for i in range ( ord ( ch ) , MAX_CHAR ) :
        count [ i ] -= 1 ;
def findRank ( str ) :
    len1 = len ( str ) ;
    mul = fact ( len1 ) ;
    rank = 1 ;
    populateAndIncreaseCount ( str ) ;
    for i in range ( len1 ) :
        mul = mul // ( len1 - i ) ;
        rank += count [ ord ( str [ i ] ) - 1 ] * mul ;
        updatecount ( str [ i ] ) ;
    return rank ;
str = " string " ;
print ( findRank ( str ) ) ;

Index/key variables: i
Accumulator variables: rank
Iterator variables: i
Python program:
def exponential ( n , x ) :
    sum = 1.0
    for i in range ( n , 0 , - 1 ) :
        sum = 1 + x * sum / i
    print ( " e ^ x ▁ = " , sum )
n = 10
x = 1.0
exponential ( n , x )

Iterator variables: i
Python program:
import random
def findCeil ( arr , r , l , h ) :
    while ( l < h ) :
        mid = l + ( ( h - l ) >> 1 ) ;
        if r > arr [ mid ] :
            l = mid + 1
        else :
            h = mid
    if arr [ l ] >= r :
        return l
    else :
        return - 1
def myRand ( arr , freq , n ) :
    prefix = [ 0 ] * n
    prefix [ 0 ] = freq [ 0 ]
    for i in range ( n ) :
        prefix [ i ] = prefix [ i - 1 ] + freq [ i ]
    r = random . randint ( 0 , prefix [ n - 1 ] ) + 1
    indexc = findCeil ( prefix , r , 0 , n - 1 )
    return arr [ indexc ]
arr = [ 1 , 2 , 3 , 4 ]
freq = [ 10 , 5 , 20 , 100 ]
n = len ( arr )
for i in range ( 5 ) :
    print ( myRand ( arr , freq , n ) )

Index/key variables: i, indexc, l, mid
Iterator variables: i
Python program:
def findTrailingZeros ( n ) :
    count = 0
    while ( n >= 5 ) :
        n //= 5
        count += n
    return count
n = 100
print ( " Count ▁ of ▁ trailing ▁ 0s ▁ " + " in ▁ 100 ! ▁ is " , findTrailingZeros ( n ) )

Accumulator variables: count, n
Python program:
def catalan ( n ) :
    if ( n == 0 or n == 1 ) :
        return 1
    catalan = [ 0 ] * ( n + 1 )
    catalan [ 0 ] = 1
    catalan [ 1 ] = 1
    for i in range ( 2 , n + 1 ) :
        for j in range ( i ) :
            catalan [ i ] += catalan [ j ] * catalan [ i - j - 1 ]
    return catalan [ n ]
for i in range ( 10 ) :
    print ( catalan ( i ) , end = " ▁ " )

Index/key variables: i, j, n
Iterator variables: i, j
Python program:
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
def catalan ( n ) :
    c = binomialCoefficient ( 2 * n , n )
    return c / ( n + 1 )
for i in range ( 10 ) :
    print ( catalan ( i ) , end = " ▁ " )

Iterator variables: i
Python program:
def catalan ( n ) :
    cat_ = 1
    print ( cat_ , " ▁ " , end = ' ' )
    for i in range ( 1 , n ) :
        cat_ *= ( 4 * i - 2 ) ;
        cat_ //= ( i + 1 ) ;
        print ( cat_ , " ▁ " , end = ' ' )
n = 5
catalan ( n )

Accumulator variables: cat_
Iterator variables: i
Python program:
def printString ( n ) :
    arr = [ 0 ] * 10000
    i = 0
    while ( n > 0 ) :
        arr [ i ] = n % 26
        n = int ( n // 26 )
        i += 1
    for j in range ( 0 , i - 1 ) :
        if ( arr [ j ] <= 0 ) :
            arr [ j ] += 26
            arr [ j + 1 ] = arr [ j + 1 ] - 1
    for j in range ( i , - 1 , - 1 ) :
        if ( arr [ j ] > 0 ) :
            print ( chr ( ord ( ' A ' ) + ( arr [ j ] - 1 ) ) , end = " " ) ;
    print ( ) ;
if __name__ == ' _ _ main _ _ ' :
    printString ( 26 ) ;
    printString ( 51 ) ;
    printString ( 52 ) ;
    printString ( 80 ) ;
    printString ( 676 ) ;
    printString ( 702 ) ;
    printString ( 705 ) ;

Index/key variables: i, j
Iterator variables: j
Python program:
def getInvCount ( arr ) :
    inv_count = 0
    for i in range ( 0 , 2 ) :
        for j in range ( i + 1 , 3 ) :
            if ( arr [ j ] [ i ] > 0 and arr [ j ] [ i ] > arr [ i ] [ j ] ) :
                inv_count += 1
    return inv_count
def isSolvable ( puzzle ) :
    invCount = getInvCount ( puzzle )
    return ( invCount % 2 == 0 )
puzzle = [ [ 1 , 8 , 2 ] , [ 0 , 4 , 3 ] , [ 7 , 6 , 5 ] ]
if ( isSolvable ( puzzle ) ) :
    print ( " Solvable " )
else :
    print ( " Not ▁ Solvable " )

Index/key variables: i, j
Accumulator variables: inv_count
Iterator variables: i, j
Python program:
MAX_ITER = 1000000
def func ( x ) :
    return ( x * x * x - x * x + 2 )
def regulaFalsi ( a , b ) :
    if func ( a ) * func ( b ) >= 0 :
        print ( " You ▁ have ▁ not ▁ assumed ▁ right ▁ a ▁ and ▁ b " )
        return - 1
    c = a
    for i in range ( MAX_ITER ) :
        c = ( a * func ( b ) - b * func ( a ) ) / ( func ( b ) - func ( a ) )
        if func ( c ) == 0 :
            break
        elif func ( c ) * func ( a ) < 0 :
            b = c
        else :
            a = c
    print ( " The ▁ value ▁ of ▁ root ▁ is ▁ : ▁ " , ' % .4f ' % c )
a = - 200
b = 300
regulaFalsi ( a , b )

Iterator variables: i
Python program:
def getSingle ( arr , n ) :
    ones = 0
    twos = 0
    for i in range ( n ) :
        twos = twos | ( ones & arr [ i ] )
        ones = ones ^ arr [ i ]
        common_bit_mask = ~ ( ones & twos )
        ones &= common_bit_mask
        twos &= common_bit_mask
    return ones
arr = [ 3 , 3 , 2 , 3 ]
n = len ( arr )
print ( " The ▁ element ▁ with ▁ single ▁ occurrence ▁ is ▁ " , getSingle ( arr , n ) )

Index/key variables: i
Accumulator variables: ones, twos
Iterator variables: i
Python program:
INT_SIZE = 32
def getSingle ( arr , n ) :
    result = 0
    for i in range ( 0 , INT_SIZE ) :
        sm = 0
        x = ( 1 << i )
        for j in range ( 0 , n ) :
            if ( arr [ j ] & x ) :
                sm = sm + 1
        if ( ( sm % 3 ) != 0 ) :
            result = result | x
    return result
arr = [ 12 , 1 , 12 , 3 , 12 , 1 , 1 , 2 , 3 , 2 , 2 , 3 , 7 ]
n = len ( arr )
print ( " The ▁ element ▁ with ▁ single ▁ occurrence ▁ is ▁ " , getSingle ( arr , n ) )

Index/key variables: j
Iterator variables: i, j
Python program:
def countSetBits ( n ) :
    bitCount = 0
    for i in range ( 1 , n + 1 ) :
        bitCount += countSetBitsUtil ( i )
    return bitCount
def countSetBitsUtil ( x ) :
    if ( x <= 0 ) :
        return 0
    return ( 0 if int ( x % 2 ) == 0 else 1 ) + countSetBitsUtil ( int ( x / 2 ) )
if __name__ == ' _ _ main _ _ ' :
    n = 4
    print ( " Total ▁ set ▁ bit ▁ count ▁ is " , countSetBits ( n ) )

Accumulator variables: bitCount
Iterator variables: i
Python program:
def countSetBits ( n ) :
    i = 0
    ans = 0
    while ( ( 1 << i ) <= n ) :
        k = 0
        change = 1 << i
        for j in range ( 0 , n + 1 ) :
            ans += k
            if change == 1 :
                k = not k
                change = 1 << i
            else :
                change -= 1
        i += 1
    return ans
if __name__ == " _ _ main _ _ " :
    n = 17
    print ( countSetBits ( n ) )

Accumulator variables: ans, change, i
Iterator variables: j
Python program:
def getLeftmostBit ( n ) :
    m = 0
    while ( n > 1 ) :
        n = n >> 1
        m += 1
    return m
def getNextLeftmostBit ( n , m ) :
    temp = 1 << m
    while ( n < temp ) :
        temp = temp >> 1
        m -= 1
    return m
def countSetBits ( n ) :
    m = getLeftmostBit ( n )
    return _countSetBits ( n , m )
def _countSetBits ( n , m ) :
    if ( n == 0 ) :
        return 0
    m = getNextLeftmostBit ( n , m )
    if ( n == ( 1 << ( m + 1 ) ) - 1 ) :
        return ( ( m + 1 ) * ( 1 << m ) )
    n = n - ( 1 << m )
    return ( n + 1 ) + countSetBits ( n ) + m * ( 1 << ( m - 1 ) )
n = 17
print ( " Total ▁ set ▁ bit ▁ count ▁ is " , countSetBits ( n ) )

Accumulator variables: m
Python program:
def getSetBitsFromOneToN ( N ) :
    two = 2
    ans = 0
    n = N
    while ( n != 0 ) :
        ans += int ( N / two ) * ( two >> 1 )
        if ( ( N & ( two - 1 ) ) > ( two >> 1 ) - 1 ) :
            ans += ( N & ( two - 1 ) ) - ( two >> 1 ) + 1
        two <<= 1 ;
        n >>= 1 ;
    return ans

Accumulator variables: ans, n, two
Python program:
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 )

Accumulator variables: n, p1, p2
Python program:
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' )

Accumulator variables: count, n
Python program:
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 ) )

Index/key variables: i, j
Accumulator variables: count
Iterator variables: i, j
Python program:
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 ) )

Index/key variables: i
Iterator variables: i
Python program:
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 ) )

Accumulator variables: count, n
Python program:
def PositionRightmostSetbit ( n ) :
    position = 1
    m = 1
    while ( not ( n & m ) ) :
        m = m << 1
        position += 1
    return position
n = 16
print ( PositionRightmostSetbit ( n ) )

Accumulator variables: position
Python program:
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 )

Accumulator variables: pos
Iterator variables: i
Python program:
def isPowerOfTwo ( n ) :
    return ( True if ( n > 0 and ( ( n & ( n - 1 ) ) > 0 ) ) else False ) ;
def findPosition ( n ) :
    if ( isPowerOfTwo ( n ) == True ) :
        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 ) ;

Accumulator variables: pos