Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def getPerms(chars):
- print('Calling getPerms("' + chars + '")')
- if len(chars) == 1:
- # BASE CASE
- print('Base case, returning', chars)
- return [chars]
- # RECURSIVE CASE
- permutations = []
- head = chars[0]
- tail = chars[1:]
- tailPermutations = getPerms(tail)
- for tailPerm in tailPermutations:
- print('For', chars, 'putting head', head, 'in all places in', tailPerm)
- for i in range(len(tailPerm) + 1):
- newPerm = tailPerm[0:i] + head + tailPerm[i:]
- print('New permutation:', newPerm)
- permutations.append(newPerm)
- print('All permutations of', chars, 'are', permutations)
- return permutations
- print('Permutations of "ABC":')
- print('Result:', ','.join(getPerms('ABC')))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement