Skip to content

Instantly share code, notes, and snippets.

@triclops200
Created June 21, 2013 19:26
Show Gist options
  • Save triclops200/5833665 to your computer and use it in GitHub Desktop.
Save triclops200/5833665 to your computer and use it in GitHub Desktop.
def swap_max(digits):
i = len(digits) - 1
while i > 0:
if digits[i] == 0:
i-= 1
else:
break
max_i = i
min_i = i
pot_i = i
z_i = -1
nz_i = i
i = len(digits) - 1
while i >= 0:
if digits[i] > digits[pot_i]:
max_i = i
min_i = pot_i
if digits[i] < digits[min_i] and digits[i] != 0:
pot_i = i
if digits[i] == 0 and z_i == -1:
z_i = i
if digits[i] != 0 and i > 0:
nz_i = i
i -= 1
if z_i != -1 and max_i != 0 and max_i < z_i:
min_i = z_i
i = nz_i
max_i = i
elif max_i == min_i and z_i != -1:
i = nz_i
if i < z_i:
min_i = z_i
max_i = i
v = digits[min_i]
digits[min_i] = digits[max_i]
digits[max_i] = v
return digits
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment