Skip to content

Instantly share code, notes, and snippets.

@rheyns
Created November 9, 2012 23:08
Show Gist options
  • Save rheyns/4048920 to your computer and use it in GitHub Desktop.
Save rheyns/4048920 to your computer and use it in GitHub Desktop.
// int [] = { 7, 0, 1, 3, 4, 0, 8, 0 };
// int [] = { 0, 0, 0, 7, 1, 3, 4, 8 }
def move_up(arr, startidx, endidx):
for i in range(endidx-1, startidx-1, -1): # i [endidx, endidx-1, ... startidx]
arr[i+1], arr[i] = (arr[i], arr[i+1])
input = [7, 0, 1, 3, 4, 0, 8, 0]
zeroidx = 0
for i in range(len(input)):
if input[i] == 0:
move_up(input, zeroidx, i)
zeroidx += 1
print input
[0, 0, 0, 7, 1, 3, 4, 8]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment