dhyuti-n/xlcost-variable-roles huggingface dataset:
Python program:
def partition ( arr , low , high ) :
    i = ( low - 1 )
    pivot = arr [ high ]
    for j in range ( low , high ) :
        if arr [ j ] <= pivot :
            i += 1
            arr [ i ] , arr [ j ] = arr [ j ] , arr [ i ]
    arr [ i + 1 ] , arr [ high ] = arr [ high ] , arr [ i + 1 ]
    return ( i + 1 )
def quickSort ( arr , low , high ) :
    if low < high :
        pi = partition ( arr , low , high )
        quickSort ( arr , low , pi - 1 )
        quickSort ( arr , pi + 1 , high )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 4 , 2 , 6 , 9 , 2 ]
    n = len ( arr )
    quickSort ( arr , 0 , n - 1 )
    for i in range ( n ) :
        print ( arr [ i ] , end = " ▁ " )

Index/key variables: high, i, j
Iterator variables: i, j
Python program:
def partition ( arr , l , h ) :
    i = ( l - 1 )
    x = arr [ h ]
    for j in range ( l , h ) :
        if arr [ j ] <= x :
            i = i + 1
            arr [ i ] , arr [ j ] = arr [ j ] , arr [ i ]
    arr [ i + 1 ] , arr [ h ] = arr [ h ] , arr [ i + 1 ]
    return ( i + 1 )
def quickSortIterative ( arr , l , h ) :
    size = h - l + 1
    stack = [ 0 ] * ( size )
    top = - 1
    top = top + 1
    stack [ top ] = l
    top = top + 1
    stack [ top ] = h
    while top >= 0 :
        h = stack [ top ]
        top = top - 1
        l = stack [ top ]
        top = top - 1
        p = partition ( arr , l , h )
        if p - 1 > l :
            top = top + 1
            stack [ top ] = l
            top = top + 1
            stack [ top ] = p - 1
        if p + 1 < h :
            top = top + 1
            stack [ top ] = p + 1
            top = top + 1
            stack [ top ] = h
arr = [ 4 , 3 , 5 , 2 , 1 , 3 , 2 , 3 ]
n = len ( arr )
quickSortIterative ( arr , 0 , n - 1 )
print ( " Sorted ▁ array ▁ is : " )
for i in range ( n ) :
    print ( " % ▁ d " % arr [ i ] ) ,

Index/key variables: h, i, j, top
Iterator variables: i, j
Python program:
def countSort ( arr , n , exp ) :
    output = [ 0 ] * n
    count = [ 0 ] * n
    for i in range ( n ) :
        count [ i ] = 0
    for i in range ( n ) :
        count [ ( arr [ i ] // exp ) % n ] += 1
    for i in range ( 1 , n ) :
        count [ i ] += count [ i - 1 ]
    for i in range ( n - 1 , - 1 , - 1 ) :
        output [ count [ ( arr [ i ] // exp ) % n ] - 1 ] = arr [ i ]
        count [ ( arr [ i ] // exp ) % n ] -= 1
    for i in range ( n ) :
        arr [ i ] = output [ i ]
def sort ( arr , n ) :
    countSort ( arr , n , 1 )
    countSort ( arr , n , n )
if __name__ == " _ _ main _ _ " :
    arr = [ 40 , 12 , 45 , 32 , 33 , 1 , 22 ]
    n = len ( arr )
    print ( " Given ▁ array ▁ is " )
    print ( * arr )
    sort ( arr , n )
    print ( " Sorted ▁ array ▁ is " )
    print ( * arr )

Index/key variables: i
Iterator variables: i
Python program:
def binarySearch ( arr , l , r , x ) :
    if ( r >= l ) :
        mid = int ( l + ( r - l ) / 2 )
        if ( arr [ mid ] == x ) : return mid
        if ( mid > l and arr [ mid - 1 ] == x ) :
            return ( mid - 1 )
        if ( mid < r and arr [ mid + 1 ] == x ) :
            return ( mid + 1 )
        if ( arr [ mid ] > x ) :
            return binarySearch ( arr , l , mid - 2 , x )
        return binarySearch ( arr , mid + 2 , r , x )
    return - 1
arr = [ 3 , 2 , 10 , 4 , 40 ]
n = len ( arr )
x = 4
result = binarySearch ( arr , 0 , n - 1 , x )
if ( result == - 1 ) :
    print ( " Element ▁ is ▁ not ▁ present ▁ in ▁ array " )
else :
    print ( " Element ▁ is ▁ present ▁ at ▁ index " , result )

Index/key variables: mid
Python program:
import sys
def printClosest ( ar1 , ar2 , m , n , x ) :
    diff = sys . maxsize
    l = 0
    r = n - 1
    while ( l < m and r >= 0 ) :
        if abs ( ar1 [ l ] + ar2 [ r ] - x ) < diff :
            res_l = l
            res_r = r
            diff = abs ( ar1 [ l ] + ar2 [ r ] - x )
        if ar1 [ l ] + ar2 [ r ] > x :
            r = r - 1
        else :
            l = l + 1
    print ( " The ▁ closest ▁ pair ▁ is ▁ [ " , ar1 [ res_l ] , " , " , ar2 [ res_r ] , " ] " )
ar1 = [ 1 , 4 , 5 , 7 ]
ar2 = [ 10 , 20 , 30 , 40 ]
m = len ( ar1 )
n = len ( ar2 )
x = 38
printClosest ( ar1 , ar2 , m , n , x )

Index/key variables: l, r, res_l, res_r
Python program:
MAX_VAL = 1000000000
def printClosest ( arr , n , x ) :
    res_l , res_r = 0 , 0
    l , r , diff = 0 , n - 1 , MAX_VAL
    while r > l :
        if abs ( arr [ l ] + arr [ r ] - x ) < diff :
            res_l = l
            res_r = r
            diff = abs ( arr [ l ] + arr [ r ] - x )
        if arr [ l ] + arr [ r ] > x :
            r -= 1
        else :
            l += 1
    print ( ' The ▁ closest ▁ pair ▁ is ▁ { } ▁ and ▁ { } ' . format ( arr [ res_l ] , arr [ res_r ] ) )
if __name__ == " _ _ main _ _ " :
    arr = [ 10 , 22 , 28 , 29 , 30 , 40 ]
    n = len ( arr )
    x = 54
    printClosest ( arr , n , x )

Index/key variables: l, r, res_l, res_r
Python program:
def countOnes ( arr , low , high ) :
    if high >= low :
        mid = low + ( high - low ) // 2
        if ( ( mid == high or arr [ mid + 1 ] == 0 ) and ( arr [ mid ] == 1 ) ) :
            return mid + 1
        if arr [ mid ] == 1 :
            return countOnes ( arr , ( mid + 1 ) , high )
        return countOnes ( arr , low , mid - 1 )
    return 0
arr = [ 1 , 1 , 1 , 1 , 0 , 0 , 0 ]
print ( " Count ▁ of ▁ 1 ' s ▁ in ▁ given ▁ array ▁ is " , countOnes ( arr , 0 , len ( arr ) - 1 ) )

Index/key variables: mid
Python program:
def minSwaps ( arr ) :
    n = len ( arr )
    maxx , minn , l , r = - 1 , arr [ 0 ] , 0 , 0
    for i in range ( n ) :
        if arr [ i ] > maxx :
            maxx = arr [ i ]
            l = i
        if arr [ i ] <= minn :
            minn = arr [ i ]
            r = i
    if r < l :
        print ( l + ( n - r - 2 ) )
    else :
        print ( l + ( n - r - 1 ) )
arr = [ 5 , 6 , 1 , 3 ]
minSwaps ( arr )

Index/key variables: i
Iterator variables: i
Python program:
R = 3
C = 3
import sys
def min ( x , y , z ) :
    if ( x < y ) :
        return x if ( x < z ) else z
    else :
        return y if ( y < z ) else z
def minCost ( cost , m , n ) :
    if ( n < 0 or m < 0 ) :
        return sys . maxsize
    elif ( m == 0 and n == 0 ) :
        return cost [ m ] [ n ]
    else :
        return cost [ m ] [ n ] + min ( minCost ( cost , m - 1 , n - 1 ) , minCost ( cost , m - 1 , n ) , minCost ( cost , m , n - 1 ) )
cost = [ [ 1 , 2 , 3 ] , [ 4 , 8 , 2 ] , [ 1 , 5 , 3 ] ]
print ( minCost ( cost , 2 , 2 ) )

Index/key variables: m, n
Python program:
R = 3
C = 3
def minCost ( cost , m , n ) :
    tc = [ [ 0 for x in range ( C ) ] for x in range ( R ) ]
    tc [ 0 ] [ 0 ] = cost [ 0 ] [ 0 ]
    for i in range ( 1 , m + 1 ) :
        tc [ i ] [ 0 ] = tc [ i - 1 ] [ 0 ] + cost [ i ] [ 0 ]
    for j in range ( 1 , n + 1 ) :
        tc [ 0 ] [ j ] = tc [ 0 ] [ j - 1 ] + cost [ 0 ] [ j ]
    for i in range ( 1 , m + 1 ) :
        for j in range ( 1 , n + 1 ) :
            tc [ i ] [ j ] = min ( tc [ i - 1 ] [ j - 1 ] , tc [ i - 1 ] [ j ] , tc [ i ] [ j - 1 ] ) + cost [ i ] [ j ]
    return tc [ m ] [ n ]
cost = [ [ 1 , 2 , 3 ] , [ 4 , 8 , 2 ] , [ 1 , 5 , 3 ] ]
print ( minCost ( cost , 2 , 2 ) )

Index/key variables: i, j, m, n
Iterator variables: i, j
Python program:
def knapSack ( W , wt , val , n ) :
    K = [ [ 0 for x in range ( W + 1 ) ] for x in range ( n + 1 ) ]
    for i in range ( n + 1 ) :
        for w in range ( W + 1 ) :
            if i == 0 or w == 0 :
                K [ i ] [ w ] = 0
            elif wt [ i - 1 ] <= w :
                K [ i ] [ w ] = max ( val [ i - 1 ] + K [ i - 1 ] [ w - wt [ i - 1 ] ] , K [ i - 1 ] [ w ] )
            else :
                K [ i ] [ w ] = K [ i - 1 ] [ w ]
    return K [ n ] [ W ]
val = [ 60 , 100 , 120 ]
wt = [ 10 , 20 , 30 ]
W = 50
n = len ( val )
print ( knapSack ( W , wt , val , n ) )

Index/key variables: W, i, n, w
Iterator variables: i, w
Python program:
def lps ( str ) :
    n = len ( str )
    L = [ [ 0 for x in range ( n ) ] for x in range ( n ) ]
    for i in range ( n ) :
        L [ i ] [ i ] = 1
    for cl in range ( 2 , n + 1 ) :
        for i in range ( n - cl + 1 ) :
            j = i + cl - 1
            if str [ i ] == str [ j ] and cl == 2 :
                L [ i ] [ j ] = 2
            elif str [ i ] == str [ j ] :
                L [ i ] [ j ] = L [ i + 1 ] [ j - 1 ] + 2
            else :
                L [ i ] [ j ] = max ( L [ i ] [ j - 1 ] , L [ i + 1 ] [ j ] ) ;
    return L [ 0 ] [ n - 1 ]
seq = " GEEKS ▁ FOR ▁ GEEKS "
n = len ( seq )
print ( " The ▁ length ▁ of ▁ the ▁ LPS ▁ is ▁ " + str ( lps ( seq ) ) )

Index/key variables: i, j
Iterator variables: cl, i
Python program:
def maxDivide ( a , b ) :
    while a % b == 0 :
        a = a / b
    return a
def isUgly ( no ) :
    no = maxDivide ( no , 2 )
    no = maxDivide ( no , 3 )
    no = maxDivide ( no , 5 )
    return 1 if no == 1 else 0
def getNthUglyNo ( n ) :
    i = 1
    count = 1
    while n > count :
        i += 1
        if isUgly ( i ) :
            count += 1
    return i
no = getNthUglyNo ( 150 )
print ( "150th ▁ ugly ▁ no . ▁ is ▁ " , no )

Accumulator variables: count, i
Python program:
def printSubStr ( str , low , high ) :
    for i in range ( low , high + 1 ) :
        print ( str [ i ] , end = " " )
def longestPalSubstr ( str ) :
    n = len ( str )
    maxLength = 1
    start = 0
    for i in range ( n ) :
        for j in range ( i , n ) :
            flag = 1
            for k in range ( 0 , ( ( j - i ) // 2 ) + 1 ) :
                if ( str [ i + k ] != str [ j - k ] ) :
                    flag = 0
            if ( flag != 0 and ( j - i + 1 ) > maxLength ) :
                start = i
                maxLength = j - i + 1
    print ( " Longest ▁ palindrome ▁ subString ▁ is : ▁ " , end = " " )
    printSubStr ( str , start , start + maxLength - 1 )
    return maxLength
if __name__ == ' _ _ main _ _ ' :
    str = " forgeeksskeegfor "
    print ( " Length is :   " , longestPalSubstr ( str ) )

Index/key variables: i
Iterator variables: i, j, k
Python program:
def Sum ( freq , i , j ) :
    s = 0
    for k in range ( i , j + 1 ) :
        s += freq [ k ]
    return s
def optCost ( freq , i , j ) :
    if j < i :
        return 0
    if j == i :
        return freq [ i ]
    fsum = Sum ( freq , i , j )
    Min = 999999999999
    for r in range ( i , j + 1 ) :
        cost = ( optCost ( freq , i , r - 1 ) + optCost ( freq , r + 1 , j ) )
        if cost < Min :
            Min = cost
    return Min + fsum
def optimalSearchTree ( keys , freq , n ) :
    return optCost ( freq , 0 , n - 1 )
if __name__ == ' _ _ main _ _ ' :
    keys = [ 10 , 12 , 20 ]
    freq = [ 34 , 8 , 50 ]
    n = len ( keys )
    print ( " Cost ▁ of ▁ Optimal ▁ BST ▁ is " , optimalSearchTree ( keys , freq , n ) )

Index/key variables: i, k
Accumulator variables: s
Iterator variables: k, r
Python program:
INT_MAX = 2147483647
def sum ( freq , i , j ) :
    s = 0
    for k in range ( i , j + 1 ) :
        s += freq [ k ]
    return s
def optimalSearchTree ( keys , freq , n ) :
    cost = [ [ 0 for x in range ( n ) ] for y in range ( n ) ]
    for i in range ( n ) :
        cost [ i ] [ i ] = freq [ i ]
    for L in range ( 2 , n + 1 ) :
        for i in range ( n - L + 2 ) :
            j = i + L - 1
            if i >= n or j >= n :
                break
            cost [ i ] [ j ] = INT_MAX
            for r in range ( i , j + 1 ) :
                c = 0
                if ( r > i ) :
                    c += cost [ i ] [ r - 1 ]
                if ( r < j ) :
                    c += cost [ r + 1 ] [ j ]
                c += sum ( freq , i , j )
                if ( c < cost [ i ] [ j ] ) :
                    cost [ i ] [ j ] = c
    return cost [ 0 ] [ n - 1 ]
if __name__ == ' _ _ main _ _ ' :
    keys = [ 10 , 12 , 20 ]
    freq = [ 34 , 8 , 50 ]
    n = len ( keys )
    print ( " Cost ▁ of ▁ Optimal ▁ BST ▁ is " , optimalSearchTree ( keys , freq , n ) )

Index/key variables: i, j, k
Accumulator variables: c, s
Iterator variables: L, i, k, r
Python program:
def countParenth ( symb , oper , n ) :
    F = [ [ 0 for i in range ( n + 1 ) ] for i in range ( n + 1 ) ]
    T = [ [ 0 for i in range ( n + 1 ) ] for i in range ( n + 1 ) ]
    for i in range ( n ) :
        if symb [ i ] == ' F ' :
            F [ i ] [ i ] = 1
        else :
            F [ i ] [ i ] = 0
        if symb [ i ] == ' T ' :
            T [ i ] [ i ] = 1
        else :
            T [ i ] [ i ] = 0
    for gap in range ( 1 , n ) :
        i = 0
        for j in range ( gap , n ) :
            T [ i ] [ j ] = F [ i ] [ j ] = 0
            for g in range ( gap ) :
                k = i + g
                tik = T [ i ] [ k ] + F [ i ] [ k ]
                tkj = T [ k + 1 ] [ j ] + F [ k + 1 ] [ j ]
                if oper [ k ] == ' & ' :
                    T [ i ] [ j ] += T [ i ] [ k ] * T [ k + 1 ] [ j ]
                    F [ i ] [ j ] += ( tik * tkj - T [ i ] [ k ] * T [ k + 1 ] [ j ] )
                if oper [ k ] == ' | ' :
                    F [ i ] [ j ] += F [ i ] [ k ] * F [ k + 1 ] [ j ]
                    T [ i ] [ j ] += ( tik * tkj - F [ i ] [ k ] * F [ k + 1 ] [ j ] )
                if oper [ k ] == ' ^ ' :
                    T [ i ] [ j ] += ( F [ i ] [ k ] * T [ k + 1 ] [ j ] + T [ i ] [ k ] * F [ k + 1 ] [ j ] )
                    F [ i ] [ j ] += ( T [ i ] [ k ] * T [ k + 1 ] [ j ] + F [ i ] [ k ] * F [ k + 1 ] [ j ] )
            i += 1
    return T [ 0 ] [ n - 1 ]
symbols = " TTFT "
operators = " | & ^ "
n = len ( symbols )
print ( countParenth ( symbols , operators , n ) )

Index/key variables: i, j, k
Iterator variables: g, gap, i, j
Python program:
def getCount ( keypad , n ) :
    if ( keypad == None or n <= 0 ) :
        return 0
    if ( n == 1 ) :
        return 10
    row = [ 0 , 0 , - 1 , 0 , 1 ]
    col = [ 0 , - 1 , 0 , 1 , 0 ]
    count = [ [ 0 ] * ( n + 1 ) ] * 10
    i = 0
    j = 0
    k = 0
    move = 0
    ro = 0
    co = 0
    num = 0
    nextNum = 0
    totalCount = 0
    for i in range ( 10 ) :
        count [ i ] [ 0 ] = 0
        count [ i ] [ 1 ] = 1
    for k in range ( 2 , n + 1 ) :
        for i in range ( 4 ) :
            for j in range ( 3 ) :
                if ( keypad [ i ] [ j ] != ' * ' and keypad [ i ] [ j ] != ' # ' ) :
                    num = ord ( keypad [ i ] [ j ] ) - 48
                    count [ num ] [ k ] = 0
                    for move in range ( 5 ) :
                        ro = i + row [ move ]
                        co = j + col [ move ]
                        if ( ro >= 0 and ro <= 3 and co >= 0 and co <= 2 and keypad [ ro ] [ co ] != ' * ' and keypad [ ro ] [ co ] != ' # ' ) :
                            nextNum = ord ( keypad [ ro ] [ co ] ) - 48
                            count [ num ] [ k ] += count [ nextNum ] [ k - 1 ]
    totalCount = 0
    for i in range ( 10 ) :
        totalCount += count [ i ] [ n ]
    return totalCount
if __name__ == " _ _ main _ _ " :
    keypad = [ [ '1' , '2' , '3' ] , [ '4' , '5' , '6' ] , [ '7' , '8' , '9' ] , [ ' * ' , '0' , ' # ' ] ]
    print ( " Count ▁ for ▁ numbers ▁ of ▁ length " , 1 , " : " , getCount ( keypad , 1 ) )
    print ( " Count ▁ for ▁ numbers ▁ of ▁ length " , 2 , " : " , getCount ( keypad , 2 ) )
    print ( " Count ▁ for ▁ numbers ▁ of ▁ length " , 3 , " : " , getCount ( keypad , 3 ) )
    print ( " Count ▁ for ▁ numbers ▁ of ▁ length " , 4 , " : " , getCount ( keypad , 4 ) )
    print ( " Count ▁ for ▁ numbers ▁ of ▁ length " , 5 , " : " , getCount ( keypad , 5 ) )

Index/key variables: co, i, j, k, move, n, nextNum, num, ro
Accumulator variables: totalCount
Iterator variables: i, j, k, move
Python program:
def getCount ( keypad , n ) :
    if ( not keypad or n <= 0 ) :
        return 0
    if ( n == 1 ) :
        return 10
    odd = [ 0 ] * 10
    even = [ 0 ] * 10
    i = 0
    j = 0
    useOdd = 0
    totalCount = 0
    for i in range ( 10 ) :
        odd [ i ] = 1
    for j in range ( 2 , n + 1 ) :
        useOdd = 1 - useOdd
        if ( useOdd == 1 ) :
            even [ 0 ] = odd [ 0 ] + odd [ 8 ]
            even [ 1 ] = odd [ 1 ] + odd [ 2 ] + odd [ 4 ]
            even [ 2 ] = odd [ 2 ] + odd [ 1 ] + odd [ 3 ] + odd [ 5 ]
            even [ 3 ] = odd [ 3 ] + odd [ 2 ] + odd [ 6 ]
            even [ 4 ] = odd [ 4 ] + odd [ 1 ] + odd [ 5 ] + odd [ 7 ]
            even [ 5 ] = odd [ 5 ] + odd [ 2 ] + odd [ 4 ] + odd [ 8 ] + odd [ 6 ]
            even [ 6 ] = odd [ 6 ] + odd [ 3 ] + odd [ 5 ] + odd [ 9 ]
            even [ 7 ] = odd [ 7 ] + odd [ 4 ] + odd [ 8 ]
            even [ 8 ] = odd [ 8 ] + odd [ 0 ] + odd [ 5 ] + odd [ 7 ] + odd [ 9 ]
            even [ 9 ] = odd [ 9 ] + odd [ 6 ] + odd [ 8 ]
        else :
            odd [ 0 ] = even [ 0 ] + even [ 8 ]
            odd [ 1 ] = even [ 1 ] + even [ 2 ] + even [ 4 ]
            odd [ 2 ] = even [ 2 ] + even [ 1 ] + even [ 3 ] + even [ 5 ]
            odd [ 3 ] = even [ 3 ] + even [ 2 ] + even [ 6 ]
            odd [ 4 ] = even [ 4 ] + even [ 1 ] + even [ 5 ] + even [ 7 ]
            odd [ 5 ] = even [ 5 ] + even [ 2 ] + even [ 4 ] + even [ 8 ] + even [ 6 ]
            odd [ 6 ] = even [ 6 ] + even [ 3 ] + even [ 5 ] + even [ 9 ]
            odd [ 7 ] = even [ 7 ] + even [ 4 ] + even [ 8 ]
            odd [ 8 ] = even [ 8 ] + even [ 0 ] + even [ 5 ] + even [ 7 ] + even [ 9 ]
            odd [ 9 ] = even [ 9 ] + even [ 6 ] + even [ 8 ]
    totalCount = 0
    if ( useOdd == 1 ) :
        for i in range ( 10 ) :
            totalCount += even [ i ]
    else :
        for i in range ( 10 ) :
            totalCount += odd [ i ]
    return totalCount
if __name__ == " _ _ main _ _ " :
    keypad = [ [ '1' , '2' , '3' ] , [ '4' , '5' , '6' ] , [ '7' , '8' , '9' ] , [ ' * ' , '0' , ' # ' ] ]
    print ( " Count ▁ for ▁ numbers ▁ of ▁ length ▁ " , 1 , " : ▁ " , getCount ( keypad , 1 ) )
    print ( " Count ▁ for ▁ numbers ▁ of ▁ length ▁ " , 2 , " : ▁ " , getCount ( keypad , 2 ) )
    print ( " Count ▁ for ▁ numbers ▁ of ▁ length ▁ " , 3 , " : ▁ " , getCount ( keypad , 3 ) )
    print ( " Count ▁ for ▁ numbers ▁ of ▁ length ▁ " , 4 , " : ▁ " , getCount ( keypad , 4 ) )
    print ( " Count ▁ for ▁ numbers ▁ of ▁ length ▁ " , 5 , " : ▁ " , getCount ( keypad , 5 ) )

Index/key variables: i
Accumulator variables: totalCount
Iterator variables: i, j
Python program:
def countRec ( n , sum ) :
    if ( n == 0 ) :
        return ( sum == 0 )
    if ( sum == 0 ) :
        return 1
    ans = 0
    for i in range ( 0 , 10 ) :
        if ( sum - i >= 0 ) :
            ans = ans + countRec ( n - 1 , sum - i )
    return ans
def finalCount ( n , sum ) :
    ans = 0
    for i in range ( 1 , 10 ) :
        if ( sum - i >= 0 ) :
            ans = ans + countRec ( n - 1 , sum - i )
    return ans
n = 2
sum = 5
print ( finalCount ( n , sum ) )

Iterator variables: i
Python program:
lookup = [ [ - 1 for i in range ( 501 ) ] for i in range ( 101 ) ]
def countRec ( n , Sum ) :
    if ( n == 0 ) :
        return Sum == 0
    if ( lookup [ n ] [ Sum ] != - 1 ) :
        return lookup [ n ] [ Sum ]
    ans = 0
    for i in range ( 10 ) :
        if ( Sum - i >= 0 ) :
            ans += countRec ( n - 1 , Sum - i )
    lookup [ n ] [ Sum ] = ans
    return lookup [ n ] [ Sum ]
def finalCount ( n , Sum ) :
    ans = 0
    for i in range ( 1 , 10 ) :
        if ( Sum - i >= 0 ) :
            ans += countRec ( n - 1 , Sum - i )
    return ans
n , Sum = 3 , 5
print ( finalCount ( n , Sum ) )

Index/key variables: Sum, n
Accumulator variables: ans
Iterator variables: i
Python program:
import math
def findCount ( n , sum ) :
    start = math . pow ( 10 , n - 1 ) ;
    end = math . pow ( 10 , n ) - 1 ;
    count = 0 ;
    i = start ;
    while ( i <= end ) :
        cur = 0 ;
        temp = i ;
        while ( temp != 0 ) :
            cur += temp % 10 ;
            temp = temp // 10 ;
        if ( cur == sum ) :
            count = count + 1 ;
            i += 9 ;
        else :
            i = i + 1 ;
    print ( count ) ;
n = 3 ;
sum = 5 ;
findCount ( n , sum ) ;

Accumulator variables: cur, i
Python program:
def getMinSquares ( n ) :
    if n <= 3 :
        return n ;
    res = n
    for x in range ( 1 , n + 1 ) :
        temp = x * x ;
        if temp > n :
            break
        else :
            res = min ( res , 1 + getMinSquares ( n - temp ) )
    return res ;
print ( getMinSquares ( 6 ) )

Iterator variables: x
Python program:
from math import ceil , sqrt
def getMinSquares ( n ) :
    dp = [ 0 , 1 , 2 , 3 ]
    for i in range ( 4 , n + 1 ) :
        dp . append ( i )
        for x in range ( 1 , int ( ceil ( sqrt ( i ) ) ) + 1 ) :
            temp = x * x ;
            if temp > i :
                break
            else :
                dp [ i ] = min ( dp [ i ] , 1 + dp [ i - temp ] )
    return dp [ n ]
print ( getMinSquares ( 6 ) )

Index/key variables: i, n
Accumulator variables: dp
Iterator variables: i, x
Python program:
import sys
def numSquares ( n ) :
    visited = [ 0 ] * ( n + 1 )
    q = [ ]
    ans = sys . maxsize
    q . append ( [ n , 0 ] )
    visited [ n ] = 1
    while ( len ( q ) > 0 ) :
        p = q [ 0 ]
        q . pop ( 0 )
        if ( p [ 0 ] == 0 ) :
            ans = min ( ans , p [ 1 ] )
        i = 1
        while i * i <= p [ 0 ] :
            path = p [ 0 ] - i * i
            if path >= 0 and ( visited [ path ] == 0 or path == 0 ) :
                visited [ path ] = 1
                q . append ( [ path , p [ 1 ] + 1 ] )
            i += 1
    return ans
print ( numSquares ( 12 ) )

Index/key variables: n, path
Accumulator variables: i, q
Python program:
import sys
def minCoins ( coins , m , V ) :
    if ( V == 0 ) :
        return 0
    res = sys . maxsize
    for i in range ( 0 , m ) :
        if ( coins [ i ] <= V ) :
            sub_res = minCoins ( coins , m , V - coins [ i ] )
            if ( sub_res != sys . maxsize and sub_res + 1 < res ) :
                res = sub_res + 1
    return res
coins = [ 9 , 6 , 5 , 1 ]
m = len ( coins )
V = 11
print ( " Minimum ▁ coins ▁ required ▁ is " , minCoins ( coins , m , V ) )

Index/key variables: i
Iterator variables: i
Python program:
import sys
def minCoins ( coins , m , V ) :
    table = [ 0 for i in range ( V + 1 ) ]
    table [ 0 ] = 0
    for i in range ( 1 , V + 1 ) :
        table [ i ] = sys . maxsize
    for i in range ( 1 , V + 1 ) :
        for j in range ( m ) :
            if ( coins [ j ] <= i ) :
                sub_res = table [ i - coins [ j ] ]
                if ( sub_res != sys . maxsize and sub_res + 1 < table [ i ] ) :
                    table [ i ] = sub_res + 1
    if table [ V ] == sys . maxsize :
        return - 1
    return table [ V ]
if __name__ == " _ _ main _ _ " :
    coins = [ 9 , 6 , 5 , 1 ]
    m = len ( coins )
    V = 11
    print ( " Minimum ▁ coins ▁ required ▁ is ▁ " , minCoins ( coins , m , V ) )

Index/key variables: V, i, j
Iterator variables: i, j
Python program:
def superSeq ( X , Y , m , n ) :
    dp = [ [ 0 ] * ( n + 2 ) for i in range ( m + 2 ) ]
    for i in range ( m + 1 ) :
        for j in range ( n + 1 ) :
            if ( not i ) :
                dp [ i ] [ j ] = j
            elif ( not j ) :
                dp [ i ] [ j ] = i
            elif ( X [ i - 1 ] == Y [ j - 1 ] ) :
                dp [ i ] [ j ] = 1 + dp [ i - 1 ] [ j - 1 ]
            else :
                dp [ i ] [ j ] = 1 + min ( dp [ i - 1 ] [ j ] , dp [ i ] [ j - 1 ] )
    return dp [ m ] [ n ]
X = " AGGTAB "
Y = " GXTXAYB "
print ( " Length ▁ of ▁ the ▁ shortest ▁ supersequence ▁ is ▁ % d " % superSeq ( X , Y , len ( X ) , len ( Y ) ) )

Index/key variables: i, j, m, n
Iterator variables: i, j
Python program:
def sumOfDigitsFrom1ToN ( n ) :
    result = 0
    for x in range ( 1 , n + 1 ) :
        result = result + sumOfDigits ( x )
    return result
def sumOfDigits ( x ) :
    sum = 0
    while ( x != 0 ) :
        sum = sum + x % 10
        x = x // 10
    return sum
n = 328
print ( " Sum ▁ of ▁ digits ▁ in ▁ numbers ▁ from ▁ 1 ▁ to " , n , " is " , sumOfDigitsFrom1ToN ( n ) )

Iterator variables: x