-
Notifications
You must be signed in to change notification settings - Fork 0
/
permutations.py
31 lines (27 loc) · 881 Bytes
/
permutations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
"""
Given a string, what permutations of the component strings are there?
"""
from itertools import permutations as perm
def permutations(s):
"""
>permutations('aab')
{'aab', 'aba', 'baa'}
>permutations('aaba')
{'aaab', 'aaba', 'abaa', 'baaa'}
>permutations('abc')
{'abc', 'acb', 'bac', 'bca', 'cab', 'cba'}
:param s: string
:return: set of permutations
"""
r = perm(s)
return set([''.join(i) for i in r])
import unittest
class TestFirst(unittest.TestCase):
def testFirst(self):
test = self
Test = self
test.assert_equals = Test.assertEqual
Test.assert_equals = Test.assertEqual
Test.assert_equals(sorted(permutations('a')), ['a']);
Test.assert_equals(sorted(permutations('ab')), ['ab', 'ba'])
Test.assert_equals(sorted(permutations('aabb')), ['aabb', 'abab', 'abba', 'baab', 'baba', 'bbaa'])