Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Duplicate Zeros by mennadiego
from collections.abc import Iterable
def duplicate_zeros(donuts: list[int]) -> Iterable[int]:
"""
You are given list of integers (int).
Your task in this mission is to duplicate (..., 🍩, ... --> ..., 🍩, 🍩, ...) all zeros (think about donuts ;-P)
and return the result as any Iterable.
:param donuts: list[int] - input list of int
:return: any iterable - output with all zeros duplicated
"""
# Get indexes of zero in input list
zero_indexes = [i for i in range(len(donuts)) if donuts[i] == 0]
# Defining extra variable to increment index progress
extra = 1
# Loop for inserting 0 value in original list
for ind in zero_indexes:
donuts.insert(ind+extra, 0)
extra += 1
return donuts
print("Example:")
print(list(duplicate_zeros([1, 0, 2, 3, 0, 4, 5, 0])))
# These "asserts" are used for self-checking
assert list(duplicate_zeros([1, 0, 2, 3, 0, 4, 5, 0])) == [
1,
0,
0,
2,
3,
0,
0,
4,
5,
0,
0,
]
assert list(duplicate_zeros([0, 0, 0, 0])) == [0, 0, 0, 0, 0, 0, 0, 0]
assert list(duplicate_zeros([100, 10, 0, 101, 1000])) == [100, 10, 0, 0, 101, 1000]
print("The mission is done! Click 'Check Solution' to earn rewards!")
Aug. 25, 2023