Next highest int from digits: Difference between revisions

Content added Content deleted
Line 2,390: Line 2,390:
</pre>
</pre>


=={{header|Ruby}}==
<syntaxhighlight lang="ruby">def next_perm(ar)
ar = ar.dup
rev_ar = ar.reverse
(a, pivot), i = rev_ar.each_cons(2).with_index(1).detect{|(e1, e2),i| e1 > e2}
return [0] if i.nil?
suffix = ar[-i..]
min_dif = suffix.map{|el|el-pivot }.reject{|el|el <= 0}.min
ri = ar.rindex{|el| el == min_dif + pivot}
ar[-(i+1)], ar[ri] = ar[ri], ar[-(i+1)]
ar[-i..] = ar[-i..].reverse
ar
end

tests = [0, 9, 12, 21, 12453, 738440, 45072010, 95322020, 9589776899767587796600]
tests.each{|t| puts "%25d -> %d" % [t, next_perm(t.to_s.chars.map(&:to_i)).join]}
</syntaxhighlight>

{{out}}
<pre> 0 -> 0
9 -> 0
12 -> 21
21 -> 0
12453 -> 12534
738440 -> 740348
45072010 -> 45072100
95322020 -> 95322200
9589776899767587796600 -> 9589776899767587900667
</pre>
=={{header|Rust}}==
=={{header|Rust}}==
<syntaxhighlight lang="rust">fn next_permutation<T: PartialOrd>(array: &mut [T]) -> bool {
<syntaxhighlight lang="rust">fn next_permutation<T: PartialOrd>(array: &mut [T]) -> bool {