dhyuti-n/xlcost-variable-roles huggingface dataset:
Python program:
def equilibrium ( arr ) :
    left_sum = [ ]
    right_sum = [ ]
    for i in range ( len ( arr ) ) :
        if ( i ) :
            left_sum . append ( left_sum [ i - 1 ] + arr [ i ] )
            right_sum . append ( right_sum [ i - 1 ] + arr [ len ( arr ) - 1 - i ] )
        else :
            left_sum . append ( arr [ i ] )
            right_sum . append ( arr [ len ( arr ) - 1 ] )
    for i in range ( len ( arr ) ) :
        if ( left_sum [ i ] == right_sum [ len ( arr ) - 1 - i ] ) :
            return ( i )
    return - 1
arr = [ - 7 , 1 , 5 , 2 , - 4 , 3 , 0 ]
print ( ' First ▁ equilibrium ▁ index ▁ is ▁ ' , equilibrium ( arr ) )

Index/key variables: i
Accumulator variables: left_sum, right_sum
Iterator variables: i
Python program:
def unboundedKnapsack ( W , n , val , wt ) :
    dp = [ 0 for i in range ( W + 1 ) ]
    ans = 0
    for i in range ( W + 1 ) :
        for j in range ( n ) :
            if ( wt [ j ] <= i ) :
                dp [ i ] = max ( dp [ i ] , dp [ i - wt [ j ] ] + val [ j ] )
    return dp [ W ]
W = 100
val = [ 10 , 30 , 20 ]
wt = [ 5 , 10 , 15 ]
n = len ( val )
print ( unboundedKnapsack ( W , n , val , wt ) )

Index/key variables: W, i, j
Iterator variables: i, j
Python program:
def ceilSearch ( arr , low , high , x ) :
    if x <= arr [ low ] :
        return low
    i = low
    for i in range ( high ) :
        if arr [ i ] == x :
            return i
        if arr [ i ] < x and arr [ i + 1 ] >= x :
            return i + 1
    return - 1
arr = [ 1 , 2 , 8 , 10 , 10 , 12 , 19 ]
n = len ( arr )
x = 3
index = ceilSearch ( arr , 0 , n - 1 , x ) ;
if index == - 1 :
    print ( " Ceiling ▁ of ▁ % d ▁ doesn ' t ▁ exist ▁ in ▁ array ▁ " % x )
else :
    print ( " ceiling ▁ of ▁ % d ▁ is ▁ % d " % ( x , arr [ index ] ) )

Index/key variables: i, index, low
Iterator variables: i
Python program:
def ceilSearch ( arr , low , high , x ) :
    if x <= arr [ low ] :
        return low
    if x > arr [ high ] :
        return - 1
    mid = ( low + high ) / 2 ;
    if arr [ mid ] == x :
        return mid
    elif arr [ mid ] < x :
        if mid + 1 <= high and x <= arr [ mid + 1 ] :
            return mid + 1
        else :
            return ceilSearch ( arr , mid + 1 , high , x )
    else :
        if mid - 1 >= low and x > arr [ mid - 1 ] :
            return mid
        else :
            return ceilSearch ( arr , low , mid - 1 , x )
arr = [ 1 , 2 , 8 , 10 , 10 , 12 , 19 ]
n = len ( arr )
x = 20
index = ceilSearch ( arr , 0 , n - 1 , x ) ;
if index == - 1 :
    print ( " Ceiling ▁ of ▁ % d ▁ doesn ' t ▁ exist ▁ in ▁ array ▁ " % x )
else :
    print ( " ceiling ▁ of ▁ % d ▁ is ▁ % d " % ( x , arr [ index ] ) )

Index/key variables: high, index, low, mid
Python program:
def getHeight ( Node ) :
    if ( Node == None ) :
        return 0
    else :
        lHeight = getHeight ( Node . left )
        rHeight = getHeight ( Node . right )
        if ( lHeight > rHeight ) :
            return ( lHeight + 1 )
        else :
            return ( rHeight + 1 )
class newNode :
    def __init__ ( self , data ) :
        self . data = data
        self . left = None
        self . right = None
def getTotalHeight ( root ) :
    if ( root == None ) :
        return 0
    return ( getTotalHeight ( root . left ) + getHeight ( root ) + getTotalHeight ( root . right ) )
if __name__ == ' _ _ main _ _ ' :
    root = newNode ( 1 )
    root . left = newNode ( 2 )
    root . right = newNode ( 3 )
    root . left . left = newNode ( 4 )
    root . left . right = newNode ( 5 )
print ( " Sum ▁ of ▁ heights ▁ of ▁ all ▁ Nodes ▁ = " , getTotalHeight ( root ) )

Class/struct names: newNode
Python program:
M = 100
def maxAverageOfPath ( cost , N ) :
    dp = [ [ 0 for i in range ( N + 1 ) ] for j in range ( N + 1 ) ]
    dp [ 0 ] [ 0 ] = cost [ 0 ] [ 0 ]
    for i in range ( 1 , N ) :
        dp [ i ] [ 0 ] = dp [ i - 1 ] [ 0 ] + cost [ i ] [ 0 ]
    for j in range ( 1 , N ) :
        dp [ 0 ] [ j ] = dp [ 0 ] [ j - 1 ] + cost [ 0 ] [ j ]
    for i in range ( 1 , N ) :
        for j in range ( 1 , N ) :
            dp [ i ] [ j ] = max ( dp [ i - 1 ] [ j ] , dp [ i ] [ j - 1 ] ) + cost [ i ] [ j ]
    return dp [ N - 1 ] [ N - 1 ] / ( 2 * N - 1 )
cost = [ [ 1 , 2 , 3 ] , [ 6 , 5 , 4 ] , [ 7 , 3 , 9 ] ]
print ( maxAverageOfPath ( cost , 3 ) )

Index/key variables: i, j
Iterator variables: i, j
Python program:
MAX = 1000
def maxCost ( mat , N ) :
    dp = [ [ 0 for i in range ( N ) ] for j in range ( N ) ]
    dp [ 0 ] [ 0 ] = mat [ 0 ] [ 0 ]
    for i in range ( 1 , N ) :
        dp [ i ] [ 0 ] = mat [ i ] [ 0 ] + dp [ i - 1 ] [ 0 ]
    for i in range ( 1 , N ) :
        for j in range ( 1 , min ( i + 1 , N ) ) :
            dp [ i ] [ j ] = mat [ i ] [ j ] + max ( dp [ i - 1 ] [ j - 1 ] , dp [ i - 1 ] [ j ] )
    result = 0
    for i in range ( N ) :
        if ( result < dp [ N - 1 ] [ i ] ) :
            result = dp [ N - 1 ] [ i ]
    return result
mat = [ [ 4 , 1 , 5 , 6 , 1 ] , [ 2 , 9 , 2 , 11 , 10 ] , [ 15 , 1 , 3 , 15 , 2 ] , [ 16 , 92 , 41 , 4 , 3 ] , [ 8 , 142 , 6 , 4 , 8 ] ]
N = 5
print ( ' Maximum ▁ Path ▁ Sum ▁ : ' , maxCost ( mat , N ) )

Index/key variables: i, j
Iterator variables: i, j
Python program:
M = 100
memo = [ [ 0 for i in range ( M ) ] for j in range ( M ) ]
def numberOfPermWithKInversion ( N , K ) :
    if ( N == 0 ) : return 0
    if ( K == 0 ) : return 1
    if ( memo [ N ] [ K ] != 0 ) :
        return memo [ N ] [ K ]
    sum = 0
    for i in range ( K + 1 ) :
        if ( i <= N - 1 ) :
            sum += numberOfPermWithKInversion ( N - 1 , K - i )
    memo [ N ] [ K ] = sum
    return sum
N = 4 ; K = 2
print ( numberOfPermWithKInversion ( N , K ) )

Index/key variables: K, N
Accumulator variables: sum
Iterator variables: i
Python program:
def isMajority ( a ) :
    mp = { }
    for i in a :
        if i in mp : mp [ i ] += 1
        else : mp [ i ] = 1
    for x in mp :
        if mp [ x ] >= len ( a ) // 2 :
            return True
    return False
a = [ 2 , 3 , 9 , 2 , 2 ]
print ( " Yes " if isMajority ( a ) else " No " )

Index/key variables: i, x
Iterator variables: i, x
Python program:
def isPairSum ( A , N , X ) :
    for i in range ( N ) :
        for j in range ( N ) :
            if ( i == j ) :
                continue
            if ( A [ i ] + A [ j ] == X ) :
                return True
            if ( A [ i ] + A [ j ] > X ) :
                break
    return 0
arr = [ 3 , 5 , 9 , 2 , 8 , 10 , 11 ]
val = 17
print ( isPairSum ( arr , len ( arr ) , val ) )

Index/key variables: i, j
Iterator variables: i, j
Python program:
def mini ( arr , l , h ) :
    mn = arr [ l ]
    for i in range ( l + 1 , h + 1 ) :
        if ( mn > arr [ i ] ) :
            mn = arr [ i ]
    return mn
def max ( arr , l , h ) :
    mx = arr [ l ]
    for i in range ( l + 1 , h + 1 ) :
        if ( mx < arr [ i ] ) :
            mx = arr [ i ]
    return mx
def minRemovals ( arr , l , h ) :
    if ( l >= h ) :
        return 0
    mn = mini ( arr , l , h )
    mx = max ( arr , l , h )
    if ( 2 * mn > mx ) :
        return 0
    return ( min ( minRemovals ( arr , l + 1 , h ) , minRemovals ( arr , l , h - 1 ) ) + 1 )
arr = [ 4 , 5 , 100 , 9 , 10 , 11 , 12 , 15 , 200 ]
n = len ( arr )
print ( minRemovals ( arr , 0 , n - 1 ) )

Index/key variables: i, l
Iterator variables: i
Python program:
def isPairSum ( A , N , X ) :
    i = 0
    j = N - 1
    while ( i < j ) :
        if ( A [ i ] + A [ j ] == X ) :
            return True
        elif ( A [ i ] + A [ j ] < X ) :
            i += 1
        else :
            j -= 1
    return 0
arr = [ 3 , 5 , 9 , 2 , 8 , 10 , 11 ]
val = 17
print ( isPairSum ( arr , len ( arr ) , val ) )

Index/key variables: i, j
Python program:
class Node :
    def __init__ ( self , key ) :
        self . data = key
        self . left = None
        self . right = None
sum = 0
def getTotalHeightUtil ( root ) :
    global sum
    if ( root == None ) :
        return 0
    lh = getTotalHeightUtil ( root . left )
    rh = getTotalHeightUtil ( root . right )
    h = max ( lh , rh ) + 1
    sum = sum + h
    return h
def getTotalHeight ( root ) :
    getTotalHeightUtil ( root )
    return sum
if __name__ == ' _ _ main _ _ ' :
    root = Node ( 1 )
    root . left = Node ( 2 )
    root . right = Node ( 3 )
    root . left . left = Node ( 4 )
    root . left . right = Node ( 5 )
    print ( " Sum ▁ of ▁ heights ▁ of ▁ all ▁ Nodes ▁ = " , getTotalHeight ( root ) )

Class/struct names: Node
Python program:
class Node :
    def __init__ ( self , data ) :
        self . data = data
        self . left = None
        self . right = None
def inOrder ( root ) :
    current = root
    stack = [ ]
    done = 0
    while True :
        if current is not None :
            stack . append ( current )
            current = current . left
        elif ( stack ) :
            current = stack . pop ( )
            print ( current . data , end = " ▁ " )
            current = current . right
        else :
            break
    print ( )
root = Node ( 1 )
root . left = Node ( 2 )
root . right = Node ( 3 )
root . left . left = Node ( 4 )
root . left . right = Node ( 5 )
inOrder ( root )

Accumulator variables: stack
Class/struct names: Node
Python program:
def numberOfPaths ( m , n ) :
    count = [ [ 0 for x in range ( n ) ] for y in range ( m ) ]
    for i in range ( m ) :
        count [ i ] [ 0 ] = 1 ;
    for j in range ( n ) :
        count [ 0 ] [ j ] = 1 ;
    for i in range ( 1 , m ) :
        for j in range ( 1 , n ) :
            count [ i ] [ j ] = count [ i - 1 ] [ j ] + count [ i ] [ j - 1 ]
    return count [ m - 1 ] [ n - 1 ]
m = 3
n = 3
print ( numberOfPaths ( m , n ) )

Index/key variables: i, j
Iterator variables: i, j
Python program:
from collections import defaultdict
def findSubsets ( arr ) :
    M = defaultdict ( int )
    for i in range ( len ( arr ) ) :
        M [ arr [ i ] ] += 1
    subsets = [ 0 ] * len ( M )
    i = 0
    for j in M :
        subsets [ i ] = M [ j ]
        i += 1
    return subsets
def subsetSum ( subsets , target ) :
    dp = [ [ 0 for x in range ( target + 1 ) ] for y in range ( len ( subsets ) + 1 ) ]
    for i in range ( len ( dp ) ) :
        dp [ i ] [ 0 ] = True
    for i in range ( 1 , len ( subsets ) + 1 ) :
        for j in range ( 1 , target + 1 ) :
            dp [ i ] [ j ] = dp [ i - 1 ] [ j ]
            if ( j >= subsets [ i - 1 ] ) :
                dp [ i ] [ j ] |= ( dp [ i - 1 ] [ j - subsets [ i - 1 ] ] )
    return dp [ len ( subsets ) ] [ target ]
def divideInto2Subset ( arr ) :
    subsets = findSubsets ( arr )
    if ( len ( arr ) % 2 == 1 ) :
        print ( " No " )
        return
    isPossible = subsetSum ( subsets , len ( arr ) // 2 )
    if ( isPossible ) :
        print ( " Yes " )
    else :
        print ( " No " )
if __name__ == " _ _ main _ _ " :
    arr = [ 2 , 1 , 2 , 3 ]
    divideInto2Subset ( arr )

Index/key variables: i, j, target
Iterator variables: i, j
Python program:
def maxProd ( n ) :
    if ( n == 0 or n == 1 ) :
        return 0
    max_val = 0
    for i in range ( 1 , n - 1 ) :
        max_val = max ( max_val , max ( i * ( n - i ) , maxProd ( n - i ) * i ) )
    return max_val ;
print ( " Maximum ▁ Product ▁ is ▁ " , maxProd ( 10 ) ) ;

Iterator variables: i
Python program:
def Min ( a , b ) :
    return min ( a , b )
def findMinInsertionsDP ( str1 , n ) :
    table = [ [ 0 for i in range ( n ) ] for i in range ( n ) ]
    l , h , gap = 0 , 0 , 0
    for gap in range ( 1 , n ) :
        l = 0
        for h in range ( gap , n ) :
            if str1 [ l ] == str1 [ h ] :
                table [ l ] [ h ] = table [ l + 1 ] [ h - 1 ]
            else :
                table [ l ] [ h ] = ( Min ( table [ l ] [ h - 1 ] , table [ l + 1 ] [ h ] ) + 1 )
            l += 1
    return table [ 0 ] [ n - 1 ] ;
str1 = " geeks "
print ( findMinInsertionsDP ( str1 , len ( str1 ) ) )

Index/key variables: h, l
Iterator variables: gap, h
Python program:
def maxCrossingSum ( arr , l , m , h ) :
    sm = 0
    left_sum = - 10000
    for i in range ( m , l - 1 , - 1 ) :
        sm = sm + arr [ i ]
        if ( sm > left_sum ) :
            left_sum = sm
    sm = 0
    right_sum = - 1000
    for i in range ( m + 1 , h + 1 ) :
        sm = sm + arr [ i ]
        if ( sm > right_sum ) :
            right_sum = sm
    return max ( left_sum + right_sum , left_sum , right_sum )
def maxSubArraySum ( arr , l , h ) :
    if ( l == h ) :
        return arr [ l ]
    m = ( l + h ) // 2
    return max ( maxSubArraySum ( arr , l , m ) , maxSubArraySum ( arr , m + 1 , h ) , maxCrossingSum ( arr , l , m , h ) )
arr = [ 2 , 3 , 4 , 5 , 7 ]
n = len ( arr )
max_sum = maxSubArraySum ( arr , 0 , n - 1 )
print ( " Maximum ▁ contiguous ▁ sum ▁ is ▁ " , max_sum )

Index/key variables: i, l
Iterator variables: i
Python program:
def max ( x , y ) :
    if ( x > y ) :
        return x
    else :
        return y
class node :
    def __init__ ( self ) :
        self . data = 0
        self . left = self . right = None
def LISS ( root ) :
    if ( root == None ) :
        return 0
    size_excl = LISS ( root . left ) + LISS ( root . right )
    size_incl = 1
    if ( root . left != None ) :
        size_incl += LISS ( root . left . left ) + LISS ( root . left . right )
    if ( root . right != None ) :
        size_incl += LISS ( root . right . left ) + LISS ( root . right . right )
    return max ( size_incl , size_excl )
def newNode ( data ) :
    temp = node ( )
    temp . data = data
    temp . left = temp . right = None
    return temp
root = newNode ( 20 )
root . left = newNode ( 8 )
root . left . left = newNode ( 4 )
root . left . right = newNode ( 12 )
root . left . right . left = newNode ( 10 )
root . left . right . right = newNode ( 14 )
root . right = newNode ( 22 )
root . right . right = newNode ( 25 )
print ( " Size ▁ of ▁ the ▁ Largest " , " ▁ Independent ▁ Set ▁ is ▁ " , LISS ( root ) )

Class/struct names: node
Python program:
def findWater ( i , j , X ) :
    if ( j > i ) :
        print ( " Incorrect ▁ Input " ) ;
        return ;
    glass = [ 0 ] * int ( i * ( i + 1 ) / 2 ) ;
    index = 0 ;
    glass [ index ] = X ;
    for row in range ( 1 , i ) :
        for col in range ( 1 , row + 1 ) :
            X = glass [ index ] ;
            glass [ index ] = 1.0 if ( X >= 1.0 ) else X ;
            X = ( X - 1 ) if ( X >= 1.0 ) else 0.0 ;
            glass [ index + row ] += ( X / 2 ) ;
            glass [ index + row + 1 ] += ( X / 2 ) ;
            index += 1 ;
    return glass [ int ( i * ( i - 1 ) / 2 + j - 1 ) ] ;
if __name__ == " _ _ main _ _ " :
    i = 2 ;
    j = 2 ;
    X = 2.0 ;
    res = repr ( findWater ( i , j , X ) ) ;
    print ( " Amount ▁ of ▁ water ▁ in ▁ jth ▁ glass ▁ of ▁ ith ▁ row ▁ is : " , res . ljust ( 8 , '0' ) ) ;

Index/key variables: index
Iterator variables: col, row
Python program:
class Pair ( object ) :
    def __init__ ( self , a , b ) :
        self . a = a
        self . b = b
def maxChainLength ( arr , n ) :
    max = 0
    mcl = [ 1 for i in range ( n ) ]
    for i in range ( 1 , n ) :
        for j in range ( 0 , i ) :
            if ( arr [ i ] . a > arr [ j ] . b and mcl [ i ] < mcl [ j ] + 1 ) :
                mcl [ i ] = mcl [ j ] + 1
    for i in range ( n ) :
        if ( max < mcl [ i ] ) :
            max = mcl [ i ]
    return max
arr = [ Pair ( 5 , 24 ) , Pair ( 15 , 25 ) , Pair ( 27 , 40 ) , Pair ( 50 , 60 ) ]
print ( ' Length ▁ of ▁ maximum ▁ size ▁ chain ▁ is ' , maxChainLength ( arr , len ( arr ) ) )

Index/key variables: i, j
Iterator variables: i, j
Class/struct names: Pair
Python program:
def minPalPartion ( str ) :
    n = len ( str )
    C = [ [ 0 for i in range ( n ) ] for i in range ( n ) ]
    P = [ [ False for i in range ( n ) ] for i in range ( n ) ]
    j = 0
    k = 0
    L = 0
    for i in range ( n ) :
        P [ i ] [ i ] = True ;
        C [ i ] [ i ] = 0 ;
    for L in range ( 2 , n + 1 ) :
        for i in range ( n - L + 1 ) :
            j = i + L - 1
            if L == 2 :
                P [ i ] [ j ] = ( str [ i ] == str [ j ] )
            else :
                P [ i ] [ j ] = ( ( str [ i ] == str [ j ] ) and P [ i + 1 ] [ j - 1 ] )
            if P [ i ] [ j ] == True :
                C [ i ] [ j ] = 0
            else :
                C [ i ] [ j ] = 100000000
                for k in range ( i , j ) :
                    C [ i ] [ j ] = min ( C [ i ] [ j ] , C [ i ] [ k ] + C [ k + 1 ] [ j ] + 1 )
    return C [ 0 ] [ n - 1 ]
str = " ababbbabbababa "
print ( ' Min ▁ cuts ▁ needed ▁ for ▁ Palindrome ▁ Partitioning ▁ is ' , minPalPartion ( str ) )

Index/key variables: i, j, k
Iterator variables: L, i, k
Python program:
class getNode :
    def __init__ ( self , data ) :
        self . data = data
        self . left = self . right = None
def countSubtreesWithSumX ( root , count , x ) :
    if ( not root ) :
        return 0
    ls = countSubtreesWithSumX ( root . left , count , x )
    rs = countSubtreesWithSumX ( root . right , count , x )
    Sum = ls + rs + root . data
    if ( Sum == x ) :
        count [ 0 ] += 1
    return Sum
def countSubtreesWithSumXUtil ( root , x ) :
    if ( not root ) :
        return 0
    count = [ 0 ]
    ls = countSubtreesWithSumX ( root . left , count , x )
    rs = countSubtreesWithSumX ( root . right , count , x )
    if ( ( ls + rs + root . data ) == x ) :
        count [ 0 ] += 1
    return count [ 0 ]
if __name__ == ' _ _ main _ _ ' :
    root = getNode ( 5 )
    root . left = getNode ( - 10 )
    root . right = getNode ( 3 )
    root . left . left = getNode ( 9 )
    root . left . right = getNode ( 8 )
    root . right . left = getNode ( - 4 )
    root . right . right = getNode ( 7 )
    x = 7
    print ( " Count ▁ = " , countSubtreesWithSumXUtil ( root , x ) )

Class/struct names: getNode
Python program:
def minchanges ( mat ) :
    N = len ( mat )
    M = len ( mat [ 0 ] )
    ans = 0
    mp = { }
    for i in range ( N // 2 ) :
        for j in range ( M // 2 ) :
            mp [ mat [ i ] [ M - 1 - j ] ] = mp . get ( mat [ i ] [ M - 1 - j ] , 0 ) + 1
            mp [ mat [ i ] [ j ] ] = mp . get ( mat [ i ] [ j ] , 0 ) + 1
            mp [ mat [ N - 1 - i ] [ M - 1 - j ] ] = mp . get ( mat [ N - 1 - i ] [ M - 1 - j ] , 0 ) + 1
            mp [ mat [ N - 1 - i ] [ j ] ] = mp . get ( mat [ N - 1 - i ] [ j ] , 0 ) + 1
            x = 0
            for it in mp :
                x = max ( x , mp [ it ] )
            ans = ans + 4 - x
            mp . clear ( )
    if ( N % 2 == 1 ) :
        for i in range ( M // 2 ) :
            if ( mat [ N // 2 ] [ i ] != mat [ N // 2 ] [ M - 1 - i ] ) :
                ans += 1
    if ( M % 2 == 1 ) :
        for i in range ( N // 2 ) :
            if ( mat [ i ] [ M // 2 ] != mat [ N - 1 - i ] [ M // 2 ] ) :
                ans += 1
    print ( ans )
mat = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 3 ] , [ 1 , 2 , 1 ] ]
minchanges ( mat )

Index/key variables: i, it, j
Accumulator variables: ans
Iterator variables: i, it, j
Python program:
class Node :
    def __init__ ( self , data ) :
        self . data = data
        self . left = None
        self . right = None
def getNode ( data ) :
    newNode = Node ( data )
    return newNode
count = 0
ptr = None
def countSubtreesWithSumXUtil ( root , x ) :
    global count , ptr
    l = 0
    r = 0
    if ( root == None ) :
        return 0
    l += countSubtreesWithSumXUtil ( root . left , x )
    r += countSubtreesWithSumXUtil ( root . right , x )
    if ( l + r + root . data == x ) :
        count += 1
    if ( ptr != root ) :
        return l + root . data + r
    return count
if __name__ == ' _ _ main _ _ ' :
    root = getNode ( 5 )
    root . left = getNode ( - 10 )
    root . right = getNode ( 3 )
    root . left . left = getNode ( 9 )
    root . left . right = getNode ( 8 )
    root . right . left = getNode ( - 4 )
    root . right . right = getNode ( 7 )
    x = 7
    ptr = root
    print ( " Count ▁ = ▁ " + str ( countSubtreesWithSumXUtil ( root , x ) ) )

Class/struct names: Node
Python program:
def q1 ( s , i ) :
    print ( " q1 - > " , end = " " ) ;
    if ( i == len ( s ) ) :
        print ( " NO " ) ;
        return ;
    if ( s [ i ] == '0' ) :
        q1 ( s , i + 1 ) ;
    else :
        q3 ( s , i + 1 ) ;
def q2 ( s , i ) :
    print ( " q2 - > " , end = " " ) ;
    if ( i == len ( s ) ) :
        print ( " NO " ) ;
        return ;
    if ( s [ i ] == '0' ) :
        q4 ( s , i + 1 ) ;
    else :
        q2 ( s , i + 1 ) ;
def q3 ( s , i ) :
    print ( " q3 - > " , end = " " ) ;
    if ( i == len ( s ) ) :
        print ( " YES " ) ;
        return ;
    if ( s [ i ] == '0' ) :
        q4 ( s , i + 1 ) ;
    else :
        q2 ( s , i + 1 ) ;
def q4 ( s , i ) :
    print ( " q4 - > " , end = " " ) ;
    if ( i == len ( s ) ) :
        print ( " YES " ) ;
        return ;
    if ( s [ i ] == '0' ) :
        q1 ( s , i + 1 ) ;
    else :
        q3 ( s , i + 1 ) ;
def q0 ( s , i ) :
    print ( " q0 - > " , end = " " ) ;
    if ( i == len ( s ) ) :
        print ( " NO " ) ;
        return ;
    if ( s [ i ] == '0' ) :
        q1 ( s , i + 1 ) ;
    else :
        q2 ( s , i + 1 ) ;
if __name__ == " _ _ main _ _ " :
    s = "010101" ;
    print ( " State ▁ transitions ▁ are " , end = " ▁ " ) ;
    q0 ( s , 0 ) ;

Index/key variables: i
Python program:
def doMatch ( A , B ) :
    for i in range ( len ( A ) ) :
        if A [ i ] != ' * ' and B [ i ] != ' * ' :
            if A [ i ] != B [ i ] :
                return False
    return True
if __name__ == ' _ _ main _ _ ' :
    A = " gee * sforgeeks "
    B = " geeksforgeeks "
    print ( int ( doMatch ( A , B ) ) )

Index/key variables: i
Iterator variables: i
Python program:
def factorial ( N ) :
    fact = 1
    for i in range ( 1 , N + 1 ) :
        fact = fact * i
    return fact
def nthTerm ( N ) :
    return ( factorial ( N ) * ( N + 2 ) // 2 )
if __name__ == " _ _ main _ _ " :
    N = 6
    print ( nthTerm ( N ) )

Iterator variables: i
Python program:
sum = [ 0 ]
max_level = [ - ( 2 ** 32 ) ]
class createNode :
    def __init__ ( self , data ) :
        self . d = data
        self . l = None
        self . r = None
def sumOfNodesAtMaxDepth ( ro , level ) :
    if ( ro == None ) :
        return
    if ( level > max_level [ 0 ] ) :
        sum [ 0 ] = ro . d
        max_level [ 0 ] = level
    elif ( level == max_level [ 0 ] ) :
        sum [ 0 ] = sum [ 0 ] + ro . d
    sumOfNodesAtMaxDepth ( ro . l , level + 1 )
    sumOfNodesAtMaxDepth ( ro . r , level + 1 )
root = createNode ( 1 )
root . l = createNode ( 2 )
root . r = createNode ( 3 )
root . l . l = createNode ( 4 )
root . l . r = createNode ( 5 )
root . r . l = createNode ( 6 )
root . r . r = createNode ( 7 )
sumOfNodesAtMaxDepth ( root , 0 )
print ( sum [ 0 ] )

Class/struct names: createNode
Python program:
class Node :
    def __init__ ( self , data ) :
        self . data = data
        self . left = None
        self . right = None
def sumMaxLevelRec ( node , Max ) :
    if node == None :
        return 0
    if Max == 1 :
        return node . data
    return ( sumMaxLevelRec ( node . left , Max - 1 ) + sumMaxLevelRec ( node . right , Max - 1 ) )
def maxDepth ( node ) :
    if node == None :
        return 0
    return 1 + max ( maxDepth ( node . left ) , maxDepth ( node . right ) )
def sumMaxLevel ( root ) :
    MaxDepth = maxDepth ( root )
    return sumMaxLevelRec ( root , MaxDepth )
if __name__ == " _ _ main _ _ " :
    root = Node ( 1 )
    root . left = Node ( 2 )
    root . right = Node ( 3 )
    root . left . left = Node ( 4 )
    root . left . right = Node ( 5 )
    root . right . left = Node ( 6 )
    root . right . right = Node ( 7 )
    print ( sumMaxLevel ( root ) )

Class/struct names: Node
Python program:
def SumofDistances ( s ) :
    v = [ [ ] for i in range ( 26 ) ]
    for i in range ( len ( s ) ) :
        v [ ord ( s [ i ] ) - ord ( ' a ' ) ] . append ( i )
    ans = 0
    for i in range ( 26 ) :
        sum = 0
        for j in range ( len ( v [ i ] ) ) :
            sum += v [ i ] [ j ]
        for j in range ( len ( v [ i ] ) ) :
            sum -= v [ i ] [ j ]
            ans += ( sum - ( len ( v [ i ] ) - 1 - j ) * ( v [ i ] [ j ] ) )
    print ( ans )
if __name__ == ' _ _ main _ _ ' :
    s = " ababa "
    SumofDistances ( s )

Index/key variables: i, j
Accumulator variables: ans, sum
Iterator variables: i, j