Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Rotate a list N places to the left.
- Examples:
- rotate(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'], 3)
- ['d', 'e', 'f', 'g', 'h', 'a', 'b', 'c']
- rotate(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'], -2)
- ['g', 'h', 'a', 'b', 'c', 'd', 'e', 'f']
- '''
- my_list = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'k']
- K = -11
- def rotate(my_list, n):
- if abs(n) > len(my_list):
- n = -(abs(n) // len(my_list))
- slice1 = my_list[:n]
- slice2 = my_list[n:]
- result = slice2 + slice1
- return result
- if __name__ == '__main__':
- for each in range(-5, 5, 2):
- result = rotate(my_list, each)
- print(f'N = {each}: {result}\n')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement