The question is USACO
I currently have made a python code for this problem,
f = open(‘shuffle.in’)
n = int(f.readline())
swap = f.readline().strip().split()
order = f.readline().strip().split()
swap = [int(i) -1 for i in swap]
for x in range(n-1,0,-1):
p1 = order[x]
index = swap[x]
p2 = order[index]
order[x] = p2
order[index] = p1
with open(‘shuffle.out’, ‘w’) as fout:
for i in range(n):
fout.write(order[i]+’\n’)
However, when I run this program my final result is incorrect giving
1234567
2222222
5555555
3333333
4444444
When I solved the basic input by hand, I also got the result my code gave me.
The solution wanted involves switching around the third and second values. Could anyone tell me where I went wrong?