Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
208 changes: 189 additions & 19 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,18 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"id": "df908bed-acc6-4b67-b33a-f3b1c564a49f",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Lista única: [1, 2, 3, 4, 5]\n"
]
}
],
"source": [
"def get_unique_list(lst):\n",
" \"\"\"\n",
Expand All @@ -43,7 +51,18 @@
" Returns:\n",
" list: A new list with unique elements from the input list.\n",
" \"\"\"\n",
" # your code goes here\n"
" # your code goes here\n",
"def get_unique_list(lst):\n",
" \"\"\"\n",
" Toma una lista y devuelve una nueva con elementos únicos.\n",
" \"\"\"\n",
" # Convertimos a set para quitar duplicados y luego a lista otra vez\n",
" return list(set(lst))\n",
"\n",
"# Prueba la función:\n",
"mi_lista = [1, 2, 3, 3, 3, 3, 4, 5]\n",
"print(f\"Lista única: {get_unique_list(mi_lista)}\") \n",
"# Debería mostrar: [1, 2, 3, 4, 5]\n"
]
},
{
Expand All @@ -60,10 +79,18 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"id": "7d5c8e34-a116-4428-ab9d-e0e15e338fff",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Mayúsculas: 2, Minúsculas: 8\n"
]
}
],
"source": [
"def count_case(string):\n",
" \"\"\"\n",
Expand All @@ -75,7 +102,27 @@
" Returns:\n",
" A tuple containing the count of uppercase and lowercase letters in the string.\n",
" \"\"\"\n",
" # your code goes here"
" # your code goes here\n",
"def count_case(string):\n",
" \"\"\"\n",
" Cuenta cuántas mayúsculas y minúsculas hay en un texto.\n",
" \"\"\"\n",
" mayusculas = 0\n",
" minusculas = 0\n",
" \n",
" for caracter in string:\n",
" if caracter.isupper():\n",
" mayusculas += 1\n",
" elif caracter.islower():\n",
" minusculas += 1\n",
" \n",
" return mayusculas, minusculas\n",
"\n",
"# Prueba la función:\n",
"texto = \"Hello World\"\n",
"mayus, mins = count_case(texto)\n",
"print(f\"Mayúsculas: {mayus}, Minúsculas: {mins}\")\n",
"# Debería mostrar: Mayúsculas: 2, Minúsculas: 8"
]
},
{
Expand All @@ -92,7 +139,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 18,
"id": "c15b91d4-cfd6-423b-9f36-76012b8792b8",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -122,7 +169,28 @@
" Returns:\n",
" int: The number of words in the sentence.\n",
" \"\"\"\n",
" # your code goes here"
" # your code goes here\n",
"import string\n",
"\n",
"def remove_punctuation(sentence):\n",
" \"\"\"\n",
" Removes all punctuation marks...\n",
" \"\"\"\n",
" # Tu código va aquí:\n",
" limpio = \"\".join(char for char in sentence if char not in string.punctuation)\n",
" return limpio\n",
"\n",
"def word_count(sentence):\n",
" \"\"\"\n",
" Counts the number of words in a given sentence...\n",
" \"\"\"\n",
" # Tu código va aquí:\n",
" # Primero usamos la función de arriba para limpiar\n",
" frase_sin_puntuacion = remove_punctuation(sentence)\n",
" \n",
" # Luego dividimos por espacios y contamos\n",
" lista_palabras = frase_sin_puntuacion.split()\n",
" return len(lista_palabras)"
]
},
{
Expand All @@ -140,12 +208,24 @@
},
{
"cell_type": "code",
"execution_count": null,
"id": "f7cfd32a-f559-47ff-81c1-2576bd4fe3bf",
"execution_count": 19,
"id": "b8ed6ec0",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"7\n"
]
}
],
"source": [
"# your code goes here"
"# Probamos la función con el ejemplo del ejercicio\n",
"resultado = word_count(\"Note : this is an example !!! Good day : )\")\n",
"\n",
"print(resultado) \n",
"# Debería aparecer un 7 justo debajo de la celda"
]
},
{
Expand All @@ -168,12 +248,64 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 15,
"id": "57f9afc7-8626-443c-9c3e-eb78ef503193",
"metadata": {},
"outputs": [],
"source": [
"# your code goes here"
"# your code goes here\n",
"def add(a, b):\n",
" return a + b\n",
"\n",
"def subtract(a, b):\n",
" return a - b\n",
"\n",
"def multiply(a, b):\n",
" return a * b\n",
"\n",
"def divide(a, b):\n",
" # Agregamos una validación para no dividir por cero\n",
" if b == 0:\n",
" return \"Error: No se puede dividir por cero\"\n",
" return a / b"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "6ffd08c0",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"15\n",
"50\n",
"Error: No se puede dividir por cero\n"
]
}
],
"source": [
"def calculate(n1, n2, operator):\n",
" \"\"\"\n",
" Coordina las funciones matemáticas según el operador recibido.\n",
" \"\"\"\n",
" if operator == '+':\n",
" return add(n1, n2)\n",
" elif operator == '-':\n",
" return subtract(n1, n2)\n",
" elif operator == '*':\n",
" return multiply(n1, n2)\n",
" elif operator == '/':\n",
" return divide(n1, n2)\n",
" else:\n",
" return \"Operador no válido\"\n",
"\n",
"# Pruebas:\n",
"print(calculate(10, 5, '+')) # Debería dar 15\n",
"print(calculate(10, 5, '*')) # Debería dar 50\n",
"print(calculate(10, 0, '/')) # Debería dar el mensaje de error"
]
},
{
Expand All @@ -192,12 +324,50 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 17,
"id": "ff3e816c-13ab-447d-a6f2-bb47a8fad2e2",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"100\n",
"24\n"
]
}
],
"source": [
"# your code goes here"
"# your code goes here\n",
"def flexible_calculate(operator, *args):\n",
" \"\"\"\n",
" Calculadora que acepta múltiples números.\n",
" \"\"\"\n",
" if not args:\n",
" return 0\n",
" \n",
" # Empezamos con el primer número de la lista\n",
" resultado = args[0]\n",
" \n",
" # Recorremos los demás números\n",
" for num in args[1:]:\n",
" if operator == '+':\n",
" resultado += num\n",
" elif operator == '-':\n",
" resultado -= num\n",
" elif operator == '*':\n",
" resultado *= num\n",
" elif operator == '/':\n",
" if num != 0:\n",
" resultado /= num\n",
" else:\n",
" return \"Error: División por cero\"\n",
" \n",
" return resultado\n",
"\n",
"# Prueba con 3 o más números:\n",
"print(flexible_calculate('+', 10, 20, 30, 40)) # 10 + 20 + 30 + 40 = 100\n",
"print(flexible_calculate('*', 2, 3, 4)) # 2 * 3 * 4 = 24"
]
},
{
Expand Down Expand Up @@ -326,7 +496,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -340,7 +510,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.4"
}
},
"nbformat": 4,
Expand Down