Skip to content

Commit

Permalink
Uploading new problem
Browse files Browse the repository at this point in the history
  • Loading branch information
dombroks committed Dec 11, 2020
1 parent 5f91449 commit f864822
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Microsoft_Problems/Microsoft_Problem_09.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""
This problem was asked by Microsoft.
Given a number in the form of a list of digits, return all possible permutations.
For example, given [1,2,3], return [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]].
"""


def get_permutations(arr):
if len(arr) < 2:
return [arr]

permutations = list()
for i, num in enumerate(arr):
arr_cp = arr[:i] + arr[i + 1:]
child_perms = get_permutations(arr_cp)
for perm in child_perms:
permutations.append([num] + perm)

return permutations


assert get_permutations([1, 2, 3]) == \
[[1, 2, 3], [1, 3, 2], [2, 1, 3],
[2, 3, 1], [3, 1, 2], [3, 2, 1]]

0 comments on commit f864822

Please sign in to comment.