Answered step by step
Verified Expert Solution
Question
1 Approved Answer
import org.junit.Rule; import org.junit.Test; import org.junit.rules.Timeout; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import java.util.Arrays; import java.util.Collection; import static org.junit.Assert.assertEquals; @RunWith(Parameterized.class) public class BilateralSwapsTest { @Rule public Timeout
import org.junit.Rule; import org.junit.Test; import org.junit.rules.Timeout; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import java.util.Arrays; import java.util.Collection; import static org.junit.Assert.assertEquals; @RunWith(Parameterized.class) public class BilateralSwapsTest { @Rule public Timeout globalTimeout = Timeout.seconds(1); // max 1 second per method private String[] a; private String[] b; private Integer[] swaps; public BilateralSwapsTest(String[][] input, Integer[] answer) throws IllegalArgumentException, InstantiationException { this.a = input[0]; this.b = input[1]; this.swaps = answer; } @Test public void test() { assertEquals(swaps, BilateralSwaps.getSwapSequence(a, b)); } @Parameterized.Parameters public static Collection testInstances() { String[][][] inputs = { {{"A_0", "A_1"}, {"B_0", "B_1"}}, {{"A_1", "A_0"}, {"B_0", "B_1"}}, {{"A_1", "A_0", "A_3", "A_2"}, {"B_1", "B_0", "B_3", "B_2"}}, {{"A_1", "A_2", "A_0"}, {"B_2", "B_0", "B_1"}}, {{"A_3", "A_2", "A_1", "A_0"}, {"B_1", "B_0"}} }; Integer[][] expOuts = { {}, // nothing to be done {0, 0, 1, 0, 0, 0}, {0, 0, 1, 1, 1, 0, 0, 1, 2, 2, 3, 3, 3, 2, 2, 3}, {0, 0, 0, 2, 1, 0, 2, 1, 2, 0, 0, 1}, {0, 0, 3, 1, 3, 0, 0, 1, 1, 0, 2, 0, 1, 0} }; Object[][] parameters = new Object[inputs.length][2]; for (int i = 0; i Exercise: Sort Your A sand BS ..., A_m-1 in some order. B is of You are given two arrays, A and B. A is of length m and contains strings A_0, A_1, length n and contains strings B_0, B_1, ..., B_n-1 in some order. You want to sort A and B so that the elements appear in each array in the ascending order (A[i] contains A_i and B[j] contains B_j). You are only permitted operations that swap an element in A with an element in B. Given an index i in A and an index j in B , you can swap the element in A[i] with the element in B[j] . You can use each (i,j) pair at most once except some particular (i,j) pair that can be used up to two times. Given the initial state of A and B , return an array that indicates the elements in A and B that need to be swapped at each step to obtain the sorted arrays. More specifically, if s swaps are needed, then the returned array contains 2s entries and entries 2k and 2k+1 are the indices of A and B , respectively, that are involved in swap number s (assuming that swaps are numbered 0 to 5-1). Exercise: Sort Your A sand BS ..., A_m-1 in some order. B is of You are given two arrays, A and B. A is of length m and contains strings A_0, A_1, length n and contains strings B_0, B_1, ..., B_n-1 in some order. You want to sort A and B so that the elements appear in each array in the ascending order (A[i] contains A_i and B[j] contains B_j). You are only permitted operations that swap an element in A with an element in B. Given an index i in A and an index j in B , you can swap the element in A[i] with the element in B[j] . You can use each (i,j) pair at most once except some particular (i,j) pair that can be used up to two times. Given the initial state of A and B , return an array that indicates the elements in A and B that need to be swapped at each step to obtain the sorted arrays. More specifically, if s swaps are needed, then the returned array contains 2s entries and entries 2k and 2k+1 are the indices of A and B , respectively, that are involved in swap number s (assuming that swaps are numbered 0 to 5-1)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started