Advertisement
Albinutte

Untitled

Mar 5th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.65 KB | None | 0 0
  1. def SortByGuide(A1, guide):
  2.     res = [0] * len(A1) # just the resulting array
  3.     guided = [0] * max(A1) # the number of elements to sort equals the largest number in guide
  4.     for i in range(len(A1)):
  5.         if guide[i] == -1: # if the element doesn't need to be sorted, put it in res right away
  6.             res[i] = A1[i]            
  7.         else:
  8.             guided[guide[i] - 1] = A1[i] # otherwise put it in corresponded position at guided
  9.     cur = 0 # now just iterate over res and put elements from guided one by one
  10.     for i in range(len(A1)):
  11.         if res[i] == 0:
  12.             res[i] = guided[cur]
  13.             cur += 1
  14.     return res
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement