Skip to content

Commit 16d28d8

Browse files
committed
Merge branch 'main' of github.com:Gordo-Master/roadmap-retos-programacion into mouredev#50-Python
2 parents 27a5dd1 + e8e858b commit 16d28d8

File tree

7 files changed

+2039
-1521
lines changed

7 files changed

+2039
-1521
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
"""
2+
Valor y referencia
3+
4+
"""
5+
6+
#Tipos de datos por valor: Tipos inmutables: int, float, str, bool, tuplas Etc.
7+
8+
my_int_a = 10
9+
my_int_b = my_int_a
10+
#my_int_b = -20 +1
11+
#my_int_a = 30
12+
print (my_int_a)
13+
print (my_int_b)
14+
15+
16+
#Tipos de datos por referencia: Tipos mutables: list, dict, set, etc.
17+
18+
my_list_a =[10, 20]
19+
#my_list_b = [30, 40]
20+
my_list_b = my_list_a
21+
my_list_b.append (30)
22+
#my_list_b = [30, 40]
23+
print (my_list_a) #dato o variable por referencia, es decir, hereda su posición de referencia y se almacenan en la misma posición de memoria. imprime esto: [10, 20, 30]
24+
print (my_list_b)
25+
26+
27+
#Funciones con datos por valor
28+
29+
30+
my_int_c = 10
31+
32+
def my_int_func(my_int: int):
33+
my_int = 20
34+
#my_int c = 30
35+
print (my_int)
36+
37+
my_int_func(my_int_c)
38+
print (my_int_c)
39+
40+
#Funciones con datos con referencia
41+
42+
def my_list_func (my_list: list):
43+
my_list_e = my_list
44+
my_list_e.append (30)
45+
46+
my_list_d = my_list_e
47+
my_list.append (40)
48+
49+
print(my_list_e)
50+
print (my_list_d)
51+
52+
53+
my_list_c =[10, 20]
54+
my_list_func(my_list_c)
55+
print (my_list_c)
56+
57+
58+
"""
59+
Extra
60+
61+
"""
62+
#Variable por valor
63+
64+
def value (value_a: int, value_b: int)-> tuple:
65+
temp = value_a
66+
value_a = value_b
67+
value_b = temp
68+
return value_a, value_b
69+
70+
71+
my_int_d = 10
72+
my_int_e = 20
73+
my_int_f, my_int_g = value (my_int_d, my_int_e)
74+
75+
print (f"{my_int_d}, {my_int_e}")
76+
print (f"{my_int_f}, {my_int_g}")
77+
78+
#Por referencia
79+
80+
def ref (value_a: list, value_b: list) -> tuple:
81+
temp = value_a
82+
#gitemp.append(50)
83+
value_a = value_b
84+
value_b = temp
85+
#value_b.append(50)
86+
#value_a.append(50)
87+
88+
return value_a, value_b
89+
90+
91+
my_int_e = [10, 20]
92+
my_int_f = [30, 40]
93+
my_int_g, my_int_h = ref (my_int_e, my_int_f)
94+
print (f"{my_int_e}, {my_int_f}")
95+
print (f"{my_int_g}, {my_int_h}")
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// recursividad javascript
2+
// a grandes rasgos es una funcion que se llama a si misma
3+
4+
function countdown(numero) {
5+
console.log(numero); // imprime el numero
6+
if (numero > 0) { // si numero es mayor a 0
7+
countdown(numero - 1); // llama la funcion countdown y le resta 1 numero
8+
} else { // de lo contrario
9+
console.log("cuenta regresiva terminada") // imprime qie la cuenta regresiva ha terminado
10+
}
11+
}
12+
countdown(100); // numero vale ahora 100
13+
14+
// DIFICULTAD EXTRA
15+
16+
function factorial(numero) {
17+
if (numero < 0) {
18+
return "Error: No se permiten números negativos "; // si el numero es menor a 0 retorna que no se permiten numeros negativos
19+
} else if (numero === 0) {
20+
return 1; // si el numero es 0 retorna 1, ya que el factorial de 0 es 1
21+
} else {
22+
return numero * factorial(numero - 1); // si el numero es mayor a 0, se multiplica el numero por el factorial - 1
23+
}
24+
}
25+
console.log(factorial(5)); // 120
26+
console.log(factorial(0)); // 1
27+
console.log(factorial(-500)); // Error: No se permiten números negativos
28+
29+
function fibonacci(numero) {
30+
if (numero <= 0) {
31+
console.log("Error: La posició debe ser mayor a 0"); // si el numero es menor o igual a 0 retorna que los numeros negativos no son permitidos
32+
return 0;
33+
} else if (numero === 1 ) {
34+
return 0;
35+
} else if (numero === 2) {
36+
return 1;
37+
} else {
38+
return fibonacci(numero - 1) + fibonacci(numero - 2); // si el numero es mayor a 1, se suma el fibonacci del numero - 1 y el fibonacci del numero - 2
39+
}
40+
}
41+
42+
console.log(fibonacci(5));
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"""/*
2+
* EJERCICIO:
3+
* Entiende el concepto de recursividad creando una función recursiva que imprima números del 100 al 0.
4+
*
5+
* DIFICULTAD EXTRA (opcional):
6+
* Utiliza el concepto de recursividad para:
7+
* - Calcular el factorial de un número concreto (la función recibe ese número).
8+
* - Calcular el valor de un elemento concreto (según su posición) en la
9+
* sucesión de Fibonacci (la función recibe la posición).
10+
*/
11+
"""
12+
#*-------------------------------------------------------------------------------------------------------------#
13+
print("-----------------------------------------------------------------------------------------------------")
14+
print("Recursividad")
15+
16+
def numero_recursivo(n):
17+
for i in range(n, -1, -1):
18+
print(i)
19+
numero_recursivo(100)
20+
21+
#*-------------------------------------------------------------------------------------------------------------#
22+
print("-----------------------------------------------------------------------------------------------------")
23+
print("Factorial")
24+
def factorial(n):
25+
resultado = 1
26+
for i in range(2, n + 1):
27+
resultado *= i
28+
return resultado
29+
30+
numero = int(input("Indique el número: "))
31+
resultado = factorial(numero)
32+
print(f"El factorial de {numero} es {resultado}")
33+
34+
#*-------------------------------------------------------------------------------------------------------------#
35+
print("-----------------------------------------------------------------------------------------------------")
36+
print("Fibonacci")
37+
def fibonacci(n):
38+
if n <= 0:
39+
return "La posición debe ser un número entero positivo."
40+
elif n == 1:
41+
return 0
42+
elif n == 2:
43+
return 1
44+
else:
45+
return fibonacci(n - 1) + fibonacci(n - 2)
46+
47+
posicion = int(input("Ingrese la posición en la sucesión de Fibonacci: "))
48+
resultado = fibonacci(posicion)
49+
print(f"El elemento en la posición {posicion} de la sucesión de Fibonacci es {resultado}")
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# ejercicio pila/stack (LIFO)
2+
stack = []
3+
# push
4+
stack.append(1)
5+
stack.append(2)
6+
stack.append(3)
7+
print(stack)
8+
# pop
9+
stack.pop()
10+
print(stack)
11+
12+
# ejercicio cola/Queue (FIFO)
13+
queue = []
14+
# enqueue
15+
queue.append(1)
16+
queue.append(2)
17+
queue.append(3)
18+
print(queue)
19+
# dequeue
20+
queue.pop(0)
21+
print(queue)
22+
23+
# EXTRA
24+
25+
# web_pila
26+
def web_navigation():
27+
stack = []
28+
stack.append("fishellVvv.com")
29+
position = 0
30+
31+
print(f"https://{stack[0]}/", end="")
32+
while True:
33+
action = input("\nAñade una url o interactúa con palabras adelante/atrás/salir: ")
34+
if action == "salir":
35+
print("Saliendo del navegador web.")
36+
break
37+
elif action == "adelante":
38+
if position < len(stack) - 1:
39+
position += 1
40+
elif action == "atrás":
41+
if position > 0:
42+
position -= 1
43+
else:
44+
for i in range(position, len(stack) - 1):
45+
stack.pop()
46+
stack.append(action)
47+
position += 1
48+
49+
print("https://", end="")
50+
for i in range(position + 1):
51+
print(f"{stack[i]}/", end="")
52+
53+
web_navigation()
54+
55+
# impresora_cola
56+
def shared_printer():
57+
queue = []
58+
59+
while True:
60+
action = input("\nAñade un documento o selecciona imprimir/salir: ")
61+
if action == "salir":
62+
print("Saliendo de la impresora.")
63+
break
64+
elif action == "imprimir":
65+
if len(queue) > 0:
66+
print(f"Imprimiendo: {queue.pop(0)}")
67+
else:
68+
queue.append(action)
69+
70+
print("Cola de impresión:")
71+
for i in range(len(queue)):
72+
print(f"- {queue[i]}")
73+
74+
shared_printer()

0 commit comments

Comments
 (0)