Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. import copy
  2.  
  3. def permute(l):
  4. """
  5. Return a list of permutations
  6.  
  7. Examples:
  8. permute([0, 1]) returns [ [0, 1], [1, 0] ]
  9.  
  10. Args:
  11. l(list): list of items to be permuted
  12.  
  13. Returns:
  14. list of permutation with each permuted item being represented by a list
  15. """
  16. if len(l) <= 1:
  17. return [l]
  18.  
  19. arrs = []
  20. item = l.pop()
  21.  
  22. arrs = permute(l)
  23.  
  24. res = []
  25.  
  26. for array_ in arrs:
  27. for i in range(len(array_)+1):
  28. s = copy.copy(array_)
  29. s.insert(i,item)
  30. res.append(s)
  31.  
  32. return res
  33.  
  34.  
  35. def check_output(output, expected_output):
  36. """
  37. Return True if output and expected_output
  38. contains the same lists, False otherwise.
  39.  
  40. Note that the ordering of the list is not important.
  41.  
  42. Examples:
  43. check_output([ [0, 1], [1, 0] ] ], [ [1, 0], [0, 1] ]) returns True
  44.  
  45. Args:
  46. output(list): list of list
  47. expected_output(list): list of list
  48.  
  49. Returns:
  50. bool
  51. """
  52. o = copy.deepcopy(output) # so that we don't mutate input
  53. e = copy.deepcopy(expected_output) # so that we don't mutate input
  54.  
  55. o.sort()
  56. e.sort()
  57. return o == e
  58.  
  59. print ("Pass" if (check_output(permute([]), [[]])) else "Fail")
  60. print ("Pass" if (check_output(permute([0]), [[0]])) else "Fail")
  61. print ("Pass" if (check_output(permute([0, 1]), [[0, 1], [1, 0]])) else "Fail")
  62. print ("Pass" if (check_output(permute([0, 1, 2]), [[0, 1, 2], [0, 2, 1], [1, 0, 2], [1, 2, 0], [2, 0, 1], [2, 1, 0]])) else "Fail")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement