Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
3 Annotated Answers solution in Clear category for Duplicate Zeros by BootzenKatzen
from collections.abc import Iterable
# Solution 1:
def duplicate_zeros(donuts: list) -> list:
s = [] # Creating a new list to add to
for x in donuts: # iterating through the current list
if x: # if there is a number (literally anything but 0,
# which python reads as False)
s.append(x) # add that number to the list
else: # if it IS a 0
s.extend([0, 0]) # extend the list with 2 0's (if we used append here you might end up
# with something like (1, 3, 6, [0, 0], 7)
# since append views everything in the parenthesis as one item
# I suppose alternatively you could do s.append(x) twice, but that's silly
return s # When you've gone through the list, return the new list
# Bonus solution 1:
def duplicate_zeros2(donuts: list[int]) -> Iterable[int]:
for don in donuts: # iterating through the list
yield from [don] * (2-bool(don)) # yeild is like return, in that it will return something from the code
# but it allows it to continue the loop until it's done.
# return would halt the code
# so for each item on the list, it will return
# item * (2-bool(don))
# bool will return True or False for whatever's in the parenthesis
# so like the first solution, if it's any number except 0 it will
# return True, and for 0s it will return False
# the reason we can subtract True/False from 2
# is that Python will read True as 1, and False as 0
# so if it's a number other than 0 it will be
# item * (2 - True (1)) or item * 1
# if it IS a 0 it would be item * (2 - False (0))
# or item * 2, so it will yeild 2 0s.
#Bonus solution 2:
def duplicate_zeros3(donuts: list[int]) -> Iterable[int]:
if not donuts: # if there's nothing on the input list
return [] # return an empty list
return [donuts[0]] + [0] * (donuts[0] == 0) + duplicate_zeros(donuts[1:])
# This can be tricky to understand at first, I know I struggled with recursive functions when I first saw one
# so let's walk through an example. Imagine our input list is [1, 0, 3]
# on the first pass, it will return [donuts[0]] which is 1, + [0] * (donuts [0] == [0])
# since donuts[0] is 1, it returns False, which Python sees as 0, so it won't append any extra 0s
# then we get to the recursive part - where it will run this function again - but only on donuts [1:]
# so it's excluding the number you just ran through, and looking at the rest of the list
# so now it's running the function on [0, 3]
# donuts[0] is now 0 - so it will return the first 0 AND [0] * True which python reads as one, so it will add
# the second 0, then it will add the result from running the function again on this shortened list[1:]
# so now we're running it on just [3], which we already know will return 3 without the bonus 0
# but it will run the function again on the rest of the list which is just [] - which will break us out of the cycle
# by returning [] to end the recursive function
# so it will have returned [1] + [0] + [0] + [3] for our final list of [1, 0, 0, 3]
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!")
Sept. 8, 2023
Comments: