Number permutations in python iterative

前端 未结 3 610
囚心锁ツ
囚心锁ツ 2021-01-26 14:04

I need to generate permutations of digits, the number can be bigger than the digit count. For my current purpose I need to generate permutations of these digits 0, 1, 2

3条回答
  •  误落风尘
    2021-01-26 14:34

    While there are ways to do this using itertools etc, here is a way that is a bit different from what you would normally do.

    If you were to have a list of these permutations in order, what you would actually have is ternary numbers that represent their place in the list. e.g. list[4] is 11 which is 4 in ternary (3*1+1*1). So you could convert the index value that you want to test into ternary and that would produce the correct value.

    While python can do conversion from an integer to its form in that base (e.g. int("11",3) outputs 4) the reverse is not implicitly implemented. There are lots of implementations out there though. Here is a good one (modified for your case):

    def digit_to_char(digit):
        if digit < 10:
            return str(digit)
        return chr(ord('a') + digit - 10)
    
    def perm(number):
        (d, m) = divmod(number, 3)
        if d > 0:
            return perm(d) + digit_to_char(m)
        return digit_to_char(m)
    

    So if you wanted to find the 20th permutation, you could do perm(20), which would give you 202. So now you can just do a regular loop through the index values that you want. With no storage of big lists in memory.

    permutation = 0
    i = 0
    while len(str(permutation)) < 20:
        permutation = perm(i)
        do_test(permutation)
        i += 1
    

提交回复
热议问题