Given an array of strings and an array of ints, associate them from longest to shortest according to the length of each string. The output format must be a string with the syntax:
name1:number1,name2:number2,name3:number3... nameN:numberN
Input example:
String[]names = {"Ana", "Miguel", "Josemanuel"} int[]numbers = {1034, 21, 537}
Output example:
"JoseManuel:1034,Miguel:537,Ana:21"
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"; } if (numbers.length != names.length) { return "The size of the arrays are not the same"; } 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){
- if(numbers == null || names == null) {
- return "array is null";
- } else if(numbers.length == 0 || names.length == 0) {
- return "length is zero";
- }
- if (numbers.length != names.length) {
- return "The size of the arrays are not the same";
- }
- 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)); } @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"};
- assertEquals("Ana:10,Juan:23,María:37,Pedro:42,Luisa:49", NamesAndNumbers.run(numbers, names));
- }
- @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));
- }
- }
Given an array of strings and an array of ints, associate them from longest to shortest according to the length of each string. The output format must be a string with the syntax:
name1:number1,name2:number2,name3:number3... nameN:numberN
Input example:
String[]names = {"Ana", "Miguel", "Josemanuel"}
int[]numbers = {1034, 21, 537}
Output example:
"JoseManuel:1034,Miguel:537,Ana:21"
public class NamesAndNumbers{ public static String run(int[]numbers, String[] names){ if (numbers.length != names.length) { return "The size of the arrays are not the same"; } return ""; } }//end class.
- public class NamesAndNumbers{
- public static String run(int[]numbers, String[] names){
- if (numbers.length != names.length) {
- return "The size of the arrays are not the same";
- }
- return "";
- }
- }//end class.
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; // TODO: Replace examples and use TDD by writing your own tests class SolutionTest { @Test void testLength() { 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;
- // TODO: Replace examples and use TDD by writing your own tests
- class SolutionTest {
@Testvoid testSomething() {}- @Test
- void testLength() {
- 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));
- }
- }