-2

Objective : Create a word generator that will create every single word possible with the 52 letters of the alphabet up until 8 chars.

I know that basic is, the generator have to be able to make x(amount of baseline in this case the alphabet) to the power of 8 but i can't put that in the right amount of code, sample :

alphabet = "abcdefghijklmnopqrstuvwxyz"
or 
alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n",...] # u get the point

and for each element in the variable of array :

for i in alphabet:
      wordlist.append(something)

that will create something like :

a ab ac ad ... aba abc abd ... abcd ... zzzzzzzz

drd0sPy
  • 63
  • 9

3 Answers3

1

You can do this using recursion, I'll leave the base and recursive cases up to you other than to say that I recommend that your alphabet be stored as an array of characters or as an array of length-1 strings in order to make it easier for you to loop over it

const int MAX_LENGTH = 8
void myFunction(String currentString) {
  if(currentString.length == MAX_LENGTH) {
    // base case
  } else {
    // recursive case
  }
}
Zim-Zam O'Pootertoot
  • 17,888
  • 4
  • 41
  • 69
0

You can do that using itertools package.

import itertools
alphabet = "abcdefghijklmnopqrstuvwxyz"
l=list(alphabet)

# as a list 
genList= ["".join(i) for j in xrange(8) for i in itertools.combinations(l, j+1)]
print genList # to test
# as generator
("".join(i) for j in xrange(8) for i in itertools.combinations(l, j+1))

If you wanna print length of r

for item in itertools.combinations(input_list, r = 8):
            yield "".join(list(item)) 

Output: All the combinations

[ ... 'ab', 'ac', 'ad', 'ae', 'af', 'ag', 'ah', 'ai', 'aj', 'ak', 'al', 'am', 'an', 'ao', 'ap', 'aq', 'ar', 'as', 'at', 'au', 'av', 'aw', 'ax', 'ay', 'az', 'bc', ... ]

user3378649
  • 5,154
  • 14
  • 52
  • 76
  • Ye i was ... aware of itertools... looking for more of a manual approach but yes that is exactly the solution... just trying to get the basic of the solution in my mind. – drd0sPy Oct 19 '14 at 05:05
  • If you are looking for algorithmic approach, check Knuth book, or this blog-post that decribe the mentioned algorithm http://www.notesandreviews.com/programming/algorithm-to-generate-permutations-explained – user3378649 Oct 19 '14 at 05:11
  • Just a quick note ... do you know why i do get a error on the code above with this : yield is outside function :O – drd0sPy Oct 19 '14 at 05:17
  • @drd0sPy : You should check this heavy algorithmic answer as well http://stackoverflow.com/questions/1506078/fast-permutation-number-permutation-mapping-algorithms/1506337#1506337 – user3378649 Oct 23 '14 at 00:45
0

You can solve these easily using itertools, but here is a simple recursion example:

from string import ascii_lowercase,ascii_uppercase
letters = " "+ascii_lowercase+ascii_uppercase # 52 letters + blank space
wordlist = []

def GetCombinations(str):
    for i in xrange(0,53):
        word = str+letters[i]
        wordlist.append(word)
        if (len(word)<8):
            GetCombinations(word)

GetCombinations("")

This simply determines all combinations (of the alphabet + blank space) with the string you passed in, recursively. You can handle trimming the whitespace easily if you desire.

grovesNL
  • 6,016
  • 2
  • 20
  • 32