Python Pre 2.6 Permutation


/ Published in: Python
Save to your folder(s)



Copy this code and paste it in your HTML
  1. def swap(string, first, second, begin, end):
  2. string[begin] = second
  3. string[end] = first
  4.  
  5.  
  6. def permutate(string, begin = 0):
  7. end = len(string) -1
  8. srange = end - begin
  9. if srange == 1:
  10. print string
  11. else:
  12. for i in range(0, srange):
  13. swap(string, string[begin], string[begin+i], begin, begin+i)
  14. permutate(string, begin+1)
  15. swap(string, string[begin], string[begin+i], begin, begin+i)

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.