Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
deque and all solution in Clear category for Rotate Hole by artemrudenko
# migrated from python 2.7
from collections import deque
def rotate(state, pipe_numbers):
queue, out = deque(state), [] # https://docs.python.org/2/library/collections.html?highlight=deque#collections.deque
for i in range(len(state)):
if all(queue[idx] for idx in pipe_numbers): # https://docs.python.org/2/library/functions.html?highlight=all#all
out.append(i)
queue.rotate(1) # https://docs.python.org/2/library/collections.html#collections.deque.rotate
return out
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert rotate([1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1], [0, 1]) == [1, 8], "Example"
assert rotate([1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1], [0, 1, 2]) == [], "Mission impossible"
assert rotate([1, 0, 0, 0, 1, 1, 0, 1], [0, 4, 5]) == [0], "Don't touch it"
assert rotate([1, 0, 0, 0, 1, 1, 0, 1], [5, 4, 5]) == [0, 5], "Two cannonballs in the same pipe"
Sept. 17, 2014
Comments: