Skip to content

Instantly share code, notes, and snippets.

@xavierskip
Created August 6, 2020 10:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xavierskip/862b39bf9f0e29d528013ac78caef00c to your computer and use it in GitHub Desktop.
Save xavierskip/862b39bf9f0e29d528013ac78caef00c to your computer and use it in GitHub Desktop.
import itertools
import random
def fillfull(chars, width):
length = len(chars)
full = length ** width
for i in range(full):
one = ""
for j in range(width):
one += chars[i%length]
i //= length
yield one
def product(*args, repeat=1):
# product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
# product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
pools = [tuple(pool) for pool in args] * repeat
result = [[]]
for pool in pools:
result = [x+[y] for x in result for y in pool]
for prod in result:
yield tuple(prod)
def burn(chars, width):
result = set()
index = 0
large = len(chars) ** width
while True:
if index == large:
break
one = ""
for i in range(width):
one += random.choice(chars)
if one in result:
continue
else:
result.add(one)
index += 1
return result
import string
chars = string.ascii_letters+string.digits
width = 4
with open('dictory.txt','w') as f:
# for i in product(chars, repeat=width):
# f.write("%s\n" %''.join(i))
# f.writelines(["%s\n" %''.join(i) for i in product(chars, repeat=width)])
f.writelines(["%s\n" %''.join(i) for i in itertools.product(chars, repeat=width)])
# set1 = set([i for i in fillfull(chars, width)])
# set2 = set([''.join(i) for i in itertools.product(chars,repeat=width)])
# set3 = burn(chars, width)
# set4 = set([''.join(i) for i in product(chars, repeat=width)])
# print(set1)
# print(set2)
# print(set3)
# print(set4)
# print(set1 == set2)
# print(set2 == set3)
# print(set3 == set4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment