forked from mouredev/roadmap-retos-programacion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAmadorQuispe.go
116 lines (109 loc) · 3 KB
/
AmadorQuispe.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"fmt"
"strings"
"unicode"
)
func main() {
message := `Hola como estas`
otherMessage := "amigo programador"
//# Concatenación
fmt.Println(message + ", " + otherMessage + ".")
//# Longitud de la cadena
fmt.Println("Longitud de la cadena es :", len(message))
//# Repetición
fmt.Println(strings.Repeat(message, 2))
//# búsqueda
fmt.Println("Empieza con 'Ho'?:", strings.HasPrefix(message, "Ho"))
fmt.Println("Termina con 'as'?:", strings.HasSuffix(message, "s"))
fmt.Println("Veces que se repite la letra 'o':", strings.Count(message, "o"))
//# conversión a mayúsculas y minúsculas
fmt.Println("Conversión a mayúsculas:", strings.ToUpper(message))
fmt.Println("Conversión a minúsculas:", strings.ToLower(message))
//# reemplazo
fmt.Println("Reemplazar 'r' por 'T':", strings.ReplaceAll(otherMessage, "r", "T"))
//# división
fmt.Println("Dividir por espacio :", strings.Split(message, " "))
//# unión
splitString := strings.Fields(message)
fmt.Println("Unir palabras de ", splitString, " :", strings.Join(splitString, " "))
//# verificación
fmt.Println("Contiene 'mo'?:", strings.Contains(message, "mo"))
//# interpolación
fmt.Printf("Mensaje :%s\n", message)
//# Acceso a carácter por el indice
fmt.Printf("Carácter en el indice 3 de %s : es '%s'\n", message, string(message[3]))
//# sub cadenas
fmt.Println("sub cadena :", message[1:4])
result := isPalindrome("radar")
fmt.Println(result)
//# EXTRA
//Palindrome, si al revertir se lee lo mismo
fmt.Printf("¿'radar' es palindrome? : %v \n", isPalindrome("radar"))
fmt.Printf("¿'hola' es palindrome? : %v \n", isPalindrome("hola"))
//Anagrama, si ambas tienen las mismas letras
fmt.Printf("¿'roma' y 'amor' son anagramas ? :%v\n", isAnagram("roma", "amor"))
//Isograma, palabra o frase en la que cada letra aparece el mismo número de veces Vivienne
fmt.Printf("¿'escritura' es Isograma ? :%v\n", isIsogram("escritura"))
fmt.Printf("¿'Dermatoglyphics' es Isograma ? :%v\n", isIsogram("Dermatoglyphics"))
fmt.Printf("¿'vivienne ss' es Isograma ? :%v\n", isIsogram("vivienne ss"))
}
func isPalindrome(s string) bool {
if len(s) <= 1 {
return true
}
iLeft := 0
iRight := len(s) - 1
for iLeft < iRight {
if s[iLeft] != s[iRight] {
return false
}
iLeft++
iRight--
}
return true
}
func isAnagram(s1, s2 string) bool {
if len(s1) != len(s2) {
return len(s1) != len(s2)
}
charCount := make(map[int32]int)
for _, char := range s1 {
charCount[char]++
}
for _, char := range s2 {
charCount[char]--
if charCount[char] < 0 {
return false
}
}
for _, count := range charCount {
if count != 0 {
return false
}
}
return true
}
func isIsogram(s string) bool {
if len(s) <= 2 {
return true
}
charCount := make(map[rune]int)
for _, char := range s {
if !unicode.IsLetter(char) {
continue
}
charCount[char]++
}
var lenIsogram int
for _, v := range charCount {
lenIsogram = v
break
}
for _, v := range charCount {
if lenIsogram != v {
return false
}
}
return true
}