dhyuti-n/xlcost-variable-roles huggingface dataset:
Python program:
def findPair ( arr , n ) :
    if ( n < 2 ) :
        print ( " - 1" )
        return
    i = 0
    first = - 1
    second = - 1
    for i in range ( n ) :
        if ( arr [ i ] > first ) :
            second = first
            first = arr [ i ]
        elif ( arr [ i ] > second ) :
            second = arr [ i ]
    print ( " n ▁ = " , first , " and ▁ r ▁ = " , second )
arr = [ 0 , 2 , 3 , 4 , 1 , 6 , 8 , 9 ]
n = len ( arr )
findPair ( arr , n )

Index/key variables: i
Iterator variables: i
Python program:
def sum ( arr , n ) :
    sum = 0
    for i in arr :
        sum += i
    sum = sum * pow ( 2 , n - 1 )
    return sum
arr = [ 2 , 1 , 5 , 6 ]
n = len ( arr )
print ( sum ( arr , n ) )

Accumulator variables: sum
Iterator variables: i
Python program:
from math import sqrt
def triangle ( n ) :
    k = 2 * n - 2 ;
    ch = 1 ;
    for i in range ( n ) :
        for j in range ( k ) :
            print ( " ▁ " , end = " " ) ;
        k = k - 1 ;
        for j in range ( i + 1 ) :
            print ( ch , end = " ▁ " ) ;
            ch += 1 ;
        print ( )
def maxHeight ( n ) :
    ans = ( sqrt ( 1 + 8.0 * n ) - 1 ) // 2 ;
    return int ( ans ) ;
if __name__ == " _ _ main _ _ " :
    N = 9 ;
    triangle ( maxHeight ( N ) ) ;

Accumulator variables: ch
Iterator variables: i, j
Python program:
def no_of_ways ( a , n ) :
    count_0 = 0
    count_1 = 0
    for i in range ( 0 , n ) :
        if ( a [ i ] == 0 ) :
            count_0 += 1
        else :
            count_1 += 1
    if ( count_1 % 2 == 0 ) :
        return count_0
    else :
        return count_1
if __name__ == ' _ _ main _ _ ' :
    n = 4
    a1 = [ 1 , 1 , 0 , 0 ]
    print ( no_of_ways ( a1 , n ) )
    n = 5
    a2 = [ 1 , 1 , 1 , 0 , 0 ]
    print ( no_of_ways ( a2 , n ) )
    n = 5
    a3 = [ 1 , 1 , 0 , 0 , 0 ]
    print ( no_of_ways ( a3 , n ) )
    n = 6
    a4 = [ 1 , 1 , 1 , 0 , 0 , 0 ]
    print ( no_of_ways ( a4 , n ) )

Index/key variables: i
Accumulator variables: count_0, count_1
Iterator variables: i
Python program:
from math import log
def prevPowerof2 ( n ) :
    p = int ( log ( n ) )
    return pow ( 2 , p )
def nextPowerOf2 ( n ) :
    p = 1
    if ( n and ( n & ( n - 1 ) ) == 0 ) :
        return n
    while ( p < n ) :
        p <<= 1
    return p
def minDiff ( n ) :
    low = prevPowerof2 ( n )
    high = nextPowerOf2 ( n )
    return min ( n - low , high - n )
n = 6
print ( minDiff ( n ) )

Accumulator variables: p
Python program:
def maxInt ( string ) :
    string2 = " "
    for i in range ( 0 , len ( string ) ) :
        if ( string [ i ] < '5' ) :
            string2 += str ( ( ord ( '9' ) - ord ( string [ i ] ) ) )
        else :
            string2 += str ( string [ i ] )
    if ( string2 [ 0 ] == '0' ) :
        string2 [ 0 ] = '9'
    return string2
if __name__ == ' _ _ main _ _ ' :
    string = "42"
    print ( maxInt ( string ) )

Index/key variables: i
Accumulator variables: string2
Iterator variables: i
Python program:
def longest_gap ( N ) :
    distance = 0
    count = 0
    first_1 = - 1
    last_1 = - 1
    while ( N > 0 ) :
        count += 1
        r = N & 1
        if ( r == 1 ) :
            if first_1 == - 1 :
                first_1 = count
            else :
                first_1 = first_1
            last_1 = count
        N = N // 2
    if ( last_1 <= first_1 ) :
        return - 1
    else :
        distance = last_1 - first_1 - 1
        return distance
N = 131
print ( longest_gap ( N ) )
N = 8
print ( longest_gap ( N ) )
N = 17
print ( longest_gap ( N ) )
N = 33
print ( longest_gap ( N ) )

Accumulator variables: count
Python program:
MAX = 10
def isFactorion ( n ) :
    fact = [ 0 ] * MAX
    fact [ 0 ] = 1
    for i in range ( 1 , MAX ) :
        fact [ i ] = i * fact [ i - 1 ]
    org = n
    sum = 0
    while ( n > 0 ) :
        d = n % 10
        sum += fact [ d ]
        n = n // 10
    if ( sum == org ) :
        return True
    return False
n = 40585
if ( isFactorion ( n ) ) :
    print ( " Yes " )
else :
    print ( " No " )

Index/key variables: d, i
Accumulator variables: sum
Iterator variables: i
Python program:
TEN = 10
def updateFreq ( n , freq ) :
    while ( n ) :
        digit = n % TEN
        freq [ digit ] += 1
        n //= TEN
def areAnagrams ( a , b ) :
    freqA = [ 0 ] * TEN
    freqB = [ 0 ] * TEN
    updateFreq ( a , freqA )
    updateFreq ( b , freqB )
    for i in range ( TEN ) :
        if ( freqA [ i ] != freqB [ i ] ) :
            return False
    return True
a = 240
b = 204
if ( areAnagrams ( a , b ) ) :
    print ( " Yes " )
else :
    print ( " No " )

Index/key variables: digit, i
Accumulator variables: n
Iterator variables: i
Python program:
def getArray ( n ) :
    ans = [ ] ;
    p2 = 1 ;
    while ( n > 0 ) :
        if ( n & 1 ) :
            ans . append ( p2 ) ;
        n >>= 1 ;
        p2 *= 2 ;
    return ans ;
if __name__ == " _ _ main _ _ " :
    n = 15 ;
    ans = getArray ( n ) ;
    for i in ans :
        print ( i , end = " ▁ " ) ;

Accumulator variables: ans, n, p2
Iterator variables: i
Python program:
M = 3 ; N = 2 ;
def getIndex ( n , shuffle ) :
    for i in range ( 3 ) :
        if ( shuffle [ i ] [ 0 ] == n ) :
            n = shuffle [ i ] [ 1 ] ;
        elif ( shuffle [ i ] [ 1 ] == n ) :
            n = shuffle [ i ] [ 0 ] ;
    print ( n ) ;
if __name__ == " _ _ main _ _ " :
    n = 3 ;
    shuffle = [ [ 3 , 1 ] , [ 2 , 1 ] , [ 1 , 2 ] ] ;
    getIndex ( n , shuffle ) ;

Index/key variables: i
Iterator variables: i
Python program:
def fact ( n ) :
    res = 1
    for i in range ( 2 , n + 1 ) :
        res = res * i
    return res
def nCr ( n , r ) :
    return fact ( n ) // ( fact ( r ) * fact ( n - r ) )
def number_of_subsequences ( arr , k , n ) :
    s = 0
    m = dict ( )
    for i in arr :
        m [ i ] = m . get ( i , 0 ) + 1
    for j in m :
        s = s + nCr ( m [ j ] , k )
    return s
arr = [ 1 , 1 , 1 , 1 , 2 , 2 , 2 ]
k = 2
n = len ( arr )
print ( number_of_subsequences ( arr , k , n ) )

Index/key variables: i, j
Iterator variables: i, j
Python program:
def sumPairs ( arr , n ) :
    sum = 0 ;
    for i in range ( n ) :
        sum = sum + ( arr [ i ] * ( 2 * n ) ) ;
    return sum ;
if __name__ == " _ _ main _ _ " :
    arr = [ 1 , 2 , 3 ] ;
    n = len ( arr ) ;
    print ( sumPairs ( arr , n ) ) ;

Index/key variables: i
Iterator variables: i
Python program:
def minSum ( arr , n ) :
    sum = 0 ;
    arr . sort ( reverse = True )
    for i in range ( n ) :
        if ( i % 4 < 2 ) :
            sum += arr [ i ] ;
    return sum ;
if __name__ == " _ _ main _ _ " :
    arr = [ 1 , 1 , 10 , 2 , 2 , 2 , 1 ] ;
    n = len ( arr ) ;
    print ( minSum ( arr , n ) ) ;

Index/key variables: i
Accumulator variables: sum
Iterator variables: i
Python program:
def countTriplets ( A , B , C ) :
    ans = 0
    for i in range ( 1 , A + 1 ) :
        for j in range ( 1 , B + 1 ) :
            for k in range ( 1 , C + 1 ) :
                if ( i * k > j * j ) :
                    ans += 1
    return ans
A = 3
B = 2
C = 2
print ( countTriplets ( A , B , C ) )

Accumulator variables: ans
Iterator variables: i, j, k
Python program:
def getCount ( A , B2 , C ) :
    count = 0
    i = 1
    while ( i < A ) :
        j = ( B2 // i ) + 1
        if ( C >= j ) :
            count = count + C - j + 1
        if ( A >= j and C >= i ) :
            count = count + ( C - i + 1 ) * ( A - j + 1 )
        if ( A >= j ) :
            A = j - 1
        i += 1
    return count
def countTriplets ( A , B , C ) :
    ans = 0
    for i in range ( 1 , B + 1 ) :
        ans = ( ans + getCount ( A , i * i , C ) )
    return ans
A = 3
B = 2
C = 2
print ( countTriplets ( A , B , C ) )

Iterator variables: i
Python program:
from math import floor , sqrt , ceil
def getSum ( n ) :
    summ = 0
    k = ( n ) ** ( .5 )
    for i in range ( 1 , floor ( k ) + 1 ) :
        summ += floor ( n / i )
    summ *= 2
    summ -= pow ( floor ( k ) , 2 )
    return summ
n = 5
print ( getSum ( n ) )

Accumulator variables: summ
Iterator variables: i
Python program:
def countUnsetBits ( n ) :
    cnt = 0 ;
    for i in range ( 1 , n + 1 ) :
        temp = i ;
        while ( temp ) :
            if ( temp % 2 == 0 ) :
                cnt += 1 ;
            temp = temp // 2 ;
    return cnt ;
if __name__ == " _ _ main _ _ " :
    n = 5 ;
    print ( countUnsetBits ( n ) ) ;

Accumulator variables: cnt
Iterator variables: i
Python program:
def graphExists ( a ) :
    while True :
        a = sorted ( a , reverse = True )
        if a [ 0 ] == 0 and a [ len ( a ) - 1 ] == 0 :
            return True
        v = a [ 0 ]
        a = a [ 1 : ]
        if v > len ( a ) :
            return False
        for i in range ( v ) :
            a [ i ] -= 1
            if a [ i ] < 0 :
                return False
a = [ 3 , 3 , 3 , 3 ]
if ( graphExists ( a ) ) :
    print ( " Yes " )
else :
    print ( " No " )

Index/key variables: i
Iterator variables: i
Python program:
def negProdSubArr ( arr , n ) :
    positive = 1
    negative = 0
    for i in range ( n ) :
        if ( arr [ i ] > 0 ) :
            arr [ i ] = 1
        else :
            arr [ i ] = - 1
        if ( i > 0 ) :
            arr [ i ] *= arr [ i - 1 ]
        if ( arr [ i ] == 1 ) :
            positive += 1
        else :
            negative += 1
    return ( positive * negative )
arr = [ 5 , - 4 , - 3 , 2 , - 5 ]
n = len ( arr )
print ( negProdSubArr ( arr , n ) )

Index/key variables: i
Accumulator variables: negative, positive
Iterator variables: i
Python program:
def sum ( n ) :
    sum = ( n * ( n + 1 ) ) // 2
    return sum
def repeatedSum ( n , k ) :
    for i in range ( k ) :
        n = sum ( n )
    return n
n = 2
k = 2
print ( repeatedSum ( n , k ) )

Iterator variables: i
Python program:
def isDivisible ( n ) :
    temp = n
    sum = 0
    while ( n ) :
        digit = n % 10
        sum += digit
        n //= 10
    n = temp
    while ( n ) :
        digit = n % 10
        if ( sum % digit != 0 ) :
            return False
        n //= 10 ;
    return True
n = 123
if ( isDivisible ( n ) ) :
    print ( " Yes " )
else :
    print ( " No " )

Accumulator variables: n, sum
Python program:
def getResult ( n ) :
    st = str ( n )
    sum = 0
    length = len ( st )
    for i in st :
        sum = sum + int ( i )
    for i in st :
        if ( sum % int ( i ) != 0 ) :
            return ' No '
    return ' Yes '
n = 123
print ( getResult ( n ) )

Iterator variables: i
Python program:
N = 100005
lpf = [ 0 ] * N ;
def least_prime_factor ( ) :
    for i in range ( 2 , N ) :
        if ( not lpf [ i ] ) :
            for j in range ( i , N , i ) :
                if ( not lpf [ j ] ) :
                    lpf [ j ] = i ;
def Mobius ( n ) :
    mobius = [ 0 ] * N ;
    for i in range ( 1 , N ) :
        if ( i == 1 ) :
            mobius [ i ] = 1 ;
        else :
            if ( lpf [ i // lpf [ i ] ] == lpf [ i ] ) :
                mobius [ i ] = 0 ;
            else :
                mobius [ i ] = - 1 * mobius [ i // lpf [ i ] ] ;
    for i in range ( 1 , n + 1 ) :
        print ( mobius [ i ] , end = " ▁ " ) ;
if __name__ == " _ _ main _ _ " :
    n = 5 ;
    least_prime_factor ( ) ;
    Mobius ( n ) ;

Index/key variables: i, j
Iterator variables: i, j
Python program:
DIGITS = 4 ; MIN = 1000 ; MAX = 9999 ;
def getBest ( prev , cur ) :
    maximum = max ( MIN , prev ) ;
    for i in range ( maximum , MAX + 1 ) :
        cnt = 0 ;
        a = i ;
        b = cur ;
        for k in range ( DIGITS ) :
            if ( a % 10 != b % 10 ) :
                cnt += 1 ;
            a //= 10 ;
            b //= 10 ;
        if ( cnt <= 1 ) :
            return i ;
    return - 1 ;
def getList ( arr , n ) :
    myList = [ ] ;
    possible = True ;
    myList . append ( 0 ) ;
    for i in range ( n ) :
        cur = arr [ i ] ;
        myList . append ( getBest ( myList [ - 1 ] , cur ) ) ;
        if ( myList [ - 1 ] == - 1 ) :
            possible = False ;
            break ;
    if ( possible ) :
        for i in range ( 1 , len ( myList ) ) :
            print ( myList [ i ] , end = " ▁ " ) ;
    else :
        print ( " - 1" ) ;
if __name__ == " _ _ main _ _ " :
    arr = [ 1095 , 1094 , 1095 ] ;
    n = len ( arr ) ;
    getList ( arr , n ) ;

Index/key variables: i
Accumulator variables: a, b, cnt, myList
Iterator variables: i, k
Boolean variables: possible
Python program:
def countFactors ( n , p ) :
    pwr = 0 ;
    while ( n > 0 and n % p == 0 ) :
        n //= p ;
        pwr += 1 ;
    return pwr ;
def getCount ( l , r , p ) :
    cnt = 0 ;
    for i in range ( l , r + 1 ) :
        cnt += countFactors ( i , p ) ;
    return cnt ;
if __name__ == " _ _ main _ _ " :
    l = 2 ; r = 8 ; p = 2 ;
    print ( getCount ( l , r , p ) ) ;

Accumulator variables: cnt, n, pwr
Iterator variables: i
Python program:
def getCount ( l , r , p ) :
    cnt = 0 ;
    val = p ;
    while ( True ) :
        a = r // val ;
        b = ( l - 1 ) // val ;
        val *= p ;
        if ( a - b ) :
            cnt += ( a - b ) ;
        else :
            break ;
    return int ( cnt ) ;
l = 2 ;
r = 8 ;
p = 2 ;
print ( getCount ( l , r , p ) ) ;

Accumulator variables: cnt, val
Python program:
def topsyTurvy ( string ) :
    for i in range ( len ( string ) ) :
        if ( string [ i ] == '2' or string [ i ] == '4' or string [ i ] == '5' or string [ i ] == '6' or string [ i ] == '7' or string [ i ] == '9' ) :
            return False ;
    return True ;
if __name__ == " _ _ main _ _ " :
    string = "1234" ;
    if ( topsyTurvy ( string ) ) :
        print ( " Yes " ) ;
    else :
        print ( " No " ) ;

Index/key variables: i
Iterator variables: i
Python program:
def countSubSeq ( arr , n , k ) :
    count = 0 ;
    for i in range ( n ) :
        if ( arr [ i ] % k == 0 ) :
            count += 1 ;
    return ( 2 ** count - 1 ) ;
if __name__ == " _ _ main _ _ " :
    arr = [ 1 , 2 , 3 , 6 ] ;
    n = len ( arr ) ;
    k = 3 ;
    print ( countSubSeq ( arr , n , k ) ) ;

Index/key variables: i
Accumulator variables: count
Iterator variables: i
Python program:
MAX = 1000001
def countNum ( N , K ) :
    sumPF = [ 0 ] * MAX ;
    for i in range ( 2 , N ) :
        if ( sumPF [ i ] == 0 ) :
            for j in range ( i , N , i ) :
                sumPF [ j ] += i ;
    count = 0 ;
    for i in range ( 2 , N ) :
        if ( sumPF [ i ] == K ) :
            count += 1 ;
    return count ;
if __name__ == " _ _ main _ _ " :
    N = 20 ; K = 7 ;
    print ( countNum ( N , K ) ) ;

Index/key variables: i, j
Accumulator variables: count
Iterator variables: i, j
Python program:
from math import sqrt
MAX = 100000
prime = [ True ] * ( MAX + 1 )
def SieveOfEratosthenes ( ) :
    for p in range ( 2 , int ( sqrt ( MAX ) ) + 1 ) :
        if ( prime [ p ] == True ) :
            for i in range ( p * p , MAX + 1 , p ) :
                prime [ i ] = False ;
def smallestPrime ( d ) :
    l = 10 ** ( d - 1 ) ;
    r = ( 10 ** d ) - 1 ;
    for i in range ( l , r + 1 ) :
        if ( prime [ i ] ) :
            return i ;
    return - 1 ;
def largestPrime ( d ) :
    l = 10 ** ( d - 1 ) ;
    r = ( 10 ** d ) - 1 ;
    for i in range ( r , l , - 1 ) :
        if ( prime [ i ] ) :
            return i ;
    return - 1 ;
if __name__ == " _ _ main _ _ " :
    SieveOfEratosthenes ( ) ;
    queries = [ 2 , 5 ] ;
    q = len ( queries ) ;
    for i in range ( q ) :
        print ( smallestPrime ( queries [ i ] ) , " ▁ " , largestPrime ( queries [ i ] ) ) ;

Index/key variables: i, p
Iterator variables: i, p
Python program:
def printMatrix ( n , k ) :
    for i in range ( n ) :
        for j in range ( n ) :
            if ( i == j ) :
                print ( k , end = " ▁ " ) ;
            else :
                print ( "0" , end = " ▁ " ) ;
        print ( ) ;
if __name__ == " _ _ main _ _ " :
    n = 3 ; k = 7 ;
    printMatrix ( n , k ) ;

Iterator variables: i, j
Python program:
def findNthNumber ( A , B , N ) :
    sum = 0
    for i in range ( 2 , N ) :
        sum = A + B
        A = B
        B = sum
    return sum
if __name__ == ' _ _ main _ _ ' :
    A = 5
    B = 7
    N = 10
    print ( findNthNumber ( A , B , N ) )

Iterator variables: i
Python program:
def isPossible ( arr , p , n , m ) :
    sum = 0 ;
    for i in range ( p ) :
        sum += arr [ i ] ;
    if ( sum == ( n * m ) ) :
        return True ;
    return False ;
if __name__ == ' _ _ main _ _ ' :
    n = 3 ;
    m = 4 ;
    arr = [ 6 , 3 , 2 , 1 ] ;
    p = len ( arr ) ;
    if ( isPossible ( arr , p , n , m ) ) :
        print ( " Yes " ) ;
    else :
        print ( " No " ) ;

Index/key variables: i
Accumulator variables: sum
Iterator variables: i
Python program:
def sum ( n ) :
    sum = 0 ;
    while ( n ) :
        sum = sum + n % 10 ;
        n = n // 10 ;
    return sum ;
def firstN ( n ) :
    num = 19 ; cnt = 1 ;
    while ( cnt != n ) :
        if ( sum ( num ) == 10 ) :
            print ( num , end = " ▁ " ) ;
            cnt += 1 ;
        num += 9 ;
if __name__ == " _ _ main _ _ " :
    n = 10 ;
    firstN ( n ) ;

Accumulator variables: cnt, num
Python program:
def sumOfLastDig ( n , m ) :
    sum = 0 ;
    k = n // m ;
    arr = [ 0 ] * 10 ;
    for i in range ( 10 ) :
        arr [ i ] = m * ( i + 1 ) % 10 ;
        sum += arr [ i ] ;
    rem = k % 10 ;
    ans = ( k // 10 ) * sum ;
    for i in range ( rem ) :
        ans += arr [ i ] ;
    return ans ;
if __name__ == " _ _ main _ _ " :
    n = 100 ; m = 3 ;
    print ( sumOfLastDig ( n , m ) ) ;

Index/key variables: i
Accumulator variables: ans, sum
Iterator variables: i
Python program:
def countSum ( arr , n ) :
    NumberOfOdds = 0 ; NumberOfEvens = 0 ;
    for i in range ( n ) :
        if ( arr [ i ] & 1 ) :
            NumberOfOdds += 1 ;
    NumberOfEvens = n - NumberOfOdds ;
    NumberOfOddSubsequences = ( 1 << NumberOfEvens ) * ( 1 << ( NumberOfOdds - 1 ) ) ;
    NumberOfEvenSubsequences = ( 1 << n ) - 1 - NumberOfOddSubsequences ;
    return ( NumberOfEvenSubsequences , NumberOfOddSubsequences ) ;
if __name__ == " _ _ main _ _ " :
    arr = [ 1 , 2 , 2 , 3 ] ;
    n = len ( arr ) ;
    ans = countSum ( arr , n ) ;
    print ( " EvenSum ▁ = " , ans [ 0 ] , end = " ▁ " ) ;
    print ( " OddSum ▁ = " , ans [ 1 ] ) ;

Index/key variables: i
Accumulator variables: NumberOfOdds
Iterator variables: i
Python program:
import math
def isPrime ( n ) :
    if ( n <= 1 ) :
        return False
    if ( n <= 3 ) :
        return True
    if ( n % 2 == 0 or n % 3 == 0 ) :
        return False
    for i in range ( 5 , int ( math . sqrt ( n ) + 1 ) , 6 ) :
        if ( n % i == 0 or n % ( i + 2 ) == 0 ) :
            return False
    return True
def nextPrime ( N ) :
    if ( N <= 1 ) :
        return 2
    prime = N
    found = False
    while ( not found ) :
        prime = prime + 1
        if ( isPrime ( prime ) == True ) :
            found = True
    return prime
N = 3
print ( nextPrime ( N ) )

Iterator variables: i
Boolean variables: found
Python program:
def Survives ( n ) :
    i = 2
    while ( True ) :
        if ( i > n ) :
            return True ;
        if ( n % i == 0 ) :
            return False ;
        n -= n // i ;
        i += 1
if __name__ == " _ _ main _ _ " :
    n = 17 ;
    if ( Survives ( n ) ) :
        print ( " Yes " ) ;
    else :
        print ( " No " ) ;

Accumulator variables: i, n
Python program:
def find_Solution ( x , n ) :
    Sum = x
    e = 2
    o = 1
    p = 1
    for i in range ( 2 , n + 1 ) :
        p += 2
        Sum += ( o / e ) * ( pow ( x , p ) / p )
        o = o * ( o + 2 )
        e = e * ( e + 2 )
    print ( round ( Sum , 10 ) )
x = - 0.5
if ( abs ( x ) >= 1 ) :
    print ( " Invalid Input " )
n = 8
find_Solution ( x , n )

Accumulator variables: Sum, p
Iterator variables: i
Python program:
def findRemainder ( n ) :
    l = n % 10
    while ( n >= 10 ) :
        n //= 10
    f = n
    remainder = f % l
    print ( remainder )
n = 5223
findRemainder ( n )

Accumulator variables: n
Python program:
def TwentyoneMatchstick ( arr , N ) :
    for i in range ( N ) :
        print ( 5 - arr [ i ] , end = " ▁ " )
arr = [ 3 , 4 , 2 , 2 ]
N = len ( arr )
TwentyoneMatchstick ( arr , N )

Index/key variables: i
Iterator variables: i
Python program:
def expressDigit ( arr , n ) :
    min = 9
    index = 0
    temp = 0
    temp = arr [ 0 ]
    for i in range ( 1 , 10 ) :
        if ( arr [ i ] < min ) :
            min = arr [ i ]
            index = i
    if ( temp < min ) :
        print ( 1 , end = " " )
        for i in range ( 1 , temp + 1 ) :
            print ( 0 , end = " " )
    else :
        for i in range ( min ) :
            print ( index , end = " " )
        print ( index )
arr = [ 2 , 2 , 1 , 2 , 1 , 1 , 3 , 1 , 1 , 1 ]
N = 10
expressDigit ( arr , N )

Index/key variables: i
Iterator variables: i
Python program:
from math import pow
def countDigits ( num ) :
    cnt = 0
    while ( num > 0 ) :
        cnt += 1
        num //= 10
    return cnt
def sumFromStart ( num , n , rem ) :
    num //= pow ( 10 , rem )
    sum = 0
    while ( num > 0 ) :
        sum += ( num % 10 )
        num //= 10
    return sum
def sumFromEnd ( num , n ) :
    sum = 0
    for i in range ( n ) :
        sum += ( num % 10 )
        num //= 10
    return sum
def getAverage ( n , k , l ) :
    totalDigits = countDigits ( n )
    if ( totalDigits < ( k + l ) ) :
        return - 1
    sum1 = sumFromEnd ( n , l )
    sum2 = sumFromStart ( n , k , totalDigits - k )
    return ( sum1 + sum2 ) / ( k + l )
if __name__ == ' _ _ main _ _ ' :
    n = 123456
    k = 2
    l = 3
    print ( getAverage ( n , k , l ) )

Accumulator variables: cnt, num, sum
Iterator variables: i
Python program:
import math
def Wythoff ( mp , n , k ) :
    tau = ( math . sqrt ( 5 ) + 1 ) / 2
    t_n_k = 0
    if ( ( n in mp ) and ( k in mp [ n ] ) ) :
        return mp [ n ] [ k ] ;
    if ( k == - 1 ) :
        return n - 1 ;
    elif ( k == 0 ) :
        t_n_k = math . floor ( n * tau ) ;
    else :
        t_n_k = Wythoff ( mp , n , k - 1 ) + Wythoff ( mp , n , k - 2 )
    if n not in mp :
        mp [ n ] = dict ( )
    mp [ n ] [ k ] = t_n_k ;
    return int ( t_n_k )
def Wythoff_Array ( n ) :
    i = 0
    j = 0
    count = 0 ;
    mp = dict ( )
    while ( count < n ) :
        print ( Wythoff ( mp , i + 1 , j + 1 ) , end = ' ' )
        count += 1
        if ( count != n ) :
            print ( " , ▁ " , end = ' ' )
        i += 1
        j -= 1
        if ( j < 0 ) :
            j = i ;
            i = 0 ;
if __name__ == ' _ _ main _ _ ' :
    n = 15 ;
    Wythoff_Array ( n ) ;

Index/key variables: k, n
Accumulator variables: count, i, j
Python program:
import math
def is_Prime ( n ) :
    if ( n < 2 ) :
        return False ;
    for i in range ( 2 , int ( math . sqrt ( n ) ) + 1 ) :
        if ( n % i == 0 ) :
            return False ;
    return True ;
def set_primes ( mp , hash , c ) :
    count = 0 ;
    i = 2
    while ( count < c ) :
        if ( is_Prime ( i ) ) :
            if 0 not in mp :
                mp [ 0 ] = dict ( )
            mp [ 0 ] [ count ] = i ;
            count += 1
            if 0 not in hash :
                hash [ 0 ] = dict ( )
            hash [ 0 ] [ count - 1 ] = 1 ;
        i += 1
def Gilbreath ( mp , hash , n , k ) :
    if ( n in hash and k in hash [ n ] and hash [ n ] [ k ] != 0 ) :
        return mp [ n ] [ k ] ;
    ans = abs ( Gilbreath ( mp , hash , n - 1 , k + 1 ) - Gilbreath ( mp , hash , n - 1 , k ) ) ;
    if n not in mp :
        mp [ n ] = dict ( )
    mp [ n ] [ k ] = ans ;
    return ans ;
def solve ( n ) :
    i = 0
    j = 0
    count = 0 ;
    mp = dict ( )
    hash = dict ( )
    set_primes ( mp , hash , 100 ) ;
    while ( count < n ) :
        print ( Gilbreath ( mp , hash , i , j ) , end = ' , ▁ ' )
        count += 1
        i -= 1
        j += 1
        if ( i < 0 ) :
            i = j ;
            j = 0 ;
if __name__ == ' _ _ main _ _ ' :
    n = 15 ;
    solve ( n ) ;

Index/key variables: count, k, n
Accumulator variables: j
Iterator variables: i