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
32
book/ebook_src/real_interview_problems/combination.py
Normal file
32
book/ebook_src/real_interview_problems/combination.py
Normal file
|
@ -0,0 +1,32 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
def combination(array):
|
||||
if len(array) < 2:
|
||||
return set(array)
|
||||
|
||||
result = set()
|
||||
for index, item in enumerate(array):
|
||||
new_array = array[:index] + array[index+1:]
|
||||
result.add(item)
|
||||
for perm in combination(new_array):
|
||||
new_item = ''.join(sorted(item + perm))
|
||||
result.add(new_item)
|
||||
|
||||
return result
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
array = ['a', 'b', 'c']
|
||||
result = set(['a', 'ac', 'ab', 'abc', 'bc', 'c', 'b'])
|
||||
assert(combination(array) == result)
|
||||
|
||||
array = ['']
|
||||
result = set([''])
|
||||
assert(combination(array) == result)
|
||||
|
||||
array = ['a']
|
||||
result = set(['a'])
|
||||
assert(combination(array) == result)
|
Loading…
Add table
Add a link
Reference in a new issue