0

I want to make something that takes in the len(char) and permutes every single combination from all letters + numbers.

So far, I got my code to do it, but in my case out of the 1679616 possible combinations (36 ** 4), only about 1061340 of them are non duplicates, (running it once), and I can't figure out how to re-loop it until it gets a total of 1679616 combinations.

I've tried changing the ran of the loop every time it gets a duplicate, but that makes the loop take up to 20hours, so I prefer not doing that. I've tried countless other ways, but all failed. Instead of using random, I want to eventually loop through each element, and find each individual combination, but I have also failed at this.

import random

elements = [A-Z, 0-9]ect
code1 = ""
perms = []
actualperms = []


for e in range(6718464):
    code1 += random.choice(elements)
    if len(code1) == 4:
    code = "VLSC" + code1.upper() + "FE12"
    perms.append(code)
    code1 = ''

for elm in perms:
    if elm not in actualperms:
        actualperms.append(elm)

with open('code.json', 'w') as outfile:
    json.dump(acutalcombos, outfile)

The output shows only 1100000 permutations, but I want the total 36**4. It also takes a long time for it to export as JSON, so If there is a faster way please tell me.

Also, I know my code is extremely inefficient, I started a couple of months ago

Majo_Jose
  • 744
  • 1
  • 10
  • 24

1 Answers1

1

Use itertools.product:

import json

from itertools import product
from string import ascii_lowercase, digits

result = list(product(ascii_lowercase + digits, repeat=4))

print(len(result) == 36 ** 4)

with open('permutations.txt', 'w') as f:
    json.dump(result, f)

Output:

True
gmds
  • 19,325
  • 4
  • 32
  • 58