Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Deque-Based Rotation solution in Clear category for Replace First by mmartsinenko
from collections import deque
from typing import Iterable
def replace_first(items: list) -> Iterable:
# Convert the list to a deque for efficient pops from the left
d = deque(items)
if len(d) > 1:
d.append(d.popleft()) # rotate leftmost element to the end
return list(d) # Convert back if you need a list
# These "asserts" are used for self-checking
print("Example:")
print(list(replace_first([1, 2, 3, 4])))
assert replace_first([1, 2, 3, 4]) == [2, 3, 4, 1]
assert replace_first([1]) == [1]
assert replace_first([]) == []
print("The mission is done! Click 'Check Solution' to earn rewards!")
Dec. 30, 2024
Comments: