Advertisement
guillermo11bq

Untitled

May 20th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.17 KB | None | 0 0
  1. def minimum_bribes(queue: list):
  2.     #assuming starting with 1
  3.     # check if its minimum
  4.     # eval movimiento negativo
  5.    
  6.     length = len(queue)
  7.     bribes = 0
  8.     enum_list = list(enumerate(queue))
  9.    
  10.     for tuple_number in enum_list:
  11.         position = tuple_number[0] + 1
  12.        
  13.         # if current number is higher than next number I must evaluate the difference because it bribed
  14.         second_cond = True if position == length else (tuple_number[1] < enum_list[tuple_number[0] + 1][1])
  15.         if position > tuple_number[1] and second_cond:
  16.             continue
  17.        
  18.         dif = abs(position - tuple_number[1])
  19.        
  20.         if dif > 2:
  21.             return 'Too chaotic'
  22.        
  23.         bribes += dif
  24.        
  25.     return bribes
  26.    
  27. def main():
  28.     assert (minimum_bribes([2, 1, 5, 3, 4]) == 3)
  29.     assert (minimum_bribes([2,3,4,5,6,7,1]) == 6)
  30.     assert (minimum_bribes([2, 5, 1, 3, 4]) == 'Too chaotic')
  31.     assert (minimum_bribes([5, 1, 2, 3, 7, 8, 6, 4]) == 'Too chaotic')
  32.     assert (minimum_bribes([1, 2, 5, 3, 7, 8, 6, 4]) == 7)
  33.     assert (minimum_bribes([1, 2, 5, 3, 4, 7, 8, 6]) == 4)
  34.    
  35.  
  36. if __name__ == "__main__":
  37.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement