-
Notifications
You must be signed in to change notification settings - Fork 0
/
ps4a.py
47 lines (31 loc) · 1.19 KB
/
ps4a.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# Problem Set 4A
# Name: neelima-j
# Time Spent: 0:50
def get_permutations(sequence):
'''
Enumerate all permutations of a given string
sequence (string): an arbitrary string to permute. Assume that it is a
non-empty string.
You MUST use recursion for this part. Non-recursive solutions will not be
accepted.
Returns: a list of all permutations of sequence
Example:
>>> get_permutations('abc')
['abc', 'acb', 'bac', 'bca', 'cab', 'cba']
Note: depending on your implementation, you may return the permutations in
a different order than what is listed here.
'''
if len(sequence) == 1:
return sequence
retList = []
for char in sequence:
new_string = ''.join(sequence.split(char))
for item in get_permutations(new_string):
retList.append(char+item)
return retList
if __name__ == '__main__':
# #EXAMPLE
example_input = 'abc'
print('Input:', example_input)
print('Expected Output:', ['abc', 'acb', 'bac', 'bca', 'cab', 'cba'])
print('Actual Output:', get_permutations(example_input))