\nword = "AGB"\n\n# get a list of lists of the ranges represented by the word\ncharRange = []\nfor c in word:\n charList = []\n # The ord is necessary because range requires integers\n for x in range(ord('A'), ord(c) + 1):\n charList = charList + [chr(x)]\n\n print "Range for character %s: " % c, charList\n \n charRange = charRange + [charList]\n \nprint "List of ranges for individual characters: ", charRange\n\n# go through each range of characters and append each one to each character in the\n# next range of characters. This emulates the 'reduce' portion.\nresults = charRange[0]\nfor curRange in range(1, len(charRange)):\n cur = []\n for x in results:\n for y in charRange[curRange]:\n cur = cur + [x + y]\n results = cur\n print "Interim results: ", results\n \nprint "Final results: " , results\n
Output:
Range for character A: ['A']
Range for character G: ['A', 'B', 'C', 'D', 'E', 'F', 'G']
Range for character B: ['A', 'B']
List of ranges for individual characters: [['A'], ['A', 'B', 'C', 'D', 'E', 'F', 'G'], ['A', 'B']]
Interim results: ['AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG']
Interim results: ['AAA', 'AAB', 'ABA', 'ABB', 'ACA', 'ACB', 'ADA', 'ADB', 'AEA', 'AEB', 'AFA', 'AFB', 'AGA', 'AGB']
Final results: ['AAA', 'AAB', 'ABA', 'ABB', 'ACA', 'ACB', 'ADA', 'ADB', 'AEA', 'AEB', 'AFA', 'AFB', 'AGA', 'AGB']