dhyuti-n/xlcost-variable-roles huggingface dataset:
Python program:
def memo ( index , evenSum , oddSum , tight ) :
    if index == len ( v ) :
        if evenSum > oddSum :
            return 1
        else :
            return 0
    if dp [ index ] [ evenSum ] [ oddSum ] [ tight ] != - 1 :
        return dp [ index ] [ evenSum ] [ oddSum ] [ tight ]
    limit = v [ index ] if tight else 9
    ans = 0
    for d in range ( limit + 1 ) :
        currTight = 0
        if d == v [ index ] :
            currTight = tight
        if d % 2 != 0 :
            ans += memo ( index + 1 , evenSum , oddSum + d , currTight )
        else :
            ans += memo ( index + 1 , evenSum + d , oddSum , currTight )
    dp [ index ] [ evenSum ] [ oddSum ] [ tight ] = ans
    return ans
def countNum ( n ) :
    global dp , v
    v . clear ( )
    num = [ ]
    while n :
        v . append ( n % 10 )
        n //= 10
    v . reverse ( )
    dp = [ [ [ [ - 1 , - 1 ] for i in range ( 180 ) ] for j in range ( 180 ) ] for k in range ( 18 ) ]
    return memo ( 0 , 0 , 0 , 1 )
if __name__ == " _ _ main _ _ " :
    dp = [ ]
    v = [ ]
    L = 2
    R = 10
    print ( countNum ( R ) - countNum ( L - 1 ) )

Index/key variables: evenSum, index, oddSum, tight
Accumulator variables: ans, n, v
Iterator variables: d
Python program:
def minCount ( K ) :
    memo = [ 10 ** 9 for i in range ( 100 ) ]
    memo [ 0 ] = 0
    for i in range ( 1 , 100 ) :
        memo [ i ] = min ( memo [ i - 1 ] + 1 , memo [ i ] )
    for i in range ( 10 , 100 ) :
        memo [ i ] = min ( memo [ i - 10 ] + 1 , memo [ i ] )
    for i in range ( 25 , 100 ) :
        memo [ i ] = min ( memo [ i - 25 ] + 1 , memo [ i ] )
    min_count = 0
    while ( K > 0 ) :
        min_count += memo [ K % 100 ]
        K //= 100
    return min_count
K = 69
print ( minCount ( K ) )

Index/key variables: i
Accumulator variables: K, min_count
Iterator variables: i
Python program:
import numpy as np ;
N = 10000 ;
def count_required_sequence ( n , arr ) :
    total_required_subsequence = 0 ;
    total_n_required_subsequence = 0 ;
    dp = np . zeros ( ( N , 2 ) ) ;
    for i in range ( n ) :
        turn_required = 0 ;
        for j in range ( - 1 , 2 , 1 ) :
            turn_required += dp [ arr [ i ] + j ] [ 0 ] ;
        required_end_i = ( total_required_subsequence + turn_required ) ;
        n_required_end_i = ( 1 + total_n_required_subsequence - turn_required ) ;
        total_required_subsequence += required_end_i ;
        total_n_required_subsequence += n_required_end_i ;
        dp [ arr [ i ] ] [ 1 ] += required_end_i ;
        dp [ arr [ i ] ] [ 0 ] += n_required_end_i ;
    return total_required_subsequence ;
if __name__ == " _ _ main _ _ " :
    arr = [ 1 , 6 , 2 , 1 , 9 ] ;
    n = len ( arr ) ;
    print ( count_required_sequence ( n , arr ) ) ;

Index/key variables: i
Accumulator variables: total_n_required_subsequence, total_required_subsequence, turn_required
Iterator variables: i, j
Python program:
import numpy as np
MAX_INDEX = 51
MAX_SUM = 2505
dp = np . ones ( ( MAX_INDEX , MAX_SUM , MAX_INDEX ) ) * - 1 ;
def waysutil ( index , sum , count , arr , K ) :
    if ( index < 0 ) :
        return 0 ;
    if ( index == 0 ) :
        if ( count == 0 ) :
            return 0 ;
        remainder = sum % count ;
        if ( remainder != 0 ) :
            return 0 ;
        average = sum // count ;
        if ( average == K ) :
            return 1 ;
    if ( dp [ index ] [ sum ] [ count ] != - 1 ) :
        return dp [ index ] [ sum ] [ count ] ;
    dontpick = waysutil ( index - 1 , sum , count , arr , K ) ;
    pick = waysutil ( index - 1 , sum + arr [ index ] , count + 1 , arr , K ) ;
    total = pick + dontpick ;
    dp [ index ] [ sum ] [ count ] = total ;
    return total ;
def ways ( N , K , arr ) :
    Arr = [ ] ;
    Arr . append ( - 1 ) ;
    for i in range ( N ) :
        Arr . append ( arr [ i ] ) ;
    answer = waysutil ( N , 0 , 0 , Arr , K ) ;
    return answer ;
if __name__ == " _ _ main _ _ " :
    arr = [ 3 , 6 , 2 , 8 , 7 , 6 , 5 , 9 ] ;
    N = len ( arr ) ;
    K = 5 ;
    print ( ways ( N , K , arr ) ) ;

Index/key variables: count, i, index, sum
Accumulator variables: Arr
Iterator variables: i
Python program:
import numpy as np
arrSize = 51
maxSum = 201
MAX = 100
inf = 999999
dp = np . zeros ( ( arrSize , maxSum ) ) ;
visit = np . zeros ( ( arrSize , maxSum ) ) ;
def RetClose ( a , b , s ) :
    if ( abs ( a - s ) < abs ( b - s ) ) :
        return a ;
    else :
        return b ;
def MinDiff ( i , sum , arr , n ) :
    if ( i == n ) :
        return 0 ;
    if ( visit [ i ] [ sum + MAX ] ) :
        return dp [ i ] [ sum + MAX ] ;
    visit [ i ] [ sum + MAX ] = 1 ;
    dp [ i ] [ sum + MAX ] = RetClose ( arr [ i ] + MinDiff ( i + 1 , sum + arr [ i ] , arr , n ) , MinDiff ( i + 1 , sum , arr , n ) , - 1 * sum ) ;
    return dp [ i ] [ sum + MAX ] ;
def FindClose ( arr , n ) :
    ans = inf ;
    for i in range ( 1 , n + 1 ) :
        ans = RetClose ( arr [ i - 1 ] + MinDiff ( i , arr [ i - 1 ] , arr , n ) , ans , 0 ) ;
    print ( ans ) ;
if __name__ == " _ _ main _ _ " :
    arr = [ 25 , - 9 , - 10 , - 4 , - 7 , - 33 ] ;
    n = len ( arr ) ;
    FindClose ( arr , n ) ;

Index/key variables: i
Iterator variables: i
Python program:
def totalPaths ( i , j , n , grid , dp ) :
    if ( i < 0 or j < 0 or i >= n or j >= n ) :
        return 0
    if ( dp [ i ] [ j ] == - 1 ) :
        if ( grid [ i ] [ j ] == 1 ) :
            dp [ i ] [ j ] = totalPaths ( i , j + 1 , n , grid , dp )
        elif ( grid [ i ] [ j ] == 2 ) :
            dp [ i ] [ j ] = totalPaths ( i + 1 , j , n , grid , dp )
        else :
            dp [ i ] [ j ] = totalPaths ( i , j + 1 , n , grid , dp ) + totalPaths ( i + 1 , j , n , grid , dp )
    return dp [ i ] [ j ]
def maxSumPath ( i , j , n , grid , dp ) :
    if ( i < 0 or j < 0 or i >= n or j >= n ) :
        return 0
    if ( dp [ i ] [ j ] == - 1 ) :
        if ( grid [ i ] [ j ] == 1 ) :
            dp [ i ] [ j ] = grid [ i ] [ j ] + maxSumPath ( i , j + 1 , n , grid , dp )
        elif ( grid [ i ] [ j ] == 2 ) :
            dp [ i ] [ j ] = grid [ i ] [ j ] + maxSumPath ( i + 1 , j , n , grid , dp )
        else :
            dp [ i ] [ j ] = grid [ i ] [ j ] + max ( maxSumPath ( i , j + 1 , n , grid , dp ) , maxSumPath ( i + 1 , j , n , grid , dp ) )
    return dp [ i ] [ j ]
if __name__ == ' _ _ main _ _ ' :
    grid = [ [ 1 , 1 , 3 , 2 , 1 ] , [ 3 , 2 , 2 , 1 , 2 ] , [ 1 , 3 , 3 , 1 , 3 ] , [ 1 , 2 , 3 , 1 , 2 ] , [ 1 , 1 , 1 , 3 , 1 ] ]
    n = len ( grid [ 0 ] )
    dp = [ [ - 1 ] * n ] * n
    dp [ n - 1 ] [ n - 1 ] = 1
    print ( " Total ▁ paths : " , totalPaths ( 0 , 0 , n , grid , dp ) )
    dp = [ [ - 1 ] * n ] * n
    dp [ n - 1 ] [ n - 1 ] = grid [ n - 1 ] [ n - 1 ]
    print ( " Maximum ▁ sum : " , maxSumPath ( 0 , 0 , n , grid , dp ) )

Index/key variables: i, j
Python program:
import numpy as np
MAX = 100000
bitscount = 32
prefix_count = np . zeros ( ( bitscount , MAX ) ) ;
def findPrefixCount ( arr , n ) :
    for i in range ( 0 , bitscount ) :
        prefix_count [ i ] [ 0 ] = ( ( arr [ 0 ] >> i ) & 1 ) ;
        for j in range ( 1 , n ) :
            prefix_count [ i ] [ j ] = ( ( arr [ j ] >> i ) & 1 ) ;
            prefix_count [ i ] [ j ] += prefix_count [ i ] [ j - 1 ] ;
def rangeOr ( l , r ) :
    ans = 0 ;
    for i in range ( bitscount ) :
        x = 0 ;
        if ( l == 0 ) :
            x = prefix_count [ i ] [ r ] ;
        else :
            x = prefix_count [ i ] [ r ] - prefix_count [ i ] [ l - 1 ] ;
        if ( x != 0 ) :
            ans = ( ans | ( 1 << i ) ) ;
    return ans ;
if __name__ == " _ _ main _ _ " :
    arr = [ 7 , 5 , 3 , 5 , 2 , 3 ] ;
    n = len ( arr ) ;
    findPrefixCount ( arr , n ) ;
    queries = [ [ 1 , 3 ] , [ 4 , 5 ] ] ;
    q = len ( queries ) ;
    for i in range ( q ) :
        print ( rangeOr ( queries [ i ] [ 0 ] , queries [ i ] [ 1 ] ) ) ;

Index/key variables: i, j, r
Iterator variables: i, j
Python program:
import numpy as np ;
def palindromeSubStrs ( s ) :
    dp = np . zeros ( ( len ( s ) , len ( s ) ) ) ;
    m = { } ;
    for i in range ( len ( s ) ) :
        dp [ i ] [ i ] = 1 ;
        m [ s [ i : i + 1 ] ] = 1 ;
    for i in range ( len ( s ) - 1 ) :
        if ( s [ i ] == s [ i + 1 ] ) :
            dp [ i ] [ i + 1 ] = 1 ;
            m [ s [ i : i + 2 ] ] = 1 ;
        else :
            dp [ i ] [ i + 1 ] = 0 ;
    for length in range ( 3 , len ( s ) + 1 ) :
        for st in range ( len ( s ) - length + 1 ) :
            end = st + length - 1 ;
            if ( s [ st ] == s [ end ] and dp [ st + 1 ] [ end - 1 ] ) :
                dp [ st ] [ end ] = 1 ;
                m [ s [ st : end + 1 ] ] = 1 ;
            else :
                dp [ st ] [ end ] = 0 ;
    return len ( m ) ;
if __name__ == " _ _ main _ _ " :
    s = " abaaa " ;
    print ( palindromeSubStrs ( s ) ) ;

Index/key variables: end, i, st
Iterator variables: i, length, st
Python program:
import sys
n = 4
m = 4
arr = [ [ 1 , 0 , 3 , - 1 ] , [ 3 , 5 , 1 , - 2 ] , [ - 2 , 0 , 1 , 1 ] , [ 2 , 1 , - 1 , 1 ] ]
cache = [ [ [ - 1 for i in range ( 5 ) ] for j in range ( 5 ) ] for k in range ( 5 ) ]
def sum ( i1 , j1 , i2 , j2 ) :
    if ( i1 == i2 and j1 == j2 ) :
        return arr [ i1 ] [ j1 ]
    return arr [ i1 ] [ j1 ] + arr [ i2 ] [ j2 ]
def maxSumPath ( i1 , j1 , i2 ) :
    j2 = i1 + j1 - i2
    if ( i1 >= n or i2 >= n or j1 >= m or j2 >= m ) :
        return 0
    if ( cache [ i1 ] [ j1 ] [ i2 ] != - 1 ) :
        return cache [ i1 ] [ j1 ] [ i2 ]
    ans = - sys . maxsize - 1
    ans = max ( ans , maxSumPath ( i1 + 1 , j1 , i2 + 1 ) + sum ( i1 , j1 , i2 , j2 ) )
    ans = max ( ans , maxSumPath ( i1 , j1 + 1 , i2 ) + sum ( i1 , j1 , i2 , j2 ) )
    ans = max ( ans , maxSumPath ( i1 , j1 + 1 , i2 + 1 ) + sum ( i1 , j1 , i2 , j2 ) )
    ans = max ( ans , maxSumPath ( i1 + 1 , j1 , i2 ) + sum ( i1 , j1 , i2 , j2 ) )
    cache [ i1 ] [ j1 ] [ i2 ] = ans
    return ans
if __name__ == ' _ _ main _ _ ' :
    print ( maxSumPath ( 0 , 0 , 0 ) )

Index/key variables: i1, i2, j1, j2
Python program:
from sys import maxsize
def BellmanFord ( graph , V , E , src ) :
    dis = [ maxsize ] * V
    dis [ src ] = 0
    for i in range ( V - 1 ) :
        for j in range ( E ) :
            if dis [ graph [ j ] [ 0 ] ] + graph [ j ] [ 2 ] < dis [ graph [ j ] [ 1 ] ] :
                dis [ graph [ j ] [ 1 ] ] = dis [ graph [ j ] [ 0 ] ] + graph [ j ] [ 2 ]
    for i in range ( E ) :
        x = graph [ i ] [ 0 ]
        y = graph [ i ] [ 1 ]
        weight = graph [ i ] [ 2 ]
        if dis [ x ] != maxsize and dis [ x ] + weight < dis [ y ] :
            print ( " Graph ▁ contains ▁ negative ▁ weight ▁ cycle " )
    print ( " Vertex ▁ Distance ▁ from ▁ Source " )
    for i in range ( V ) :
        print ( " % d TABSYMBOL TABSYMBOL % d " % ( i , dis [ i ] ) )
if __name__ == " _ _ main _ _ " :
    graph = [ [ 0 , 1 , - 1 ] , [ 0 , 2 , 4 ] , [ 1 , 2 , 3 ] , [ 1 , 3 , 2 ] , [ 1 , 4 , 2 ] , [ 3 , 2 , 5 ] , [ 3 , 1 , 1 ] , [ 4 , 3 , - 3 ] ]
    BellmanFord ( graph , V , E , 0 )

Index/key variables: i, j, src, x, y
Iterator variables: i, j
Python program:
m , x = [ 0 ] * 1000 , [ 0 ] * 22
a = [ [ 0 for i in range ( 22 ) ] for j in range ( 1000 ) ]
ans = 0
def dfs ( u , p ) :
    global ans
    for i in range ( 0 , len ( g [ u ] ) ) :
        v = g [ u ] [ i ]
        if v != p :
            dfs ( v , u )
            for i in range ( 0 , 22 ) :
                a [ u ] [ i ] += a [ v ] [ i ]
    pp = 0
    for i in range ( 0 , 22 ) :
        if ( not ( ( a [ u ] [ i ] > 0 and x [ i ] - a [ u ] [ i ] > 0 ) or ( a [ u ] [ i ] == 0 and x [ i ] == 0 ) ) ) :
            pp = 1
            break
    if pp == 0 :
        ans += 1
if __name__ == " _ _ main _ _ " :
    n = 4
    g = [ [ ] for i in range ( n + 1 ) ]
    m [ 1 ] = 1
    m [ 2 ] = 3
    m [ 3 ] = 2
    m [ 4 ] = 3
    for i in range ( 1 , n + 1 ) :
        y , k = m [ i ] , 0
        while y != 0 :
            p = y % 2
            if p == 1 :
                x [ k ] += 1
                a [ i ] [ k ] += 1
            y = y // 2
            k += 1
    g [ 1 ] . append ( 2 )
    g [ 2 ] . append ( 1 )
    g [ 1 ] . append ( 3 )
    g [ 3 ] . append ( 1 )
    g [ 1 ] . append ( 4 )
    g [ 4 ] . append ( 1 )
    dfs ( 1 , 0 )
    print ( ans )

Index/key variables: i, k, u, v
Iterator variables: i
Python program:
global maxn
maxn = 16
def precompute ( ) :
    dp = [ - 1 for i in range ( maxn ) ]
    dp [ 0 ] = 0
    v = [ 4 , 6 , 9 ]
    for i in range ( 1 , maxn , 1 ) :
        for k in range ( 3 ) :
            j = v [ k ]
            if ( i >= j and dp [ i - j ] != - 1 ) :
                dp [ i ] = max ( dp [ i ] , dp [ i - j ] + 1 )
    return dp
def Maximum_Summands ( dp , n ) :
    if ( n < maxn ) :
        return dp [ n ]
    else :
        t = int ( ( n - maxn ) / 4 ) + 1
        return t + dp [ n - 4 * t ]
if __name__ == ' _ _ main _ _ ' :
    n = 12
    dp = precompute ( )
    print ( Maximum_Summands ( dp , n ) )

Index/key variables: i, k, n
Iterator variables: i, k
Python program:
import sys
def minCostJumpsDP ( A , k ) :
    size = len ( A )
    x = [ sys . maxsize ] * ( size )
    x [ 0 ] = 0
    for i in range ( size ) :
        j = i + 1
        while j < i + k + 1 and j < size :
            x [ j ] = min ( x [ j ] , x [ i ] + abs ( A [ i ] - A [ j ] ) )
            j += 1
    return x [ size - 1 ]
if __name__ == " _ _ main _ _ " :
    input_ = [ 83 , 26 , 37 , 35 , 33 , 35 , 56 ]
    print ( minCostJumpsDP ( input_ , 3 ) )

Index/key variables: i, j
Iterator variables: i
Python program:
N = 4
def getSum ( r1 , r2 , c1 , c2 , dp ) :
    return dp [ r2 ] [ c2 ] - dp [ r2 ] [ c1 ] - dp [ r1 ] [ c2 ] + dp [ r1 ] [ c1 ]
def sumFound ( K , S , grid ) :
    dp = [ [ 0 for i in range ( N + 1 ) ] for j in range ( N + 1 ) ]
    for i in range ( N ) :
        for j in range ( N ) :
            dp [ i + 1 ] [ j + 1 ] = dp [ i + 1 ] [ j ] + dp [ i ] [ j + 1 ] - dp [ i ] [ j ] + grid [ i ] [ j ]
    for i in range ( 0 , N ) :
        for j in range ( 0 , N ) :
            sum = getSum ( i , i + K , j , j + K , dp )
            if ( sum == S ) :
                return True
    return False
grid = [ [ 1 , 2 , 3 , 4 ] , [ 5 , 6 , 7 , 8 ] , [ 9 , 10 , 11 , 12 ] , [ 13 , 14 , 15 , 16 ] ]
K = 2
S = 14
if ( sumFound ( K , S , grid ) ) :
    print ( " Yes " )
else :
    print ( " No " )

Index/key variables: c1, c2, i, j, r1, r2
Iterator variables: i, j
Python program:
def bfs ( n , m , num ) :
    q = [ ]
    q . append ( num )
    while len ( q ) > 0 :
        stepNum = q [ 0 ]
        q . pop ( 0 ) ;
        if ( stepNum <= m and stepNum >= n ) :
            print ( stepNum , end = " ▁ " )
        if ( num == 0 or stepNum > m ) :
            continue
        lastDigit = stepNum % 10
        stepNumA = stepNum * 10 + ( lastDigit - 1 )
        stepNumB = stepNum * 10 + ( lastDigit + 1 )
        if ( lastDigit == 0 ) :
            q . append ( stepNumB )
        elif ( lastDigit == 9 ) :
            q . append ( stepNumA )
        else :
            q . append ( stepNumA )
            q . append ( stepNumB )
def displaySteppingNumbers ( n , m ) :
    for i in range ( 10 ) :
        bfs ( n , m , i )
n , m = 0 , 21
displaySteppingNumbers ( n , m )

Accumulator variables: q
Iterator variables: i
Python program:
from collections import deque as queue
row = 5
col = 5
minDistance = [ [ 0 for i in range ( col + 1 ) ] for i in range ( row + 1 ) ]
visited = [ [ 0 for i in range ( col + 1 ) ] for i in range ( row + 1 ) ]
que = queue ( )
def isValid ( grid , i , j ) :
    if ( i < 0 or j < 0 or j >= col or i >= row or grid [ i ] [ j ] or visited [ i ] [ j ] ) :
        return False
    return True
def findMinPathminDistance ( grid , sourceRow , sourceCol ) :
    if ( sourceCol == 0 or sourceCol == col - 1 or sourceRow == 0 or sourceRow == row - 1 ) :
        return 0
    minFromSource = row * col
    for i in range ( row ) :
        for j in range ( col ) :
            minDistance [ i ] [ j ] = row * col
    que . appendleft ( [ sourceRow , sourceCol ] )
    minDistance [ sourceRow ] [ sourceCol ] = 0 ;
    visited [ sourceRow ] [ sourceCol ] = 1 ;
    while ( len ( que ) > 0 ) :
        cell = que . pop ( )
        cellRow = cell [ 0 ]
        cellCol = cell [ 1 ]
        if ( isValid ( grid , cellRow + 1 , cellCol ) ) :
            que . appendleft ( [ cellRow + 1 , cellCol ] )
            minDistance [ cellRow + 1 ] [ cellCol ] = min ( minDistance [ cellRow + 1 ] [ cellCol ] , minDistance [ cellRow ] [ cellCol ] + 1 )
            visited [ cellRow + 1 ] [ cellCol ] = 1
        if ( isValid ( grid , cellRow - 1 , cellCol ) ) :
            que . appendleft ( [ cellRow - 1 , cellCol ] )
            minDistance [ cellRow - 1 ] [ cellCol ] = min ( minDistance [ cellRow - 1 ] [ cellCol ] , minDistance [ cellRow ] [ cellCol ] + 1 )
            visited [ cellRow - 1 ] [ cellCol ] = 1
        if ( isValid ( grid , cellRow , cellCol + 1 ) ) :
            que . appendleft ( [ cellRow , cellCol + 1 ] )
            minDistance [ cellRow ] [ cellCol + 1 ] = min ( minDistance [ cellRow ] [ cellCol + 1 ] , minDistance [ cellRow ] [ cellCol ] + 1 )
            visited [ cellRow ] [ cellCol + 1 ] = 1 ;
        if ( isValid ( grid , cellRow , cellCol - 1 ) ) :
            que . appendleft ( [ cellRow , cellCol - 1 ] )
            minDistance [ cellRow ] [ cellCol - 1 ] = min ( minDistance [ cellRow ] [ cellCol - 1 ] , minDistance [ cellRow ] [ cellCol ] + 1 )
            visited [ cellRow ] [ cellCol - 1 ] = 1
    for i in range ( col ) :
        minFromSource = min ( minFromSource , minDistance [ 0 ] [ i ] ) ;
    for i in range ( col ) :
        minFromSource = min ( minFromSource , minDistance [ row - 1 ] [ i ] ) ;
    for i in range ( row ) :
        minFromSource = min ( minFromSource , minDistance [ i ] [ 0 ] ) ;
    for i in range ( row ) :
        minFromSource = min ( minFromSource , minDistance [ i ] [ col - 1 ] ) ;
    if ( minFromSource == row * col ) :
        return - 1
    return minFromSource
sourceRow = 3
sourceCol = 3
grid = [ [ 1 , 1 , 1 , 1 , 0 ] , [ 0 , 0 , 1 , 0 , 1 ] , [ 0 , 0 , 1 , 0 , 1 ] , [ 1 , 0 , 0 , 0 , 1 ] , [ 1 , 1 , 0 , 1 , 0 ] ]
print ( findMinPathminDistance ( grid , sourceRow , sourceCol ) )

Index/key variables: cellCol, cellRow, i, j, sourceCol, sourceRow
Iterator variables: i, j
Python program:
def findMinOperations ( S ) :
    S = S [ : : - 1 ]
    n = len ( S )
    dp = [ [ 0 ] * 2 ] * ( n + 1 )
    if ( S [ 0 ] == '0' ) :
        dp [ 0 ] [ 0 ] = 0
    else :
        dp [ 0 ] [ 0 ] = 1
    dp [ 0 ] [ 1 ] = 1
    for i in range ( 1 , n ) :
        if ( S [ i ] == '0' ) :
            dp [ i ] [ 0 ] = dp [ i - 1 ] [ 0 ]
            dp [ i ] [ 1 ] = 1 + min ( dp [ i - 1 ] [ 1 ] , dp [ i - 1 ] [ 0 ] )
        else :
            dp [ i ] [ 1 ] = dp [ i - 1 ] [ 1 ] ;
            dp [ i ] [ 0 ] = 1 + min ( dp [ i - 1 ] [ 0 ] , dp [ i - 1 ] [ 1 ] )
    return dp [ n - 1 ] [ 0 ]
if __name__ == " _ _ main _ _ " :
    S = "100"
    print ( findMinOperations ( S ) )
    S = "111" ;
    print ( findMinOperations ( S ) )

Index/key variables: i
Iterator variables: i
Python program:
MAX_VAL = 10000000
dp = [ [ - 1 for i in range ( 100 ) ] for i in range ( 100 ) ]
def find ( index , openbrk , n , adj ) :
    if ( openbrk < 0 ) :
        return MAX_VAL
    if ( index == n ) :
        if ( openbrk == 0 ) :
            return 0
        else :
            return MAX_VAL
    if ( dp [ index ] [ openbrk ] != - 1 ) :
        return dp [ index ] [ openbrk ]
    dp [ index ] [ openbrk ] = min ( adj [ index ] [ 1 ] + find ( index + 1 , openbrk + 1 , n , adj ) , adj [ index ] [ 0 ] + find ( index + 1 , openbrk - 1 , n , adj ) )
    return dp [ index ] [ openbrk ]
if __name__ == ' _ _ main _ _ ' :
    n = 4 ;
    adj = [ [ 5000 , 3000 ] , [ 6000 , 2000 ] , [ 8000 , 1000 ] , [ 9000 , 6000 ] ]
    print ( find ( 1 , 1 , n , adj ) + adj [ 0 ] [ 1 ] )

Index/key variables: index, openbrk
Python program:
def getNumberOfWays ( N , Coins ) :
    ways = [ 0 ] * ( N + 1 ) ;
    ways [ 0 ] = 1 ;
    for i in range ( len ( Coins ) ) :
        for j in range ( len ( ways ) ) :
            if ( Coins [ i ] <= j ) :
                ways [ j ] += ways [ ( int ) ( j - Coins [ i ] ) ] ;
    return ways [ N ] ;
def printArray ( coins ) :
    for i in coins :
        print ( i ) ;
if __name__ == ' _ _ main _ _ ' :
    Coins = [ 1 , 5 , 10 ] ;
    print ( " The ▁ Coins ▁ Array : " ) ;
    printArray ( Coins ) ;
    print ( " Solution : " , end = " " ) ;
    print ( getNumberOfWays ( 12 , Coins ) ) ;

Index/key variables: N, i, j
Iterator variables: i, j
Python program:
def find ( m , n , adj ) :
    sweet = [ 0 ] * ( n + 1 )
    dp = [ [ [ 0 for i in range ( n + 1 ) ] for i in range ( n + 1 ) ] for i in range ( n + 1 ) ]
    sweet [ 0 ] = 0
    for i in range ( 1 , m + 1 ) :
        sweet [ i ] = adj [ i - 1 ]
    for i in range ( m + 1 ) :
        for k in range ( n + 1 ) :
            dp [ i ] [ 0 ] [ k ] = 0
        for k in range ( 1 , n + 1 ) :
            dp [ i ] [ k ] [ 0 ] = - 1
    for i in range ( m + 1 ) :
        for j in range ( 1 , n + 1 ) :
            for k in range ( 1 , n + 1 ) :
                dp [ i ] [ j ] [ k ] = - 1
                if ( i > 0 and j >= k and sweet [ k ] > 0 and dp [ i - 1 ] [ j - k ] [ k ] != - 1 ) :
                    dp [ i ] [ j ] [ k ] = dp [ i - 1 ] [ j - k ] [ k ] + sweet [ k ]
                if ( dp [ i ] [ j ] [ k ] == - 1 or ( dp [ i ] [ j ] [ k - 1 ] != - 1 and dp [ i ] [ j ] [ k ] > dp [ i ] [ j ] [ k - 1 ] ) ) :
                    dp [ i ] [ j ] [ k ] = dp [ i ] [ j ] [ k - 1 ]
    if ( dp [ m ] [ n ] [ n ] == - 1 ) :
        return 0
    else :
        return dp [ m ] [ n ] [ n ]
m = 3
adj = [ 2 , 1 , 3 , 0 , 4 , 10 ]
n = len ( adj )
print ( find ( m , n , adj ) )

Index/key variables: i, j, k, m, n
Iterator variables: i, j, k
Python program:
def find ( start , adj , n , dp ) :
    if ( start == n ) :
        return 0
    if ( dp [ start ] != - 1 ) :
        return dp [ start ]
    dp [ start ] = 0
    one = 0
    zero = 0
    for k in range ( start , n , 1 ) :
        if ( adj [ k ] == '1' ) :
            one += 1
        else :
            zero += 1
        if ( one > zero ) :
            dp [ start ] = max ( dp [ start ] , find ( k + 1 , adj , n , dp ) + k - start + 1 )
        else :
            dp [ start ] = max ( dp [ start ] , find ( k + 1 , adj , n , dp ) )
    return dp [ start ]
if __name__ == ' _ _ main _ _ ' :
    adj = "100110001010001"
    n = len ( adj )
    dp = [ - 1 for i in range ( n + 1 ) ]
    print ( find ( 0 , adj , n , dp ) )

Index/key variables: k, start
Accumulator variables: one, zero
Iterator variables: k
Python program:
def isVowel ( ch ) :
    if ( ch == ' a ' or ch == ' e ' or ch == ' i ' or ch == ' o ' or ch == ' u ' ) :
        return True
    return False
def lcs ( X , Y , m , n ) :
    L = [ [ 0 for i in range ( n + 1 ) ] for j in range ( m + 1 ) ]
    i , j = 0 , 0
    for i in range ( m + 1 ) :
        for j in range ( n + 1 ) :
            if ( i == 0 or j == 0 ) :
                L [ i ] [ j ] = 0
            elif ( ( X [ i - 1 ] == Y [ j - 1 ] ) and isVowel ( X [ i - 1 ] ) ) :
                L [ i ] [ j ] = L [ i - 1 ] [ j - 1 ] + 1
            else :
                L [ i ] [ j ] = max ( L [ i - 1 ] [ j ] , L [ i ] [ j - 1 ] )
    return L [ m ] [ n ]
X = " aieef "
Y = " klaief "
m = len ( X )
n = len ( Y )
print ( " Length ▁ of ▁ LCS ▁ = " , lcs ( X , Y , m , n ) )

Index/key variables: i, j, m, n
Iterator variables: i, j