Search code examples
pythonlistnumbers

Python - displaying conic sequence with only 4 digit sequencing


I am writing a program to determine the conic sequence of a user's inputted data set, for example - [0000,1,2222,9999],

I was struggling with sequencing the conic sequence using only a 4 digit classification, instead of the typical 8/16 binary approaches.


I have tried this:

for t in permutations(numbers, 4):
print(''.join(t))

But it does not assign a unique value to the inputted data, and instead overrides previous ones.

How can I go about doing this?


Solution

  • Since your list only contains the numbers 0 through 9 and you're looping over that list, printing the content as you go, it will only print 0 through 9.

    Since all possible combinations (or rather permutations, because that's what you're asking about) of the normal decimal digits are just the numbers 0 through 9999, you could do this instead:

    for i in range(10000):
        print(i)
    

    See https://docs.python.org/3/library/functions.html#func-range for more on range().

    But that doesn't print numbers like '0' as '0000'. To do that (in Python 3, which is probably what you should be using):

    for i in range(10000):
        print(f"{i:04d}")
    

    See https://docs.python.org/3/reference/lexical_analysis.html#f-strings for more on f-strings.

    Of course, if you need permutations of something other than digits, you can't use this method. You'd do something like this instead:

    from itertools import permutations
    
    xs = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
    
    for t in permutations(xs, 4):
        print(''.join(t))
    

    See https://docs.python.org/3/library/itertools.html#itertools.permutations for more on permutations() and the difference with combinations().