this is what I tried but it output different strings
import string
import random
def id_generator(size=6, chars=string.ascii_uppercase + string.digits):
return ''.join(random.choice("abcdefghijklmnopqrstuvwxyz") for _ in range(5))
print(id_generator())
For this problem randomizing things doesn't sound a good idea.
Because
We should take a look to real example of this kind of problems. Real examples has rules to make sure
I am living in Turkey and Turkish ID numbers has some (those of I am aware of) rules:
In the university that I work, every student is given a number. The rule:
Now coming to your question. The image you showed is just the permutation of a given list (alphabet).
For this you should understand the number of permutation grows fast. n!/(n-r)!
where n
is number of elements (in your example the alphabet) and r
is the length of string (in your example the ID).
Here Python's generators come to rescue.
We need a function to generate permutation. And if it give a state, it should start from there:
def nex_permutation(list_of_elements, r, start_from):
res = ""
if len(start_from) != r:
raise ValueError(f"start_from length and r are not equal")
carry = 1
for i in start_from[::-1]:
ind = list_of_elements.index(i) + carry
carry = ind // len(list_of_elements)
res += list_of_elements[ind % len(list_of_elements)]
return res[::-1]
Notice: with this code
start_from
contains only elements from list_of_elements
I leave these problems to you.
Now we can have a function using nex_permutation
to generate next one. Here We will use yield
instead of return
. Thus this function become a generator. And with generators you can calculate the next element as long as you want.
def permuataion(list_of_elements, r, start_from=None):
if start_from is None:
start_from = list_of_elements[0] * r
yield start_from
while True:
start_from = nex_permutation(list_of_elements, r, start_from=start_from)
yield start_from
Now let's to use it:
a = permuataion("abcd", 4)
for _ in range(3):
print(next(a))
output:
aaaa
aaab
aaac
Test if it continues:
a = permuataion("abcd", 4, start_from="aabd")
for _ in range(3):
print(next(a))
output:
aaca
aacb
aacc