Search code examples
pythongenerator

Python sequential number generator with 0-9 and A-Z


I'm trying to accomplish a number generator which increments numbers (0-9) and characters (A-Z not case-sensitive) for unique identification of some object in our software project. The output string should have a length of 14.

The generator should be able to create a range of sequential numbers/characters (see examples below) which will be used on potential hundres of devices running some python applications with databases. We would like to implement following logic and 'provide' these sequential number ranges to the devices so we make sure we have unique ids across all systems. We would prefer this logic instead of classic random id creation.

Increment should start from right to left. Numbers start with 0-9. If 9 is reached, the index should then increment from A-Z. If Z is reached, the left index number should be incremented from 0 to 1 or 9 => A-Z(one higher). The right index should then again be incremented by the same logic starting by 0 => 1-9 and afterwards A-Z.

A few example outputs:

  • 00000000000009
  • 0000000000000A
  • 0000000000000B

Or:

  • 0000000000000Z
  • 00000000000010
  • 00000000000011

And so on.

The function should also be able to start at a given number, for e.g. 00000000005X which then continues with 00000000005Y etc..

How can I build this in Python?


Solution

  • This program only uses the string module, and not itertools I also added constants called LENGTH and START which you can change

    Code:

    from string import ascii_uppercase
    
    sequence = [str(i) for i in range(10)] + list(ascii_uppercase)
    
    LENGTH = 14
    START = '0' * LENGTH
    
    last = list(START)
    while last != list('Z' * LENGTH):
        print(''.join(last))
        i = 1
        while last[-i] == 'Z':
            last[-i] = '0'
            i += 1
        last = last[:-i] + [sequence[sequence.index(last[-i]) + 1]] + last[LENGTH-i+1:]
    

    Output:

    00000000000000 
    00000000000001 
    00000000000002 
    00000000000003 
    00000000000004 
    00000000000005 
    00000000000006 
    00000000000007 
    00000000000008 
    00000000000009 
    0000000000000A 
    0000000000000B 
    0000000000000C 
    0000000000000D 
    0000000000000E 
    0000000000000F 
    0000000000000G 
    0000000000000H 
    0000000000000I 
    0000000000000J 
    0000000000000K 
    0000000000000L 
    0000000000000M 
    0000000000000N 
    0000000000000O 
    0000000000000P 
    0000000000000Q 
    0000000000000R 
    0000000000000S 
    0000000000000T 
    0000000000000U 
    0000000000000V 
    0000000000000W 
    0000000000000X 
    0000000000000Y 
    0000000000000Z 
    00000000000010 
    00000000000011 
    00000000000012 
    00000000000013 
    00000000000014 
    00000000000015 
    00000000000016 
    00000000000017 
    00000000000018 
    00000000000019 
    0000000000001A 
    0000000000001B 
    0000000000001C 
    0000000000001D 
    0000000000001E 
    0000000000001F 
    0000000000001G
    ...