Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def SortByGuide(A1, guide):
- res = [0] * len(A1) # just the resulting array
- guided = [0] * max(A1) # the number of elements to sort equals the largest number in guide
- for i in range(len(A1)):
- if guide[i] == -1: # if the element doesn't need to be sorted, put it in res right away
- res[i] = A1[i]
- else:
- guided[guide[i] - 1] = A1[i] # otherwise put it in corresponded position at guided
- cur = 0 # now just iterate over res and put elements from guided one by one
- for i in range(len(A1)):
- if res[i] == 0:
- res[i] = guided[cur]
- cur += 1
- return res
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement