algorithm - Python: all possible words (permutations) of fixed length in mini-alphabet -


let's have string so:

abcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()-_+={}[]\:;"'?/>.<,`~|€ 

this basicly list of characters on keyboard. how possible combinations for, let's say, "word" made of 8 of these chars? know there going millions of possibilities.

cheers!

difference between permutations , combinations

you either looking permutation or combination.

'abc' , 'bac' different permutations, same combination {a,b,c}.

permutations of 'abc': '', 'a', 'b', 'c', 'ab', 'ba', 'ac', 'ca', 'bc', 'cb', 'abc', 'acb', 'bac', 'bca', 'cab', 'cba'

combinations of 'abc': {}, {'a'}, {'b'}, {'c'}, {'a','b'}, {'b','c'}, {'a','c'}, {'a','b','c'}


in python

use from itertools import * (since functions there should in default namespace), or import itertools if you'd like.

if care permutations:

permutations(yourstring, 8) 

if care combinations :

combinations(yourstring, 8) 

in other languages

in other languages, there simple recursive or iterative algorithms generate these. see wikipedia or stackoverflow. e.g. http://en.wikipedia.org/wiki/permutation#systematic_generation_of_all_permutations


important note

do note number of permutations n!, example string have

  • (69 choose 8) = 8 billion combinations of length 8, , therefore...
  • (69 choose 8) * 8! ~= 3.37 × 10^14 permutations of length 8.

you'll run out of memory if storing every permutation. if don't (because you're reducing them), it'll take long time run, maybe somewhere between 1-10 days on modern computer.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -