Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import copy
- def permute(l):
- """
- Return a list of permutations
- Examples:
- permute([0, 1]) returns [ [0, 1], [1, 0] ]
- Args:
- l(list): list of items to be permuted
- Returns:
- list of permutation with each permuted item being represented by a list
- """
- if len(l) <= 1:
- return [l]
- arrs = []
- item = l.pop()
- arrs = permute(l)
- res = []
- for array_ in arrs:
- for i in range(len(array_)+1):
- s = copy.copy(array_)
- s.insert(i,item)
- res.append(s)
- return res
- def check_output(output, expected_output):
- """
- Return True if output and expected_output
- contains the same lists, False otherwise.
- Note that the ordering of the list is not important.
- Examples:
- check_output([ [0, 1], [1, 0] ] ], [ [1, 0], [0, 1] ]) returns True
- Args:
- output(list): list of list
- expected_output(list): list of list
- Returns:
- bool
- """
- o = copy.deepcopy(output) # so that we don't mutate input
- e = copy.deepcopy(expected_output) # so that we don't mutate input
- o.sort()
- e.sort()
- return o == e
- print ("Pass" if (check_output(permute([]), [[]])) else "Fail")
- print ("Pass" if (check_output(permute([0]), [[0]])) else "Fail")
- print ("Pass" if (check_output(permute([0, 1]), [[0, 1], [1, 0]])) else "Fail")
- 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