fork download
  1. def count_swaps(source, target):
  2. lookup = {val: idx for idx, val in enumerate(target)}
  3. source = [lookup[v] for v in source]
  4. print(lookup)
  5. print(source)
  6. swaps = 0
  7. for i in range(len(source)):
  8. while source[i] != i:
  9. swap_idx = source[i]
  10. print(swap_idx)
  11. source[i], source[swap_idx] = source[swap_idx], source[i]
  12. swaps += 1
  13. print(source)
  14. return swaps
  15.  
  16. print(count_swaps([1,2,3], [3,1,2]))
Success #stdin #stdout 0.02s 9608KB
stdin
Standard input is empty
stdout
{3: 0, 1: 1, 2: 2}
[1, 2, 0]
1
[2, 1, 0]
2
[0, 1, 2]
2