-2

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?

5
  • 1
    for i in range(10000), or use itertools combinations Commented Oct 30, 2021 at 22:29
  • That doesn't help, it just prints the number list 10000 times. I want to show all 4 digit combinations, sorry if I was not clear enough Commented Oct 30, 2021 at 22:30
  • 1
    did you actually try what I've suggested? i will become all the values between [0 - 9999] Commented Oct 30, 2021 at 22:31
  • 1
    to add to @Mahrkeenerh before printing convert the number to string and use .zfill(4) (for performance probably do it only if the number is below a 1000) Commented Oct 30, 2021 at 22:32
  • Can you please make an answer showing what the command should look like for me? Thank you! Commented Oct 30, 2021 at 22:35

2 Answers 2

3

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().

Sign up to request clarification or add additional context in comments.

Comments

0

You can also do something like this, if you want to change some information by the future :

import math

NUMBERS = [0,1,2,3,4,5,6,7,8,9]
DIGITS = 4
MAX_ITERS = int(math.pow(len(NUMBERS), DIGITS))

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

2 Comments

this sounds overly complicated for a simple issue.
also, len(NUMBERS) ** DIGITS works just fine, no need for math

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.