Junior — Senior
Random permutation of array elements
livecode
Task condition
Given an array containing exactly ten integers. The task is to shuffle its elements in any order, similar to shuffling a deck of cards.
The solution must pass a check that verifies the following:
- After shuffling, the array is not empty.
- The size of the array remains the same.
- The array contains all the original values, but their order may change.
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
class ArraysTest {
private static final int[] INPUT_VALUES = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
@Test
void shouldNotRetrieveEmpty() {
assertThat(Arrays.shuffle(INPUT_VALUES)).isNotEmpty();
}
@Test
void shouldContainSameLength() {
assertThat(Arrays.shuffle(INPUT_VALUES).length).isEqualTo(10);
}
@Test
void shouldContainSameValuesInAnyOrder() {
assertThat(Arrays.shuffle(INPUT_VALUES)).containsExactlyInAnyOrder(INPUT_VALUES);
}
}
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
class ArraysTest {
private static final int[] INPUT_VALUES = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
@Test
void shouldNotRetrieveEmpty() {
assertThat(Arrays.shuffle(INPUT_VALUES)).isNotEmpty();
}
@Test
void shouldContainSameLength() {
assertThat(Arrays.shuffle(INPUT_VALUES).length).isEqualTo(10);
}
@Test
void shouldContainSameValuesInAnyOrder() {
assertThat(Arrays.shuffle(INPUT_VALUES)).containsExactlyInAnyOrder(INPUT_VALUES);
}
}