Return -1 if it's not possible to increase the number in this way. e.g. 11, 321
def next_bigger(num):
digits = list(map(int, str(num)))
best = [-1] * 10
for i in range(len(digits)-1, -1, -1):
val = digits[i]
if not best[val] == -1:
digits[i], digits[best[val]] = digits[best[val]], digits[i]
return int(''.join(map(str, digits)))
else:
best = [i if (best[j] == -1 or val <= digits[best[j]]) and val > j else best[j] for j in range(len(best))]
return -1
test.assert_equals(next_bigger(89), 98)
test.assert_equals(next_bigger(126), 162)
test.assert_equals(next_bigger(2713), 2731)
test.assert_equals(next_bigger(1693), 1963)