dhyuti-n/xlcost-variable-roles huggingface dataset:
Python program:
def findWaitingTime ( processes , n , wt ) :
    rt = [ 0 ] * n
    for i in range ( n ) :
        rt [ i ] = processes [ i ] [ 1 ]
    complete = 0
    t = 0
    minm = 999999999
    short = 0
    check = False
    while ( complete != n ) :
        for j in range ( n ) :
            if ( ( processes [ j ] [ 2 ] <= t ) and ( rt [ j ] < minm ) and rt [ j ] > 0 ) :
                minm = rt [ j ]
                short = j
                check = True
        if ( check == False ) :
            t += 1
            continue
        rt [ short ] -= 1
        minm = rt [ short ]
        if ( minm == 0 ) :
            minm = 999999999
        if ( rt [ short ] == 0 ) :
            complete += 1
            check = False
            fint = t + 1
            wt [ short ] = ( fint - proc [ short ] [ 1 ] - proc [ short ] [ 2 ] )
            if ( wt [ short ] < 0 ) :
                wt [ short ] = 0
        t += 1
def findTurnAroundTime ( processes , n , wt , tat ) :
    for i in range ( n ) :
        tat [ i ] = processes [ i ] [ 1 ] + wt [ i ]
def findavgTime ( processes , n ) :
    wt = [ 0 ] * n
    tat = [ 0 ] * n
    findWaitingTime ( processes , n , wt )
    findTurnAroundTime ( processes , n , wt , tat )
    print ( " Processes ▁ Burst ▁ Time ▁ Waiting " , " Time ▁ Turn - Around ▁ Time " )
    total_wt = 0
    total_tat = 0
    for i in range ( n ) :
        total_wt = total_wt + wt [ i ]
        total_tat = total_tat + tat [ i ]
        print ( " ▁ " , processes [ i ] [ 0 ] , " TABSYMBOL TABSYMBOL " , processes [ i ] [ 1 ] , " TABSYMBOL TABSYMBOL " , wt [ i ] , " TABSYMBOL TABSYMBOL " , tat [ i ] )
    print ( " Average waiting time = % .5 f   " % ( total_wt / n ) )
    print ( " Average ▁ turn ▁ around ▁ time ▁ = ▁ " , total_tat / n )
if __name__ == " _ _ main _ _ " :
    proc = [ [ 1 , 6 , 1 ] , [ 2 , 8 , 1 ] , [ 3 , 7 , 2 ] , [ 4 , 3 , 3 ] ]
    n = 4
    findavgTime ( proc , n )

Index/key variables: i, j, short
Accumulator variables: complete, t
Iterator variables: i, j
Boolean variables: check
Python program:
def findNumberOfStrings ( N ) :
    counts = [ ]
    for i in range ( 5 ) :
        counts . append ( 1 )
    for i in range ( 2 , N + 1 ) :
        for j in range ( 3 , - 1 , - 1 ) :
            counts [ j ] += counts [ j + 1 ]
    ans = 0
    for c in counts :
        ans += c
    return ans
N = 2
print ( findNumberOfStrings ( N ) )

Index/key variables: j
Accumulator variables: ans, counts
Iterator variables: c, i, j
Python program:
dp = [ [ - 1 for x in range ( 9000 + 5 ) ] for y in range ( 1000 + 5 ) ]
mod = 1000000007
def check ( sum , x , y ) :
    while ( sum > 0 ) :
        ln = sum % 10
        if ( ln != x and ln != y ) :
            return 0
        sum //= 10
    return 1
def countNumbers ( n , x , y , sum ) :
    global dp
    if ( n == 0 ) :
        return check ( sum , x , y )
    if ( dp [ n ] [ sum ] != - 1 ) :
        return dp [ n ] [ sum ] % mod
    option1 = countNumbers ( n - 1 , x , y , sum + x ) % mod
    option2 = countNumbers ( n - 1 , x , y , sum + y ) % mod
    dp [ n ] [ sum ] = ( option1 + option2 ) % mod
    return dp [ n ] [ sum ]
if __name__ == " _ _ main _ _ " :
    N = 3
    X = 1
    Y = 5
    print ( countNumbers ( N , X , Y , 0 ) % mod )

Index/key variables: n, sum
Python program:
def solve ( a , s , n , prev , mask , dp ) :
    if ( mask == 0 ) :
        return 0 ;
    if ( dp [ mask ] [ prev + 1 ] != - 1 ) :
        return dp [ mask ] [ prev + 1 ] ;
    ans = 10000 ;
    for i in range ( len ( s ) ) :
        id = ord ( s [ i ] ) - ord ( ' a ' ) ;
        if ( check ( mask , id ) ) :
            if ( prev == - 1 ) :
                ans = min ( ans , solve ( a , s , n , id , mask ^ ( 1 << id ) , dp ) ) ;
            else :
                ans = min ( ans , a [ prev ] [ id ] + solve ( a , s , n , id , mask ^ ( 1 << id ) , dp ) ) ;
    dp [ mask ] [ prev + 1 ] = ans ;
    return ans ;
def check ( mask , i ) :
    c = ( mask & ( 1 << i ) ) ;
    return c != 0 ;
def generatePermutation ( mask , n , a , s ) :
    dp = [ [ - 1 for i in range ( n + 5 ) ] for j in range ( ( 1 << n ) + 5 ) ]
    for i in range ( len ( s ) ) :
        id = ord ( s [ i ] ) - ord ( ' a ' ) ;
        mask |= ( 1 << id ) ;
    print ( solve ( a , s , n , - 1 , mask , dp ) ) ;
if __name__ == ' _ _ main _ _ ' :
    N = 5 ;
    str = " abcde " ;
    mat = [ [ 0 , 5 , 1 , 5 , 3 ] , [ 4 , 0 , 9 , 4 , 2 ] , [ 7 , 9 , 0 , 10 , 7 ] , [ 1 , 2 , 8 , 0 , 2 ] , [ 3 , 9 , 7 , 7 , 0 ] ] ;
    generatePermutation ( 0 , N , mat , str ) ;

Index/key variables: i, id, mask, prev
Iterator variables: i
Python program:
inf = 10000000
def minCost ( a , i , j , k , prefix , dp ) :
    if ( i >= j ) :
        return 0
    if ( dp [ i ] [ j ] != - 1 ) :
        return dp [ i ] [ j ]
    best_cost = inf
    for pos in range ( i , j ) :
        left = minCost ( a , i , pos , k , prefix , dp )
        right = minCost ( a , pos + 1 , j , k , prefix , dp )
        best_cost = min ( best_cost , left + right + ( k * Combine ( prefix , i , j ) ) )
    dp [ i ] [ j ] = best_cost
    return dp [ i ] [ j ]
def preprocess ( a , n ) :
    p = [ 0 ] * n
    p [ 0 ] = a [ 0 ]
    for i in range ( 1 , n ) :
        p [ i ] = p [ i - 1 ] + a [ i ]
    return p
def Combine ( p , i , j ) :
    if ( i == 0 ) :
        return p [ j ]
    else :
        return p [ j ] - p [ i - 1 ]
if __name__ == " _ _ main _ _ " :
    n = 4
    a = [ 4 , 5 , 6 , 7 ]
    k = 3
    dp = [ [ - 1 for x in range ( n + 1 ) ] for y in range ( n + 1 ) ]
    prefix = preprocess ( a , n )
    print ( minCost ( a , 0 , n - 1 , k , prefix , dp ) )

Index/key variables: i, j
Iterator variables: i, pos
Python program:
def ReverseBitonic ( arr ) :
    N = len ( arr )
    lds = [ 1 for i in range ( N + 1 ) ]
    for i in range ( 1 , N ) :
        for j in range ( 0 , i ) :
            if ( ( arr [ i ] < arr [ j ] ) and ( lds [ i ] < lds [ j ] + 1 ) ) :
                lds [ i ] = lds [ j ] + 1
    lis = [ 1 for i in range ( N + 1 ) ]
    for i in reversed ( range ( N - 1 ) ) :
        for j in reversed ( range ( i - 1 , N ) ) :
            if ( arr [ i ] < arr [ j ] and lis [ i ] < lis [ j ] + 1 ) :
                lis [ i ] = lis [ j ] + 1
    maximum = lis [ 0 ] + lds [ 0 ] - 1
    for i in range ( 1 , N ) :
        maximum = max ( ( lis [ i ] + lds [ i ] - 1 ) , maximum )
    return maximum
arr = [ 0 , 8 , 4 , 12 , 2 , 10 , 6 , 14 , 1 , 9 , 5 , 13 , 3 , 11 , 7 , 15 ]
print ( " Length ▁ of ▁ LBS ▁ is " , ReverseBitonic ( arr ) )

Index/key variables: i, j
Iterator variables: i, j
Python program:
def findPath ( maze , xpos , ypos , key ) :
    if xpos < 0 or xpos >= len ( maze ) or ypos < 0 or ypos >= len ( maze ) :
        return False
    if maze [ xpos ] [ ypos ] == '1' :
        if key == True :
            if xpos == len ( maze ) - 1 and ypos == len ( maze ) - 1 :
                return True
            return findPath ( maze , xpos + 1 , ypos , False ) or findPath ( maze , xpos , ypos + 1 , False )
        return False
    if xpos == len ( maze ) - 1 and ypos == len ( maze ) - 1 :
        return True
    return findPath ( maze , xpos + 1 , ypos , key ) or findPath ( maze , xpos , ypos + 1 , key )
def mazeProb ( maze , xpos , ypos ) :
    key = True
    if findPath ( maze , xpos , ypos , key ) :
        return True
    return False
if __name__ == " _ _ main _ _ " :
    maze = [ [ '0' , '0' , '1' ] , [ '1' , '0' , '1' ] , [ '1' , '1' , '0' ] ]
    n = len ( maze )
    if mazeProb ( maze , 0 , 0 ) :
        print ( " Yes " )
    else :
        print ( " No " )

Index/key variables: xpos, ypos
Boolean variables: key
Python program:
import sys
def FindMinimumCost ( ind , a , n , k , dp ) :
    if ( ind == ( n - 1 ) ) :
        return 0
    elif ( dp [ ind ] != - 1 ) :
        return dp [ ind ]
    else :
        ans = sys . maxsize
        for i in range ( 1 , k + 1 ) :
            if ( ind + i < n ) :
                ans = min ( ans , abs ( a [ ind + i ] - a [ ind ] ) + FindMinimumCost ( ind + i , a , n , k , dp ) )
            else :
                break
        dp [ ind ] = ans
        return ans
if __name__ == ' _ _ main _ _ ' :
    a = [ 10 , 30 , 40 , 50 , 20 ]
    k = 3
    n = len ( a )
    dp = [ - 1 for i in range ( n ) ]
    print ( FindMinimumCost ( 0 , a , n , k , dp ) )

Index/key variables: ind
Iterator variables: i
Python program:
def sieve_modified ( ) :
    for i in range ( 2 , MAX , 2 ) :
        for j in range ( i , MAX , i ) :
            prefix [ j ] += i
    for i in range ( 1 , MAX ) :
        prefix [ i ] += prefix [ i - 1 ]
def sumEvenFactors ( L , R ) :
    return ( prefix [ R ] - prefix [ L - 1 ] )
if __name__ == " _ _ main _ _ " :
    MAX = 100000
    prefix = [ 0 ] * MAX
    sieve_modified ( )
    l , r = 6 , 10
    print ( sumEvenFactors ( l , r ) )

Index/key variables: R, i, j
Iterator variables: i, j
Python program:
def count ( pos , Sum , rem , tight , nonz , num ) :
    if pos == len ( num ) :
        if rem == 0 and Sum == n :
            return 1
        return 0
    if dp [ pos ] [ Sum ] [ rem ] [ tight ] != - 1 :
        return dp [ pos ] [ Sum ] [ rem ] [ tight ]
    ans = 0
    if tight :
        limit = 9
    else :
        limit = num [ pos ]
    for d in range ( 0 , limit + 1 ) :
        if d == 0 and nonz :
            continue
        currSum = Sum + d
        currRem = ( rem * 10 + d ) % m
        currF = int ( tight or ( d < num [ pos ] ) )
        ans += count ( pos + 1 , currSum , currRem , currF , nonz or d , num )
    dp [ pos ] [ Sum ] [ rem ] [ tight ] = ans
    return ans
def solve ( x ) :
    num = [ ]
    global dp
    while x > 0 :
        num . append ( x % 10 )
        x //= 10
    num . reverse ( )
    dp = [ [ [ [ - 1 , - 1 ] for i in range ( M ) ] for j in range ( 165 ) ] for k in range ( M ) ]
    return count ( 0 , 0 , 0 , 0 , 0 , num )
if __name__ == " _ _ main _ _ " :
    L , R = 1 , 100
    n , m , M = 8 , 2 , 20
    dp = [ ]
    print ( solve ( R ) - solve ( L ) )

Index/key variables: Sum, pos, rem, tight
Accumulator variables: ans, num, x
Iterator variables: d
Python program:
def countInRangeUtil ( pos , cnt , tight , num ) :
    if pos == len ( num ) :
        if cnt <= K :
            return 1
        return 0
    if dp [ pos ] [ cnt ] [ tight ] != - 1 :
        return dp [ pos ] [ cnt ] [ tight ]
    ans = 0
    limit = 9 if tight else num [ pos ]
    for dig in range ( limit + 1 ) :
        currCnt = cnt
        if dig != 0 :
            currCnt += 1
        currTight = tight
        if dig < num [ pos ] :
            currTight = 1
        ans += countInRangeUtil ( pos + 1 , currCnt , currTight , num )
    dp [ pos ] [ cnt ] [ tight ] = ans
    return dp [ pos ] [ cnt ] [ tight ]
def countInRange ( x ) :
    global dp , K , M
    num = [ ]
    while x :
        num . append ( x % 10 )
        x //= 10
    num . reverse ( )
    dp = [ [ [ - 1 , - 1 ] for i in range ( M ) ] for j in range ( M ) ]
    return countInRangeUtil ( 0 , 0 , 0 , num )
if __name__ == " _ _ main _ _ " :
    dp = [ ]
    M = 20
    K = 0
    L = 1
    R = 1000
    K = 3
    print ( countInRange ( R ) - countInRange ( L - 1 ) )
    L = 9995
    R = 10005
    K = 2
    print ( countInRange ( R ) - countInRange ( L - 1 ) )

Index/key variables: cnt, pos, tight
Accumulator variables: ans, currCnt, num, x
Iterator variables: dig
Python program:
N = 1000 ;
dp = [ [ - 1 for x in range ( N ) ] for y in range ( N ) ] ;
adj = [ 1 , 0 , 0 , 0 ] ;
def find ( index , openbrk , n ) :
    if ( openbrk < 0 ) :
        return 0 ;
    if ( index == n ) :
        if ( openbrk == 0 ) :
            return 1 ;
        else :
            return 0 ;
    if ( dp [ index ] [ openbrk ] != - 1 ) :
        return dp [ index ] [ openbrk ] ;
    if ( adj [ index ] == 1 ) :
        dp [ index ] [ openbrk ] = find ( index + 1 , openbrk + 1 , n ) ;
    else :
        dp [ index ] [ openbrk ] = ( find ( index + 1 , openbrk + 1 , n ) + find ( index + 1 , openbrk - 1 , n ) ) ;
    return dp [ index ] [ openbrk ] ;
n = 2 ;
print ( find ( 0 , 0 , 2 * n ) ) ;

Index/key variables: index, openbrk
Python program:
ans = 0 ;
def findMaxValUtil ( arr , n , num , maxLimit , ind ) :
    global ans
    if ( ind == n ) :
        ans = max ( ans , num )
        return
    if ( num - arr [ ind ] >= 0 ) :
        findMaxValUtil ( arr , n , num - arr [ ind ] , maxLimit , ind + 1 )
    if ( num + arr [ ind ] <= maxLimit ) :
        findMaxValUtil ( arr , n , num + arr [ ind ] , maxLimit , ind + 1 )
def findMaxVal ( arr , n , num , maxLimit ) :
    global ans
    ind = 0
    findMaxValUtil ( arr , n , num , maxLimit , ind )
    return ans
num = 1
arr = [ 3 , 10 , 6 , 4 , 5 ]
n = len ( arr )
maxLimit = 15
print ( findMaxVal ( arr , n , num , maxLimit ) )

Index/key variables: ind
Python program:
def printSets ( set1 , set2 ) :
    for i in range ( 0 , len ( set1 ) ) :
        print ( " { } ▁ " . format ( set1 [ i ] ) , end = " " ) ;
    print ( " " )
    for i in range ( 0 , len ( set2 ) ) :
        print ( " { } ▁ " . format ( set2 [ i ] ) , end = " " ) ;
    print ( " " )
def findSets ( arr , n , set1 , set2 , sum1 , sum2 , pos ) :
    if ( pos == n ) :
        if ( sum1 == sum2 ) :
            printSets ( set1 , set2 )
            return True
        else :
            return False
    set1 . append ( arr [ pos ] )
    res = findSets ( arr , n , set1 , set2 , sum1 + arr [ pos ] , sum2 , pos + 1 )
    if ( res ) :
        return res
    set1 . pop ( )
    set2 . append ( arr [ pos ] )
    res = findSets ( arr , n , set1 , set2 , sum1 , sum2 + arr [ pos ] , pos + 1 )
    if not res :
        set2 . pop ( )
    return res
def isPartitionPoss ( arr , n ) :
    sum = 0
    for i in range ( 0 , n ) :
        sum += arr [ i ]
    if ( sum % 2 != 0 ) :
        return False
    set1 = [ ]
    set2 = [ ]
    return findSets ( arr , n , set1 , set2 , 0 , 0 , 0 )
arr = [ 5 , 5 , 1 , 11 ]
n = len ( arr )
if ( isPartitionPoss ( arr , n ) == False ) :
    print ( " - 1" )

Index/key variables: i, pos
Accumulator variables: sum
Iterator variables: i
Python program:
import math
def maximumSumSubarray ( arr , n ) :
    min_prefix_sum = 0
    res = - math . inf
    prefix_sum = [ ]
    prefix_sum . append ( arr [ 0 ] )
    for i in range ( 1 , n ) :
        prefix_sum . append ( prefix_sum [ i - 1 ] + arr [ i ] )
    for i in range ( n ) :
        res = max ( res , prefix_sum [ i ] - min_prefix_sum )
        min_prefix_sum = min ( min_prefix_sum , prefix_sum [ i ] )
    return res
arr1 = [ - 2 , - 3 , 4 , - 1 , - 2 , 1 , 5 , - 3 ]
n1 = len ( arr1 )
print ( maximumSumSubarray ( arr1 , n1 ) )
arr2 = [ 4 , - 8 , 9 , - 4 , 1 , - 8 , - 1 , 6 ]
n2 = len ( arr2 )
print ( maximumSumSubarray ( arr2 , n2 ) )

Index/key variables: i
Accumulator variables: prefix_sum
Iterator variables: i
Python program:
DP_s = 9
def getNumMonotone ( ln ) :
    DP = [ [ 0 ] * DP_s for i in range ( ln ) ]
    for i in range ( DP_s ) :
        DP [ 0 ] [ i ] = i + 1
    for i in range ( ln ) :
        DP [ i ] [ 0 ] = 1
    for i in range ( 1 , ln ) :
        for j in range ( 1 , DP_s ) :
            DP [ i ] [ j ] = DP [ i - 1 ] [ j ] + DP [ i ] [ j - 1 ]
    return DP [ ln - 1 ] [ DP_s - 1 ]
print ( getNumMonotone ( 10 ) )

Index/key variables: i, j
Iterator variables: i, j
Python program:
def largestSubset ( a , n ) :
    dp = [ 0 for i in range ( n ) ]
    dp [ n - 1 ] = 1 ;
    for i in range ( n - 2 , - 1 , - 1 ) :
        mxm = 0 ;
        for j in range ( i + 1 , n ) :
            if a [ j ] % a [ i ] == 0 or a [ i ] % a [ j ] == 0 :
                mxm = max ( mxm , dp [ j ] )
        dp [ i ] = 1 + mxm
    return max ( dp )
a = [ 1 , 3 , 6 , 13 , 17 , 18 ]
n = len ( a )
print ( largestSubset ( a , n ) )

Index/key variables: i, j
Iterator variables: i, j
Python program:
def countFriendsPairings ( n ) :
    dp = [ 0 for i in range ( n + 1 ) ]
    for i in range ( n + 1 ) :
        if ( i <= 2 ) :
            dp [ i ] = i
        else :
            dp [ i ] = dp [ i - 1 ] + ( i - 1 ) * dp [ i - 2 ]
    return dp [ n ]
n = 4
print ( countFriendsPairings ( n ) )

Index/key variables: i, n
Iterator variables: i
Python program:
def countFriendsPairings ( n ) :
    a , b , c = 1 , 2 , 0 ;
    if ( n <= 2 ) :
        return n ;
    for i in range ( 3 , n + 1 ) :
        c = b + ( i - 1 ) * a ;
        a = b ;
        b = c ;
    return c ;
n = 4 ;
print ( countFriendsPairings ( n ) ) ;

Iterator variables: i
Python program:
R , C = 5 , 3
def precompute ( mat , ryt , dwn ) :
    for j in range ( C - 1 , - 1 , - 1 ) :
        for i in range ( 0 , R ) :
            if mat [ i ] [ j ] == 0 :
                ryt [ i ] [ j ] = 0
            else :
                ryt [ i ] [ j ] = ryt [ i ] [ j + 1 ] + 1
    for i in range ( R - 1 , - 1 , - 1 ) :
        for j in range ( 0 , C ) :
            if mat [ i ] [ j ] == 0 :
                dwn [ i ] [ j ] = 0
            else :
                dwn [ i ] [ j ] = dwn [ i + 1 ] [ j ] + 1
def solveRowSwap ( ryt ) :
    b = [ 0 ] * R
    ans = 0
    for j in range ( 0 , C ) :
        for i in range ( 0 , R ) :
            b [ i ] = ryt [ i ] [ j ]
        b . sort ( )
        for i in range ( 0 , R ) :
            ans = max ( ans , b [ i ] * ( R - i ) )
    return ans
def solveColumnSwap ( dwn ) :
    b = [ 0 ] * C
    ans = 0
    for i in range ( 0 , R ) :
        for j in range ( 0 , C ) :
            b [ j ] = dwn [ i ] [ j ]
        b . sort ( )
        for i in range ( 0 , C ) :
            ans = max ( ans , b [ i ] * ( C - i ) )
    return ans
def findMax1s ( mat ) :
    ryt = [ [ 0 for i in range ( C + 2 ) ] for j in range ( R + 2 ) ]
    dwn = [ [ 0 for i in range ( C + 2 ) ] for j in range ( R + 2 ) ]
    precompute ( mat , ryt , dwn )
    rswap = solveRowSwap ( ryt )
    cswap = solveColumnSwap ( dwn )
    if rswap > cswap : print ( " Row Swap " , rswap )
    else : print ( " Column Swap " , cswap )
if __name__ == " _ _ main _ _ " :
    mat = [ [ 0 , 0 , 0 ] , [ 1 , 1 , 0 ] , [ 1 , 1 , 0 ] , [ 0 , 0 , 0 ] , [ 1 , 1 , 0 ] ]
    findMax1s ( mat )

Index/key variables: i, j
Iterator variables: i, j
Python program:
def lcsOf3 ( X , Y , Z , m , n , o ) :
    L = [ [ [ 0 for i in range ( o + 1 ) ] for j in range ( n + 1 ) ] for k in range ( m + 1 ) ]
    for i in range ( m + 1 ) :
        for j in range ( n + 1 ) :
            for k in range ( o + 1 ) :
                if ( i == 0 or j == 0 or k == 0 ) :
                    L [ i ] [ j ] [ k ] = 0
                elif ( X [ i - 1 ] == Y [ j - 1 ] and X [ i - 1 ] == Z [ k - 1 ] ) :
                    L [ i ] [ j ] [ k ] = L [ i - 1 ] [ j - 1 ] [ k - 1 ] + 1
                else :
                    L [ i ] [ j ] [ k ] = max ( max ( L [ i - 1 ] [ j ] [ k ] , L [ i ] [ j - 1 ] [ k ] ) , L [ i ] [ j ] [ k - 1 ] )
    return L [ m ] [ n ] [ o ]
X = ' AGGT12'
Y = '12TXAYB '
Z = '12XBA '
m = len ( X )
n = len ( Y )
o = len ( Z )
print ( ' Length ▁ of ▁ LCS ▁ is ' , lcsOf3 ( X , Y , Z , m , n , o ) )

Index/key variables: i, j, k, m, n, o
Iterator variables: i, j, k
Python program:
def longestSubsequence ( A , N ) :
    L = [ 1 ] * N
    hm = { }
    for i in range ( 1 , N ) :
        if abs ( A [ i ] - A [ i - 1 ] ) == 1 :
            L [ i ] = 1 + L [ i - 1 ]
        elif hm . get ( A [ i ] + 1 , 0 ) or hm . get ( A [ i ] - 1 , 0 ) :
            L [ i ] = 1 + max ( hm . get ( A [ i ] + 1 , 0 ) , hm . get ( A [ i ] - 1 , 0 ) )
        hm [ A [ i ] ] = L [ i ]
    return max ( L )
A = [ 1 , 2 , 3 , 4 , 5 , 3 , 2 ]
N = len ( A )
print ( longestSubsequence ( A , N ) )

Index/key variables: i
Iterator variables: i
Python program:
def printShortestSuperSeq ( x , y ) :
    m = len ( x )
    n = len ( y )
    dp = [ [ 0 for i in range ( n + 1 ) ] for j in range ( n + 1 ) ]
    for i in range ( m + 1 ) :
        for j in range ( n + 1 ) :
            if i == 0 :
                dp [ i ] [ j ] = j
            elif j == 0 :
                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 ] )
    string = " "
    i = m
    j = n
    while i > 0 and j > 0 :
        if x [ i - 1 ] == y [ j - 1 ] :
            string += x [ i - 1 ]
            i -= 1
            j -= 1
        elif dp [ i - 1 ] [ j ] > dp [ i ] [ j - 1 ] :
            string += y [ j - 1 ]
            j -= 1
        else :
            string += x [ i - 1 ]
            i -= 1
    while i > 0 :
        string += x [ i - 1 ]
        i -= 1
    while j > 0 :
        string += y [ j - 1 ]
        j -= 1
    string = list ( string )
    string . reverse ( )
    return ' ' . join ( string )
if __name__ == " _ _ main _ _ " :
    x = " AGGTAB "
    y = " GXTXAYB "
    print ( printShortestSuperSeq ( x , y ) )

Index/key variables: i, j
Accumulator variables: string
Iterator variables: i, j
Python program:
def findLongestRepeatingSubSeq ( str ) :
    n = len ( str )
    dp = [ [ 0 for i in range ( n + 1 ) ] for j in range ( n + 1 ) ]
    for i in range ( 1 , n + 1 ) :
        for j in range ( 1 , n + 1 ) :
            if ( str [ i - 1 ] == str [ j - 1 ] and i != j ) :
                dp [ i ] [ j ] = 1 + dp [ i - 1 ] [ j - 1 ]
            else :
                dp [ i ] [ j ] = max ( dp [ i ] [ j - 1 ] , dp [ i - 1 ] [ j ] )
    return dp [ n ] [ n ]
if __name__ == ' _ _ main _ _ ' :
    str = " aabb "
    print ( " The ▁ length ▁ of ▁ the ▁ largest ▁ subsequence ▁ that ▁ repeats ▁ itself ▁ is ▁ : ▁ " , findLongestRepeatingSubSeq ( str ) )

Index/key variables: i, j, n
Iterator variables: i, j
Python program:
def countRec ( digits , esum , osum , isOdd , n ) :
    if digits == n :
        return ( esum - osum == 1 )
    if lookup [ digits ] [ esum ] [ osum ] [ isOdd ] != - 1 :
        return lookup [ digits ] [ esum ] [ osum ] [ isOdd ]
    ans = 0
    if isOdd :
        for i in range ( 10 ) :
            ans += countRec ( digits + 1 , esum , osum + i , False , n )
    else :
        for i in range ( 10 ) :
            ans += countRec ( digits + 1 , esum + i , osum , True , n )
    lookup [ digits ] [ esum ] [ osum ] [ isOdd ] = ans
    return ans
def finalCount ( n ) :
    global lookup
    digits = 0
    lookup = [ [ [ [ - 1 , - 1 ] for i in range ( 500 ) ] for j in range ( 500 ) ] for k in range ( 50 ) ]
    ans = 0
    esum = 0
    osum = 0
    for i in range ( 1 , 10 ) :
        ans += countRec ( digits + 1 , esum + i , osum , True , n )
    return ans
if __name__ == " _ _ main _ _ " :
    lookup = [ ]
    n = 3
    print ( " Count ▁ of ▁ % d ▁ digit ▁ numbers ▁ is ▁ % d " % ( n , finalCount ( n ) ) )

Index/key variables: digits, esum, isOdd, osum
Accumulator variables: ans
Iterator variables: i
Python program:
def numberOfPaths ( p , q ) :
    dp = [ 1 for i in range ( q ) ]
    for i in range ( p - 1 ) :
        for j in range ( 1 , q ) :
            dp [ j ] += dp [ j - 1 ]
    return dp [ q - 1 ]
print ( numberOfPaths ( 3 , 3 ) )

Index/key variables: j
Iterator variables: i, j
Python program:
class Solution :
    def Solve ( self , A ) :
        ans = 2
        n = len ( A )
        if n <= 2 :
            return n
        llap = [ 2 ] * n
        A . sort ( )
        for j in range ( n - 2 , - 1 , - 1 ) :
            i = j - 1
            k = j + 1
            while ( i >= 0 and k < n ) :
                if A [ i ] + A [ k ] == 2 * A [ j ] :
                    llap [ j ] = max ( llap [ k ] + 1 , llap [ j ] )
                    ans = max ( ans , llap [ j ] )
                    i -= 1
                    k += 1
                elif A [ i ] + A [ k ] < 2 * A [ j ] :
                    k += 1
                else :
                    i -= 1
        return ans
obj = Solution ( )
a = [ 9 , 4 , 7 , 2 , 10 ]
print ( obj . Solve ( a ) )

Index/key variables: i, j, k
Iterator variables: j
Class/struct names: Solution
Python program:
def Substr ( Str , target ) :
    t = 0
    Len = len ( Str )
    i = 0
    for i in range ( Len ) :
        if ( t == len ( target ) ) :
            break
        if ( Str [ i ] == target [ t ] ) :
            t += 1
        else :
            t = 0
    if ( t < len ( target ) ) :
        return - 1
    else :
        return ( i - t )
print ( Substr ( " GeeksForGeeks " , " Fr " ) )
print ( Substr ( " GeeksForGeeks " , " For " ) )

Index/key variables: i, t
Iterator variables: i