dhyuti-n/xlcost-variable-roles huggingface dataset:
Python program:
def minCost ( arr , n ) :
    sortedarr = [ ]
    total_cost = 0
    for i in range ( n ) :
        sortedarr . append ( [ arr [ i ] , i ] )
    sortedarr . sort ( )
    overall_minimum = sortedarr [ 0 ] [ 0 ]
    vis = [ False ] * n
    for i in range ( n ) :
        if vis [ i ] and sortedarr [ i ] [ 1 ] == i :
            continue
        v = [ ]
        j = i
        size = 0
        while vis [ j ] == False :
            vis [ j ] = True
            v . append ( sortedarr [ j ] [ 0 ] )
            j = sortedarr [ j ] [ 1 ]
            size += 1
        if size != 0 :
            local_minimum = v [ 0 ]
            result1 = 0
            result2 = 0
            for k in range ( 1 , size ) :
                result1 += local_minimum * v [ k ]
            for k in range ( size ) :
                result2 += overall_minimum * v [ k ]
            result2 += ( overall_minimum * local_minimum )
            total_cost += min ( result1 , result2 )
    return total_cost
A = [ 1 , 8 , 9 , 7 , 6 ]
ans = minCost ( A , len ( A ) )
print ( ans )

Index/key variables: i, j, k
Accumulator variables: result1, result2, size, sortedarr, total_cost, v
Iterator variables: i, k
Python program:
def maximizeFinalElement ( arr , n ) :
    arr . sort ( ) ;
    if ( arr [ 0 ] != 1 ) :
        arr [ 0 ] = 1 ;
    for i in range ( 1 , n ) :
        if ( arr [ i ] - arr [ i - 1 ] > 1 ) :
            arr [ i ] = arr [ i - 1 ] + 1 ;
    return arr [ n - 1 ] ;
if __name__ == ' _ _ main _ _ ' :
    n = 4 ;
    arr = [ 3 , 1 , 3 , 4 ] ;
    max = maximizeFinalElement ( arr , n ) ;
    print ( max ) ;

Index/key variables: i
Iterator variables: i
Python program:
def maxSubMatSum ( mat ) :
    n = len ( mat )
    m = len ( mat [ 0 ] )
    dp = [ [ 0 ] * m for _ in range ( n ) ]
    dp [ n - 1 ] [ m - 1 ] = mat [ n - 1 ] [ m - 1 ]
    res = dp [ n - 1 ] [ m - 1 ]
    for i in range ( m - 2 , - 1 , - 1 ) :
        dp [ n - 1 ] [ i ] = ( mat [ n - 1 ] [ i ] + dp [ n - 1 ] [ i + 1 ] )
        res = max ( res , dp [ n - 1 ] [ i ] )
    for i in range ( n - 2 , - 1 , - 1 ) :
        dp [ i ] [ m - 1 ] = ( mat [ i ] [ m - 1 ] + dp [ i + 1 ] [ m - 1 ] )
        res = max ( res , dp [ i ] [ m - 1 ] )
    for i in range ( n - 2 , - 1 , - 1 ) :
        for j in range ( m - 2 , - 1 , - 1 ) :
            dp [ i ] [ j ] = ( mat [ i ] [ j ] + dp [ i ] [ j + 1 ] + dp [ i + 1 ] [ j ] - dp [ i + 1 ] [ j + 1 ] )
            res = max ( res , dp [ i ] [ j ] )
    return res
if __name__ == ' _ _ main _ _ ' :
    mat = [ [ - 6 , - 4 , - 1 ] , [ - 3 , 2 , 4 ] , [ 2 , 5 , 8 ] ]
    print ( maxSubMatSum ( mat ) )

Index/key variables: i, j
Iterator variables: i, j
Python program:
def formAnInequality ( integers , inequalities ) :
    integers . sort ( )
    lowerIndex = 0
    higherIndex = len ( integers ) - 1
    sb = " "
    for ch in inequalities :
        if ( ch == ' < ' ) :
            sb += ( " ▁ " + chr ( integers [ lowerIndex ] ) + " ▁ " + ch )
            lowerIndex += 1
        else :
            sb += ( " ▁ " + chr ( integers [ higherIndex ] ) + " ▁ " + ch )
            higherIndex -= 1
    sb += ( " ▁ " + chr ( integers [ lowerIndex ] ) )
    return sb
if __name__ == " _ _ main _ _ " :
    integers = [ 2 , 5 , 1 , 0 ]
    inequalities = [ ' < ' , ' > ' , ' < ' ]
    output = formAnInequality ( integers , inequalities )
    print ( output )

Index/key variables: higherIndex, lowerIndex
Accumulator variables: sb
Iterator variables: ch
Python program:
def maxMinDifference ( arr , n , k ) :
    if ( k >= n - 1 ) :
        return 0
    arr . sort ( )
    ans = arr [ n - 1 ] - arr [ 0 ]
    i = k
    j = n - 1
    while i >= 0 :
        ans = min ( arr [ j ] - arr [ i ] , ans )
        i -= 1
        j -= 1
    return ans
if __name__ == ' _ _ main _ _ ' :
    arr = [ 1 , 4 , 6 , 11 , 15 ]
    N = len ( arr )
    K = 3
    print ( maxMinDifference ( arr , N , K ) )

Index/key variables: i, j
Python program:
def sortarray ( arr , N ) :
    if ( N == 3 ) :
        print ( " NO " )
    elif ( N % 4 == 0 or N % 4 == 1 ) :
        print ( " YES " )
        print ( N // 2 )
        k = 1
        for l in range ( N // 4 ) :
            print ( k , k + 1 , N )
            print ( k + 1 , N , N - 1 )
            k = k + 2
            N = N - 2
    else :
        print ( " NO " )
if __name__ == " _ _ main _ _ " :
    N = 5
    arr = [ 5 , 4 , 3 , 2 , 1 ]
    sortarray ( arr , N )

Iterator variables: l
Python program:
MAX = int ( 1e5 + 5 )
def find_missing ( interval ) :
    vis = [ 0 ] * ( MAX )
    for i in range ( len ( interval ) ) :
        start = interval [ i ] [ 0 ]
        end = interval [ i ] [ 1 ]
        vis [ start ] += 1
        vis [ end + 1 ] -= 1
    for i in range ( 1 , MAX ) :
        vis [ i ] += vis [ i - 1 ]
        if ( vis [ i ] == 0 ) :
            print ( i )
            return
interval = [ [ 0 , 14 ] , [ 86 , 108 ] , [ 22 , 30 ] , [ 5 , 17 ] ]
find_missing ( interval )

Index/key variables: i, start
Iterator variables: i
Python program:
def find_missing ( interval ) :
    interval . sort ( )
    mx = 0
    for i in range ( len ( interval ) ) :
        if ( interval [ i ] [ 0 ] > mx ) :
            print ( mx )
            return
        else :
            mx = max ( mx , interval [ i ] [ 1 ] + 1 )
    print ( mx )
if __name__ == " _ _ main _ _ " :
    interval = [ [ 0 , 14 ] , [ 86 , 108 ] , [ 22 , 30 ] , [ 5 , 17 ] ]
    find_missing ( interval ) ;

Index/key variables: i
Iterator variables: i
Python program:
def minBox ( arr , n ) :
    box = n
    arr . sort ( )
    curr_box , next_box = 0 , 1
    while ( curr_box < n and next_box < n ) :
        if ( arr [ curr_box ] < arr [ next_box ] ) :
            box = box - 1
            curr_box = curr_box + 1
            next_box = next_box + 1
        elif ( arr [ curr_box ] == arr [ next_box ] ) :
            next_box = next_box + 1
    print ( box )
size = [ 1 , 2 , 3 ]
n = len ( size )
minBox ( size , n )

Index/key variables: curr_box, next_box
Python program:
import math
def sortString ( arr , n , moves ) :
    pos = [ ]
    indices = [ ]
    flag = False
    for i in range ( n ) :
        if ( arr [ i ] != i ) :
            if ( arr [ arr [ arr [ i ] ] ] == i and arr [ arr [ i ] ] != i ) :
                temp = arr [ arr [ i ] ]
                indices . append ( [ i , arr [ i ] , arr [ arr [ i ] ] ] )
                sw = arr [ i ]
                arr [ i ] = arr [ arr [ i ] ]
                arr [ sw ] = sw
                sw = arr [ i ]
                arr [ i ] = arr [ temp ]
                arr [ temp ] = sw
        if ( arr [ i ] != i ) :
            pos . append ( i )
    for i in range ( n ) :
        if ( arr [ i ] != i ) :
            pos1 = i
            pos2 = arr [ i ]
            pos3 = arr [ arr [ i ] ]
            if ( pos3 != pos1 ) :
                indices . append ( [ pos1 , pos2 , pos3 ] )
                arr [ pos1 ] , arr [ pos2 ] = arr [ pos2 ] , arr [ pos1 ]
                arr [ pos1 ] , arr [ pos3 ] = arr [ pos3 ] , arr [ pos1 ]
                pos . remove ( pos2 )
                if pos3 in pos :
                    pos . remove ( pos3 )
                if ( arr [ pos1 ] == pos1 ) :
                    pos . remove ( pos1 )
            else :
                if ( pos3 == pos [ 0 ] ) :
                    it = 0
                    if ( pos [ 0 ] != pos [ - 1 ] ) :
                        it = it + 1
                        pos3 = pos [ it ]
                        if ( pos [ it ] != pos [ - 1 ] and pos3 == pos2 ) :
                            it = it + 1
                            pos3 = pos [ it ]
                        elif ( pos [ it ] == pos [ - 1 ] and pos3 == pos2 ) :
                            flag = True
                            break
                    else :
                        flag = True
                        break
                indices . append ( [ pos1 , pos2 , pos3 ] )
                arr [ pos1 ] , arr [ pos2 ] = arr [ pos2 ] , arr [ pos1 ]
                arr [ pos1 ] , arr [ pos3 ] = arr [ pos3 ] , arr [ pos1 ]
                pos . remove ( pos2 )
        if ( arr [ i ] != i ) :
            i = i - 1
    if ( flag == True or len ( indices ) > moves ) :
        print ( " Not ▁ Possible " )
    else :
        print ( len ( indices ) )
s = " adceb "
arr = [ ]
for i in s :
    arr . append ( ord ( i ) - ord ( ' a ' ) )
sortString ( arr , len ( s ) , math . floor ( len ( s ) / 2 ) )

Index/key variables: i, it, pos1, pos2, pos3, sw, temp
Accumulator variables: arr, indices, pos
Iterator variables: i
Boolean variables: flag
Python program:
from typing import Any , List
def countReductions ( v : List [ Any ] , K : int ) -> int :
    sum = 0
    for i in v :
        sum += i [ 0 ]
    if ( sum <= K ) :
        return 0
    v . sort ( key = lambda a : a [ 0 ] - a [ 1 ] )
    i = 0
    while ( sum > K and i < len ( v ) ) :
        sum -= ( v [ i ] [ 0 ] - v [ i ] [ 1 ] )
        i += 1
    if ( sum <= K ) :
        return i
    return - 1
if __name__ == " _ _ main _ _ " :
    N = 4
    K = 25
    v = [ [ 0 , 0 ] for _ in range ( N ) ]
    v [ 0 ] = [ 10 , 5 ]
    v [ 1 ] = [ 20 , 9 ]
    v [ 2 ] = [ 12 , 10 ]
    v [ 3 ] = [ 4 , 2 ]
    print ( countReductions ( v , K ) )

Index/key variables: Any, i
Accumulator variables: sum
Iterator variables: i
Python program:
class node :
    def __init__ ( self , x ) :
        self . data = x
        self . next = None
def setData ( head ) :
    tmp = head
    while ( tmp != None ) :
        print ( tmp . data , end = " ▁ - > ▁ " )
        tmp = tmp . next
def getData ( head , num ) :
    temp = node ( - 1 )
    tail = head
    temp . data = num
    temp . next = None
    if ( head == None ) :
        head = temp
        tail = temp
    else :
        while ( tail != None ) :
            if ( tail . next == None ) :
                tail . next = temp
                tail = tail . next
            tail = tail . next
    return head
def mergelists ( head1 , head2 ) :
    tail = head1
    while ( tail != None ) :
        if ( tail . next == None and head2 != None ) :
            tail . next = head2
            break
        tail = tail . next
    return head1
def sortlist ( head1 ) :
    curr = head1
    temp = head1
    while ( curr . next != None ) :
        temp = curr . next
        while ( temp != None ) :
            if ( temp . data < curr . data ) :
                t = temp . data
                temp . data = curr . data
                curr . data = t
            temp = temp . next
        curr = curr . next
if __name__ == ' _ _ main _ _ ' :
    head1 = node ( - 1 )
    head2 = node ( - 1 )
    head1 = None
    head2 = None
    head1 = getData ( head1 , 4 )
    head1 = getData ( head1 , 7 )
    head1 = getData ( head1 , 5 )
    head2 = getData ( head2 , 2 )
    head2 = getData ( head2 , 1 )
    head2 = getData ( head2 , 8 )
    head2 = getData ( head2 , 1 )
    head1 = mergelists ( head1 , head2 )
    sortlist ( head1 )
    setData ( head1 )

Class/struct names: node
Python program:
def findMedian ( a , n ) :
    if ( n % 2 != 0 ) :
        return a [ int ( n / 2 ) ]
    return ( a [ int ( ( n - 1 ) / 2 ) ] + a [ int ( n / 2 ) ] ) / 2.0
def kStrongest ( arr , n , k ) :
    arr . sort ( )
    median = findMedian ( arr , n )
    diff = [ 0 ] * ( n )
    for i in range ( n ) :
        diff [ i ] = abs ( median - arr [ i ] )
    i = 0
    j = n - 1
    while ( k > 0 ) :
        if ( diff [ i ] > diff [ j ] ) :
            print ( arr [ i ] , end = " ▁ " )
            i += 1
        else :
            print ( arr [ j ] , end = " ▁ " )
            j -= 1
        k -= 1
arr = [ 1 , 2 , 3 , 4 , 5 ]
k = 3
n = len ( arr )
kStrongest ( arr , n , k )

Index/key variables: i, j
Accumulator variables: k
Iterator variables: i
Python program:
def sorting_possible ( a , b , n ) :
    sorted = True
    type1 = 0
    type0 = 0
    for i in range ( 1 , n ) :
        if ( a [ i ] < a [ i - 1 ] ) :
            sorted = False
            break
    for i in range ( n ) :
        if ( b [ i ] == 0 ) :
            type0 += 1
        else :
            type1 += 1
    if ( sorted != False ) :
        return True
    elif ( type1 == n or type0 == n ) :
        return False
    else :
        return True
a = [ 15 , 1 , 2 , 17 , 6 ]
b = [ 1 , 1 , 0 , 1 , 1 ]
n = len ( a )
res = sorting_possible ( a , b , n )
if ( res != False ) :
    print ( " Yes " )
else :
    print ( " No " )

Index/key variables: i
Accumulator variables: type0, type1
Iterator variables: i
Boolean variables: sorted
Python program:
class Pair :
    def __init__ ( self , first , second ) :
        self . first = first
        self . second = second
infi = 1000000000 ;
class Node :
    def __init__ ( self , vertexNumber ) :
        self . vertexNumber = vertexNumber
        self . children = [ ]
    def Add_child ( self , vNumber , length ) :
        p = Pair ( vNumber , length ) ;
        self . children . append ( p ) ;
def dijkstraDist ( g , s , path ) :
    dist = [ infi for i in range ( len ( g ) ) ]
    visited = [ False for i in range ( len ( g ) ) ]
    for i in range ( len ( g ) ) :
        path [ i ] = - 1
    dist [ s ] = 0 ;
    path [ s ] = - 1 ;
    current = s ;
    sett = set ( )
    while ( True ) :
        visited [ current ] = True ;
        for i in range ( len ( g [ current ] . children ) ) :
            v = g [ current ] . children [ i ] . first ;
            if ( visited [ v ] ) :
                continue ;
            sett . add ( v ) ;
            alt = dist [ current ] + g [ current ] . children [ i ] . second ;
            if ( alt < dist [ v ] ) :
                dist [ v ] = alt ;
                path [ v ] = current ;
        if current in sett :
            sett . remove ( current ) ;
        if ( len ( sett ) == 0 ) :
            break ;
        minDist = infi ;
        index = 0 ;
        for a in sett :
            if ( dist [ a ] < minDist ) :
                minDist = dist [ a ] ;
                index = a ;
        current = index ;
    return dist ;
def printPath ( path , i , s ) :
    if ( i != s ) :
        if ( path [ i ] == - 1 ) :
            print ( " Path ▁ not ▁ found ! ! " ) ;
            return ;
        printPath ( path , path [ i ] , s ) ;
        print ( path [ i ] + " ▁ " ) ;
if __name__ == ' _ _ main _ _ ' :
    v = [ ]
    n = 4
    s = 0 ;
    for i in range ( n ) :
        a = Node ( i ) ;
        v . append ( a ) ;
    v [ 0 ] . Add_child ( 1 , 1 ) ;
    v [ 0 ] . Add_child ( 2 , 4 ) ;
    v [ 1 ] . Add_child ( 2 , 2 ) ;
    v [ 1 ] . Add_child ( 3 , 6 ) ;
    v [ 2 ] . Add_child ( 3 , 3 ) ;
    path = [ 0 for i in range ( len ( v ) ) ] ;
    dist = dijkstraDist ( v , s , path ) ;
    for i in range ( len ( dist ) ) :
        if ( dist [ i ] == infi ) :
            print ( " { 0 } ▁ and ▁ { 1 } ▁ are ▁ not ▁ " + " connected " . format ( i , s ) ) ;
            continue ;
        print ( " Distance ▁ of ▁ { } th ▁ vertex ▁ from ▁ source ▁ vertex ▁ { } ▁ is : ▁ { } " . format ( i , s , dist [ i ] ) ) ;

Index/key variables: a, current, i, s, v
Accumulator variables: sett
Iterator variables: a, i
Class/struct names: Node, Pair
Python program:
def sortPermutation ( arr , n ) :
    ans = [ ]
    p = [ ]
    visited = [ 0 ] * 200005
    for i in range ( 1 , n + 1 ) :
        if ( arr [ i ] == i ) :
            visited [ i ] = 1
            continue
        else :
            if ( visited [ i ] == False ) :
                x = i
                v = [ ]
                while ( visited [ x ] == False ) :
                    visited [ x ] = 1
                    v . append ( x )
                    x = arr [ x ]
                if ( ( len ( v ) - 3 ) % 2 == 0 ) :
                    for i in range ( 1 , len ( v ) , 2 ) :
                        ans . append ( [ v [ 0 ] , v [ i ] , v [ i + 1 ] ] )
                    continue
                p . append ( v [ 0 ] )
                p . append ( v [ len ( v ) - 1 ] )
                for i in range ( 1 , len ( v ) - 1 , 2 ) :
                    ans . append ( [ v [ 0 ] , v [ i ] , v [ i + 1 ] ] )
    if ( len ( p ) % 4 ) :
        print ( - 1 )
        return
    for i in range ( 0 , len ( p ) , 4 ) :
        ans . append ( [ p [ i ] , p [ i + 1 ] , p [ i + 2 ] ] )
        ans . append ( p [ i [ + 2 ] , p [ i ] , p [ i + 3 ] ] )
    print ( len ( ans ) )
    for i in ans :
        print ( i [ 0 ] , i [ 1 ] , i [ 2 ] )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 0 , 3 , 2 , 4 , 1 ]
    n = 4
    sortPermutation ( arr , n )

Index/key variables: i, x
Accumulator variables: ans, p, v
Iterator variables: i
Python program:
def maximizeval10 ( a , n , k ) :
    increments = 0
    ans = 0
    v = [ ]
    for i in range ( n ) :
        ans += ( a [ i ] // 10 )
        if ( a [ i ] == 1000 ) :
            continue
        else :
            v . append ( 10 - a [ i ] % 10 )
            increments += ( 100 - ( ( a [ i ] ) // 10 ) - 1 ) ;
    v . sort ( )
    sum = 0
    for i in range ( len ( v ) ) :
        sum += v [ i ]
        if ( sum <= k ) :
            ans += 1
        else :
            break
    if ( sum < k ) :
        remaining = k - sum
        ans += min ( increments , remaining // 10 )
    print ( ans )
if __name__ == " _ _ main _ _ " :
    N = 4
    X = 6
    A = [ 4 , 8 , 8 , 8 ]
    maximizeval10 ( A , N , X )

Index/key variables: i
Accumulator variables: ans, increments, sum, v
Iterator variables: i
Python program:
def partition ( arr , l , h ) :
    pivot = arr [ l ]
    i = l + 1
    j = h
    while ( i <= j ) :
        while ( i <= h and arr [ i ] < pivot ) :
            i += 1
        while ( j > l and arr [ j ] > pivot ) :
            j -= 1
        if ( i < j ) :
            temp = arr [ i ]
            arr [ i ] = arr [ j ]
            arr [ j ] = temp
            i += 1
            j -= 1
        else :
            i += 1
    arr [ l ] = arr [ j ]
    arr [ j ] = pivot
    return j
def sortArray ( arr , l , h ) :
    if ( l >= h ) :
        return
    pivot = partition ( arr , l , h )
    sortArray ( arr , l , pivot - 1 )
    sortArray ( arr , pivot + 1 , h )
def findMaxIntervals ( start , end , n , R ) :
    ans = 0
    prev = 0
    currActive = 0
    i = 0
    j = 0
    if ( start [ 0 ] > 0 ) :
        ans += 1
    while ( i < n and j < n ) :
        if ( start [ i ] < end [ j ] ) :
            i += 1
            currActive += 1
        elif ( start [ i ] > end [ j ] ) :
            j += 1
            currActive -= 1
        else :
            i += 1
            j += 1
        if ( currActive == 0 ) :
            ans += 1
    if ( end [ n - 1 ] < R ) :
        ans += 1
    return ans
if __name__ == ' _ _ main _ _ ' :
    R = 10
    N = 3
    start = [ 2 , 5 , 8 ]
    end = [ 3 , 9 , 10 ]
    sortArray ( start , 0 , N - 1 )
    sortArray ( end , 0 , N - 1 )
    print ( findMaxIntervals ( start , end , N , R ) )

Index/key variables: i, j, l
Accumulator variables: ans, currActive
Python program:
def number ( a , n , p , k ) :
    a . sort ( )
    pre = [ ]
    for i in range ( n ) :
        pre . append ( 0 )
    ans = 0
    val = 0
    i = 0
    j = 0
    pre [ 0 ] = a [ 0 ]
    if pre [ 0 ] <= p :
        ans = 1
    for i in range ( 1 , k - 1 ) :
        pre [ i ] = pre [ i - 1 ] + a [ i ]
        if pre [ i ] <= p :
            ans = i + 1
    pre [ k - 1 ] = a [ k - 1 ]
    for i in range ( k - 1 , n ) :
        if i >= k :
            pre [ i ] += pre [ i - k ] + a [ i ]
        if pre [ i ] <= p :
            ans = i + 1
    return ans
n = 5
arr = [ 2 , 4 , 3 , 5 , 7 ]
p = 11
k = 2
print ( number ( arr , n , p , k ) )

Index/key variables: i
Accumulator variables: pre
Iterator variables: i
Python program:
def maxProfit ( arr ) :
    arr . sort ( reverse = True )
    ans = 0
    for i in range ( len ( arr ) ) :
        if ( arr [ i ] - ( 1 * i ) ) > 0 :
            ans += ( arr [ i ] - ( 1 * i ) )
        if ( arr [ i ] - ( 1 * i ) ) == 0 :
            break
    return ans
if __name__ == " _ _ main _ _ " :
    arr = [ 6 , 6 , 6 ]
    print ( maxProfit ( arr ) )

Index/key variables: i
Accumulator variables: ans
Iterator variables: i
Python program:
def check_vector ( A , n , p ) :
    pos = [ 0 for i in range ( len ( A ) ) ]
    for i in range ( len ( p ) ) :
        pos [ p [ i ] - 1 ] = 1
    flag = 1
    for i in range ( n ) :
        if ( pos [ i ] == 0 ) :
            continue
        j = i
        while ( j < n and pos [ j ] ) :
            j += 1
        p = A [ : i ]
        q = A [ i : i + j + 1 ]
        r = A [ i + j + 1 : len ( A ) ]
        q . sort ( reverse = False )
        A = p + q + r
        i = j
    for i in range ( n - 1 ) :
        if ( A [ i ] > A [ i + 1 ] ) :
            flag = 0
            break
    if ( flag == 1 ) :
        print ( " Yes " )
    else :
        print ( " No " ) ;
if __name__ == ' _ _ main _ _ ' :
    A = [ 3 , 2 , 1 ]
    p = [ 1 , 2 ]
    check_vector ( A , len ( A ) , p )

Index/key variables: i, j
Iterator variables: i
Python program:
from functools import cmp_to_key
mp = { }
def comp ( a , b ) :
    for i in range ( min ( len ( a ) , len ( b ) ) ) :
        if ( mp [ a [ i ] ] != mp [ b [ i ] ] ) :
            if mp [ a [ i ] ] < mp [ b [ i ] ] :
                return - 1
            else :
                return 1
    if ( len ( a ) < len ( b ) ) :
        return - 1
    else :
        return 1
def printSorted ( words , order ) :
    for i in range ( len ( order ) ) :
        mp [ order [ i ] ] = i
    words = sorted ( words , key = cmp_to_key ( comp ) )
    for x in words :
        print ( x , end = " ▁ " )
words = [ " word " , " world " , " row " ]
order = " worldabcefghijkmnpqstuvxyz "
printSorted ( words , order )

Index/key variables: i
Iterator variables: i, x
Python program:
def sortDiff ( arr , n ) :
    arr . sort ( )
    out = [ ]
    while n :
        out . append ( arr . pop ( n // 2 ) )
        n = n - 1
    print ( * out )
    return out
if __name__ == " _ _ main _ _ " :
    arr = [ 8 , 1 , 2 , 3 , 0 ]
    n = 5
    sortDiff ( arr , n )

Accumulator variables: out
Python program:
def minimumCost ( arr , N , M ) :
    arr . sort ( )
    pref = [ ]
    pref . append ( arr [ 0 ] )
    for i in range ( 1 , N ) :
        pref . append ( arr [ i ] + pref [ i - 1 ] )
    for i in range ( M , N ) :
        pref [ i ] += pref [ i - M ]
    for i in range ( N ) :
        print ( pref [ i ] , end = ' ▁ ' )
arr = [ 6 , 19 , 3 , 4 , 4 , 2 , 6 , 7 , 8 ]
M = 2
N = len ( arr )
minimumCost ( arr , N , M ) ;

Index/key variables: i
Accumulator variables: pref
Iterator variables: i
Python program:
one = [ " " , " one ▁ " , " two ▁ " , " three ▁ " , " four ▁ " , " five ▁ " , " six ▁ " , " seven ▁ " , " eight ▁ " , " nine ▁ " , " ten ▁ " , " eleven ▁ " , " twelve ▁ " , " thirteen ▁ " , " fourteen ▁ " , " fifteen ▁ " , " sixteen ▁ " , " seventeen ▁ " , " eighteen ▁ " , " nineteen ▁ " ]
ten = [ " " , " " , " twenty ▁ " , " thirty ▁ " , " forty ▁ " , " fifty ▁ " , " sixty ▁ " , " seventy ▁ " , " eighty ▁ " , " ninety ▁ " ]
def numToWords ( n , s ) :
    st = " "
    if ( n > 19 ) :
        st += ten [ n // 10 ] + one [ n % 10 ]
    else :
        st += one [ n ]
    if ( n ) :
        st += s
    return st
def convertToWords ( n ) :
    out = " "
    out += numToWords ( ( n // 10000000 ) , " crore ▁ " )
    out += numToWords ( ( ( n // 100000 ) % 100 ) , " lakh ▁ " )
    out += numToWords ( ( ( n // 1000 ) % 100 ) , " thousand ▁ " )
    out += numToWords ( ( ( n // 100 ) % 10 ) , " hundred ▁ " )
    if ( n > 100 and n % 100 ) :
        out += " and ▁ "
    out += numToWords ( ( n % 100 ) , " " )
    return out
def sortArr ( arr , n ) :
    vp = [ ]
    for i in range ( n ) :
        vp . append ( ( convertToWords ( arr [ i ] ) , arr [ i ] ) ) ;
    vp . sort ( )
    for i in range ( len ( vp ) ) :
        print ( vp [ i ] [ 1 ] , end = " ▁ " )
if __name__ == " _ _ main _ _ " :
    arr = [ 12 , 10 , 102 , 31 , 15 ]
    n = len ( arr )
    sortArr ( arr , n )

Index/key variables: i, n
Accumulator variables: vp
Iterator variables: i