-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path26.py
32 lines (28 loc) · 995 Bytes
/
26.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
def print_permutation(permutation):
print('(', end='')
print(*['+{}'.format(p) if p > 0 else str(p) for p in permutation], end='')
print(')')
def main():
permutation = input()[1:-1]
permutation = list(map(int, permutation.split()))
n = len(permutation)
for k in range(n):
for i in range(n):
if abs(permutation[i]) == k + 1:
break
if i == k and permutation[i] > 0:
continue
if i < k:
permutation[i : k + 1] = permutation[i : k + 1][::-1]
for j in range(i, k + 1):
permutation[j] = -permutation[j]
else:
permutation[k : i + 1] = permutation[k : i + 1][::-1]
for j in range(k, i + 1):
permutation[j] = -permutation[j]
print_permutation(permutation)
if permutation[k] < 0:
permutation[k] = -permutation[k]
print_permutation(permutation)
if __name__ == '__main__':
main()