-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path(Implementation)Absolute_Permutation.py
More file actions
44 lines (34 loc) · 1.02 KB
/
Copy path(Implementation)Absolute_Permutation.py
File metadata and controls
44 lines (34 loc) · 1.02 KB
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
#!/bin/python3
import math
import os
import random
import re
import sys
# Complete the absolutePermutation function below.
def absolutePermutation(n, k):
position = set([i for i in range(1, n + 1)])
new_permutation = []
for i in range(1, n + 1):
if ((k + i) in position) and ((i - k) in position):
new_permutation.append(min(k + i, i - k))
position.remove(min(k + i, i - k))
elif (k + i) in position:
new_permutation.append(k + i)
position.remove(i + k)
elif (i - k) in position:
new_permutation.append(i - k)
position.remove(i - k)
else:
return [-1]
return new_permutation
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
t = int(input())
for t_itr in range(t):
nk = input().split()
n = int(nk[0])
k = int(nk[1])
result = absolutePermutation(n, k)
fptr.write(' '.join(map(str, result)))
fptr.write('\n')
fptr.close()