Next highest int from digits: Difference between revisions
Content added Content deleted
Line 416: | Line 416: | ||
=={{header|Java}}== |
=={{header|Java}}== |
||
Additional testing is performed, including a number with all unique digits and a number with duplicate digits. Included test of all permutations, that the correct number of permutations is achieved, and that each |
Additional testing is performed, including a number with all unique digits and a number with duplicate digits. Included test of all permutations, that the order and correct number of permutations is achieved, and that each permutation is different than all others. If a library is not used, then this testing will provide a better proof of correctness. |
||
<lang java> |
<lang java> |
||
import java.math.BigInteger; |
import java.math.BigInteger; |
||
Line 422: | Line 422: | ||
import java.util.ArrayList; |
import java.util.ArrayList; |
||
import java.util.Collections; |
import java.util.Collections; |
||
import java.util.HashMap; |
|||
import java.util.List; |
import java.util.List; |
||
import java.util.Map; |
|||
public class NextHighestIntFromDigits { |
public class NextHighestIntFromDigits { |
||
Line 441: | Line 443: | ||
private static void testAll(String s) { |
private static void testAll(String s) { |
||
⚫ | |||
⚫ | |||
String sOrig = s; |
|||
⚫ | |||
String sPrev = s; |
String sPrev = s; |
||
⚫ | |||
// Check permutation order. Each is greater than the last |
|||
boolean orderOk = true; |
|||
Map <String,Integer> uniqueMap = new HashMap<>(); |
|||
uniqueMap.put(s, 1); |
|||
while ( (s = next(s)).compareTo("0") != 0 ) { |
while ( (s = next(s)).compareTo("0") != 0 ) { |
||
count++; |
count++; |
||
if ( Long.parseLong(s) < Long.parseLong(sPrev) ) { |
|||
orderOk = false; |
|||
} |
|||
uniqueMap.merge(s, 1, (v1, v2) -> v1 + v2); |
|||
sPrev = s; |
sPrev = s; |
||
} |
} |
||
System.out.printf(" Order: OK = %b%n", orderOk); |
|||
// Test last permutation |
|||
String reverse = new StringBuilder(sOrig).reverse().toString(); |
|||
System.out.printf(" Last permutation: Actual = %s, Expected = %s, OK = %b%n", sPrev, reverse, sPrev.compareTo(reverse) == 0); |
|||
// Check permutations unique |
|||
boolean unique = true; |
|||
for ( String key : uniqueMap.keySet() ) { |
|||
if ( uniqueMap.get(key) > 1 ) { |
|||
unique = false; |
|||
} |
|||
} |
|||
System.out.printf(" Permutations unique: OK = %b%n", unique); |
|||
// Check expected count. |
|||
Map<Character,Integer> charMap = new HashMap<>(); |
|||
for ( char c : sOrig.toCharArray() ) { |
|||
charMap.merge(c, 1, (v1, v2) -> v1 + v2); |
|||
} |
|||
long permCount = factorial(sOrig.length()); |
|||
for ( char c : charMap.keySet() ) { |
|||
permCount /= factorial(charMap.get(c)); |
|||
} |
|||
System.out.printf(" Permutation count: Actual = %d, Expected = %d, OK = %b%n", count, permCount, count == permCount); |
|||
} |
|||
private static long factorial(long n) { |
|||
long fact = 1; |
|||
for (long num = 2 ; num <= n ; num++ ) { |
|||
fact *= num; |
|||
} |
|||
return fact; |
|||
} |
} |
||
Line 496: | Line 542: | ||
return sb.toString(); |
return sb.toString(); |
||
} |
} |
||
} |
} |
||
</lang> |
</lang> |
||
Line 511: | Line 556: | ||
9,589,776,899,767,587,796,600 -> 9,589,776,899,767,587,900,667 |
9,589,776,899,767,587,796,600 -> 9,589,776,899,767,587,900,667 |
||
3,345,333 -> 3,353,334 |
3,345,333 -> 3,353,334 |
||
Test all |
Test all permutations of: 12345 |
||
Order: OK = true |
|||
1 12,345 |
|||
Last permutation: Actual = 54321, Expected = 54321, OK = true |
|||
2 12,354 OK |
|||
Permutations unique: OK = true |
|||
3 12,435 OK |
|||
Permutation count: Actual = 120, Expected = 120, OK = true |
|||
4 12,453 OK |
|||
⚫ | |||
5 12,534 OK |
|||
Order: OK = true |
|||
... output removed to save space ... |
|||
Last permutation: Actual = 22111, Expected = 22111, OK = true |
|||
119 54,312 OK |
|||
Permutations unique: OK = true |
|||
120 54,321 OK |
|||
Permutation count: Actual = 10, Expected = 10, OK = true |
|||
⚫ | |||
1 11,122 |
|||
2 11,212 OK |
|||
3 11,221 OK |
|||
4 12,112 OK |
|||
5 12,121 OK |
|||
6 12,211 OK |
|||
7 21,112 OK |
|||
8 21,121 OK |
|||
9 21,211 OK |
|||
10 22,111 OK |
|||
</pre> |
</pre> |
||