Created
November 9, 2012 23:08
-
-
Save rheyns/4048920 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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