Skip to content

Commit 0ef77cc

Browse files
authored
Merge pull request #8688 from bluefeatherdev/reto-04-dart
#4 - Dart
2 parents 6d0ff87 + aad810d commit 0ef77cc

File tree

1 file changed

+209
-0
lines changed

1 file changed

+209
-0
lines changed
Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
/*
2+
* EJERCICIO:
3+
* Muestra ejemplos de todas las operaciones que puedes realizar con cadenas de caracteres
4+
* en tu lenguaje. Algunas de esas operaciones podrían ser (busca todas las que puedas):
5+
* - Acceso a caracteres específicos, subcadenas, longitud, concatenación, repetición, recorrido,
6+
* conversión a mayúsculas y minúsculas, reemplazo, división, unión, interpolación, verificación...
7+
*
8+
* DIFICULTAD EXTRA (opcional):
9+
* Crea un programa que analice dos palabras diferentes y realice comprobaciones
10+
* para descubrir si son:
11+
* - Palíndromos
12+
* - Anagramas
13+
* - Isogramas
14+
*/
15+
16+
/// [DIFICULTAD EXTRA]:
17+
void checkTwoWords(String word1, String word2) {
18+
/// Analizar si son [palíndromos] individualmente:
19+
var reversedWord1 = word1.split('').reversed.join();
20+
var reversedWord2 = word2.split('').reversed.join();
21+
print('¿"$word1" es palíndrome? : ${word1 == reversedWord1}');
22+
print('¿"$word2" es palíndrome? : ${word2 == reversedWord2}');
23+
24+
/// Analizar si son [anagramas] entre sí:
25+
dynamic charsWord1 = word1.split('')..sort();
26+
dynamic charsWord2 = word2.split('')..sort();
27+
charsWord1 = charsWord1.join();
28+
charsWord2 = charsWord2.join();
29+
print('¿"$word1" y "$word2" son anagramas? : ${charsWord1 == charsWord2}');
30+
31+
/// Analizar si son [isogramas] individualmente:
32+
bool isIsogram(String word) {
33+
bool isIsogramWord = true;
34+
Set setWord = word.split('').toSet();
35+
List listWord = setWord.toList();
36+
for (var char in listWord) {
37+
var countChar = RegExp(char).allMatches(word).length;
38+
if (countChar > 1) {
39+
isIsogramWord = false;
40+
break;
41+
}
42+
}
43+
return isIsogramWord;
44+
}
45+
print('¿"$word1" es un isograma? : ${isIsogram(word1)}');
46+
print('¿"$word2" es un isograma? : ${isIsogram(word2)}');
47+
}
48+
49+
void main() {
50+
/// Cadenas de ejemplo para operaciones:
51+
String myString = 'Hello, Dart!';
52+
String text1 = 'Blue';
53+
String text2 = 'Feather';
54+
String text3 = 'Banana';
55+
String text4 = ' Hello ';
56+
57+
/// 1. [Concatenación]:
58+
print(text1 + ' ' + text2); // Blue Feather
59+
print('Blue' ' ' 'Feather'); // Blue Feather
60+
61+
/// 2. [Repetición]:
62+
print(text1 * 2); // BlueBlue
63+
print(text2 * 3); // FeatherFeatherFeather
64+
65+
/// 3. [Indexación]:
66+
print(text1[0]); // B
67+
print(text1[1]); // l
68+
print(text1[2]); // u
69+
print(text1[3]); // e
70+
print(text1[text1.length - 1]); // e
71+
72+
/// 4. [Longitud]:
73+
print(text1.length); // 4
74+
75+
/// 5. [Slicing]:
76+
print(myString.substring(0, 5)); // Hello
77+
print(myString.substring(7, 11)); // Dart
78+
79+
/// 6. [Búsqueda]:
80+
print(myString.contains('Dart')); // true
81+
82+
/// 7. [Reemplazo]:
83+
print(text3.replaceAll('a', 'e')); // Benene
84+
print(text3.replaceAllMapped('a',
85+
(w) => 'e' // Benene
86+
));
87+
print(text3.replaceFirst('a', 'e')); // Benana
88+
print(text3.replaceFirstMapped('a',
89+
(w) => 'e' // Benana
90+
));
91+
print(text3.replaceRange(0, 2, 'Le')); // Lenana
92+
93+
/// 8. [División]:
94+
print(myString.split(', ')); // [Hello, Dart!]
95+
96+
/// 9. [Mayúsculas y minúsculas]:
97+
print(myString.toLowerCase()); // hello, dart!
98+
print(myString.toUpperCase()); // HELLO, DART!
99+
100+
/// 10. [Eliminar espacio en extremos]:
101+
print(text4.trim()); // hello
102+
print(text4.trimLeft()); // hello
103+
print(text4.trimRight()); // hello
104+
105+
/// 11. [Búsqueda al principio y al final]:
106+
print(text1.startsWith('B')); // true
107+
print(text1.startsWith('b')); // false
108+
print(text1.endsWith('e')); // true
109+
print(text1.endsWith('E')); // false
110+
111+
/// 12. [Búsqueda de posición]:
112+
print(text1.indexOf('u')); // 2
113+
print(myString.indexOf('Dart')); // 7
114+
115+
/// 13. [Búsqueda de ocurrencias]:
116+
final countA
117+
= RegExp('a').allMatches(text3).length;
118+
print(countA); // 3
119+
120+
/// 14. [Interpolación]:
121+
print('text1: ${text1}'); // text1: Blue
122+
print('text2: ${text2}'); // text2: Feather
123+
print('text3: ${text3}'); // text3: Banana
124+
125+
/// 15. [Lista de caracteres]:
126+
print(text1.split('')); // [B, l, u, e]
127+
print(text2.split('')); // [F, e, a, t, h, e, r]
128+
print(text3.split('')); // [B, a, n, a, n, a]
129+
130+
/// 16. [Lista de cadenas a cadena]:
131+
List<String> list = [text1, ' ', text2];
132+
print(list.join()); // Blue Feather
133+
134+
/// 17. [Cadenas a tipos numéricos]:
135+
print(int.parse('23')); // 23
136+
print(double.parse('1.6180')); // 1.818
137+
print(num.parse('23')); // 23
138+
print(num.parse('1.6180')); // 1.818
139+
140+
/// 18. Otros métodos: `.allMatches()`:
141+
var exp = RegExp(r'a');
142+
var found = exp.allMatches(text3);
143+
for (var match in found) {
144+
print('Match "${match.group(0)}" en posición ${match.start}');
145+
}
146+
147+
/// 19. Otros métodos: `.codeUnitAt()`:
148+
print(text1.codeUnitAt(0)); // 66
149+
print(text1.codeUnitAt(1)); // 108
150+
print(text1.codeUnitAt(2)); // 117
151+
print(text1.codeUnitAt(3)); // 101
152+
153+
/// 20. Otros métodos: `.compareTo()`:
154+
print(text1.compareTo(text1)); // 0
155+
print(text1.compareTo(text2)); // -1
156+
print(text1.compareTo(text3)); // 1
157+
158+
/// 21. Otros métodos: `.lastIndexOf()`:
159+
print(text3.lastIndexOf('a')); // 5
160+
161+
/// 22. Otros métodos: `.matchAsPrefix()`:
162+
var regExp = RegExp(r'Dart');
163+
var match = regExp.matchAsPrefix(myString, 7);
164+
print(match); // Match found
165+
166+
/// 23. Otros métodos: `.padLeft()`:
167+
print(myString.padLeft(16, '+')); // ++++Hello, Dart!
168+
169+
/// 24. Otros métodos: `.padRight()`:
170+
print(myString.padRight(16, '+')); // Hello, Dart!++++
171+
172+
/// 25. Otros métodos: `.splitMapJoin()`:
173+
print(text4.splitMapJoin(RegExp(r'Hello'),
174+
onMatch: (m) => '${m[0]}',
175+
onNonMatch: (n) => '*',
176+
)); // *Hello*
177+
178+
/// 25. Otros métodos: `.toString()`:
179+
print(23.toString()); // '23'
180+
181+
/// 26. [Getters de String]:
182+
print(myString.codeUnits); // [72, 101, 108, 108, 111, 44, 32, 68, 97, 114, 116, 33]
183+
print(myString.hashCode); // 181428680
184+
print(myString.isEmpty); // false
185+
print(myString.isNotEmpty); // true
186+
print(myString.length); // 12
187+
print(myString.runes); // (72, 101, 108, 108, 111, 44, 32, 68, 97, 114, 116, 33)
188+
print(myString.runtimeType); // String
189+
190+
/// 27. Operador `*` en String:
191+
print('Hello' * 2); // HelloHello
192+
193+
/// 28. Operador `+` en String:
194+
print('Hello' + ' ' + 'Dart'); // Hello Dart
195+
196+
/// 29. Operador `==` en String:
197+
print('Flutter' == 'React'); // False
198+
199+
/// 30. Operador `!=` en String:
200+
print('Flutter' != 'React'); // True
201+
202+
/// 31. Operador `[]` en String:
203+
print('Hello'[1]); // e
204+
205+
/// [DIFICULTAD EXTRA]:
206+
checkTwoWords('ana', 'john'); // ejemplo para palíndromes
207+
checkTwoWords('amor', 'roma'); // ejemplo para anagramas
208+
checkTwoWords('hola', 'hello'); // ejemplo para isogramas
209+
}

0 commit comments

Comments
 (0)