Python3.8 - How To get total combinations which can be made with min length and max length

265 views Asked by At

I am actually making a tool which takes minimum length & maximum length and then characters to use and then in the given criteria it writes every possible combination to a wordlist, but is there a way to get how many combinations can be made using Min Length, Max length & Characters

My Code is:

import itertools

min_len = int(input("Min Len: "))
max_len = int(input("Max Len: "))
characters = str(input("Characters To use: "))

for n in range(min_len, max_len + 1):
    for xs in itertools.product(characters, repeat=n):
        word = ''.join(xs)
        print(f"Writing {word}")

I Am On Python3.8, Linux

Thanks For Helping In Advance!

2

There are 2 answers

0
Frank Yellin On BEST ANSWER

Given w characters, you can create w**n strings of length n. So you're looking for w**min_length + ...... + w**max_length

0
SAIKAT On

This code will work...

from itertools import chain, combinations
min_len = int(input("Min Len: "))
max_len = int(input("Max Len: "))
characters = str(input("Characters To use: "))
combination = list(chain.from_iterable(combinations(characters, i) for i in range(min_len, max_len+1)))
for comb in combination:
    print("".join(comb))