I need to generate every possible combination from a given charset to a given range.
Like,

charset=list(map(str,"abcdefghijklmnopqrstuvwxyz"))
range=10

And the out put should be,

[a,b,c,d..................,zzzzzzzzzy,zzzzzzzzzz]

I know I can do this using already in use libraries.But I need to know how they really works.If anyone can give me a commented code of this kind of algorithm in Python or any programming language readable,I would be very grateful.

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

6

What have you tried? And is this homework? Shouldn't be super difficult...
– Silas RayJul 31 '12 at 19:11

13

list(map(str, "abc...")) is the most useless piece of code ever.
– JBernardoJul 31 '12 at 19:12

10 Answers
10

If you REALLY want to brute force it, try this, but it will take you a ridiculous amount of time:

your_list = 'abcdefghijklmnopqrstuvwxyz'
complete_list = []
for current in xrange(10):
a = [i for i in your_list]
for y in xrange(current):
a = [x+i for i in your_list for x in a]
complete_list = complete_list+a

On a smaller example, where list = 'ab' and we only go up to 5, this prints the following:

@Madushan: to be honest, the answer you picked is not code I'd show my teacher..
– Martijn Pieters♦Jul 31 '12 at 19:39

@Madushan: The meat of my method is the itertools.product method, which has a implementation listed in the documentation, so you can understand how we generate all possible combinations easily. What I prefer? When generating this amount of data, you really need to use generators like this.
– Martijn Pieters♦Jul 31 '12 at 20:00

This will iterate through all combinations of 'a','b','c' and 'd' and create combinations with a total length of 1 to 4. ie. a,b,c,d,aa,ab.........,dddc,dddd. generator is an itertool object and you can loop through normally like this,

for password in generator:
''.join(password)

Each password is infact of type tuple and you can work on them as you normally do.

This is not suitable for password creation since itertools skips possible outcomes. Example: look for all combinations between 'ab' for example which should be 4. aa, ba, ab and bb. Now itertools will only return 3 possibilites with combinations_with_replacement('ab', 2) such as: aa, ab, bb !
– Daniel BieglerOct 11 '18 at 2:28

You wanted a brute-force algorithm so I gave it to you. Now, it is a very long method when the password begins to be largest because it grows exponentially (it took 62 sec to find the password 'solved'). You could also use an already existing password-dictionnary or generate it with tolls like cupp (github)

import os
import sys
Zeichen=["a","b","c","d","e","f","g","h"­,"i","j","k","l","m","n","o","p","q­","r","s","­;t","u","v","w","x","y","z"]
def start(): input("Enter to start")
def Gen(stellen): if stellen==1: for i in Zeichen: print(i) elif stellen==2: for i in Zeichen: for r in Zeichen: print(i+r) elif stellen==3: for i in Zeichen: for r in Zeichen: for t in Zeichen: print(i+r+t) elif stellen==4: for i in Zeichen: for r in Zeichen: for t in Zeichen: for u in Zeichen: print(i+r+t+u) elif stellen==5: for i in Zeichen: for r in Zeichen: for t in Zeichen: for u in Zeichen: for o in Zeichen: print(i+r+t+u+o) else: print("done")
#*********************
start()
Gen(1)
Gen(2)
Gen(3)
Gen(4)
Gen(5)

Simple solution using the itertools and string modules

# modules to easily set characters and iterate over them
import itertools, string
# character limit so you don't run out of ram
maxChar = int(input('Character limit for password: '))
# file to save output to, so you can look over the output without using so much ram
output_file = open('insert filepath here', 'a+')
# this is the part that actually iterates over the valid characters, and stops at the
# character limit.
x = list(map(''.join, itertools.permutations(string.ascii_lowercase, maxChar)))
# writes the output of the above line to a file
output_file.write(str(x))
# saves the output to the file and closes it to preserve ram
output_file.close()

I piped the output to a file to save ram, and used the input function so you can set the character limit to something like "hiiworld". Below is the same script but with a more fluid character set using letters, numbers, symbols, and spaces.