-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4271 from kontroldev/main
Reto #30- swift
- Loading branch information
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
Retos/Reto #30 - EL TECLADO T9 [Media]/swift/kontroldev.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* 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 | ||
*/ | ||
import Foundation | ||
|
||
func t9ToLetters(_ input: String) -> String { | ||
let t9Mapping: [Character: String] = [ | ||
"2": "ABC", "3": "DEF", "4": "GHI", | ||
"5": "JKL", "6": "MNO", "7": "PQRS", | ||
"8": "TUV", "9": "WXYZ" | ||
] | ||
|
||
var result = "" | ||
var currentDigits = [Character]() | ||
|
||
for char in input { | ||
if char.isNumber { | ||
currentDigits.append(char) | ||
} else if char == "-" { | ||
if let digit = currentDigits.first, let letters = t9Mapping[digit] { | ||
result += String(letters[letters.index(letters.startIndex, offsetBy: currentDigits.count - 1)]) | ||
} | ||
currentDigits.removeAll() | ||
} | ||
} | ||
|
||
if let digit = currentDigits.first, let letters = t9Mapping[digit] { | ||
result += String(letters[letters.index(letters.startIndex, offsetBy: currentDigits.count - 1)]) | ||
} | ||
|
||
return result | ||
} | ||
|
||
|
||
let input = "6-666-88-777-33-3-33-888" | ||
let output = t9ToLetters(input) | ||
print(output) |