dhyuti-n/xlcost-variable-roles huggingface dataset:
Python program:
max = 1000 ;
def getFrequency ( tree , idx , i ) :
    sum = 0 ;
    while ( idx > 0 ) :
        sum += tree [ idx ] [ i ] ;
        idx -= ( idx & - idx ) ;
    return sum ;
def update ( tree , idx , val , i ) :
    while ( idx <= max ) :
        tree [ idx ] [ i ] += val ;
        idx += ( idx & - idx ) ;
def qType1 ( tree , l , x , str1 ) :
    update ( tree , l , - 1 , ord ( str1 [ l - 1 ] ) - 97 + 1 ) ;
    list1 = list ( str1 )
    list1 [ l - 1 ] = x ;
    str1 = ' ' . join ( list1 ) ;
    update ( tree , l , 1 , ord ( str1 [ l - 1 ] ) - 97 + 1 ) ;
def qType2 ( tree , l , r , str1 ) :
    count = 0 ;
    for i in range ( 1 , 27 ) :
        if ( l == 1 ) :
            if ( getFrequency ( tree , r , i ) % 2 == 1 ) :
                count += 1 ;
        else :
            if ( ( getFrequency ( tree , r , i ) - getFrequency ( tree , l - 1 , i ) ) % 2 == 1 ) :
                count += 1 ;
    print ( " Yes " ) if ( count <= 1 ) else print ( " No " ) ;
def buildBIT ( tree , str1 , n ) :
    for i in range ( n ) :
        update ( tree , i + 1 , 1 , ord ( str1 [ i ] ) - 97 + 1 ) ;
str1 = " geeksforgeeks " ;
n = len ( str1 ) ;
tree = [ [ 0 for x in range ( 27 ) ] for y in range ( max ) ] ;
buildBIT ( tree , str1 , n ) ;
qType1 ( tree , 4 , ' g ' , str1 ) ;
qType2 ( tree , 1 , 4 , str1 ) ;
qType2 ( tree , 2 , 3 , str1 ) ;
qType1 ( tree , 10 , ' t ' , str1 ) ;
qType2 ( tree , 10 , 11 , str1 ) ;

Index/key variables: i, idx
Accumulator variables: count, sum
Iterator variables: i
Python program:
def isequal ( str ) :
    n = len ( str )
    num = 0
    x = 1
    i = n - 1
    for i in range ( n - 1 , - 1 , - 1 ) :
        if ( '0' <= str [ i ] and str [ i ] <= '9' ) :
            num = ( ord ( str [ i ] ) - ord ( '0' ) ) * x + num
            x = x * 10
            if ( num >= n ) :
                return false
        else :
            break
    return num == i + 1
if __name__ == " _ _ main _ _ " :
    str = " geeksforgeeks13"
    print ( " Yes " ) if isequal ( str ) else print ( " No " )

Index/key variables: i
Iterator variables: i
Python program:
import sys
def kAnagrams ( str1 , str2 , k ) :
    flag = 0
    list1 = [ ]
    if ( len ( str1 ) != len ( str2 ) ) :
        sys . exit ( )
    arr1 = list ( str1 )
    arr2 = list ( str2 )
    arr1 . sort ( )
    arr2 . sort ( )
    for i in range ( len ( str1 ) ) :
        if ( arr1 [ i ] != arr2 [ i ] ) :
            list1 . append ( arr2 [ i ] )
    if ( len ( list1 ) <= k ) :
        flag = 1
    if ( flag == 1 ) :
        return True
    else :
        return False
if __name__ == " _ _ main _ _ " :
    str1 = " fodr "
    str2 = " gork "
    k = 2
    kAnagrams ( str1 , str2 , k )
    if ( kAnagrams ( str1 , str2 , k ) == True ) :
        print ( " Yes " )
    else :
        print ( " No " )

Index/key variables: i
Accumulator variables: list1
Iterator variables: i
Python program:
def check ( S , k ) :
    n = len ( S )
    if ( n % k != 0 ) :
        return False
    mp = { }
    for i in range ( 0 , n , k ) :
        mp [ S [ i : k ] ] = mp . get ( S [ i : k ] , 0 ) + 1
    if ( len ( mp ) == 1 ) :
        return True
    if ( len ( mp ) != 2 ) :
        return False
    for i in mp :
        if i == ( n // k - 1 ) or mp [ i ] == 1 :
            return True
    return False
if check ( " abababcd " , 2 ) :
    print ( " Yes " )
else :
    print ( " No " )

Index/key variables: i
Iterator variables: i
Python program:
MAX_CHAR = 26
def printGrouped ( string ) :
    n = len ( string )
    count = [ 0 ] * MAX_CHAR
    for i in range ( n ) :
        count [ ord ( string [ i ] ) - ord ( " a " ) ] += 1
    for i in range ( n ) :
        while count [ ord ( string [ i ] ) - ord ( " a " ) ] :
            print ( string [ i ] , end = " " )
            count [ ord ( string [ i ] ) - ord ( " a " ) ] -= 1
        count [ ord ( string [ i ] ) - ord ( " a " ) ] = 0
if __name__ == " _ _ main _ _ " :
    string = " geeksforgeeks "
    printGrouped ( string )

Index/key variables: i
Iterator variables: i
Python program:
MAX_CHAR = 26
def sortByPattern ( str , pat ) :
    global MAX_CHAR
    count = [ 0 ] * MAX_CHAR
    for i in range ( 0 , len ( str ) ) :
        count [ ord ( str [ i ] ) - 97 ] += 1
    index = 0 ;
    str = " "
    for i in range ( 0 , len ( pat ) ) :
        j = 0
        while ( j < count [ ord ( pat [ i ] ) - ord ( ' a ' ) ] ) :
            str += pat [ i ]
            j = j + 1
            index += 1
    return str
pat = " bca "
str = " abc "
print ( sortByPattern ( str , pat ) )

Index/key variables: i
Accumulator variables: index, str
Iterator variables: i
Python program:
def flip ( ch ) :
    return '1' if ( ch == '0' ) else '0'
def getFlipWithStartingCharcter ( str , expected ) :
    flipCount = 0
    for i in range ( len ( str ) ) :
        if ( str [ i ] != expected ) :
            flipCount += 1
        expected = flip ( expected )
    return flipCount
def minFlipToMakeStringAlternate ( str ) :
    return min ( getFlipWithStartingCharcter ( str , '0' ) , getFlipWithStartingCharcter ( str , '1' ) )
if __name__ == " _ _ main _ _ " :
    str = "0001010111"
    print ( minFlipToMakeStringAlternate ( str ) )

Index/key variables: i
Accumulator variables: flipCount
Iterator variables: i
Python program:
def remainderWith7 ( num ) :
    series = [ 1 , 3 , 2 , - 1 , - 3 , - 2 ] ;
    series_index = 0 ;
    result = 0 ;
    for i in range ( ( len ( num ) - 1 ) , - 1 , - 1 ) :
        digit = ord ( num [ i ] ) - 48 ;
        result += digit * series [ series_index ] ;
        series_index = ( series_index + 1 ) % 6 ;
        result %= 7 ;
    if ( result < 0 ) :
        result = ( result + 7 ) % 7 ;
    return result ;
str = "12345" ;
print ( " Remainder ▁ with ▁ 7 ▁ is " , remainderWith7 ( str ) ) ;

Index/key variables: i, series_index
Accumulator variables: result
Iterator variables: i
Python program:
def isReversible ( str ) :
    i = 0 ; j = len ( str ) - 1 ;
    while ( i < j ) :
        if ( str [ i ] != str [ j ] ) :
            return False ;
        i += 1 ;
        j -= 1 ;
    return True ;
str = " aba " ;
if ( isReversible ( str ) ) :
    print ( " YES " ) ;
else :
    print ( " NO " ) ;

Index/key variables: i, j
Python program:
CHAR_SIZE = 256
def checkPattern ( Str , pat ) :
    label = [ - 1 ] * CHAR_SIZE
    order = 1
    for i in range ( len ( pat ) ) :
        label [ ord ( pat [ i ] ) ] = order
        order += 1
    last_order = - 1
    for i in range ( len ( Str ) ) :
        if ( label [ ord ( Str [ i ] ) ] != - 1 ) :
            if ( label [ ord ( Str [ i ] ) ] < last_order ) :
                return False
            last_order = label [ ord ( Str [ i ] ) ]
    return True
if __name__ == ' _ _ main _ _ ' :
    Str = " engineers ▁ rock "
    pattern = " gsr "
    print ( checkPattern ( Str , pattern ) )

Index/key variables: i
Accumulator variables: order
Iterator variables: i
Python program:
def followsPattern ( string , pattern ) :
    patternSet = set ( )
    for i in range ( len ( pattern ) ) :
        patternSet . add ( pattern [ i ] )
    modifiedString = string
    for i in range ( len ( string ) - 1 , - 1 , - 1 ) :
        if not modifiedString [ i ] in patternSet :
            modifiedString = modifiedString [ : i ] + modifiedString [ i + 1 : ]
    for i in range ( len ( modifiedString ) - 1 , 0 , - 1 ) :
        if modifiedString [ i ] == modifiedString [ i - 1 ] :
            modifiedString = modifiedString [ : i ] + modifiedString [ i + 1 : ]
    if len ( pattern ) != len ( modifiedString ) :
        return False
    for i in range ( len ( pattern ) ) :
        if pattern [ i ] != modifiedString [ i ] :
            return False
    return True
if __name__ == " _ _ main _ _ " :
    string = " engineers ▁ rock "
    pattern = " er "
    print ( " Expected : ▁ true , ▁ Actual : " , followsPattern ( string , pattern ) )
    string = " engineers ▁ rock "
    pattern = " egr "
    print ( " Expected : ▁ false , ▁ Actual : " , followsPattern ( string , pattern ) )
    string = " engineers ▁ rock "
    pattern = " gsr "
    print ( " Expected : ▁ false , ▁ Actual : " , followsPattern ( string , pattern ) )
    string = " engineers ▁ rock "
    pattern = " eger "
    print ( " Expected : ▁ true , ▁ Actual : " , followsPattern ( string , pattern ) )

Index/key variables: i
Accumulator variables: patternSet
Iterator variables: i
Python program:
def encodedChar ( str , k ) :
    expand = " "
    i = 0
    while ( i < len ( str ) ) :
        while ( i < len ( str ) and ord ( str [ i ] ) >= ord ( ' a ' ) and ord ( str [ i ] ) <= ord ( ' z ' ) ) :
            temp += str [ i ]
            i += 1
        while ( i < len ( str ) and ord ( str [ i ] ) >= ord ( '1' ) and ord ( str [ i ] ) <= ord ( '9' ) ) :
            freq = freq * 10 + ord ( str [ i ] ) - ord ( '0' )
            i += 1
        for j in range ( 1 , freq + 1 , 1 ) :
            expand += temp
    if ( freq == 0 ) :
        expand += temp
    return expand [ k - 1 ]
if __name__ == ' _ _ main _ _ ' :
    str = " ab4c12ed3"
    k = 21
    print ( encodedChar ( str , k ) )

Index/key variables: i
Accumulator variables: expand, temp
Iterator variables: j
Python program:
def findMinLength ( strList ) :
    return len ( min ( arr , key = len ) )
def allContainsPrefix ( strList , str , start , end ) :
    for i in range ( 0 , len ( strList ) ) :
        word = strList [ i ]
        for j in range ( start , end + 1 ) :
            if word [ j ] != str [ j ] :
                return False
    return True
def CommonPrefix ( strList ) :
    index = findMinLength ( strList )
    low , high = 0 , index - 1
    while low <= high :
        mid = int ( low + ( high - low ) / 2 )
        if allContainsPrefix ( strList , strList [ 0 ] , low , mid ) :
            prefix = prefix + strList [ 0 ] [ low : mid + 1 ]
            low = mid + 1
        else :
            high = mid - 1
    return prefix
arr = [ " geeksforgeeks " , " geeks " , " geek " , " geezer " ]
lcp = CommonPrefix ( arr )
if len ( lcp ) > 0 :
    print ( " The ▁ longest ▁ common ▁ prefix ▁ is ▁ " + str ( lcp ) )
else :
    print ( " There ▁ is ▁ no ▁ common ▁ prefix " )

Index/key variables: i, j
Iterator variables: i, j
Python program:
def to_upper ( s ) :
    for i in range ( len ( s ) ) :
        if ( ' a ' <= s [ i ] <= ' z ' ) :
            s = s [ 0 : i ] + chr ( ord ( s [ i ] ) & ( ~ ( 1 << 5 ) ) ) + s [ i + 1 : ] ;
    return s ;
if __name__ == ' _ _ main _ _ ' :
    string = " geeksforgeeks " ;
    print ( to_upper ( string ) ) ;

Index/key variables: i
Iterator variables: i
Python program:
def commonPrefixUtil ( str1 , str2 ) :
    result = " "
    n1 , n2 = len ( str1 ) , len ( str2 )
    i , j = 0 , 0
    while i <= n1 - 1 and j <= n2 - 1 :
        if str1 [ i ] != str2 [ j ] :
            break
        result += str1 [ i ]
        i , j = i + 1 , j + 1
    return result
def commonPrefix ( arr , low , high ) :
    if low == high :
        return arr [ low ]
    if high > low :
        mid = low + ( high - low ) // 2
        str1 = commonPrefix ( arr , low , mid )
        str2 = commonPrefix ( arr , mid + 1 , high )
        return commonPrefixUtil ( str1 , str2 )
if __name__ == " _ _ main _ _ " :
    arr = [ " geeksforgeeks " , " geeks " , " geek " , " geezer " ]
    n = len ( arr )
    ans = commonPrefix ( arr , 0 , n - 1 )
    if len ( ans ) :
        print ( " The ▁ longest ▁ common ▁ prefix ▁ is " , ans )
    else :
        print ( " There ▁ is ▁ no ▁ common ▁ prefix " )

Index/key variables: i, j, low
Accumulator variables: result
Python program:
def commonPrefixUtil ( str1 , str2 ) :
    result = " " ;
    n1 = len ( str1 )
    n2 = len ( str2 )
    i = 0
    j = 0
    while i <= n1 - 1 and j <= n2 - 1 :
        if ( str1 [ i ] != str2 [ j ] ) :
            break
        result += str1 [ i ]
        i += 1
        j += 1
    return ( result )
def commonPrefix ( arr , n ) :
    prefix = arr [ 0 ]
    for i in range ( 1 , n ) :
        prefix = commonPrefixUtil ( prefix , arr [ i ] )
    return ( prefix )
if __name__ == " _ _ main _ _ " :
    arr = [ " geeksforgeeks " , " geeks " , " geek " , " geezer " ]
    n = len ( arr )
    ans = commonPrefix ( arr , n )
    if ( len ( ans ) ) :
        print ( " The ▁ longest ▁ common ▁ prefix ▁ is ▁ - " , ans ) ;
    else :
        print ( " There ▁ is ▁ no ▁ common ▁ prefix " )

Index/key variables: i, j
Accumulator variables: result
Iterator variables: i
Python program:
def findCombinations ( input : list , table : list ) -> list :
    out , temp = [ ] , [ ]
    mp = dict ( )
    index = 0
    for d in input :
        if d not in mp :
            mp [ d ] = index
        temp . clear ( )
        for i in range ( len ( table [ d - 1 ] ) ) :
            if index == 0 :
                s = table [ d - 1 ] [ i ]
                out . append ( s )
            if index > 0 :
                for string in out :
                    s = table [ d - 1 ] [ i ]
                    if mp [ d ] != index :
                        s = string [ mp [ d ] ]
                    string = string + s
                    temp . append ( string )
                if mp [ d ] != index :
                    break
        if index > 0 :
            out = temp . copy ( )
        index += 1
    return out
if __name__ == " _ _ main _ _ " :
    table = [ [ ' A ' , ' B ' , ' C ' ] , [ ' D ' , ' E ' , ' F ' ] , [ ' G ' , ' H ' , ' I ' ] , [ ' J ' , ' K ' , ' L ' ] , [ ' M ' , ' N ' , ' O ' ] , [ ' P ' , ' Q ' , ' R ' ] , [ ' S ' , ' T ' , ' U ' ] , [ ' V ' , ' W ' , ' X ' ] , [ ' Y ' , ' Z ' ] ]
    input = [ 1 , 2 , 1 ]
    out = findCombinations ( input , table )
    for it in out :
        print ( it , end = " ▁ " )

Index/key variables: d, i
Accumulator variables: index, out, temp
Iterator variables: d, i, it, string
Python program:
def zigZagConcat ( s , n ) :
    if ( n <= 1 ) :
        return s
    result = " "
    for rowNum in range ( n ) :
        i = rowNum
        up = True
        while ( i < len ( s ) ) :
            result += s [ i ]
            if ( rowNum == 0 or rowNum == n - 1 ) :
                i += ( 2 * n - 2 )
            else :
                if ( up ) :
                    i += ( 2 * ( n - rowNum ) - 2 )
                else :
                    i += rowNum * 2
                up ^= True
    return result
str = " GEEKSFORGEEKS "
n = 3
print ( zigZagConcat ( str , n ) )

Index/key variables: i
Accumulator variables: result, up
Iterator variables: rowNum
Boolean variables: up
Python program:
def pattern ( st , length ) :
    for i in range ( length ) :
        j = length - 1 - i
        for k in range ( length ) :
            if ( k == i or k == j ) :
                print ( st [ k ] , end = " " )
            else :
                print ( " ▁ " , end = " " )
        print ( )
if __name__ == " _ _ main _ _ " :
    st = " geeksforgeeks "
    length = len ( st )
    pattern ( st , length )

Index/key variables: k
Iterator variables: i, k
Python program:
NO_OF_CHARS = 256
def canFormPalindrome ( st ) :
    count = [ 0 ] * ( NO_OF_CHARS )
    for i in range ( 0 , len ( st ) ) :
        count [ ord ( st [ i ] ) ] = count [ ord ( st [ i ] ) ] + 1
    odd = 0
    for i in range ( 0 , NO_OF_CHARS ) :
        if ( count [ i ] & 1 ) :
            odd = odd + 1
        if ( odd > 1 ) :
            return False
    return True
if ( canFormPalindrome ( " geeksforgeeks " ) ) :
    print ( " Yes " )
else :
    print ( " No " )
if ( canFormPalindrome ( " geeksogeeks " ) ) :
    print ( " Yes " )
else :
    print ( " No " )

Index/key variables: i
Iterator variables: i
Python program:
def generateGray ( n ) :
    if ( n <= 0 ) :
        return [ "0" ]
    if ( n == 1 ) :
        return [ "0" , "1" ]
    recAns = generateGray ( n - 1 )
    mainAns = [ ]
    for i in range ( len ( recAns ) ) :
        s = recAns [ i ]
        mainAns . append ( "0" + s )
    for i in range ( len ( recAns ) - 1 , - 1 , - 1 ) :
        s = recAns [ i ]
        mainAns . append ( "1" + s )
    return mainAns
def generateGrayarr ( n ) :
    arr = generateGray ( n )
    print ( * arr , sep =   " " )
generateGrayarr ( 3 )

Index/key variables: i
Accumulator variables: mainAns
Iterator variables: i
Python program:
def removeChars ( string1 , string2 ) :
    for i in string2 :
        while i in string1 :
            itr = string1 . find ( i )
            string1 = string1 . replace ( i , ' ' )
    return string1
if __name__ == " _ _ main _ _ " :
    string1 = " geeksforgeeks "
    string2 = " mask "
    print ( removeChars ( string1 , string2 ) )

Iterator variables: i
Python program:
def removeDuplicate ( str , n ) :
    index = 0
    for i in range ( 0 , n ) :
        for j in range ( 0 , i + 1 ) :
            if ( str [ i ] == str [ j ] ) :
                break
        if ( j == i ) :
            str [ index ] = str [ i ]
            index += 1
    return " " . join ( str [ : index ] )
str = " geeksforgeeks "
n = len ( str )
print ( removeDuplicate ( list ( str ) , n ) )

Index/key variables: i, index, j
Iterator variables: i, j
Python program:
def getans ( i , v , q ) :
    if ( i == len ( q ) ) :
        return True
    for j in range ( len ( v ) ) :
        if ( v [ j ] >= q [ i ] ) :
            v [ j ] -= q [ i ]
            if ( getans ( i + 1 , v , q ) ) :
                return True
            v [ j ] += q [ i ]
    return False
def Check ( c , b ) :
    m = { }
    for i in range ( len ( b ) ) :
        if b [ i ] in m :
            m [ b [ i ] ] += 1
        else :
            m [ b [ i ] ] = 1
    v = [ ]
    for key , value in m . items ( ) :
        v . append ( value )
    check = getans ( 0 , v , c )
    if ( check ) :
        print ( " YES " )
    else :
        print ( " NO " )
if __name__ == ' _ _ main _ _ ' :
    c = [ 1 , 3 , 3 ]
    b = [ 2 , 2 , 2 , 2 , 4 , 4 , 4 ]
    Check ( c , b )

Index/key variables: i, j
Accumulator variables: v
Iterator variables: i, j, key, value
Python program:
def maximumXOR ( arr , n , K ) :
    K += 1
    maxXor = - 10 ** 9
    for i in range ( 1 << n ) :
        if ( bin ( i ) . count ( '1' ) == K ) :
            cur_xor = 0
            for j in range ( n ) :
                if ( i & ( 1 << j ) ) :
                    cur_xor = cur_xor ^ arr [ j ]
            maxXor = max ( maxXor , cur_xor )
    return maxXor
if __name__ == ' _ _ main _ _ ' :
    arr = [ 1 , 2 , 3 , 4 ]
    N = len ( arr )
    M = 2
    print ( maximumXOR ( arr , N , M ) )

Index/key variables: j
Iterator variables: i, j
Python program:
import sys
INT_MAX = sys . maxsize
def isValid ( board , i , j , K ) :
    if ( board [ i ] [ j ] <= K ) :
        return True
    return False
def findPath ( board , X , Y , M , N , K ) :
    if ( X < 0 or X == M or Y < 0 or Y == N ) :
        return True
    if ( isValid ( board , X , Y , K ) ) :
        board_XY = board [ X ] [ Y ]
        board [ X ] [ Y ] = INT_MAX
        if ( findPath ( board , X + 1 , Y , M , N , K - board_XY ) or findPath ( board , X - 1 , Y , M , N , K - board_XY ) or findPath ( board , X , Y + 1 , M , N , K - board_XY ) or findPath ( board , X , Y - 1 , M , N , K - board_XY ) ) :
            return True ;
        board [ X ] [ Y ] = board_XY
    return False
if __name__ == " _ _ main _ _ " :
    grid = [ [ 25 , 5 , 25 , 25 , 25 , 25 ] , [ 25 , 1 , 1 , 5 , 12 , 25 ] , [ 25 , 1 , 12 , 0 , 15 , 25 ] , [ 22 , 1 , 11 , 2 , 19 , 15 ] , [ 25 , 2 , 2 , 1 , 12 , 15 ] , [ 25 , 9 , 10 , 1 , 11 , 25 ] , [ 25 , 25 , 25 , 25 , 25 , 25 ] ]
    K = 17
    M = len ( grid )
    N = len ( grid [ 0 ] )
    X = 2
    Y = 3
    if ( findPath ( grid , X , Y , M , N , K ) ) :
        print ( " Yes " )
    else :
        print ( " No " )

Index/key variables: X, Y, i, j
Python program:
def isPerfect ( x ) :
    sum_div = 1
    for i in range ( 2 , int ( x / 2 + 1 ) ) :
        if ( x % i == 0 ) :
            sum_div = sum_div + i
    if ( sum_div == x ) :
        return 1
    else :
        return 0
def subsetSum ( arr , n ) :
    total = 1 << n
    for i in range ( total ) :
        sum = 0
        for j in range ( n ) :
            if ( i & ( 1 << j ) != 0 ) :
                sum = sum + arr [ j ]
        if ( isPerfect ( sum ) ) :
            print ( sum , " ▁ " )
arr = [ 5 , 4 , 6 ]
N = len ( arr )
subsetSum ( arr , N )

Index/key variables: j
Iterator variables: i, j
Python program:
def solve ( N , P1 , P2 , X , Move , QuitP1 , QuitP2 ) :
    if ( N == 0 or ( QuitP1 and QuitP2 ) ) :
        print ( " Number ▁ of ▁ pens ▁ remaining ▁ in ▁ the ▁ box : ▁ " , N )
        print ( " Number ▁ of ▁ pens ▁ collected ▁ by ▁ P1 : ▁ " , P1 )
        print ( " Number ▁ of ▁ pens ▁ collected ▁ by ▁ P2 : ▁ " , P2 )
        return
    if ( Move == 0 and QuitP1 == False ) :
        req_P1 = int ( pow ( 2 , X ) )
        if ( req_P1 <= N ) :
            P1 += req_P1
            N -= req_P1
        else :
            QuitP1 = True
    elif ( Move == 1 and QuitP2 == False ) :
        req_P2 = int ( pow ( 3 , X ) )
        if ( req_P2 <= N ) :
            P2 += req_P2
            N -= req_P2
        else :
            QuitP2 = True
    X += 1
    if ( Move == 1 ) :
        Move = 0
    else :
        Move = 1
    solve ( N , P1 , P2 , X , Move , QuitP1 , QuitP2 )
def PenGame ( N ) :
    P1 = 0
    P2 = 0
    X = 0
    Move = False
    QuitP1 = False
    QuitP2 = False
    solve ( N , P1 , P2 , X , Move , QuitP1 , QuitP2 )
N = 22
PenGame ( N )

Boolean variables: Move, QuitP1, QuitP2
Python program:
def checkUntil ( num , K , N , ans ) :
    if ( N == 1 ) :
        ans . append ( num )
        return
    if ( ( num % 10 + K ) <= 9 ) :
        checkUntil ( 10 * num + ( num % 10 + K ) , K , N - 1 , ans )
    if ( K ) :
        if ( ( num % 10 - K ) >= 0 ) :
            checkUntil ( 10 * num + num % 10 - K , K , N - 1 , ans )
def check ( K , N , ans ) :
    for i in range ( 1 , 10 ) :
        checkUntil ( i , K , N , ans )
def print_list ( ans ) :
    for i in range ( len ( ans ) ) :
        print ( ans [ i ] , end = " , ▁ " )
if __name__ == " _ _ main _ _ " :
    N = 4
    K = 8 ;
    ans = [ ]
    check ( K , N , ans )
    print_list ( ans )

Index/key variables: i
Iterator variables: i
Python program:
minSum = 1000000000
def getMinPathSum ( graph , visited , necessary , source , dest , currSum ) :
    global minSum
    if ( src == dest ) :
        flag = True ;
        for i in necessary :
            if ( not visited [ i ] ) :
                flag = False ;
                break ;
        if ( flag ) :
            minSum = min ( minSum , currSum ) ;
        return ;
    else :
        visited [ src ] = True ;
        for node in graph [ src ] :
            if not visited [ node [ 0 ] ] :
                visited [ node [ 0 ] ] = True ;
                getMinPathSum ( graph , visited , necessary , node [ 0 ] , dest , currSum + node [ 1 ] ) ;
                visited [ node [ 0 ] ] = False ;
        visited [ src ] = False ;
if __name__ == ' _ _ main _ _ ' :
    graph = dict ( )
    graph [ 0 ] = [ [ 1 , 2 ] , [ 2 , 3 ] , [ 3 , 2 ] ] ;
    graph [ 1 ] = [ [ 4 , 4 ] , [ 0 , 1 ] ] ;
    graph [ 2 ] = [ [ 4 , 5 ] , [ 5 , 6 ] ] ;
    graph [ 3 ] = [ [ 5 , 7 ] , [ 0 , 1 ] ] ;
    graph [ 4 ] = [ [ 6 , 4 ] ] ;
    graph [ 5 ] = [ [ 6 , 2 ] ] ;
    graph [ 6 ] = [ [ 7 , 11 ] ] ;
    n = 7 ;
    source = 0 ;
    dest = 6 ;
    visited = [ False for i in range ( n + 1 ) ]
    necessary = [ 2 , 4 ] ;
    getMinPathSum ( graph , visited , necessary , source , dest , 0 ) ;
    if ( minSum == 1000000000 ) :
        print ( - 1 )
    else :
        print ( minSum )

Index/key variables: i, src
Iterator variables: i, node
Boolean variables: flag
Python program:
def comb ( arr , Len , r , ipos , op , opos , Sum ) :
    if ( opos == r ) :
        sum2 = 0
        for i in range ( opos ) :
            sum2 = sum2 + op [ i ]
        if ( Sum == sum2 ) :
            for i in range ( opos ) :
                print ( op [ i ] , end = " , ▁ " )
            print ( )
        return
    if ( ipos < Len ) :
        comb ( arr , Len , r , ipos + 1 , op , opos , Sum )
        op [ opos ] = arr [ ipos ]
        comb ( arr , Len , r , ipos + 1 , op , opos + 1 , Sum )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 4 , 6 , 8 , 2 , 12 ]
    K = 3
    S = 20
    N = len ( arr )
    op = [ 0 ] * N
    comb ( arr , N , K , 0 , op , 0 , S )

Index/key variables: i, ipos, opos
Iterator variables: i