Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def getCombos(chars, k):
- print("Start of getCombos('" + chars + "', " + str(k) + ")")
- if k == 0:
- # BASE CASE
- print("For getCombos('" + chars + "', " + str(k) + ") base case returns ['']")
- return [''] # If k asks for 0-combinations, return '' as the selection of zero letters from chars.
- elif chars == '':
- # BASE CASE - TODO - why does this *have* to come second?
- print("For getCombos('" + chars + "', " + str(k) + ') base case returns []')
- return [] # An empty set has no combinations for any k.
- # RECURSIVE CASE
- combinations = []
- head = chars[:1]
- tail = chars[1:]
- print("For getCombos('" + chars + "', " + str(k) + ") part 1, get combos of tail '" + tail + "'")
- tailCombos = getCombos(tail, k - 1)
- print("For getCombos('" + chars + "', " + str(k) + ') part 1, combos of tail are', tailCombos)
- print(' Adding head', head, 'to tail combos:')
- for combo in tailCombos:
- print(' New combination', head + combo)
- combinations.append(head + combo)
- print("For getCombos('" + chars + "', " + str(k) + ") part 2, include combos from getCombos('" + tail + "', " + str(k) + ')')
- combinations.extend(getCombos(tail, k))
- print("For getCombos('" + chars + "', " + str(k) + ') results are', combinations)
- return combinations
- #print('3-Combinations of "ABCD":')
- print('Results:', getCombos('ABC', 2))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement