mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-02 06:46:18 -04:00
🏣 Clean up for arxiv
This commit is contained in:
parent
1b969e7db3
commit
41756cb10c
280 changed files with 2 additions and 11 deletions
31
book/ebook_src/real_interview_problems/permutation.py
Normal file
31
book/ebook_src/real_interview_problems/permutation.py
Normal file
|
@ -0,0 +1,31 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
def permutation(array):
|
||||
if len(array) < 2:
|
||||
return [array]
|
||||
|
||||
result = []
|
||||
for index, letter in enumerate(array):
|
||||
new_array = array[:index] + array[index+1:]
|
||||
for perm in permutation(new_array):
|
||||
result.append(letter + perm)
|
||||
|
||||
return result
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
word = 'abc'
|
||||
result = ['abc', 'acb', 'bac', 'bca', 'cab', 'cba']
|
||||
assert(permutation(word) == result)
|
||||
|
||||
word = ''
|
||||
result = ['']
|
||||
assert(permutation(word) == result)
|
||||
|
||||
word = 'a'
|
||||
result = ['a']
|
||||
assert(permutation(word) == result)
|
Loading…
Add table
Add a link
Reference in a new issue