Skip to content

Instantly share code, notes, and snippets.

@juanitobanca
Last active March 14, 2017 20:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juanitobanca/cb363cd193866a4b958b3d63b8e26a1a to your computer and use it in GitHub Desktop.
Save juanitobanca/cb363cd193866a4b958b3d63b8e26a1a to your computer and use it in GitHub Desktop.
def lexico( w ):
x = y = -1
# Paso 1
for i in xrange(len(w)-2, -1, -1):
if ord(w[i]) < ord(w[i+1]):
x = i
break
if x == -1:
return None
# Paso 2
for i in xrange(len(w)-1, x-1, -1):
if ord(w[x]) < ord(w[i]):
y = i
break
if y == -1:
return None
# Paso 3
w[x], w[y] = w[y], w[x]
# Paso 4
p1 = w[:x+1]
p2 = w[x+1:]
p2.reverse()
p1 += p2
# Retornar nueva permutacion
return p1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment