Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Take 'em Out, Put 'em Back solution in Clear category for Sort Except Zero by adamspj
from collections.abc import Iterable
def except_zero(items: list[int]) -> Iterable[int]:
zero_placements: list[int] = []
for index, value in enumerate(items): # Recording where zeros are placed
if value == 0:
zero_placements.append(index)
for index in zero_placements[-1::-1]: # Index in reverse, pop all the zeros
items.pop(index)
items.sort() # Sort non-zero ints
for index in zero_placements: # Put the zeros back in
items.insert(index, 0)
return items
Feb. 22, 2024
Comments: