All problems
Bitset Rotate
Problem
Rotate a bitset left or right by k positions.
bits is a list of 0s and 1s, k is the rotation distance, and direction is "left" or "right". Reduce k modulo the length first; an empty bitset stays empty.
Return the rotated list.
Hint
A left rotation by k moves the first k bits to the end; a right rotation does the reverse.
Test cases
case 1
in: [[1,0,1,1,0],2,"left"]
expected: [1,1,0,1,0]
case 2
in: [[1,0,1,1,0],2,"right"]
expected: [1,0,1,0,1]
case 3
in: [[],3,"left"]
expected: []
case 4
in: [[1,1,1],5,"right"]
expected: [1,1,1]
case 5
in: [[1,0],0,"left"]
expected: [1,0]
Starter codePython
def bitset_rotate(bits, k, direction):
# Your code here
pass5 test cases · run your code against them in the editor