I have the following problem in my algorithms textbook:
Write a computer program that generates and counts how many words of 5 letters of the English alphabet can be formed, with the condition that no 2 consecutive letters can be consonants or vowels.
From the statement, I understand that the output should have this form
c means consonant and
v means vowel.
The way I approached this problem was to write a function that generates all the words with 5 letters given a permutation of the sets (since there are 2 available sets, consonants and vowels). There are only 2 permutations of the sets available:
vcvcv. And I call the function
generate_words for every permutation of the sets.
My question is: Is it a good practice to hardcode the permutation of the sets? Would have been a better solution to write a function that generates this information?
# We use this variable just o track the number of generated solutions. counter = 1 def _print_solution(set_master, solution): result = [set_master[set_index][element] for set_index, element in enumerate(solution)] global counter # ljust(6) because, in our case, the maximum number of words is 21 66 00 # which has 6 digits. print str(counter).ljust(6), ', '.join(map(str, result)) counter += 1 def _generate_words(set_master, solution, set_counter): if set_counter == len(set_master): _print_solution(set_master, solution) return for i in range(0, len(set_master[set_counter])): solution.append(i) _generate_words(set_master, solution, set_counter + 1) solution.pop() def generate_words(set_master): solution =  _generate_words(set_master, solution, 0) if __name__ == "__main__": vowels = ['a', 'e', 'i', 'o', 'u'] consonants = ['b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'x', 'z'] generate_words([vowels, consonants, vowels, consonants, vowels]) generate_words([consonants, vowels, consonants, vowels, consonants])