Skip to content

Commit

Permalink
Merge pull request #4391 from masdos/RETO-30
Browse files Browse the repository at this point in the history
Reto #30 - Java
  • Loading branch information
Roswell468 authored Jul 31, 2023
2 parents 04f601c + ce75811 commit 93cb584
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ public static void main(String[] args) {
}

public static boolean isValidMathOperation(String operation) {
return operation.matches("^(-?\\d(.\\d)?)+(\\s[-+*/%]\\s(-?\\d(.\\d)?)+)+$");
return operation.matches("^(-?\\d(\.\\d)?)+(\\s[-+*/%]\\s(-?\\d(\.\\d)?)+)+$");
}
}
55 changes: 55 additions & 0 deletions Retos/Reto #30 - EL TECLADO T9 [Media]/java/masdos.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import java.util.Arrays;
import java.util.HashMap;
import java.util.stream.Collectors;

/*
* Los primeros dispositivos móviles tenían un teclado llamado T9
* con el que se podía escribir texto utilizando únicamente su
* teclado numérico (del 0 al 9).
*
* Crea una función que transforme las pulsaciones del T9 a su
* representación con letras.
* - Debes buscar cuál era su correspondencia original.
* - Cada bloque de pulsaciones va separado por un guión.
* - Si un bloque tiene más de un número, debe ser siempre el mismo.
* - Ejemplo:
* Entrada: 6-666-88-777-33-3-33-888
* Salida: MOUREDEV
*/
public class masdos {

public static void main(String[] args) {
System.out.println(translateT9Code("6-666-88-777-33-3-33-888"));
}

public static String translateT9Code(String t9Code) {
if (!t9Code.matches(
"^(0|([2-58])\\2{0,2}|([679])\\3{0,3})(-(0|([2-58])\\6{0,2}|([679])\\7{0,3}))*$")) {
throw new IllegalArgumentException(
"The T9Code has invalid format, T9Code: " + t9Code + ". Example: '555-44-22'");
}
HashMap<String, String[]> t9KeyMap = createT9KeyMap();
return Arrays.stream(t9Code.split("-"))
.map(
splitCode -> {
String key = String.valueOf(splitCode.charAt(0));
int value = splitCode.length() - 1;
return t9KeyMap.get(key)[value];
})
.collect(Collectors.joining());
}

private static HashMap<String, String[]> createT9KeyMap() {
HashMap<String, String[]> t9KeyMap = new HashMap<>();
t9KeyMap.put("0", new String[] {" "});
t9KeyMap.put("2", new String[] {"A", "B", "C"});
t9KeyMap.put("3", new String[] {"D", "E", "F"});
t9KeyMap.put("4", new String[] {"G", "H", "I"});
t9KeyMap.put("5", new String[] {"J", "K", "L"});
t9KeyMap.put("6", new String[] {"M", "N", "O", "Ñ"});
t9KeyMap.put("7", new String[] {"P", "Q", "R", "S"});
t9KeyMap.put("8", new String[] {"T", "U", "V"});
t9KeyMap.put("9", new String[] {"W", "X", "Y", "Z"});
return t9KeyMap;
}
}

0 comments on commit 93cb584

Please sign in to comment.