Strings
Data Structures
import java.util.*; import org.junit.Test; import static org.junit.Assert.assertEquals; import org.junit.runners.JUnit4; public class SolutionTest { public static String generarLetra() { StringBuilder phrase = new StringBuilder(); for (int i = 0; i < 1; i++) { char letter = (char) (new Random().nextInt('Z' - 'A') + 'A'); phrase.append(new Random().nextBoolean() ? letter : Character.toLowerCase(letter)); } return phrase.toString(); } public static String generator() { StringBuilder sb = new StringBuilder(); // Genera entre 1 y 10 letras aleatorias en mayúscula o minúscula int numLetters = new Random().nextInt(9) + 1; for (int i = 0; i < numLetters; i++) { sb.append(generarLetra()).append(" "); } // Añadir las letras y los números a la cadena en el formato letra número for (int i = 0; i < numLetters; i++) { int randomNums = new Random().nextInt(100) + 10; String nums = String.valueOf(randomNums); sb.append(nums); randomNums = new Random().nextInt(100) + 10; nums = String.valueOf(randomNums); sb.append(generarLetra()); sb.append(nums); if (i < numLetters - 1) sb.append(" "); } //Metemos todos los valores aleatorios en un ArrayList y desordenamos el patron ArrayList<String> list = new ArrayList<>(List.of(sb.toString().split(" "))); Collections.shuffle(list); StringBuilder result = new StringBuilder(); int i = 0; while (i < list.size() - 1) { result.append(list.get(i)); result.append(" "); i++; } result.append(list.get(i)); return result.toString(); } @Test public void sampleTests() { //Sample tests assertEquals("a:11a1 b:222b22 c:3c33", Kata.keyAlphabet("a b c 11a1 222b22 3c33")); assertEquals("a:11a1,222a33 b:222b22 c:3c33", Kata.keyAlphabet("222a33 a b c 11a1 222b22 3c33 5")); assertEquals("a:11a1,12a1,13a1 c:33c3 d:2d2", Kata.keyAlphabet("a d c x 11a1 2d2 33c3 13a1 12a1")); assertEquals("", Kata.keyAlphabet("")); assertEquals("a:11a1 b:2b2 c:33c3", Kata.keyAlphabet("a b c d 11a1 33c3 2b2")); assertEquals("a:11a1 b:2b2 c:33c3", Kata.keyAlphabet("a b c 11a1 33c3 2b2 d44")); //Tests assertEquals("a:a90,a90 b:12b6,33b4", Kata.keyAlphabet("a b c d e f g h i j k l m n o p q r s t u v w y z 33b4 5x5 12b6 A90 A90")); assertEquals("a:a11", Kata.keyAlphabet("a a a11")); } @Test public void randomTest(){ for(int i = 0; i < 5; i++){ String input = generator(); String expected = Kata.keyAlphabet(input); assertEquals(expected,Kata.keyAlphabet(input)); System.out.println(expected); } } }
- import java.util.*;
- import org.junit.Test;
- import static org.junit.Assert.assertEquals;
- import org.junit.runners.JUnit4;
- public class SolutionTest {
- public static String generarLetra() {
- StringBuilder phrase = new StringBuilder();
- for (int i = 0; i < 1; i++) {
- char letter = (char) (new Random().nextInt('Z' - 'A') + 'A');
- phrase.append(new Random().nextBoolean() ? letter : Character.toLowerCase(letter));
- }
- return phrase.toString();
- }
- public static String generator() {
- StringBuilder sb = new StringBuilder();
- // Genera entre 1 y 10 letras aleatorias en mayúscula o minúscula
- int numLetters = new Random().nextInt(9) + 1;
- for (int i = 0; i < numLetters; i++) {
- sb.append(generarLetra()).append(" ");
- }
- // Añadir las letras y los números a la cadena en el formato letra número
- for (int i = 0; i < numLetters; i++) {
- int randomNums = new Random().nextInt(100) + 10;
- String nums = String.valueOf(randomNums);
- sb.append(nums);
- randomNums = new Random().nextInt(100) + 10;
- nums = String.valueOf(randomNums);
- sb.append(generarLetra());
- sb.append(nums);
- if (i < numLetters - 1) sb.append(" ");
- }
- //Metemos todos los valores aleatorios en un ArrayList y desordenamos el patron
- ArrayList<String> list = new ArrayList<>(List.of(sb.toString().split(" ")));
- Collections.shuffle(list);
- StringBuilder result = new StringBuilder();
- int i = 0;
- while (i < list.size() - 1) {
- result.append(list.get(i));
- result.append(" ");
- i++;
- }
- result.append(list.get(i));
- return result.toString();
- }
- @Test
- public void sampleTests() {
- //Sample tests
- assertEquals("a:11a1 b:222b22 c:3c33", Kata.keyAlphabet("a b c 11a1 222b22 3c33"));
- assertEquals("a:11a1,222a33 b:222b22 c:3c33", Kata.keyAlphabet("222a33 a b c 11a1 222b22 3c33 5"));
- assertEquals("a:11a1,12a1,13a1 c:33c3 d:2d2", Kata.keyAlphabet("a d c x 11a1 2d2 33c3 13a1 12a1"));
- assertEquals("", Kata.keyAlphabet(""));
- assertEquals("a:11a1 b:2b2 c:33c3", Kata.keyAlphabet("a b c d 11a1 33c3 2b2"));
- assertEquals("a:11a1 b:2b2 c:33c3", Kata.keyAlphabet("a b c 11a1 33c3 2b2 d44"));
- //Tests
- assertEquals("a:a90,a90 b:12b6,33b4", Kata.keyAlphabet("a b c d e f g h i j k l m n o p q r s t u v w y z 33b4 5x5 12b6 A90 A90"));
- assertEquals("a:a11", Kata.keyAlphabet("a a a11"));
- }
- @Test
- public void randomTest(){
- for(int i = 0; i < 5; i++){
- String input = generator();
- String expected = Kata.keyAlphabet(input);
- assertEquals(expected,Kata.keyAlphabet(input));
- System.out.println(expected);
- }
- }
- }
Strings
import java.util.*; public class Kata { public static String keyAlphabet(String str) { String[] arr = str.split(" "); List<String> listKey = new ArrayList<String>(); PriorityQueue<String> listValue = new PriorityQueue<>(); TreeMap<String, String> alphabet = new TreeMap<>(); for (String item : arr) { if (item.length() == 1 && item.charAt(0) >= 'a' && item.charAt(0) <= 'z') { listKey.add(item); } else { listValue.add(item); } } List<String> values = new ArrayList<>(listValue); for (String value : values) { for (String s : listKey) { if (value.contains(s)) { if (alphabet.containsKey(s)) { alphabet.put(s, alphabet.get(s) + "," + value); } else { alphabet.put(s, value); } } } } return ""; } }
- import java.util.*;
- public class Kata {
- public static String keyAlphabet(String str) {
- String[] arr = str.split(" ");
- List<String> listKey = new ArrayList<String>();
- PriorityQueue<String> listValue = new PriorityQueue<>();
- TreeMap<String, String> alphabet = new TreeMap<>();
- for (String item : arr) {
- if (item.length() == 1 && item.charAt(0) >= 'a' && item.charAt(0) <= 'z') {
- listKey.add(item);
- } else {
- listValue.add(item);
- }
- }
- List<String> values = new ArrayList<>(listValue);
- for (String value : values) {
- for (String s : listKey) {
- if (value.contains(s)) {
- if (alphabet.containsKey(s)) {
- alphabet.put(s, alphabet.get(s) + "," + value);
- } else {
- alphabet.put(s, value);
- }
- }
- }
- }
- return "";
- }
- }
Strings
import java.util.*; public class Kata { public static String keyAlphabet(String str) { String[] arr = str.split(" "); List<String> listKey = new ArrayList<String>(); PriorityQueue<String> listValue = new PriorityQueue<>(); TreeMap<String, String> alphabet = new TreeMap<>(); for (String item : arr) { if (item.length() == 1 && item.charAt(0) >= 'a' && item.charAt(0) <= 'z') { listKey.add(item); } else { listValue.add(item); } } List<String> values = new ArrayList<>(listValue); return ""; } }
- import java.util.*;
- public class Kata {
- public static String keyAlphabet(String str) {
- String[] arr = str.split(" ");
- List<String> listKey = new ArrayList<String>();
- PriorityQueue<String> listValue = new PriorityQueue<>();
- TreeMap<String, String> alphabet = new TreeMap<>();
- for (String item : arr) {
- if (item.length() == 1 && item.charAt(0) >= 'a' && item.charAt(0) <= 'z') {
- listKey.add(item);
- } else {
- listValue.add(item);
- }
- }
- List<String> values = new ArrayList<>(listValue);
- return "";
- }
- }
import java.util.*; public class Kata { public static String keyAlphabet(String str) { String[] arr = str.split(" "); List<String> listKey = new ArrayList<String>(); PriorityQueue<String> listValue = new PriorityQueue<>(); TreeMap<String, String> alphabet = new TreeMap<>(); for (String item : arr) { if (item.length() == 1 && item.charAt(0) >= 'a' && item.charAt(0) <= 'z') { listKey.add(item); } else { listValue.add(item); } } return ""; } }
- import java.util.*;
- public class Kata {
- public static String keyAlphabet(String str) {
String[] arr = str.split(" ");List<String> listKey = new ArrayList<String>();PriorityQueue<String> listValue = new PriorityQueue<>();TreeMap<String, String> alphabet = new TreeMap<>();- String[] arr = str.split(" ");
- List<String> listKey = new ArrayList<String>();
- PriorityQueue<String> listValue = new PriorityQueue<>();
- TreeMap<String, String> alphabet = new TreeMap<>();
- for (String item : arr) {
- if (item.length() == 1 && item.charAt(0) >= 'a' && item.charAt(0) <= 'z') {
- listKey.add(item);
- } else {
- listValue.add(item);
- }
- }
- return "";
- }
- }