[Python] Difference between passing an array in recursion vs passing a string in recursion...

Discussão em 'Python' iniciado por Stack, Março 31, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to understand the difference between these two recursion methods for listing the coin combinations:

    In the first method, I passed a string through recursion:

    def cc_list(amount, n, combination_string):
    if amount == 0:
    print("Combi", combination_string[:-2])
    return 1
    elif n == 0 or amount < 0:
    return 0
    else:
    return cc_list(amount-denomination(n), n, combination_string + str(denomination(n)) + " + ") + cc_list(amount, n-1, combination_string)

    def denomination(n):
    x = coins[n-1]
    return x

    cc_list(100,3,"")


    This seems to produce the results with no problem:

    Combi 50 + 50
    Combi 50 + 20 + 20 + 10
    Combi 50 + 20 + 10 + 10 + 10
    Combi 50 + 10 + 10 + 10 + 10 + 10
    Combi 20 + 20 + 20 + 20 + 20
    Combi 20 + 20 + 20 + 20 + 10 + 10
    Combi 20 + 20 + 20 + 10 + 10 + 10 + 10
    Combi 20 + 20 + 10 + 10 + 10 + 10 + 10 + 10
    Combi 20 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10
    Combi 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10


    Next, I tried passing an array to count the number of occurrences for each coin and print them out:

    def cc_list(amount, n, counters):
    if amount == 0:
    print("Combi", str(print_50(counters[2])), str(print_20(counters[1])), str(print_10(counters[0])))
    return 1
    elif n == 0 or amount < 0:
    return 0
    else:
    return cc_list(amount-denomination(n), n, update_counter(n, counters)) + cc_list(amount, n-1, counters)

    def update_counter(n, c):
    c[n-1] += 1
    return c

    cc_list(100, 3, [0,0,0])


    But this time I get the result below:

    Combi 50 + 50
    Combi 50 + 50 20 + 20 + 20 10
    Combi 50 + 50 20 + 20 + 20 10 + 10 + 10 + 10
    Combi 50 + 50 20 + 20 + 20 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10
    Combi 50 + 50 20 + 20 + 20 + 20 + 20 + 20 + 20 + 20 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10
    Combi 50 + 50 20 + 20 + 20 + 20 + 20 + 20 + 20 + 20 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10
    Combi 50 + 50 20 + 20 + 20 + 20 + 20 + 20 + 20 + 20 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10
    Combi 50 + 50 20 + 20 + 20 + 20 + 20 + 20 + 20 + 20 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10
    Combi 50 + 50 20 + 20 + 20 + 20 + 20 + 20 + 20 + 20 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10
    Combi 50 + 50 20 + 20 + 20 + 20 + 20 + 20 + 20 + 20 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10


    In the array result, my guess is that the array of counters is being passed on to the next recursion. However, the string in the first method doesn't seem to be passed on to the next recursion. I'm wondering why that's the case, would appreciate help with this.

    Continue reading...

Compartilhe esta Página