Last active
March 14, 2017 20:02
-
-
Save juanitobanca/cb363cd193866a4b958b3d63b8e26a1a to your computer and use it in GitHub Desktop.
This file contains 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
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