Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. '''
  2. Rotate a list N places to the left.
  3. Examples:
  4. rotate(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'], 3)
  5. ['d', 'e', 'f', 'g', 'h', 'a', 'b', 'c']
  6.  
  7. rotate(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'], -2)
  8. ['g', 'h', 'a', 'b', 'c', 'd', 'e', 'f']
  9. '''
  10.  
  11.  
  12. my_list = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'k']
  13. K = -11
  14.  
  15.  
  16. def rotate(my_list, n):
  17. if abs(n) > len(my_list):
  18. n = -(abs(n) // len(my_list))
  19. slice1 = my_list[:n]
  20. slice2 = my_list[n:]
  21. result = slice2 + slice1
  22. return result
  23.  
  24.  
  25. if __name__ == '__main__':
  26. for each in range(-5, 5, 2):
  27. result = rotate(my_list, each)
  28. print(f'N = {each}: {result}\n')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement