Question: Please write in JAVA import static org.junit.Assert.*; import org.junit.After; import org.junit.Before; import org.junit.Test; import java.math.BigInteger; import java.util.*; import java.util.zip.CRC32; public class ManhattanTest { @Test public
Please write in JAVA
| import static org.junit.Assert.*; | |
| import org.junit.After; | |
| import org.junit.Before; | |
| import org.junit.Test; | |
| import java.math.BigInteger; | |
| import java.util.*; | |
| import java.util.zip.CRC32; | |
| public class ManhattanTest { | |
| @Test public void testTotalArea() { | |
| /* Explicit test cases */ | |
| int[] s1 = {2, 6, 9, 12, 15}; | |
| int[] e1 = {3, 8, 10, 14, 20}; | |
| int[] h1 = {3, 3, 4, 3, 2}; | |
| assertEquals(29, Manhattan.totalArea(s1, e1, h1)); | |
| int[] s2 = {3, 7, 8, 17, 24, 26, 27}; | |
| int[] e2 = {5, 18, 18, 28, 37, 29, 41}; | |
| int[] h2 = {1, 3, 2, 2, 3, 2, 1}; | |
| assertEquals(90, Manhattan.totalArea(s2, e2, h2)); | |
| /* Pseudorandom fuzz tester */ | |
| CRC32 check = new CRC32(); | |
| Random rng = new Random(777); | |
| for(int i = 2; i | |
| int n = rng.nextInt(3 * i) + 1; | |
| int[] s = new int[n]; | |
| int[] e = new int[n]; | |
| int[] h = new int[n]; | |
| for(int j = 0; j | |
| s[j] = rng.nextInt(4 * n); | |
| } | |
| Arrays.sort(s); | |
| for(int j = 0; j | |
| int w = 1 + rng.nextInt(2 * n); | |
| e[j] = s[j] + w; | |
| if(j > 0 && s[j-1] == s[j]) { e[j] = Math.max(e[j], e[j-1] + 1); } | |
| h[j] = 1 + (j / w) + rng.nextInt(3); | |
| } | |
| int r = Manhattan.totalArea(s, e, h); | |
| check.update(r); | |
| } | |
| assertEquals(2174298203L, check.getValue()); | |
| } | |
| } |
make sure the code can pass the test, will give up the vote. Thanks
Lab 25: Manhattan Skyline JUnit: ManhattanTest.java Manhattan skyline problem is a well-known exercise in computational geometry, with room for many educational and interesting variations. Its setup makes for a good illustration of the sweep line algorithm used to efficiently solve this problem. This general technique of sweep line will then solve many other problems that take place on the two-dimensional plane. In the basic version of this problem, a number of tall buildings (perhaps built in the heyday of some midwestern city on a flat plain) are seen from a distance against the horizon far away. Our vantage point is located far enough so that each building is seen as a two-dimensional silhouette rectangle. From our point of view towards the horizon, each individual silhouette has starting and ending x- coordinates s and e that of course satisfy s events = new ArrayListoO; with these "enter" and "exit" events encoded so that the building b entering the view is encoded as -1-b, and the building b exiting is encoded as b-1. (We need to do this encoding of events into integers in this tricky way so that each even is encoded to something nonzero, because there is no way to tell zero and minus zero apart!) Add these 2n event encodings into events, and sort them with a custom Comparator
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
