[Python] How do I make an index to combination script closed form in python?

Discussão em 'Python' iniciado por Stack, Julho 1, 2025.

  1. Stack

    Stack Membro Participativo

    I'm coding in python and I want to make my index to combination script closed form. There is currently a while loop in the script that is making the code lag.

    My code so far is:

    from math import floor

    def index_to_combination(index, min_length=40):
    iterable = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789`~!@#$%^&*()-_=+[{]}\\|;:'\",<.>/? "

    digits_str = str(iterable) # '123456789'
    base = len(digits_str)

    result = ''
    while True:
    q = floor(index / base)
    rem = index - base * q
    result = digits_str[rem] + result
    index = q
    if index == 0 and len(result) >= min_length:
    break

    return result


    print(index_to_combination(123456789))


    I want to make this closed form by removing the loop. Can someone show me how to do this?

    Continue reading...

Compartilhe esta Página