-
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 #4631 from JassonCu/main
Reto #30 - Go
- Loading branch information
Showing
2 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
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,74 @@ | ||
/* | ||
* 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 | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
func T9ToLetters(input string) string { | ||
t9Mapping := map[rune]string{ | ||
'2': "ABC", | ||
'3': "DEF", | ||
'4': "GHI", | ||
'5': "JKL", | ||
'6': "MNO", | ||
'7': "PQRS", | ||
'8': "TUV", | ||
'9': "WXYZ", | ||
'0': " ", | ||
} | ||
|
||
var result strings.Builder | ||
prevKey := rune(' ') | ||
keyCount := 0 | ||
|
||
for _, char := range input { | ||
if char == '-' { | ||
if keyCount > 0 { | ||
letters := t9Mapping[prevKey] | ||
result.WriteString(string(letters[keyCount-1])) | ||
} | ||
prevKey = ' ' | ||
keyCount = 0 | ||
} else if val, exists := t9Mapping[char]; exists { | ||
if prevKey != char { | ||
if keyCount > 0 { | ||
letters := t9Mapping[prevKey] | ||
result.WriteString(string(letters[keyCount-1])) | ||
} | ||
prevKey = char | ||
keyCount = 0 | ||
} | ||
keyCount = (keyCount + 1) % (len(val) + 1) | ||
} | ||
} | ||
|
||
if keyCount > 0 { | ||
letters := t9Mapping[prevKey] | ||
result.WriteString(string(letters[keyCount-1])) | ||
} | ||
|
||
return result.String() | ||
} | ||
|
||
func main() { | ||
input := "5-555-22-7777-666-66" | ||
output := T9ToLetters(input) | ||
fmt.Println("Salida:", output) // Debería imprimir "Salida: JASSON" | ||
} | ||
|
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,58 @@ | ||
/* | ||
* Crea una función que sea capaz de leer el número representado por el ábaco. | ||
* - El ábaco se representa por un array con 7 elementos. | ||
* - Cada elemento tendrá 9 "O" (aunque habitualmente tiene 10 para realizar operaciones) | ||
* para las cuentas y una secuencia de "---" para el alambre. | ||
* - El primer elemento del array representa los millones, y el último las unidades. | ||
* - El número en cada elemento se representa por las cuentas que están a la izquierda del alambre. | ||
* | ||
* Ejemplo de array y resultado: | ||
* ["O---OOOOOOOO", | ||
* "OOO---OOOOOO", | ||
* "---OOOOOOOOO", | ||
* "OO---OOOOOOO", | ||
* "OOOOOOO---OO", | ||
* "OOOOOOOOO---", | ||
* "---OOOOOOOOO"] | ||
* | ||
* Resultado: 1.302.790 | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
func decodeAbacus(abacus []string) int { | ||
total := 0 | ||
multiplier := 1 | ||
|
||
for i := len(abacus) - 1; i >= 0; i-- { | ||
count := strings.Count(abacus[i], "O") | ||
|
||
if count > 0 { | ||
total += count * multiplier | ||
} | ||
|
||
multiplier *= 10 | ||
} | ||
|
||
return total | ||
} | ||
|
||
func main() { | ||
abacus := []string{ | ||
"O---OOOOOOOO", | ||
"OOO---OOOOOO", | ||
"---OOOOOOOOO", | ||
"OO---OOOOOOO", | ||
"OOOOOOO---OO", | ||
"OOOOOOOOO---", | ||
"---OOOOOOOOO", | ||
} | ||
|
||
number := decodeAbacus(abacus) | ||
fmt.Printf("Resultado: %d\n", number) | ||
} |