Skip to content

Instantly share code, notes, and snippets.

@elais
Created October 8, 2014 21:03
Show Gist options
  • Save elais/4e3be7725f71ca67dbfb to your computer and use it in GitHub Desktop.
Save elais/4e3be7725f71ca67dbfb to your computer and use it in GitHub Desktop.
def maxNum(M, k):
x = [int(x) for x in str(M)]
y = k
z = sorted(x, reverse = True) # nondeterminism
def swap(i, j, l):
tmp = l[i]
l[i] = l[j]
l[j] = tmp
return l
def find_index(number, i, l):
num = number
for x in range(i, len(l)):
if l[x] == num:
return x
for i in xrange(y):
if x[i] < z[i]:
index = find_index(z[i], i, x)
x = swap(i, index, x)
X = int(''.join([str(i) for i in x]))
print X
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment