Next Biggest Number Same Digits
<p>I don't see anything wrong with my code but I can't seem to return -1 when the input cannot produce a next bigger number, i.e. input of <code>531</code> which is descending. </p>

<pre><code>import itertools as it
def next_bigger(n):
    if sorted("531", reverse = True) == list("531"):
        return -1
    s = tuple(str(n))
    for x in it.dropwhile(lambda x: x &lt;= s, it.permutations(sorted(s))):
        return int(''.join(x))
    return s
</code></pre>

<p>Can someone please help?</p>

<p>You can simply use an <code>if</code> statement at the beginning of your function to test whether the number is already in reverse sorted order. If it is sorted <code>return -1</code> straight away:</p>

<pre><code>&gt;&gt;&gt; sorted("531", reverse = True) == list("531")
True
</code></pre>

python integer