in reply to Next permutation
Just one simple? regex :)
#!/usr/bin/perl # https://perlmonks.org/?node_id=1230512 use strict; use warnings; $_ = 117; print "start at $_\n"; s/.*\K # find the last (.) # digit such that (.*) # there is a later (latest) (.)(??{$1 >= $3 and 'x'}) # digit greater than it (.*) # and get rest # swap those two digits ( $1 & $3 ) # then reverse everything after the first swapped digit / $3 . reverse $2.$1.$4 /xe; print " next is $_\n";
Outputs:
start at 117 next is 171
|
|---|