Question: Please write in JAVA import static org.junit.Assert.*; import org.junit.After; import org.junit.Before; import org.junit.Test; import java.util.Random; import java.io.*; import java.util.*; import java.util.zip.CRC32; public class P2J6Test {
Please write in JAVA
| import static org.junit.Assert.*; | |
| import org.junit.After; | |
| import org.junit.Before; | |
| import org.junit.Test; | |
| import java.util.Random; | |
| import java.io.*; | |
| import java.util.*; | |
| import java.util.zip.CRC32; | |
| public class P2J6Test { | |
| private static final int SEED = 12345; | |
| @Test public void testSumOfDistinctCubes() { | |
| // Explicit test cases | |
| String b1 = "[4, 3]"; | |
| assertEquals(b1, P2J6.sumOfDistinctCubes(91).toString()); | |
| String b2 = "[5]"; | |
| assertEquals(b2, P2J6.sumOfDistinctCubes(125).toString()); | |
| String b3 = "[4, 3, 2]"; | |
| assertEquals(b3, P2J6.sumOfDistinctCubes(99).toString()); | |
| String b4 = "[7, 2]"; | |
| assertEquals(b4, P2J6.sumOfDistinctCubes(351).toString()); | |
| String b5 = "[11, 4]"; | |
| assertEquals(b5, P2J6.sumOfDistinctCubes(1395).toString()); | |
| String b6 = "[]"; | |
| assertEquals(b6, P2J6.sumOfDistinctCubes(2020).toString()); | |
| // Pseudorandom fuzz tester | |
| CRC32 check = new CRC32(); | |
| Random rng = new Random(SEED); | |
| int c = 1, step = 2, next = 10; | |
| while(c > 0) { | |
| List | |
| check.update(result.toString().getBytes()); | |
| c += rng.nextInt(step) + 1; | |
| if(c > next) { | |
| next = 2 * next; | |
| step = 2 * step; | |
| } | |
| } | |
| assertEquals(4219145223L, check.getValue()); | |
| } | |
| private String createString(String alphabet, Random rng, int n) { | |
| String result = ""; | |
| for(int i = 0; i | |
| result += alphabet.charAt(rng.nextInt(alphabet.length())); | |
| } | |
| return result; | |
| } | |
| } |
make sure the code can pass the test, will give up the vote. Thanks
In most of the mainstream computer science education at university level, teaching of recursion tends to nerf down this mighty blade to cut through exponential possibilities. Recursion is used mainly as a toy to simulate some linear loop to compute factorials or Fibonacci numbers. This gains nothing over the everyday alternative of ordinary for-loops, the way that any competent coder would have automatically done it anyway. This also tends to leave the students dazed and confused about the general usefulness of recursion, since from their point of view, recursion only ever seems to be used to solve toy problems in a needlessly mathematical and highfaluting manner. However, as ought to become amply evident somewhere in the third or fourth year, the power of recursion lies in its ability to branch to multiple directions one at a time, which allows a recursive method to explore a potentially exponentially large branching tree of possibilities and backtrack on failure to previous choice point until it either finds one branch that works, or alternatively collect the results from all of the working branches. In this spirit, this last transition lab uses branching recursions to solve two interesting combinatorial problems. So without any further ado, create a new class P2J6 to write the two methods in. public static List
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
