Skip to content

Commit f4f31e4

Browse files
Merge branch 'main' of github.com:mouredev/roadmap-retos-programacion into JesusAEE
2 parents c134b1f + c37218a commit f4f31e4

File tree

7 files changed

+1787
-868
lines changed

7 files changed

+1787
-868
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# https://python.org
2+
3+
# Esto es un comentario de una sola línea
4+
5+
"""Esto es
6+
un comentario
7+
de varias líneas"""
8+
9+
variable = "Esto es una variable"
10+
CONSTANTE = """En Python, no existen las constantes como tal
11+
por lo que se tiene que poner en mayúsculas"""
12+
13+
cadena_texto = "Hola, soy una cadena de texto"
14+
15+
entero = 32
16+
17+
flotantes = 32.5
18+
19+
booleano_positivo = True
20+
21+
booleano_negativo = False
22+
23+
print ("¡Hola, Python!")
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
//! Operaciones con cadenas
2+
3+
//* Concatenacion de cadenas
4+
let str1 = "Hola";
5+
let str2 = "Mundo";
6+
let result = str1 + " " + str2; // "Hola Mundo"
7+
8+
//* Acceso a caracteres
9+
let str = "JavaScript";
10+
let char = str[0]; // "J"
11+
let charAt = str.charAt(0); // "J"
12+
13+
//* Longitud de una cadena
14+
let string = "Hola Mundo";
15+
let length = string.length; // 10
16+
17+
//* Cambio de mayusculas a minusculas
18+
let str3 = "Hola Mundo";
19+
let upperCase = str3.toUpperCase(); // "HOLA MUNDO"
20+
let lowerCase = str3.toLowerCase(); // "hola mundo"
21+
22+
//* Extraccion de subcadenas
23+
let str4 = "Hola Mundo";
24+
let substring = str4.substring(0, 4); // "Hola"
25+
let slice = str4.slice(5); // "Mundo"
26+
27+
//* Division de una cadena
28+
let str5 = "Hola Mundo";
29+
let split = str5.split(" "); // ["Hola", "Mundo"]
30+
31+
//* Reemplazo de texto
32+
let str6 = "Hola Mundo";
33+
let replaced = str6.replace("Mundo", "JavaScript"); // "Hola JavaScript"
34+
35+
//* Busqueda de texto
36+
let str7 = "Hola Mundo";
37+
let indexOf = str7.indexOf("Mundo"); // 5
38+
let includes = str7.includes("Mundo"); // true
39+
40+
//* Eliminar espacios en blanco
41+
let str8 = " Hola Mundo ";
42+
let trimmed = str8.trim(); // "Hola Mundo"
43+
44+
//* Repetir cadenas
45+
let str9 = "Hola";
46+
let repeated = str9.repeat(3); // "HolaHolaHola"
47+
48+
//* Conversion de una cadena en un arreglo de caracteres
49+
let str10 = "Hola";
50+
let array = Array.from(str10); // ["H", "o", "l", "a"]
51+
52+
//* Verificacion de inicio y final de una cadena
53+
let str11 = "Hola Mundo";
54+
let startsWith = str11.startsWith("Hola"); // true
55+
let endsWith = str11.endsWith("Mundo"); // true
56+
57+
//* Interpolacion de cadenas (template literals)
58+
let nombre = "Juan";
59+
let saludo = `Hola, ${nombre}`; // "Hola, Juan"
60+
61+
//* Obtener codigo de caracter
62+
let str12 = "A";
63+
let charCode = str12.charCodeAt(0); // 65
64+
65+
//* Recorrido
66+
let str13 = "JavaScript";
67+
for (let i = 0; i < str13.length; i++) {
68+
console.log(str13[i]);
69+
}
70+
// J
71+
// a
72+
// v
73+
// a
74+
// S
75+
// c
76+
// r
77+
// i
78+
// p
79+
// t
80+
81+
//* Union
82+
let arr = ["Hola", "Mundo"];
83+
let joined = arr.join(" "); // "Hola Mundo"
84+
85+
//! Ejercicio Extra
86+
// Polindromo: es una palabra, numero o frace que se lee igual de izquierda a derea que de derecha a izquierda
87+
function isPalindromo(stringA) {
88+
let palindromo = Array.from(stringA);
89+
let revercedPolindromo = [];
90+
for (let i = stringA.length - 1; i >= 0; i--) {
91+
revercedPolindromo.push(stringA[i]);
92+
}
93+
let isPalindromo = true;
94+
for (let j = 0; j < palindromo.length; j++) {
95+
if (palindromo[j].toLowerCase() !== revercedPolindromo[j].toLowerCase()) {
96+
isPalindromo = false;
97+
break;
98+
}
99+
}
100+
if (isPalindromo) {
101+
return `${stringA} es palindromo`;
102+
} else {
103+
return `${stringA} no es palindromo`;
104+
}
105+
}
106+
// Anagrama: es una palabra o frace que se forma reordenando las letras de otra palabra o frace original, utilizando exactamente las mismas letras con la misma frecuencia.
107+
function cleanSort(string) {
108+
return string
109+
.toLowerCase()
110+
.replace(/[^a-z0-9]/g, "")
111+
.split("")
112+
.sort()
113+
.join("");
114+
}
115+
function isAnagrama(stringA, stringB) {
116+
let string1 = cleanSort(stringA);
117+
let string2 = cleanSort(stringB);
118+
119+
if (string1 === string2) {
120+
return `${stringA} y ${stringB} son Anagramas`;
121+
} else {
122+
return `${stringA} y ${stringB} no son Anagramas`;
123+
}
124+
}
125+
// Isograma: es una palabra o frace en la que ninguna letra se repita
126+
function isIsograma(string) {
127+
let newString = string.toLowerCase().replace(/[^a-z]/g, "");
128+
let letters = new Set();
129+
130+
for (const letter of newString) {
131+
if (letters.has(letter)) {
132+
return `${string} no es un Isograma`;
133+
}
134+
letters.add(letter);
135+
}
136+
return `${string} es un Isograma`;
137+
}
138+
139+
console.log(isPalindromo("radar"));
140+
console.log(isAnagrama("amors", "roma"));
141+
console.log(isIsograma("isogramas"));
Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
open Printf
2+
3+
(******************************************************************************)
4+
(* *)
5+
(* Liskov Substitution Principle *)
6+
(* *)
7+
(* The LSP states that subclasses of a parent class should be able to beha- *)
8+
(* ve just like their parent classes. Or the official statement: let phi(x) *)
9+
(* be a property provable about objects of type T, then phi(y) should be *)
10+
(* true for objects y ofthe type S where S is a subtype of T. *)
11+
(* All of this means that we should be able to replace objects of a super- *)
12+
(* class with objects of its subclasses without breaking the client's code. *)
13+
(* *)
14+
(* It also states some rules: *)
15+
(* 1. A subclasss method should not strengthen its pre-condition: In human- *)
16+
(* readable terms: overriden methods should not change their parameters *)
17+
(* for subtypes of those parameter types. *)
18+
(* 2. A subclass method should not weaken its post-condition: We should not *)
19+
(* return a more abstract type than the specified return type, but we *)
20+
(* can definitely return a subtype of the return type. *)
21+
(* 3. Methods should not break the invariance of the superclass' method. *)
22+
(* *)
23+
(******************************************************************************)
24+
25+
class virtual lsp_non_compliant_acount (owner' : string) =
26+
object
27+
val mutable balance = 0.0
28+
val owner = owner'
29+
method virtual deposit : float -> unit
30+
method virtual withdraw : float -> unit
31+
method show_balance = printf "Current balance for %s: $%f\n" owner balance
32+
end
33+
34+
class savings_account (owner' : string) =
35+
object
36+
inherit lsp_non_compliant_acount owner'
37+
38+
method deposit amount =
39+
balance <- balance +. amount;
40+
printf "%s deposited %f into their account!\n" owner amount
41+
42+
method withdraw amount =
43+
if balance < amount
44+
then failwith "Not enough funds for withdrawal"
45+
else begin
46+
balance <- balance -. amount;
47+
printf "%s withdrew %f from their account!\n" owner amount
48+
end
49+
end
50+
51+
class fixed_term_account (owner' : string) =
52+
object
53+
inherit lsp_non_compliant_acount owner'
54+
55+
method deposit amount =
56+
balance <- balance +. amount;
57+
printf "%s deposited %f into a fixed term!\n" owner amount
58+
59+
method withdraw = failwith "Unsupported method"
60+
end
61+
62+
let _ =
63+
(* Client Code for an LSP-breaking example *)
64+
let acct : lsp_non_compliant_acount ref =
65+
ref @@ new savings_account "Luis Lopez"
66+
in
67+
!acct#deposit 456.53;
68+
!acct#withdraw 245.35;
69+
!acct#show_balance;
70+
(* The code above worked great, but what if I were to swap the account ref
71+
value for a subclass that doen't do withdrawals and instead throws an
72+
exception which completely breaks the LSP. *)
73+
acct := new fixed_term_account "Moure Dev";
74+
!acct#deposit 456.53;
75+
begin
76+
try !acct#withdraw 245.35 with
77+
| Failure msg -> print_endline msg
78+
end;
79+
!acct#show_balance
80+
;;
81+
82+
(* Let's try to make the example LSP compliant. The most feasible solution is
83+
to stop making assumptions about the withdrawal capabilities of these accts.
84+
Next step is to break a superclass [Account] (or interface) into their own
85+
subclasses or subinterfaces with extended capabilities that other concrete
86+
classes can inherit or extend in turn. This way if the client code requires
87+
accounts that have withdrawal capabilities, then we can start using our
88+
specialized subtype that performs a withdrawal operation. *)
89+
90+
class virtual account (owner' : string) =
91+
object
92+
val mutable balance = 0.0
93+
val owner = owner'
94+
method virtual deposit : float -> unit
95+
method show_balance = printf "Current balance for %s: $%f\n" owner balance
96+
end
97+
98+
class virtual withdrawable owner' =
99+
object
100+
inherit account owner'
101+
method virtual withdraw : float -> unit
102+
end
103+
104+
class fixed_term owner' =
105+
object
106+
inherit account owner'
107+
108+
(* Any other methods or members corresponding to a specialized fixed-term
109+
class go here, otherwise there would be no point in extending.*)
110+
method deposit amount =
111+
balance <- balance +. amount;
112+
printf "%s deposited %f into their fixed term!\n" owner amount
113+
end
114+
115+
class savings owner' =
116+
object
117+
inherit withdrawable owner'
118+
119+
method deposit amount =
120+
balance <- balance +. amount;
121+
printf "%s deposited %f into their savings account!\n" owner amount
122+
123+
method withdraw amount =
124+
if balance < amount
125+
then failwith "Not enough funds for withdrawal"
126+
else begin
127+
balance <- balance -. amount;
128+
printf "%s withdrew %f from their savings account!\n" owner amount
129+
end
130+
end
131+
132+
let _ =
133+
(* Client Code for an LSP-compliant example *)
134+
let acct : withdrawable = new savings_account "Luis Lopez" in
135+
acct#deposit 319.35;
136+
acct#withdraw 300.00;
137+
acct#show_balance
138+
;;
139+
140+
(* Now, the client code is specific in that it only accepts subclasses that
141+
are 'withdrawable' and if I were to pass it an instance of [fixed_term]
142+
then the compiler would let us know, thus not breaking the LSP.
143+
144+
The compiler error says:
145+
- This expression has type fixed_term but an expression was expdected of
146+
type withdrawable The first object type has no method withdraw. *)
147+
(* let acct2 : withdrawable = new fixed_term "John Doe" in *)
148+
(* acct#withdraw 100.0 *)
149+
150+
(*
151+
* DIFICULTAD EXTRA (opcional):
152+
* ============================
153+
* Crea una jerarquía de vehículos. Todos ellos deben poder acelerar y frenar, así como
154+
* cumplir el LSP.
155+
* Instrucciones:
156+
* 1. Crea la clase Vehículo.
157+
* 2. Añade tres subclases de Vehículo.
158+
* 3. Implementa las operaciones "acelerar" y "frenar" como corresponda.
159+
* 4. Desarrolla un código que compruebe que se cumple el LSP.
160+
*)
161+
162+
let clamp a b x = if x < a then a else if x > b then b else x
163+
164+
class virtual vehicle (top_speed' : float) (accel : float) =
165+
object
166+
val top_speed = top_speed'
167+
val acceleration = accel
168+
val mutable speed = 0.0
169+
170+
method accelerate' =
171+
let clamp_speed = clamp 0.0 top_speed in
172+
speed <- clamp_speed (speed +. acceleration)
173+
174+
method virtual accelerate : unit
175+
method get_speed = speed
176+
method brake = speed <- 0.0
177+
end
178+
179+
class boat =
180+
object (self)
181+
inherit vehicle 80.0 1.52
182+
method anchor = print_endline "Anchoring boat..."
183+
184+
method accelerate =
185+
self#accelerate';
186+
print_endline "Boat accelerated"
187+
end
188+
189+
class motorbike =
190+
object (self)
191+
inherit vehicle 186.0 25.4
192+
193+
method accelerate =
194+
self#accelerate';
195+
print_endline "Motorbike accelerated"
196+
end
197+
198+
class tesla =
199+
object (self)
200+
inherit vehicle 200.0 9.82
201+
val mutable charge = 0
202+
203+
method accelerate =
204+
self#accelerate';
205+
charge <- clamp 0 100 (charge - 1);
206+
print_endline "Tesla accelerated"
207+
208+
method recharge = charge <- 100
209+
end
210+
211+
let _ =
212+
let v : vehicle = new motorbike in
213+
print_newline ();
214+
for i = 1 to 10 do
215+
v#accelerate;
216+
printf "Motorbike's speed: %f\n" v#get_speed
217+
done;
218+
v#brake;
219+
print_endline "Invoking #brake on a motorbike";
220+
printf "Motorbike's speed: %f\n" v#get_speed
221+
;;

0 commit comments

Comments
 (0)