File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed
Roadmap/06 - RECURSIVIDAD/python Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change
1
+ ################ Recursividad ###################
2
+ '''
3
+ Decimos que una función es recursiva cuando dentro de la función se llama a si misma
4
+ Para hacer que sea recursiva debemos tener un caso base que hará finalizar el bucle de llamadas.
5
+ '''
6
+
7
+ def countdown (number : int = 100 ):
8
+ if number == 0 :
9
+ print (0 )
10
+ else :
11
+ print (number )
12
+ countdown (number = number - 1 )
13
+
14
+ countdown (100 )
15
+
16
+
17
+ ##################### EXTRA ################################
18
+
19
+ #Calcular el factorial de un número
20
+ def factorial (number : int ) -> int :
21
+ if number == 0 or number == 1 :
22
+ return 1
23
+ else :
24
+ return number * factorial (number = number - 1 )
25
+
26
+ print (factorial (6 ))
27
+
28
+ # Calcular valor de un elemento según su posición en la sucesión de Fibonacci
29
+
30
+
31
+ def calcular_valor_fibonacci (position : int ) -> int :
32
+
33
+ if position == 1 :
34
+ return 0
35
+ elif position == 2 :
36
+ return 1
37
+ else :
38
+ return calcular_valor_fibonacci (position = position - 1 ) + calcular_valor_fibonacci (position = position - 2 )
39
+
40
+ print (calcular_valor_fibonacci (7 ))
41
+ ##################### FIN EXTRA ################################
You can’t perform that action at this time.
0 commit comments