Last active
August 24, 2020 13:22
-
-
Save fakuivan/2d155efc5d13fc86b0c98c047b175408 to your computer and use it in GitHub Desktop.
Ejemplos info
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
#!/usr/bin/env python3 | |
# Un Entrenador, quiere almacenar los tiempos de una carrera de 100 metros de 5 atletas. | |
# Cada Atleta realiza 5 pruebas de las cuales se registran los tiempos. | |
# Se propone crear un diccionario que como claves tengan los nombres de los atletas y cargar | |
# como valores del diccionario una lista con los tiempos de cada uno. | |
# Se deberá presentar el siguiente menú. | |
# 1-Carga de Nombres y Tiempos de los Atletas. | |
# 2-Mostrar que Atleta obtuvo el menor tiempo y en cual de las 5 pruebas fue.(0,1,2,3,4) | |
# 3-Mostrar el nombre y promedio de tiempo de cada atleta. | |
# 4-Mostar todos los datos (nombres de Atletas y tiempos) | |
# F o f-Finalizar. | |
# Observaciones: | |
# Los tiempos ingresados NO pueden ser negativos, en caso de que se ingrese un valor negativo debería pedir | |
# reingreso previo a mostrar un cartel que diga: No se pueden ingresar valores de tiempo negativo. | |
# Si en el menú se ingresa una opción que sea distinta de 1,2,3,4,f o F, se deberá mostar el cartel que diga: | |
# Opción no valida. Ingrese una opción nuevamente. | |
# Solo finaliza el programa si se presiona f ó F. | |
def leer_tiempo(cartel: str) -> float: | |
while True: | |
entrada = input(cartel) | |
try: | |
entrada_float = float(entrada) | |
except ValueError: | |
print(f"{entrada!r} no es un número.") | |
continue | |
if not entrada_float > 0: | |
print("Demasiado rápido! Los valores de tiempo deben ser mayores a cero.") | |
continue | |
return entrada_float | |
def mostrar_menu() -> str: | |
while True: | |
print(""" | |
1-Carga de Nombres y Tiempos de los Atletas. | |
2-Mostrar que Atleta obtuvo el menor tiempo y en cual de las 5 pruebas fue. | |
3-Mostrar el nombre y promedio de tiempo de cada atleta. | |
4-Mostar todos los datos (nombres de Atletas y tiempos) | |
F o f-Finalizar. | |
""") | |
op = input("Ingrese una opción: ") | |
if op in "1234fF": | |
return op.lower() | |
print(f"La opción {op!r} no es válida") | |
def cargar_atleta(): | |
tiempos = list() | |
nombre = input("Nombre del participante: ") | |
for i in range(5): | |
tiempos.append(leer_tiempo(f"Tiempo en prueba número {i+1}: ")) | |
return nombre, tiempos | |
atletas = dict() | |
op = "1" | |
while op in "1234": | |
op = mostrar_menu() | |
if len(atletas) < 1 and op in "234": | |
print("Ingrese algún atleta") | |
continue | |
if op == "1": | |
nombre, tiempos = cargar_atleta() | |
atletas[nombre] = tiempos | |
elif op == "2": | |
tiempos_para_min = list() | |
for atleta, tiempos in atletas.items(): | |
for intento, tiempo in enumerate(tiempos): | |
tiempos_para_min.append([tiempo, [atleta, intento]]) | |
menor_tiempo, (atleta, intento) = min(tiempos_para_min, key=lambda entrada: entrada[0]) | |
print(f"Atleta {atleta!r} obtuvo el menor tiempo en el intento {intento+1} con {menor_tiempo} segundos") | |
elif op == "3": | |
for atleta, tiempos in atletas.items(): | |
print(f"Promedio para {atleta!r}: {sum(tiempos)/len(tiempos)}") | |
elif op == "4": | |
for atleta, tiempos in atletas.items(): | |
print(f"Atleta: {atleta!r}\t Tiempos (segundos): {tiempos!r}") |
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
// Ejemplo para computación 2do electrónica | |
#include <iostream> | |
template<typename T> | |
struct Node { | |
Node<T>* next; | |
T data; | |
Node(T data, Node<T>* next=nullptr): data(data), next(next) {} | |
}; | |
using BinaryNode = Node<bool>; | |
// Won't increment null pointers | |
void increment_counter(BinaryNode* counter) | |
{ | |
for (auto current_node = counter; current_node != nullptr; current_node = current_node->next) { | |
if (current_node->data == true) { | |
current_node->data = false; | |
if (current_node->next == nullptr) { | |
current_node->next = new BinaryNode(1); | |
break; | |
} | |
} else { | |
current_node->data = true; | |
break; | |
} | |
} | |
} | |
template<typename T> | |
T counter_to_number(BinaryNode* counter) | |
{ | |
T number = 0; | |
T shifted = 1; | |
for (auto current_node = counter; current_node != nullptr; current_node = current_node->next) { | |
number |= (shifted * (current_node->data ? 1 : 0)); | |
shifted = shifted << 1; | |
} | |
return number; | |
} | |
int main() | |
{ | |
char op = 'h'; | |
auto counter = new BinaryNode(0); | |
while (op != '0') { | |
if (op == 'i') { | |
increment_counter(counter); | |
} else if (op == 'p') { | |
std::cout << "Binary form (starting by least significant bit): "; | |
for (auto current_node = counter; current_node != nullptr; current_node = current_node->next) { | |
std::cout << current_node->data ? "1" : "0"; | |
} | |
std::cout << std::endl; | |
std::cout << "Decimal form: " << counter_to_number<int>(counter) << std::endl; | |
} else if (op == 'h') { | |
std::cout << "i: increment counter" << std::endl; | |
std::cout << "p: print counter" << std::endl; | |
std::cout << "h: show this help" << std::endl; | |
std::cout << "0: exit" << std::endl; | |
} else if (op != '0') { | |
std::cout << "'" << op << "'" << "is not a valid option." << std::endl; | |
} | |
std::cin >> op; | |
} | |
std::cout << "puto" << std::endl; | |
} | |
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
#include <iostream> | |
int factorial(int); | |
int fibonacci(int); | |
int sum_of_terms(int); | |
int main() { | |
int n; | |
std::cout << "n: "; | |
std::cin >> n; | |
std::cout << "factorial(n): " << factorial(n) << std::endl; | |
std::cout << "fibonacci(n): " << fibonacci(n) << std::endl; | |
std::cout << "sum_of_terms(n): " << sum_of_terms(n) << std::endl; | |
return 0; | |
} | |
/* | |
int fibonacci(int n) { | |
if (n == 0) { | |
return 0; | |
} else if (n == 1) { | |
return 1; | |
} else { | |
return fibonacci(n - 2) + fibonacci(n - 1); | |
} | |
}*/ | |
// {0, 1, 0+1, 1+(0+1), (0+1)+(1+(0+1)), .. , [n-2], [n-1], [n-2] + [n-1]} | |
int fibonacci(int n) { | |
return n == 0 || n == 1 ? n : fibonacci(n - 2) + fibonacci(n - 1); | |
} | |
// 1 * 2 * 3 * .. * (n-1) * n | |
// (n-1)! * n == n! | |
int factorial(int n) { | |
return n > 1 ? factorial(n - 1) * n : 1; | |
} | |
// 1 + 2 + 3 + .. + n | |
int sum_of_terms(int n) { | |
return n > 0 ? sum_of_terms(n - 1) + n : 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment