mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-02 06:46:18 -04:00
cleanup
This commit is contained in:
parent
4d9c04e61d
commit
1d476139a8
17 changed files with 250 additions and 1 deletions
32
real_interview_problems/combination.py
Normal file
32
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