import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals; class SolutionTest { @Test void lengthIsntZeroTest() { assertEquals("length is zero", NamesAndNumbers.run(new int[1], new String[0])); assertNotEquals("length is zero", NamesAndNumbers.run(new int[1], new String[1])); } @Test void arrayIsntNullTest() { String[]arrStr = null; assertEquals("array is null", NamesAndNumbers.run(new int[1], arrStr)); assertNotEquals("array is null", NamesAndNumbers.run(new int[1], new String[1])); } @Test void syntaxTest() { int[] numbers = {10, 37, 23, 49, 42}; String[] names = {"Juan", "María", "Pedro", "Ana", "Luisa"}; int[] nums = {10}; String[] nams = {"Ana"}; assertEquals("Ana:10,Juan:23,María:37,Pedro:42,Luisa:49", NamesAndNumbers.run(numbers, names)); assertEquals("Ana:10", NamesAndNumbers.run(nums, nams)); } @Test void testLengthError() { String names[] = new String[] { "Ana", "Miguel", "Jose Manuel" }; int numbers[] = new int[] { 5, 20 }; assertEquals("The size of the arrays are not the same", NamesAndNumbers.run(numbers, names)); } }
- import org.junit.jupiter.api.Test;
- import static org.junit.jupiter.api.Assertions.assertEquals;
- import static org.junit.jupiter.api.Assertions.assertNotEquals;
- class SolutionTest {
- @Test
- void lengthIsntZeroTest() {
- assertEquals("length is zero", NamesAndNumbers.run(new int[1], new String[0]));
- assertNotEquals("length is zero", NamesAndNumbers.run(new int[1], new String[1]));
- }
- @Test
- void arrayIsntNullTest() {
- String[]arrStr = null;
- assertEquals("array is null", NamesAndNumbers.run(new int[1], arrStr));
- assertNotEquals("array is null", NamesAndNumbers.run(new int[1], new String[1]));
- }
- @Test
- void syntaxTest() {
- int[] numbers = {10, 37, 23, 49, 42};
- String[] names = {"Juan", "María", "Pedro", "Ana", "Luisa"};
- int[] nums = {10};
- String[] nams = {"Ana"};
- assertEquals("Ana:10,Juan:23,María:37,Pedro:42,Luisa:49", NamesAndNumbers.run(numbers, names));
- assertEquals("Ana:10", NamesAndNumbers.run(nums, nams));
- }
- @Test
- void testLengthError() {
- String names[] = new String[] { "Ana", "Miguel", "Jose Manuel" };
- int numbers[] = new int[] { 5, 20 };
- assertEquals("The size of the arrays are not the same", NamesAndNumbers.run(numbers, names));
- }
- }
import java.util.regex.*; import java.util.Arrays; import java.util.Comparator; public class NamesAndNumbers{ public static String run(int[]numbers, String[] names){ if(numbers == null || names == null) { return "array is null"; } else if(numbers.length == 0 || names.length == 0) { return "length is zero"; } Arrays.sort(names, Comparator.comparingInt(String::length)); Arrays.sort(numbers); StringBuilder cadenaValue = new StringBuilder(); for(int i = 0; i<names.length; i++) { cadenaValue.append(names[i]).append(":").append(numbers[i]).append(","); } cadenaValue.deleteCharAt(cadenaValue.length() - 1); String outputValue = cadenaValue.toString(); return outputValue; } }//end class.
- import java.util.regex.*;
- import java.util.Arrays;
- import java.util.Comparator;
- public class NamesAndNumbers{
- public static String run(int[]numbers, String[] names){
String outputValue = "error";- if(numbers == null || names == null) {
- return "array is null";
- } else if(numbers.length == 0 || names.length == 0) {
- return "length is zero";
- }
return outputValue;- Arrays.sort(names, Comparator.comparingInt(String::length));
- Arrays.sort(numbers);
- StringBuilder cadenaValue = new StringBuilder();
- for(int i = 0; i<names.length; i++) {
- cadenaValue.append(names[i]).append(":").append(numbers[i]).append(",");
- }
- cadenaValue.deleteCharAt(cadenaValue.length() - 1);
- String outputValue = cadenaValue.toString();
- return outputValue;
- }
- }//end class.
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals; class SolutionTest { @Test void lengthIsntZeroTest() { assertEquals("length is zero", NamesAndNumbers.run(new int[1], new String[0])); assertNotEquals("length is zero", NamesAndNumbers.run(new int[1], new String[1])); } @Test void arrayIsntNullTest() { String[]arrStr = null; assertEquals("array is null", NamesAndNumbers.run(new int[1], arrStr)); assertNotEquals("array is null", NamesAndNumbers.run(new int[1], new String[1])); } @Test void syntaxTest() { int[] numbers = {10, 37, 23, 49, 42}; String[] names = {"Juan", "María", "Pedro", "Ana", "Luisa"}; assertEquals("Ana:10,Juan:23,María:37,Pedro:42,Luisa:49", NamesAndNumbers.run(numbers, names)); } }
- import org.junit.jupiter.api.Test;
- import static org.junit.jupiter.api.Assertions.assertEquals;
- import static org.junit.jupiter.api.Assertions.assertNotEquals;
- class SolutionTest {
- @Test
- void lengthIsntZeroTest() {
- assertEquals("length is zero", NamesAndNumbers.run(new int[1], new String[0]));
- assertNotEquals("length is zero", NamesAndNumbers.run(new int[1], new String[1]));
- }
- @Test
- void arrayIsntNullTest() {
- String[]arrStr = null;
- assertEquals("array is null", NamesAndNumbers.run(new int[1], arrStr));
- assertNotEquals("array is null", NamesAndNumbers.run(new int[1], new String[1]));
- }
- @Test
- void syntaxTest() {
- int[] numbers = {10, 37, 23, 49, 42};
- String[] names = {"Juan", "María", "Pedro", "Ana", "Luisa"};
- assertEquals("Ana:10,Juan:23,María:37,Pedro:42,Luisa:49", NamesAndNumbers.run(numbers, names));
- }
- }