Note: I am not an official teacher or GSI for any of the courses listed on this site unless otherwise stated. Any problems and notes are created from my own observations and may not accurately reflect the course's content.

Mutation #1

Implement the function zeroes_to_end, which takes in a list of numbers and returns the list with all of the zeroes placed at the end. All other numbers should stay in the order of the original list. The function should mutate the original list to accomplish this. You many NOT create any new lists in your solution.
Hint: int(True) is 1 and sum([ ]) is 0.

def zeroes_to_end(lst):
	""" Move all zeroes in a list to the end through mutation.

		   >>> zeroes_to_end([0, 1, 0, 0, 2, 0, 3, 0])
		   [1, 2, 3, 0, 0, 0, 0, 0]
		   >>> zeroes_to_end([ ])
		   []
		   >>> a = [1, 2, 0, 3, 0, 0, 4]
		   >>> zeroes_to_end(a)
		   [1, 2, 3, 4, 0, 0, 0]
		   >>> a
		   [1, 2, 3, 4, 0, 0, 0]
		   """
	i = 0
	while sum(____________________________):
		if _____________________________________ :
			lst.append(____________________________)
		i += ____________________________________
	return lst

Solution

Have a question? Found an error? Email me at imran.khaliq@berkeley.edu with the URL of the page and a short description.