-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3349 from mikelm2020/miguellopezmdev
#6-Python
- Loading branch information
Showing
2 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Recursividad | ||
# Crea una función recursiva que imprima números del 100 al 0 | ||
|
||
|
||
def numbers(number: int = 100): | ||
if number >= 0: | ||
print(number) | ||
numbers(number - 1) | ||
|
||
|
||
# Dificultad extra | ||
# Utiliza el concepto de recursividad para: | ||
# Calcular el factorial de un número concreto (la funcion recibe ese número) | ||
|
||
|
||
def factorial(number: int): | ||
if number == 1: | ||
return 1 | ||
|
||
if number >= 2: | ||
return number * factorial(number - 1) | ||
|
||
|
||
# Calcular el valor de un elemento concreto (según su posición) en la | ||
# sucesión de Fibonacci (la función recibe la posición). | ||
|
||
|
||
def fibonacci(position: int): | ||
if position == 0: | ||
return 0 | ||
if position == 1: | ||
return 1 | ||
if position >= 2: | ||
return fibonacci(position - 2) + fibonacci(position - 1) | ||
|
||
|
||
if __name__ == "__main__": | ||
# numbers() | ||
print(factorial(5)) | ||
print(fibonacci(15)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
# operaciones: introduce - introducir recover - recuperar | ||
|
||
|
||
class Stack: | ||
def __init__(self): | ||
# Crea una pila vacía | ||
self.items = [] | ||
|
||
def introduce(self, element): | ||
# Agrega el elemento element a la pila | ||
self.items.append(element) | ||
|
||
def recover(self): | ||
# Devuelve el elemento tope y lo elimina | ||
# Si la pila está vacía levanta una excepción | ||
try: | ||
return self.items.pop() | ||
except IndexError: | ||
raise ValueError("La pila está vacía") | ||
|
||
def is_empty(self): | ||
# Devuelve True si la pila esta vacía | ||
return self.items == [] | ||
|
||
def size(self): | ||
return len(self.items) | ||
|
||
|
||
class Queue: | ||
def __init__(self): | ||
# Crea una cola vacía | ||
self.items = [] | ||
|
||
def introduce(self, element): | ||
# Agrega el elemento element a la cola | ||
self.items.append(element) | ||
|
||
def recover(self): | ||
# Devuelve el primer elemento y lo elimina | ||
# Si la cola está vacía levanta una excepción | ||
try: | ||
return self.items.pop(0) | ||
except IndexError: | ||
raise ValueError("La cola está vacía") | ||
|
||
def is_empty(self): | ||
# Devuelve True si la cola esta vacía | ||
return self.items == [] | ||
|
||
|
||
def web_navigation(): | ||
navigation = Stack() | ||
|
||
while True: | ||
action_of_navigate = input( | ||
"Escribe la URL o las palabras atras, adelante o salir" | ||
) | ||
|
||
if action_of_navigate == "adelante": | ||
pass | ||
elif action_of_navigate == "atras": | ||
if not navigation.is_empty(): | ||
if navigation.size() > 1: | ||
page = navigation.recover() | ||
else: | ||
page = navigation.items[0] | ||
elif action_of_navigate == "salir": | ||
break | ||
else: | ||
navigation.introduce(action_of_navigate) | ||
page = navigation.items | ||
|
||
if not navigation.is_empty(): | ||
print(f"Estas en {page}") | ||
|
||
|
||
def shared_printing(): | ||
printing_queue = Queue() | ||
|
||
while True: | ||
action = input("Añade un documento o escribe imprimir/salir: ") | ||
|
||
if action == "salir": | ||
break | ||
elif action == "imprimir": | ||
if not printing_queue.is_empty(): | ||
print(f"Imprimiendo el documento {printing_queue.recover()}") | ||
else: | ||
printing_queue.introduce(action) | ||
|
||
print(f"La cola de impresión restante es: {printing_queue.items}") | ||
|
||
|
||
if __name__ == "__main__": | ||
# web_navigation() | ||
shared_printing() |