Next highest int from digits: Difference between revisions
Content deleted Content added
Alpha bravo (talk | contribs) Added AutoHotkey |
Alpha bravo (talk | contribs) Added AutoHotkey |
||
Line 118: | Line 118: | ||
45072100 |
45072100 |
||
95322200</pre> |
95322200</pre> |
||
===Scanning Version=== |
|||
<lang AutoHotkey>Next_highest_int(num){ |
|||
Loop % StrLen(num){ |
|||
i := A_Index |
|||
if (left := SubStr(num, 0-i, 1)) < (right := SubStr(num, 1-i, 1)) |
|||
break |
|||
} |
|||
if !(left < right) |
|||
return 0 |
|||
x := StrLen(num) - i |
|||
num := swap(num, x, x+1) |
|||
Rdigits := rSort(SubStr(num, 1-i)) |
|||
return SubStr(num,1, StrLen(num)-StrLen(Rdigits)) . Rdigits |
|||
} |
|||
swap(str, l, i){ |
|||
x := StrSplit(str), var := x[l], x[l] := x[i], x[i] := var |
|||
Loop, % x.count() |
|||
res .= x[A_Index] |
|||
return res |
|||
} |
|||
rSort(num){ |
|||
Arr := [] |
|||
for i, v in StrSplit(num) |
|||
Arr[v, i] := v |
|||
for i, obj in Arr |
|||
for k, v in obj |
|||
res .= v |
|||
return res |
|||
}</lang> |
|||
Examples:<lang AutoHotkey>MsgBox % "" Next_highest_int(0) |
|||
. "`n" Next_highest_int(9) |
|||
. "`n" Next_highest_int(12) |
|||
. "`n" Next_highest_int(21) |
|||
. "`n" Next_highest_int(12453) |
|||
. "`n" Next_highest_int(738440) |
|||
. "`n" Next_highest_int(45072010) |
|||
. "`n" Next_highest_int(95322020) |
|||
. "`n" Next_highest_int("9589776899767587796600") ; pass long numbers as text (between quotes)</lang> |
|||
{{out}} |
|||
<pre>0 |
|||
0 |
|||
21 |
|||
0 |
|||
12534 |
|||
780344 |
|||
45072100 |
|||
95322200 |
|||
9589776899767587900667</pre> |
|||
=={{header|C}}== |
=={{header|C}}== |