Home > front end >  How do i find/write a String/code generator with prefix char and order (preferably in Python or Java
How do i find/write a String/code generator with prefix char and order (preferably in Python or Java

Time:12-07

I am trying to figure out how i can get all possible combinations of an 8 characters long code/string. But this code/string must meet the following two requirements:

  1. the first character is always the same.
  2. and the order of characters is also always the same.

To make it clear I will give an example.

(A)XX-000-XX

So (A) is static, the two consecutive capital letters are variable. The three numbers are also variable and the last two capital letters are also variable.

I have since found generators that can fix the (A). But I haven't been able to find the other requirement of the prefix order yet.

So I thought maybe I should write it myself. Can someone point me in the right direction? Thank you very much!

Kind regards, Manolo

CodePudding user response:

What about this? You can adjust the input for random.randiant to limit your character pool.

`strings = ["","",""]

#generate 3 random characters
for str in strings:
    random_integer = random.randint(0, MAX_LIMIT)
    # append random character to string
    str  = (chr(random_integer))

#Write string with .format
print("({m[0]}){m[1]}{m[1]}-{m[2]}{m[2]}-{m[1]}{m[1]}".format(m=strings)`

CodePudding user response:

This create all combinations. The idea is to create all combinations of uppercase for the XX and using digits for the 000 parts

from string import ascii_uppercase, digits 
from itertools import product

for a,b,c in product(product(ascii_uppercase,repeat=2), product(digits ,repeat=3), product(ascii_uppercase,repeat=2)):
   print(f"(A){''.join(a)}-{''.join(b)}-{''.join(c)}")

# sample: (A)AA-282-KV

hint

All combinations are: 26^4*10^3 = 456976000

  • Related